Wonky Tntfill

Discussion in 'Plugin Development' started by mehboss, Feb 9, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    mehboss

    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?
     
    Last edited: Feb 10, 2019
  2. Offline

    DerDonut

    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
     
    Last edited: Feb 10, 2019
  3. Offline

    mehboss

    @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
     
  4. Offline

    DerDonut

    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 :confused:
     
    Last edited: Feb 10, 2019
  5. Offline

    mehboss

    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
     
  6. Offline

    DerDonut

    I did.
    Like in the screenshots
     
  7. Offline

    mehboss

    Ummm, how?


    Sent from my iPhone using Tapatalk
     
  8. Offline

    DerDonut

    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)
     
  9. Offline

    mehboss

    Any fixes?


    Sent from my iPhone using Tapatalk
     
Thread Status:
Not open for further replies.

Share This Page