Reset tool durability to default

Hey all,

Just learning to make some basic plugins.

I would like my users to be able to use the /repair function that I’m making. The function would work by holding an item, using the command and then the item would be reset to the max durability.

I have two questions:

  • Is there a way to find out what the maximum durability is?
  • Is there a call that checks if an item is actually a tool or a piece of armor?

This is what I am doing currently and players are able to reset wool and other blocks to default colours:

if (src instanceof Player)
{
Player player = (Player) src;

        if (player.getItemInHand().isPresent())
        {
            ItemStack itemInHand = player.getItemInHand().get();//this makes a copy of the time
            if(itemInHand.supports(Keys.ITEM_DURABILITY))
            {
                PrisonTools.plugin.getLogger().info("Player " + player.getName() + " is trying to use repair on " +
                        itemInHand.getItem().getName() + " which has " + itemInHand.get(Keys.ITEM_DURABILITY).toString() + " durability.");
                itemInHand.offer(Keys.ITEM_DURABILITY, 1561);//then we modify that copy
                player.setItemInHand(itemInHand);//then give it back to player
            }
            else
            {
                src.sendMessage(TextSerializers.FORMATTING_CODE.deserialize("This item does not support repairing."));
            }
        }
    }
1 Like

A couple things on your code:
First, the ItemStack is live, it’s not a copy. No need to set it back onto the hand. Never mind, that’s wrong.
Second, your Text construction method can be easily shortened to Text.of(<string>).
Third. it’s a good idea to use .get().toString() instead of .toString() on an Optional.
Fourth, when you’re calling getItemInHand(), a good habit to get into is storing the Optional, and then calling isPresent() and get() on the variable instead of calling the method multiple times.
Lastly, a durability of 0 is max durability, and a durability of 1561 (in this case) is one hit away from breaking. Minecraft’s weird like that.
If you want to still find the max durability, though, you can use this:
itemInHand.getItem().getDefaultProperty(UseLimitProperty.class).get().getValue() which will return the max number of uses before breaking.

Hi there, pie_flavor

Unfortunately, I must be working with copies, because if I remove the line setting the item back into the hand, the item is not updated. I started using that line because of this forum post

Thank you for your advice about the text, string and optionals. I was unable to do the latter, despite looking at the docs, due to lack of experience and familiarity with both optionals and the Sponge api.

I was also unable to incorporate your last piece of advice about getting the default property of UseLimitProperty, as I was getting the error below in console, accompanied by “Error occurred while executing command: No Value Present” in the chat.

[15:40:06] [Server thread/ERROR] [Sponge]: Error occurred while executing command ‘repair’ for source EntityPlayerMP[‘SparkVGX’/38, l=‘world’, x=39.16, y=88.00, z=10.07]: No value present
java.util.NoSuchElementException: No value present
at java.util.Optional.get(Optional.java:135) ~[?:1.8.0_91]

This happened regardless of item it was used on. Please forgive me, but could provide an example of how I should be using

itemInHand.getItem().getDefaultProperty(UseLimitProperty.class).get().getValue()

Sorry, I have just clicked in terms of zero being max durability regardless of how many uses the instance has.

With this in mind, I understand that I only need to find out if the item is tool or armour before performing the repair.

if (itemInHand.getItem().getDefaultProperty(ToolTypeProperty.class).isPresent() ||
(itemInHand.getItem().getDefaultProperty(ArmorTypeProperty.class).isPresent()))

Forgive me, but can someone explain how I make this work? I thought that checking if the item I want to repair had either of these properties, it would work.

I don’t think those things are implemented yet. You’re using the API right, the backend’s just not doing the proper thing.

Yes, that method explicitly makes a copy. See below, it does ItemStack#copy(). I suspect this is functioning as designed.

1 Like