Hello, I am currently doing a shop plugin. When you click on the mouse wheel in an inventory of the shop on an item (pa example of the stone) it must remove all the blocks of stone that the player has in his inventory. The problem is that if he has an item / block (in this case the stone), in different slots, when I click on the stone in the shop inventory, it will remove the stone than a single slot instead of all the slots containing the stone. My code: https://pastebin.com/4rGDaUc3 Thank you (sorry I use a translator)
You have a break; inside your if statement. This means when a match is found, it will remove the match and stop the loop. This is why it only removes one. Remove the break.
Thank you but the message "§aVous avez effectué la vente avec succès !" is sent to the player as many times as there is slot with the item. I would like this message to be sent once I noticed another problem: For example if I have stacks of dirt in different slots and that in the inventory of shop I click on the dirt with the wheel of the mouse, it must give me money in function from what I sold. But in my example, it will give me the money of 64 * dirt price + 128 * dirt price instead of 64 * dirt price + 64 * dirt price because I have 64 dirt blocks in 2 different slots EDIT by Moderator: merged posts, please use the edit button instead of double posting.
Move the message outside the loop. I would guess that happens because your variable 'count' contains the amount of items from the last found ItemStack. Try changing 'count * price' to 'item.getAmount() * price'.
Thank you it works! I also made sure that when we click right on an item in the inventory of shop, it asks him in the chat how much the amount he wants this item. I still have the same problem: when the player wants to sell for example 1 stone and he has 1 stack of stone in 2 different slots, it will remove 1 stone from each slot containing stone instead of 1 stone for the total of the stone he has in his inventory (in this case, 1 stone out of the 128 of his inventory). My code: https://pastebin.com/EpszxGrH Thank you ! Help ? EDIT by Moderator: merged posts, please use the edit button instead of double posting.
@Fish28 Try adding something like this: Code:Java //Outside the loopint toRemove = /*the amount of items you want to remove*/;int removed = 0;// Inside the loopif (removed < toRemove) { // Remove item removed++;} else break; That should check if it should remove any items, if it shouldn't remove any more items then it breaks out of the loop. I just came up with this on the spot so there is probably a better way.
Thanks but the amount of items I want to remove is in the loop Code: for (int slot: matches.keySet ()) { so I can't put the variables : "toRemove" and "removed" in my code?
@Fish28 Then loop the inventory for items to remove. :7 Code:Java int toRemove = 0;for (inventory.getContents()) { if (item == null) continue; if (item.getType() != Material./*material to sell*/) continue; toRemove += item.getAmount();}int removed = 0;// Do your thing here
Yeah but to remove the blocks I have to be in the loop Code: for(ItemStack items : main.inventoryClick.sold.get(p).keySet()) { for (ItemStack item : p.getInventory().getContents()) { if (item == null) continue; if (item.getType() != items.getType() && item.getData().getData() != items.getData().getData()) continue; toRemove += item.getAmount(); } } int removed = 0; if (removed < toRemove) { p.sendMessage(main.prefix + "§aVous avez effectué la vente avec succès !"); removed++; } else break;
@Fish28 Ah, i didn't notice you got the material from 'items', my bad. :7 Is the the material of all the itemstacks in the map the same? If so, why do you get the material from that? Isn't that the material you want to remove?