[SPIGOT-7420] Comparing isSimilar method allowing to ignore durability Created: 05/Jul/23  Updated: 25/Dec/24

Status: Open
Project: Spigot
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Minor
Reporter: David White Assignee: Unassigned
Resolution: Unresolved Votes: 2
Labels: None

Version: latest
Guidelines Read: Yes

 Description   

Don't really feel like figuring out how to make a PR with the hub-hosted source right now for such a minor feature but I think that this feature should be added as it will benefit a lot of use to custom recipes using RecipeChoice.ExactChoice. I have seen people make threads about complaining how damaged items don't register in recipes so why not just add it as a feature as I don't see any harm in doing so. Here is the requested pull:

https://hub.spigotmc.org/stash/projects/SPIGOT/repos/bukkit/browse/src/main/java/org/bukkit/inventory/ItemStack.java#283

Change this method and add extra method to this:

 

/**
 * Added in 1.20.1
 *
 * This method is the same as equals, but does not consider stack size
 * (amount). This method will also compare the item damage if applicable.
 *
 * @param stack the item stack to compare to
 * @param ignoreDurability if set to true then will ignore the items durability
 * @return true if the two stacks are equal, ignoring the amount
 */
@Utility
public boolean isSimilar(@Nullable ItemStack stack, boolean ignoreDurability) {
    if (stack == null) {
        return false;    
    }
    if (stack == this) {
        return true;
    }
    Material comparisonType = (this.type.isLegacy()) ? Bukkit.getUnsafe().fromLegacy(this.getData(), true) : this.type; // This may be called from legacy item stacks, try to get the right material
    if (comparisonType != stack.getType() || hasItemMeta() != stack.hasItemMeta() || (!ignoreDurability && getDurability() != stack.getDurability())) {
    	return false;
    }
    if (!hasItemMeta()) {
    	return true;
    }
    ItemMeta thisMeta = getItemMeta();
    ItemMeta stackMeta = stack.getItemMeta();
    if (ignoreDurability) {
    	((Damageable) thisMeta).setDamage(0);
    	((Damageable) stackMeta).setDamage(0);
    }
    return Bukkit.getItemFactory().equals(thisMeta, stackMeta);
}
/**
 * This method is the same as equals, but does not consider stack size
 * (amount). This method will also compare the item damage if applicable.
 *
 * @param stack the item stack to compare to
 * @return true if the two stacks are equal, ignoring the amount
 */
@Utility
public boolean isSimilar(@Nullable ItemStack stack) {
    return isSimilar(stack, false);
}

I have to manually change the item metas durability to 0 because even though there is a getDurability() comparison, there is another check for meta damage in the ItemFactory method.

And then change this line in RecipeChoice.ExactChoice class here:

https://hub.spigotmc.org/stash/projects/SPIGOT/repos/bukkit/browse/src/main/java/org/bukkit/inventory/RecipeChoice.java#200

Change to this line instead:

if (t.isSimilar(match, true)) { 

This will now ignore item durability for exact recipe choices. Could bring it a step further and add a getter and setter for a boolean internally to allow user control over the durability check but that would be too much extra to post here, although if you can please do.

 



 Comments   
Comment by Kami Rori [ 10/Jun/24 ]

There will be a response. Be patient

You can also implement a comparison method yourself. That's what I did.

Comment by David White [ 08/Jun/24 ]

So is this just never going to get a response and I wasted my time?

Generated at Sat Apr 05 10:22:32 UTC 2025 using Jira 10.3.3#10030003-sha1:d220e3fefc8dfc6d47f522d3b9a20c1455e12b7b.