Item durability problem

hi, in the following code, when i walk with the item in hand, it work but otherwise, it don’t work:

public class UnclaimFinder {
    public static void init() {
//        On boucle sur les joueurs:
        for(Player player : Sponge.getServer().getOnlinePlayers()) {
//            La main principal:
            Optional<ItemStack> handOptional = player.getItemInHand(HandTypes.MAIN_HAND);
            if(handOptional.isPresent()) {
//                Ce qu'il a en main:
                Tool.getLogger().info("Main: " + player.getItemInHand(HandTypes.MAIN_HAND).get().getType().getName());

//                Ce que tient le joueur en main:
                ItemStack hand = handOptional.get();

                PlayerInventory inventory = (PlayerInventory)player.getInventory();

//                On boucle sur l'inventaire du joueur:
                for(org.spongepowered.api.item.inventory.Inventory item : inventory.slots()) {
                    Slot slot = (Slot) item;

//                    Si le slot est pas vide:
                        ItemStack itemSlot = slot.peek().get();

//                        L'item qu'on chercher:
                        Optional<ItemType> unclaimfinderOptional = Sponge.getRegistry().getType(ItemType.class, "minecraft:diamond_pickaxe");
//                        Optional<ItemType> unclaimfinderOptional = AdminToolMod.UNCLAIMFINDEROptional;

//                        Si il est sur le serveur:
                        if(unclaimfinderOptional.isPresent()) {
                            ItemType unclaimfinder = unclaimfinderOptional.get();

//                            Si le slot de l'inventaire est le même que l'item cherché:
                            if(itemSlot.getType().equals(unclaimfinder)) {
//                                Si on le tient pas en main:
                                if(!hand.getType().equals(unclaimfinder)) {
                                    Optional<Integer> DURABILITYOptional = itemSlot.get(Keys.ITEM_DURABILITY);
                                    if(DURABILITYOptional.isPresent()) {
//                                        Ne fonctionne pas ici:
                                        itemSlot.offer(Keys.ITEM_DURABILITY, itemSlot.get(Keys.ITEM_DURABILITY).get() + 1);

                                        String durabilityS2 = "   " + itemSlot.getType().getName() + " - " + itemSlot.get(Keys.ITEM_DURABILITY).get() + " / 2000";
                                } else {
                                    hand.offer(Keys.ITEM_DURABILITY, hand.get(Keys.ITEM_DURABILITY).get()-1);
                                    String durabilityS2 = "   "+ hand.getType().getName() + " - " + hand.get(Keys.ITEM_DURABILITY).get() + " / 2000";

Some context would be helpful. What is it that you’re trying to do and what exactly is working/not working?

if i walk with a test item (in this code) i want the item durability to decrease and it work well but when i use another item and this one is still on a hotbar slot, i want the durability of this test item to increase and it don’t work at all.
and is there a maximum durability ?

I think the problem is that after calling itemSlot.offer, you’re not putting the modified item back in the slot.

The ItemStack returned from slot.peek() is a copy of the item, so you need to set the modified copy back into the slot by calling slot.set(itemSlot)

[google trad]
it works, thank you. how does one know maximum durability?

cela fonctionne, merci. comment on connait la durabilité maximum ?

I found UseLimitProperty which might be what you’re looking for.

Also, if the data API is implemented in this way, Keys.ITEM_DURABILITY has a value type of: MutableBoundedValue<Integer> - which supports the getMinValue() and getMaxValue() functions.