How to prevent stealing from inventory

Discussion in 'Spigot Plugin Development' started by Kyllian, Apr 14, 2017.

  1. Hey,

    This is my code:

    Code (Text):
    @EventHandler
        public void onInventoryClickEvent(InventoryClickEvent e) {
            if (e.getInventory() instanceof CraftingInventory) {
                if (e.getCurrentItem().getType() == Material.BARRIER) {
                    e.setCancelled(true);
                    return;
                }
            }
        }
    There is one problem. When I jitterclick and close the inventory I can get the barrier out of the inventory. How can I disable/prevent this?
     
  2. CraftingInventory
    I am pretty sure it's PlayerInventory
     
  3. I'm pretty sure it's not.

    I forgot to mention that I want it to work on workbenches only (And maybe player crafting thingys)
     
  4. just do
    e.getInventory().equals(e.getWhoClicked().getInventory())

    and check e.getInventory() != null before
     
  5. Then check if the inventory is clicked equals to the inventory you want
     
  6. But that still doesnt prevent it from stealing

    Because the event gets canceled but when i click fast im able to craft the item
     
  7. You could try changing the event priority to run first, and then cancel it at the very beginning of the event. Then don't cancel it if it is not in the circumstance you want.
     
  8. Like this?
    Code (Text):
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onInventoryClickEvent(InventoryClickEvent e) {
            e.setCancelled(true);
            if (e.getInventory() instanceof CraftingInventory) {
                if (e.getCurrentItem().getType() == Material.BARRIER) {
                    e.setCancelled(true);
                    return;
                }
            }
            e.setCancelled(false);
        }
    }
     
  9. Yes, that should work
     
    • Winner Winner x 1
  10. [​IMG]
     
    • Funny Funny x 1
  11. [​IMG]
     
    • Funny Funny x 1
  12. tumblr_lpbwptqG331qio765o1_500.gif
     
    • Agree Agree x 1
  13. [​IMG]
     
  14. [​IMG]
     
  15. Code (Text):
    @EventHandler
        public void onPlayerBarrier(InventoryClickEvent e) {
            if (e.getClickedInventory() == null) return;
            if (!(e.getWhoClicked() instanceof Player)) return;
            Player player = (Player) e.getWhoClicked();
            if (!e.getClickedInventory().equals(player.getInventory())) return;
            ItemStack item = e.getCurrentItem();
            if (item == null) return;
            if (item.getType() == Material.AIR) return;
         
            // DO YOUR OTHER TEST HERE
         
            if (item.getType() == Material.BARRIER) {
                e.setCancelled(true);
            }
        }
    use this code it's work
     
  16. Hey thanks for the code :D

    But I made this:

    Code (Text):
    @EventHandler(priority = EventPriority.HIGHEST)
        public void onInventoryClickEvent(InventoryClickEvent e) {
            e.setCancelled(true);
                if (e.getCurrentItem().getType() == Material.BARRIER) {
                    if (e.getCurrentItem().hasItemMeta()) {
                        if (e.getCurrentItem().getItemMeta().getDisplayName().equals(ct.cc(main.getConfig().getString("CannotCraftItemName")))) {
                            return;
                        }
                    }
                }
            e.setCancelled(false);
        }
    }
    Is this any better?
     
  17. some error you forgot to check if inventory is null or item is null
     
  18. and don't check the config every time, cache data in variable
     
  19. The code will work, except you need to check if the current item is null
     
  20. But, How can you click something if you dont have an inventory open (aka null)