Anti Wither Breaking Plugin Need Help! ASAP

Discussion in 'Spigot Plugin Help' started by unalike, Jul 30, 2018.

  1. I am making a plugin that disables the Wither from breaking certain blocks that the user can choose. Everything works on it except for the skulls the wither shoots. When the block is set the wither will not delete those blocks around it but the skull will still break them. I wrote the code exactly the same way for both but only one entity cancels. I can;t find anything online to help.

    Code (Text):
    @EventHandler
        public void witherBreak(EntityChangeBlockEvent e) {
         
            Material block = e.getBlock().getType();
            List<String> list = config.getStringList("UnbreakableBlocks");

            if (e.getEntity() instanceof Wither) {
                for(String x : list) {
                    if(block.toString().equals(x)) {
                        e.setCancelled(true);
                    }
                }
            }
            else if(e.getEntity() instanceof WitherSkull) {
                for(String x : list) {
                    if(block.toString().equals(x)) {
                        e.setCancelled(true);
                        System.out.println("WitherSkull Canceled");
                     
                    }
                }
            }
        }
     
  2. Have you tried using the block break event?
     
  3. I'm pretty sure block break even only triggers when a player breaks the block.
     
  4. Oh yea, I just read the documentation.
    Have you tried if the event gets called if the wither skull breaks a block? And which EntityType it returns?
     
  5. I fixed it with some nasty looking nested enhanced for loops to pull the entire blocklist that is generated when there is an explosion. Then I tested every block to see if it is in my ban list.

    Code (Text):
    @EventHandler
        public void onEntityExplodeEvent(EntityExplodeEvent e){
            if (e.getEntity() instanceof WitherSkull)
            {
                List<Block> list = new ArrayList<Block>();
                List<String> banlist = config.getStringList("UnbreakableBlocks");
                list.addAll(e.blockList());
                for(Block block:list) {
                    for(String x: banlist) {
                        if(block.getType().toString().equals(x)) {
                            e.setCancelled(true);
                        }
                    }
                }
            }
        }
     
  6. Yea that's probably the best way to do it.
     
  7. Also why dont you make the banList list a value in your main class? Instead of getting it everytime when the event is called.