Checking if chest contents is null?

Discussion in 'Plugin Development' started by shohouku, Aug 20, 2014.

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

    shohouku

    I'm getting both debug messages, if there are items in the inventory.

    Any idea why? am I missing something?

    Code:java
    1. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
    2. if(e.getClickedBlock().getType().equals(e.getClickedBlock().getType().CHEST)) {
    3. System.out.println("cancelling");
    4. if(e.getClickedBlock().getState() instanceof Chest) {
    5. Chest chest = (Chest) e.getClickedBlock().getState();
    6. ItemStack[] items = chest.getBlockInventory().getContents();
    7. for(ItemStack item : items)
    8. {
    9. if(item != null) {
    10. System.out.println("should not be cancelling");
    11. return;
    12. } else {
    13. if(item == null) {
    14. System.out.println("cancelling");
    15. e.setCancelled(true);
    16. }
    17. }
    18. }
    19. }
    20. }
    21. }

    EDIT: I put e.setCancelled(false); for the should not be cancelling, it works but it's spamming a whole lotta messages so I aint using that way...
     
  2. Offline

    Tecno_Wizard

    shohouku
    Does it save every slot, regardless of if anything is in it? Also, Having 2 canceling statements makes things really confusing...
    Can you be a bit more specific on what you're trying to do with this?
     
  3. Offline

    shohouku


    if the chest is empty... it should cancel, if the chest isn't empty it shouldn't cancel

    But both methods are happening, I don't know why, maybe I should add a scheduler? because I just remembered I'm manually adding items in when I instantly open the inventory :p

    I'll try and see if its solved...
     
  4. Offline

    Tecno_Wizard

    shohouku,
    Actually, right now both should happen many many, many times...
    What exactly are you trying to do with this?
     
  5. Offline

    shohouku


    destroy the chest when theres nothing inside the chest.
     
Thread Status:
Not open for further replies.

Share This Page