Hey, i want to check the player's inventory, to find out if he has a specific item, i use that Code (Java): ItemStack composant = new ItemStack(Material.WHITE_GLAZED_TERRACOTTA, 6); ItemMeta composantM = composant.getItemMeta(); composantM.setDisplayName(ChatColor.WHITE + "Minerai de Cuivre"); composant.setItemMeta(composantM); if(p.getInventory().containsAtLeast(composant, 6)) But I would like to check only the name of the item and not all (lore, enchantment or other) If you could give me a solution, it will be with great pleasure Thanks for watching
Code (Java): final Map<Integer,? extends ItemStack> composantCandidates = inventory.all(Material.WHITE_GLAZED_TERRACOTTA); for(ItemStack stack: composantCandidates.values()) { if (stack.getName().equals("Minerai de Cuivre")) // found! }
You get all the Items in the target inventory where the material is white glazed terracotta. Then you loop through all of the results and filter out any that do not match your name.
i use inventory click event Code (Java): @EventHandler public void onclickGaucheCraftApprenti1(InventoryClickEvent e) { ItemStack it = e.getCurrentItem(); Player p = (Player) e.getWhoClicked(); if(it == null) return; if(e.isLeftClick() && it.getType() == Material.BLUE_DYE && it.hasItemMeta() && it.getItemMeta().hasDisplayName() && it.getItemMeta().getDisplayName().equalsIgnoreCase("Craft : Lingot de Cuivre")) { if(p.hasPermission("forgeron.recette.apprenti.2")) { if(p.getInventory().firstEmpty() == -1) { p.sendMessage(ChatColor.RED + "[Forge] -> Inventaire plein, impossible de créer un objet"); return; }else { ItemStack composant = new ItemStack(Material.WHITE_GLAZED_TERRACOTTA, 6); ItemMeta composantM = composant.getItemMeta(); composantM.setDisplayName(ChatColor.WHITE + "Minerai de Cuivre"); composantM.setLore(Arrays.asList(ChatColor.GRAY + "Type : " + ChatColor.WHITE + "Composant d'artisanat")); composant.setItemMeta(composantM); if(p.getInventory().containsAtLeast(composant, 6)) {
what's the stack.getName and inventory.all, have add ((HumanEntity) stack) otherwise it doesn't work and i don't understand how can initialized inventory.all
stack.getName() is an abbreviated version of stack.getItemMeta().getDisplayName() You don't initialize inventory.all, you just call the Method of inventory. Like you called p.getInventory().containsAtLeast(), you just call p.getInventory().all(...)
I really don't understand how it works Code (Java): ItemStack composant = new ItemStack(Material.WHITE_GLAZED_TERRACOTTA, 6); ItemMeta composantM = composant.getItemMeta(); composantM.setDisplayName(ChatColor.WHITE + "Minerai de Cuivre"); composantM.setLore(Arrays.asList(ChatColor.GRAY + "Type : " + ChatColor.WHITE + "Composant d'artisanat")); composant.setItemMeta(composantM); final Map<Integer,? extends ItemStack> composantCandidates = p.getInventory().all(Material.WHITE_GLAZED_TERRACOTTA); for(ItemStack stack: composantCandidates.values()) { if (((HumanEntity) stack).getName().equals("Minerai de Cuivre")) {
stack is just an ItemStack, no need to cast. You can simply call stack.getItemMeta().getDisplayName() to check for the name
i need to do that but don't work, remove all items Code (Java): final Map<Integer,? extends ItemStack> composantCandidates = p.getInventory().all(Material.WHITE_GLAZED_TERRACOTTA); for(ItemStack stack: composantCandidates.values()) { stack.setAmount(6); if (stack.getItemMeta().getDisplayName().equalsIgnoreCase("Minerai de Cuivre")) { p.getInventory().removeItem(new ItemStack(stack));
So, currently you have the stack, setting the amount to 6 (if it‘s a White glazed terracotta) and then removing it, if it has the name. I doubt this is what you want to do. Do you want to decrement the ItemStack or what is it exactly you want to do?
when player click on my item menu that remove 6 white glazed terracotta (check if player has 6 white..) and after that server give an other item (with my command) but i need to remove 6 white glazed terracotta
In that case, the situation gets a little more tricky... Code (Java): final Map<Integer,? extends ItemStack> candidates = player.getInventory().all(Material.WHITE_GLAZED_TERRACOTTA); int count = 0; final List<ItemStack> removals = new ArrayList<>(); for (ItemStack candidate: candidates.values()) { if (candidate.getItemMeta().getDisplayName().equals("Minerai de Cuivre")) { final int amount = candidate.getAmount(); if (amount >= 6) { candidate.setAmount(amount - 6); break; } else { if (count + amount >= 6) { removals.forEach(stack -> player.getInventory().removeItem(stack)); candidate.setAmount(amount - (6 - count)); break; } else { count += amount; removals.add(candidate); } } } } Note that this is not fully tested
So the way this code works is that it removes either zero or six items. If you, for example, have 2 Item Stacks and one contains 2 Items, the other one 5 Items, the first stack will after a call of this method have one Item left and the other has none left. So what is it exactly that you want, taking ItemStacks that have exactly an mount of six or more, or taking ItemStacks such that exactly 6 or none are taken out. Case 1: You can throw away the algorithm and just check if the stack (candidate) has an amount of 6 or more, if yes -> reduce by 6, return. For two or more ItemStacks do that until you have reached your limit Case 2: You just replace every occurrence of 6 with 12; or an appropriate variable reflecting your needs (n * 6, where n is the count of quarried stacks. If you want to take as much stacks as possible, you would loop until the end and finally removing the remainders if there are 6 and not if the amount is smaller).