Comparing isSimilar method allowing to ignore durability

    • Type: New Feature
    • Resolution: Unresolved
    • Priority: Minor
    • None
    • Affects Version/s: None
    • None
    • latest
    • Yes

      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.

       

            Assignee:
            Unassigned
            Reporter:
            David White
            Votes:
            2 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: