EnderCyrstal Damage

Discussion in 'Spigot Plugin Development' started by xguysprison, Aug 7, 2018.

  1. Im trying to make sure that my EnderCrystal Cant get destroyed. This is my code but they still get destroyed/explode by explosions
    Code (Java):
    @EventHandler
        public void onDamageByExplosion(EntityDamageEvent e) {

            if (e.getEntity() instanceof EnderCrystal) {
                e.setCancelled(true);
            }

        }

        @EventHandler
        public void onExplode(EntityExplodeEvent event) {
            if (event.getEntity() instanceof EnderCrystal) {
                event.setCancelled(true);
            }

        }

        @EventHandler
        public void onDamageByEntity(EntityDamageByEntityEvent e) {

            if (e.getEntity() instanceof EnderCrystal) {
                e.setCancelled(true);
                    }
            }
     
  2. replace all
    Code (Text):
    if (e.getEntity() instanceof EnderCrystal) {
    with
    Code (Text):
    if (e.getEntityType().equals(EntityType.ENDER_CRYSTAL)) {
     
    or if you want to use instanceof, you can use
    Code (Text):
    if (e.getEntity() instanceof org.bukkit.entity.EnderCrystal) {  
     
    #2 MrRicky_, Aug 8, 2018
    Last edited: Aug 8, 2018
  3. None of these change how the code works...
     
  4. Have you registered your listeners?
     
  5. I tried it works
     
  6. @MrRicky_ Hey man it fixed some explosions. Like fireballs but not tnt.
    Video:


    Full code:
    Code (Java):
        @EventHandler
        public void onDamageByExplosion(EntityDamageEvent e) {
            if (e.getEntityType().equals(EntityType.ENDER_CRYSTAL)) {
                e.setCancelled(true);
            }

        }

        @EventHandler
        public void onExplode(EntityExplodeEvent event) {
            if (event.getEntityType().equals(EntityType.ENDER_CRYSTAL)) {
                event.setCancelled(true);
            }

        }

        @EventHandler
        public void onDamageByEntity(EntityDamageByEntityEvent e) {

            if (e.getEntityType().equals(EntityType.ENDER_CRYSTAL)) {

                if (e.getDamager() instanceof Player) {
                    Player player = (Player) e.getDamager();
                    if (e.getEntity().getCustomName() == "Red Tower 1") {
                        redtower1health--;

                        player.sendMessage(e.getEntity().getCustomName() + " has " + redtower1health + " left");
                        ScoreBoardManager.updateScoreBoard(player);
                        if (redtower1health <= 0) {
                            e.setCancelled(true);
                            EnderCrystal crystalred1 = (EnderCrystal) e.getEntity();
                            crystalred1.remove();
                            redTower1Alive = false;
                        }
                        e.setCancelled(true);
                    }

                    if (e.getEntity().getCustomName() == "Red Tower 2") {
                        redtower2health--;

                        player.sendMessage(e.getEntity().getCustomName() + " has " + redtower2health + " left");
                        ScoreBoardManager.updateScoreBoard(player);
                        if (redtower2health <= 0) {
                            e.setCancelled(true);
                            EnderCrystal crystalred2 = (EnderCrystal) e.getEntity();
                            crystalred2.remove();
                            redTower2Alive = false;
                        }
                        e.setCancelled(true);
                    }
                    if (e.getEntity().getCustomName() == "Blue Tower 1") {
                        bluetower1health--;

                        player.sendMessage(e.getEntity().getCustomName() + " has " + bluetower1health + " left");
                        ScoreBoardManager.updateScoreBoard(player);
                        if (bluetower1health <= 0) {
                            e.setCancelled(true);
                            EnderCrystal crystalblue1 = (EnderCrystal) e.getEntity();
                            crystalblue1.remove();
                            blueTower1Alive = false;
                        }
                        e.setCancelled(true);
                    }
                    if (e.getEntity().getCustomName() == "Blue Tower 2") {
                        bluetower2health--;

                        player.sendMessage(e.getEntity().getCustomName() + " has " + bluetower2health + " left");
                        ScoreBoardManager.updateScoreBoard(player);
                        if (bluetower2health <= 0) {
                            e.setCancelled(true);
                            EnderCrystal crystalblue2 = (EnderCrystal) e.getEntity();
                            crystalblue2.remove();
                            blueTower2Alive = false;

                        }
                        e.setCancelled(true);
                    }

                    if (e.getEntity().getCustomName() == "Blue Core") {
                        blueCoreHealth--;
                        player.sendMessage(e.getEntity().getCustomName() + " has " + blueCoreHealth + " left");
                        if (blueCoreHealth <= 0) {
                            e.setCancelled(true);
                            EnderCrystal crystalblue2 = (EnderCrystal) e.getEntity();
                            crystalblue2.remove();
                            Bukkit.broadcastMessage("Red Team has Won the game");
                            e.setCancelled(true);
                        }

                        if (e.getEntity().getCustomName() == "Red Core") {
                            redCoreHealth--;
                            player.sendMessage(e.getEntity().getCustomName() + " has " + redCoreHealth + " left");
                            if (redCoreHealth <= 0) {
                                e.setCancelled(true);
                                EnderCrystal crystalblue1 = (EnderCrystal) e.getEntity();
                                crystalblue1.remove();
                                Bukkit.broadcastMessage("Blue Team has Won the game");
                            }
                            e.setCancelled(true);
                        }
                    }

                }
                e.setCancelled(true);
            }
        }
    @basicmark Yeah i have.
     
  7. because you used /tnt, try to place the tnt and give it fire manually
     
  8. EnderCrystals call EntityDamageEvent and ExplosionPrimeEvent when they receive damage. It's strange that the damage event wasn't enough of a fix... Try to handle the prime event too and see how that goes?
     
  9. its still an explosion. Anyways i need to be able to stop any explosion. I made a small plugin to test this further where i create an explosion to test if it destroys the endercrystal which it sadly does.
    Command Code:
    Code (Java):
    public class ExplosionCommand implements CommandExecutor {

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("ExplosionTest")) {
                player.getWorld().createExplosion(player.getEyeLocation().getX(),
                        player.getEyeLocation().getY(), player.getEyeLocation().getZ(), 5, false,
                        false);
            }
            return false;
        }

    }
    Listeners Code:
    Code (Java):
    public class Listeners implements Listener {

        @EventHandler
        public void onDamageByExplosion(EntityDamageEvent e) {
            if (e.getEntityType().equals(EntityType.ENDER_CRYSTAL)) {
                e.setCancelled(true);
            }

        }

        @EventHandler
        public void onExplode(EntityExplodeEvent event) {
            if (event.getEntityType().equals(EntityType.ENDER_CRYSTAL)) {
                event.setCancelled(true);
            }

        }

        @EventHandler
        public void onDamageByEntity(EntityDamageByEntityEvent e) {

            if (e.getEntityType().equals(EntityType.ENDER_CRYSTAL)) {
                e.setCancelled(true);
            }

        }
     
    }
     
    @iPyronic
    @MrRicky_
     
  10. @iPyronic ExplosionPrimeEvent was the solution thx so much
     
    • Friendly Friendly x 1