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());
Tool.getLogger().info("");
// 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:
if(slot.peek().isPresent()){
ItemStack itemSlot = slot.peek().get();
Tool.getLogger().info(itemSlot.getType().getName());
// 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";
Tool.getLogger().info(durabilityS2);
}
} 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";
Tool.getLogger().info(durabilityS2);
}
}
}
}
}
}
}
}
}