Solved Detect Mob Health

Discussion in 'Spigot Plugin Development' started by Jaffaaaa, Aug 8, 2018.

Thread Status:
Not open for further replies.
  1. I'm creating a boss (wither) and I need to detect when his health is at half? How do I do this?

    Class (if you need it):
    Code (Java):
    package xyz.theducc.play.wither;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Sound;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Wither;

    import xyz.theducc.play.main;

    public class spawnWitherBoss implements CommandExecutor {

       
        public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) {
            if (!label.equalsIgnoreCase("spawnWitherBoss")) {
                return false;
            }
           
            if (!sender.hasPermission("theduccbosses.witherboss")) {
                sender.sendMessage(ChatColor.DARK_RED + "You do not have permission to do this command");
                return false;
            }
           
          //---------- FIRST ANNOUNCEMENT START -----------
           
            for (final Player player : Bukkit.getOnlinePlayers()) {
                player.playSound(player.getLocation(), Sound.ENDERDRAGON_DEATH, 1.0f, 1.0f);
            }
           
            Bukkit.broadcastMessage(ChatColor.YELLOW + "--------------------------------------------");
            Bukkit.broadcastMessage(ChatColor.GREEN + "" + ChatColor.BOLD + ">> " + ChatColor.RED + "" + ChatColor.BOLD +"Boss Xela" + ChatColor.GOLD + " is currently entering the PvPArena!" + ChatColor.GREEN + "" + ChatColor.BOLD + " << ");
            Bukkit.broadcastMessage(ChatColor.DARK_PURPLE + "/warp PvP" + ChatColor.BLUE + " - " + ChatColor.LIGHT_PURPLE + "PvP has been disabled in the arena!");
            Bukkit.broadcastMessage(ChatColor.YELLOW + "--------------------------------------------");
           
            ConsoleCommandSender console = Bukkit.getServer().getConsoleSender();
            String xelaInvadingTitle = "title @a title ['',{'text':'Xela','color':'dark_purple','bold':true},{'text':' is invading...','color':'light_purple','bold':false}]";
            String xelaInvadingSubTitle = "title @a subtitle ['',{'text':'/warp PvP','color':'red','bold':true},{'text':' -','color':'aqua','bold':false},{'text':' PvP is disabled until boss death.','color':'dark_red'}]";
            Bukkit.dispatchCommand((CommandSender)console, xelaInvadingTitle);
            Bukkit.dispatchCommand((CommandSender)console, xelaInvadingSubTitle);
           
            //---------- FIRST ANNOUNCEMENT OVER -----------
            //---------- 30 SECONDS LATER THIS >> WITHER SPAWNS ANNOUNCEMENT START -----------
           
            Bukkit.getScheduler().runTaskLater(main.getPlugin(main.class), new Runnable() {
                @Override
                public void run()  {
                    Bukkit.broadcastMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "Boss: " + ChatColor.RED + "15 seconds 'till boss spawns...");
                    for (final Player player : Bukkit.getOnlinePlayers()) {
                        player.playSound(player.getLocation(), Sound.NOTE_SNARE_DRUM, 1.0f, 1.0f);
                    }
                }
            }, 300);
           
            Bukkit.getScheduler().runTaskLater(main.getPlugin(main.class), new Runnable() {
                @Override
                public void run()  {
                    Bukkit.broadcastMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "Boss: " + ChatColor.RED + "5 seconds 'till boss spawns...");
                    for (final Player player : Bukkit.getOnlinePlayers()) {
                        player.playSound(player.getLocation(), Sound.NOTE_PIANO, 1.0f, 1.0f);
                    }
                }
            }, 500);
           
            Bukkit.getScheduler().runTaskLater(main.getPlugin(main.class), new Runnable() {
                @Override
                public void run()  {
                    Bukkit.broadcastMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "Boss: " + ChatColor.RED + "4 seconds 'till boss spawns...");
                    for (final Player player : Bukkit.getOnlinePlayers()) {
                        player.playSound(player.getLocation(), Sound.NOTE_PLING, 1.0f, 1.0f);
                    }
                }
            }, 520);
           
            Bukkit.getScheduler().runTaskLater(main.getPlugin(main.class), new Runnable() {
                @Override
                public void run()  {
                    Bukkit.broadcastMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "Boss: " + ChatColor.RED + "3 seconds 'till boss spawns...");
                    for (final Player player : Bukkit.getOnlinePlayers()) {
                        player.playSound(player.getLocation(), Sound.NOTE_PLING, 1.0f, 1.0f);
                    }
                }
            }, 540);
           
            Bukkit.getScheduler().runTaskLater(main.getPlugin(main.class), new Runnable() {
                @Override
                public void run()  {
                    Bukkit.broadcastMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "Boss: " + ChatColor.RED + "2 seconds 'till boss spawns...");
                    for (final Player player : Bukkit.getOnlinePlayers()) {
                        player.playSound(player.getLocation(), Sound.NOTE_PLING, 1.0f, 1.0f);
                    }
                }
            }, 560);
           
            Bukkit.getScheduler().runTaskLater(main.getPlugin(main.class), new Runnable() {
                @Override
                public void run()  {
                    Bukkit.broadcastMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "Boss: " + ChatColor.RED + "1 seconds 'till boss spawns...");
                    for (final Player player : Bukkit.getOnlinePlayers()) {
                        player.playSound(player.getLocation(), Sound.NOTE_PLING, 1.0f, 1.0f);
                    }
                }
            }, 580);
           
            Bukkit.getScheduler().runTaskLater(main.getPlugin(main.class), new Runnable() {
                @Override
                public void run()  {
                    Player playerOof = (Player) sender;
                   
                    World pvpWorld = Bukkit.getWorld("world");
                    Wither witherEnt = (Wither) pvpWorld.spawnEntity(playerOof.getLocation(), EntityType.WITHER);
                    witherEnt.setCustomName(ChatColor.DARK_PURPLE + "" + ChatColor.BOLD + "Xela");
                   
                    Bukkit.broadcastMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "Boss: " + ChatColor.RED + "Boss has entered the world...");
                   
                    for (final Player player : Bukkit.getOnlinePlayers()) {
                        player.playSound(player.getLocation(), Sound.WITHER_SPAWN, 1.0f, 1.0f);
                    }
                   
                }
            }, 600);
           
            //---------- WITHER SPAWNS ANNOUNCEMENT OVER -----------

        return false;
        }
    }
     
     
  2. Use EntityDamageEvent, if the entity is wither then cast it to a LivingEntity then get its maxHealth, currentHealth and check if it's half.
     
  3. The Damageable interface has a getHealth function. I'm not fully sure if it also works on bosses.
     
  4. So many bad habit in here.
    1) You are not following name conditions. Classes use CamelCase while other parts use LowerCamelCase like methods, fields etc.
    2) Why are you dispatching a command for sending titles? Use the API for that ...
    3) Why are you using so many runnables? Use a single repeated task with a runnable that act like a counter. There is no need to flood the scheduler with so many tasks. Even more, is not optimized at all and looks ugly.
    4) You are casting sender before checking if it was a player or not. (Player playerOof = (Player) sender;)

    If you want to check when the wither reaches a specific health, listen for the EntityDamageEvent, check if the entity is the same you have spawned (Using metadata or maybe save the UUID of the wither inside a map). If it reaches that specific amount, work from there.
     
  5. Do I do this in the same command file or create a new event file?
     
  6. Should be in another event file but the effect will be the same.
     
  7. Could someone start me off? I'm not asking to be spoonfed but after getting started I'm usually fine.

    Code (Java):

    public class witherBossEvent implements Listener {
        @EventHandler
        public void onAttackEvent(EntityDamageEvent event){
            Entity e = event.getEntity();
            if(e.getClass().equals(spawnWitherBoss.class)){
                if (e instanceof LivingEntity) {
                    ((LivingEntity) e).getHealth();
                   
                    if (((LivingEntity) e).getHealth() <= 150) {
                        Bukkit.broadcastMessage("ALERT: BOSS NOW AT 150 HEALTH");
                    }
                }
            }
        }
    }

     
     
    #7 Jaffaaaa, Aug 9, 2018
    Last edited: Aug 9, 2018
  8. To check if entity type is Wither you sould use this:
    Code (Java):
    if (e.getEntity().getType().equals(EntityType.WITHER)) {
        // ENTITY IS WITHER
    }
    Checking if entity is a LivingEntity:
    Code (Java):
    if (e.getEntity().isValid()) {
        // CAN BE CASTED TO A LIVINGENTITY
    }
    After the cast you can access the wither health.

    This should be enough to make this work.
     
  9. I can't do .getHealth anymore for e.
    How do I pull the boss?
     
    #10 Jaffaaaa, Aug 9, 2018
    Last edited: Aug 9, 2018
  10. .
     
  11. Code (Java):
    Wither witherEvent = (Wither) event.getEntity();
                    if (witherEvent.getHealth() <= 150) {
                        Bukkit.broadcastMessage("ALERT: BOSS NOW AT 150 HEALTH");
    I did it but I don't get the message in chat.
     
  12. Debug, each time the event is called and the entity is wither broadcast its health.
     
  13. Didn't work. Nothing broadcasts.
    My code:
    Code (Java):
    package xyz.theducc.play.wither;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Wither;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;

    public class witherBossEvent implements Listener {
        @EventHandler
        public void onAttackEvent(EntityDamageEvent event){
            Entity e = event.getEntity();
            if (e.getType().equals(EntityType.WITHER)) {
               
                Wither witherEvent = (Wither) event.getEntity();
               
                double witherHealth = witherEvent.getHealth();
                Bukkit.broadcast((String) String.valueOf(witherHealth), "duckbosses.shout");
               
                if (e.isValid()) {
                        Bukkit.broadcastMessage("ALERT: BOSS NOW AT 150 HEALTH");
                }
            }
        }
    }
     
  14. Did you register the event? btw cast it to livingentity you don't need the wither features.
     
    • Like Like x 1
  15. Wow I am dumb.
    Forgot to register the event haha. I'll let you know how I get on. Might have some problems along the way. :)
     
    • Like Like x 1
  16. No need to call yourself a dumb, next time you won't forget, learn from mistakes.
     
  17. @Jaffaaaa please mark this thread as solved if your problem is solved.
     
Thread Status:
Not open for further replies.