Chests considered trapped chests bug

Discussion in 'Spigot Plugin Development' started by Kompye, Sep 14, 2019.

  1. Hello. Working on a plugin that blocks chests, hoppers, and so on. I don't know why, but only chests get block and when adding the code to send a message to the trapped chest, it was getting executed when clicking on the normal chest. Here is the code:
    Code (Java):
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {

           
            if (e.getAction() != Action.RIGHT_CLICK_BLOCK) {
                return;
            }
           
            final Player player = e.getPlayer();
            final ItemStack item = player.getInventory().getItemInMainHand();
            final Block block = e.getClickedBlock();


            //chest
            if (block.getType() != Material.CHEST) {
                return;
            }
            if (!plugin.getConfig().getBoolean("chest.enabled", true)) {
                return;
            }
            if (item == null || item.getType() == Material.AIR) {
                e.setCancelled(true);
                return;
            }
            if (item.getType().isBlock() && player.isSneaking()) {
                return;
            }
            e.setCancelled(true);
            //trapped_chest
            if (block.getType() != Material.TRAPPED_CHEST) {
                return;
            }
            if (!plugin.getConfig().getBoolean("trapped_chest.enabled", true)) {
                return;
            }
            if (item == null || item.getType() == Material.AIR) {
                e.setCancelled(true);
                return;
            }
            if (item.getType().isBlock() && player.isSneaking()) {
                return;
            }
            e.setCancelled(true);
            //ender_chest
            if (block.getType() != Material.ENDER_CHEST) {
                return;
            }
            if (!plugin.getConfig().getBoolean("ender_chest.enabled", true)) {
                return;
            }
            if (item == null || item.getType() == Material.AIR) {
                e.setCancelled(true);
                return;
            }
            if (item.getType().isBlock() && player.isSneaking()) {
                return;
            }
            e.setCancelled(true);
            //hopper
            if (block.getType() != Material.HOPPER) {
                return;
            }
            if (!plugin.getConfig().getBoolean("hopper.enabled", true)) {
                return;
            }
            if (item == null || item.getType() == Material.AIR) {
                e.setCancelled(true);
                return;
            }
            if (item.getType().isBlock() && player.isSneaking()) {
                return;
            }
            e.setCancelled(true);
            //shulker_box
            if (block.getState() instanceof ShulkerBox) {
                return;
            }
            if (!plugin.getConfig().getBoolean("shulker_Box.enabled", true)) {
                return;
            }
            if (item == null || item.getType() == Material.AIR) {
                e.setCancelled(true);
                return;
            }
            if (item.getType().isBlock() && player.isSneaking()) {
                return;
            }
            e.setCancelled(true);
            //dispenser
            if (block.getType() != Material.DISPENSER) {
                return;
            }
            if (!plugin.getConfig().getBoolean("dispenser.enabled", true)) {
                return;
            }
            if (item == null || item.getType() == Material.AIR) {
                e.setCancelled(true);
                return;
            }
            if (item.getType().isBlock() && player.isSneaking()) {
                return;
            }
            e.setCancelled(true);
            //dropper
            if (block.getType() != Material.DROPPER) {
                return;
            }
            if (!plugin.getConfig().getBoolean("dropper.enabled", true)) {
                return;
            }
            if (item == null || item.getType() == Material.AIR) {
                e.setCancelled(true);
                return;
            }
            if (item.getType().isBlock() && player.isSneaking()) {
                return;
            }
            e.setCancelled(true);
        }
     
  2. Code (Java):
    if (block.getType().equals(Material.TRAPPED_CHEST)) {
             
    }
         
    if (block.getType().equals(Material.CHEST)) {
             
    }
    Works for me.
     
  3. For me it doesn't.
     
  4. Well then don't use != when comparing materials.
     
  5. ... can't you see what I am doing?
    Code (Java):
            final Player player = e.getPlayer();
            final ItemStack item = player.getInventory().getItemInMainHand();
            final Block block = e.getClickedBlock();


            //chest
            if (block.getType() != Material.CHEST) {
                return;
            }
            if (!plugin.getConfig().getBoolean("chest.enabled", true)) {
                return;
            }
            if (item == null || item.getType() == Material.AIR) {
                e.setCancelled(true);
                return;
            }
            if (item.getType().isBlock() && player.isSneaking()) {
                return;
            }
            e.setCancelled(true);
     
  6. You are perfectly fine comparing Material enums with == and !=. In fact. it's preferable as it's null-safe.

    @OP
    I'll cut some parts of your code here for you:
    Code (Java):
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            // [--]
            //chest
            if (block.getType() != Material.CHEST) {
                return;
            }
            // [--]
            //trapped_chest
            if (block.getType() != Material.TRAPPED_CHEST) {
                return;
            }
            // [--]
            //ender_chest
            if (block.getType() != Material.ENDER_CHEST) {
                return;
            }
            // [--]
            //hopper
            if (block.getType() != Material.HOPPER) {
                return;
            }
            // [--]
            //dispenser
            if (block.getType() != Material.DISPENSER) {
                return;
            }
            // [--]
            //dropper
            if (block.getType() != Material.DROPPER) {
                return;
            }
            // [--]
            e.setCancelled(true);
        }
    Can you really not tell why this doesn't work?
    You're returning as soon as you see material that's not a CHEST. So if you see a TRAPPED_CHEST, you're returning right there. To get to the end of your method, your block's type would have to be CHEST, TRAPPED_CHEST, ENDER_CHEST, DISPENSER and DROPPER all at the same time. This will (obviously) never be the case.

    What you want is to create a set (I suggest using the EnumSet implementation) and return if the block's type is not contained within said set. If you want different behaviours for the different types of containers, you can use a switch statement.
     
    • Winner Winner x 2
    • Agree Agree x 1
  7. omg, I can't believe I didn't see that. Thank you!
     
  8. Enum#equals as far as I know internally checks with ==, for that matter, comparing with == or != is HIGHLY suggested as it is also null-safe.

    Null safe:

    null.equals(...) throws NPE
    null == ... doesn't throw NPE
     
    • Like Like x 1
  9. I mean, I already covered that...
     
  10. So the best way to do it is by creating a class listener for every block?
     
  11. No. I already told you what you should do:
     
  12. Yeah, but I really can't understand anything from there. I need an example.
     
  13. It's really quite simple. You create an EnumSet of the materials you wish to check for with EnumSet#of. You check if Set#contains the type of the block. That's it.

    And no, I'm not going to spoonfeed you code. You're the one that's going to have to write the code. I'm just here to give you the tools to do so.
     
    • Like Like x 1
  14. I also have to check if they are disabled or enabled in the config
     
  15. An easy way to do this would be to name the sections exactly the same as the enum constants. That way you could check the config for material.name() + ".enabled" or you can lowercase the name if you wish.
     
  16. I can't understand what EnumSet is
     
  17. I think you should learn java. If you don't know what is a Set, read the javadocs.
    @drives_a_ford already linked it:
     
    • Agree Agree x 2