Savoir si un joueur a un item renommé dans son inventaire

ShE3py

Enbogueuse
Support
26 Septembre 2015
4 086
157
455
247
21
Mìlhüsa
Java:
public static List<ItemStack> getRenamedItems(Inventory inventory, Material expectedMaterial, String expectedName) {
   List<ItemStack> stacks = new ArrayList<>();
  
   for(ItemStack stack : inventory.getStorageContents()) {
      if(stack != null && stack.getType() == expectedMaterial && stack.hasItemMeta()) {
         ItemMeta meta = stack.getItemMeta();
        
         if(meta.hasDisplayName() && meta.getDisplayName().equals(expectedName)) {
            stacks.add(stack);
         }
      }
   }
  
   return stacks;
}


Player player = ...;
List<ItemStack> stacks = getRenamedItems(player.getInventory(), Material.CAKE, "Kougelhopf");

int cakes = stacks.stream().mapToInt(ItemStack::getAmount).sum();

// ou
int cakes = 0;
for(ItemStack cakeStack : stacks)
   cakes += cakeStack.getAmount();
 
Dernière édition: