Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tool rules: Ensure get_speed and is_correct_for_drops check for tags #566

Merged
merged 7 commits into from
Feb 22, 2025

Conversation

Jayryn
Copy link
Contributor

@Jayryn Jayryn commented Feb 19, 2025

Description

This PR fixes an issue where get_speed and is_correct_for_drops were not checking for tagged blocks (#tag), causing them to return incorrect default values (1.0 for speed and false for drops). This change is necessary to mitigate block breaking miscalculation by returning wrong speed or items not being dropped when mining. I am unaware of any issues or limitations with this as of now. It worked correctly in a testing environment.

Changes

✅ Fixed get_speed: Now correctly retrieves the speed from tagged blocks instead of always defaulting to 1.0.
✅ Fixed is_correct_for_drops: Properly checks if a block belongs to a tag before returning false.
✅ Updated documentation: Improved inline comments for clarity.

How to Review
• Check that the updated logic correctly looks up tagged blocks.
• Ensure function behavior is consistent with the intended tool rules.

I am unaware of any open issues that relate to this PR.

Please follow our Coding Guidelines

…for tags

Both functions were ignoring tags (`#tag`), causing incorrect default values.
- Now correctly checks tags before returning speed or drop validity.
- Ensures tool rules work as expected for both direct and tagged block matches.
replace manual stripping with strip_prefix
Make clippy happy and use strip_prefix instead of manual stripping. Also removing unnecessary reference
continue;
// No tool? Use default speed
let Some(tool) = &self.item.components.tool else {
return 1.0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should pull out the default speed to a constant value to avoid magic numbers

Comment on lines +71 to +95
// Return false if no tool component exists
let Some(tool) = &self.item.components.tool else {
return false;
};

for rule in tool.rules {
// Skip rules without a drop condition
let Some(correct_for_drops) = rule.correct_for_drops else {
continue;
};

for entry in rule.blocks {
if entry.eq(&block) {
return correct_for_drops;
}

if entry.starts_with('#') {
// Check if block exists within the tag group
if let Some(blocks) =
get_tag_values(RegistryKey::Block, entry.strip_prefix('#').unwrap())
{
if blocks.iter().flatten().any(|s| s == block) {
return correct_for_drops;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can all be de-duplicated by using the above suggestion

Comment on lines +41 to +60
for rule in tool.rules {
// Skip if speed is not set
let Some(speed) = rule.speed else {
continue;
};

for entry in rule.blocks {
if entry.eq(&block) {
return speed;
}

if entry.starts_with('#') {
// Check if block is in the tag group
if let Some(blocks) =
get_tag_values(RegistryKey::Block, entry.strip_prefix('#').unwrap())
{
if blocks.iter().flatten().any(|s| s == block) {
return speed;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a method associated with ToolComponent

return speed;
}

if entry.starts_with('#') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be done better as:

match entry.strip_prefix("#") {
  Some(blocks) => ...
  None => (check equality of block with entry)
}

@Snowiiii
Copy link
Collaborator

Can you may upstream master so clippy is happy with the master code

@Jayryn
Copy link
Contributor Author

Jayryn commented Feb 21, 2025

Can you may upstream master so clippy is happy with the master code

I'll update everything on Monday / Tuesday as I'm currently abroad. I'll also upstream master code ^^

@Snowiiii
Copy link
Collaborator

I want to have this merged so i can move on with effects, Further optimations can be applied later. Thank you @Jayryn 👍

@Snowiiii Snowiiii merged commit 0fb1b76 into Pumpkin-MC:master Feb 22, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants