Code: int tntCount = 0; for (ItemStack i : p.getInventory().getContents()) { if (i != null && i.getType() == Material.TNT) { tntCount += i.getAmount(); } } while (tntCount > 0) { for (Dispenser d : dispensers) { if (d.getInventory().firstEmpty() != -1 || (!d.getInventory().addItem(new ItemStack(Material.TNT)).isEmpty())) { d.getInventory().addItem(new ItemStack(Material.TNT, 1)); p.getInventory().removeItem(new ItemStack(Material.TNT, 1)); tntCount--; } } } BEFORE COMMAND: click here AFTER COMMAND: click here DISPENSER 1: click here DISPENSER 2: click here If I have two rows of tnt in my inventory, it doesn't seem to distribute it correctly and somehow adds more tnt in my inventory. Any ideas?
I think you actually add 1 tnt into the inventory at this point. I Also thought .addItem() would return nothing so I wonder why you can check if its empty. But I'm pretty sure you know how to work with your IDE and if thats wrong your plugin would probably not worked as good as it is. So I guess that is correct. And there would be also issues if there are other items in the inventory. I would remove the first while loop and replace the condition I quoted with if(tntCount > 0) that should work. I also wonder if the '||' operator makes sense... I would say '&&' would be more useful in this case
@DerDonut I did some research earlier and it says addItem returns items that couldn’t be added (if there isn’t another not full stack + if the inventory is full) If I remove the while statement and have the if statement, the code would only run once. I need it to run for every piece of tnt. I think || would be more useful because if their inventory is full, but the tnt is stacked 63 in one of the slots, the first statement would return false because there is no available slot. Sent from my iPhone using Tapatalk
Okay.. I'll try the code in a minute and see what else I can find @mehboss I used exact your code and tested it in 1.8 and 1.13. For me eveything works just fine
Try it with 2 rows of tnt. It starts acting up with higher quantities of tnt + more than one dispenser. Sent from my iPhone using Tapatalk
So the only problem I got was that if you can't divide the tnt perfectly so that like one tnt would be left over, then there are some more tnt in the dispensers then you had in your inventory before (not more then the amount of dispensers but still some)