[Tutorial] Utilizing the Boss Health Bar

Discussion in 'Resources' started by chasechocolate, Jul 5, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    chasechocolate

    So while I was away, I wrote up this class that allows you to send custom messages or loading bars using the boss health bar. I am not exactly sure how the entity IDs work (as in if you can send packets using the same entity ID to different players at the same time, maybe Comphenix knows?), but it seems to work fine on my test server with only me online. Here is the class:

    Code moved to Gists! View them here:

    Examples:
    Version 1:
    Code:java
    1. //Display text
    2. PacketUtils.displayTextBar(ChatColor.GREEN + "Hello there, " + player.getName() + "!", player);
    3.  
    4. //Loading bar, may be useful for gun-based plugins
    5. PacketUtils.displayLoadingBar("Reloading...", "Reloaded!", player, 10, true);

    Version 2:
    Code:java
    1. //Display bar 75% loaded for 3 seconds, overriding the existing bar (if there is one)
    2. BarAPI.displayBar(player, "Some message", 75.0F, 3, true);
    3.  
    4. //Display loading bar that lasts for 6 seconds, loading down, that doesn't override an existing bar (if there is one)
    5. BarAPI.displayLoadingBar(player, "Loading...", "Load complete!", 6, false, false);

    If you need any other usage, just Tahg me, but I think everything else if self-explanatory if you look at the methods in the class.

    So there you go, I think it's a pretty cool way to send players messages (someone should make a message broadcasting plugin with this!). If anyone sees any potential errors in this code, please be sure to point it out because I haven't done much with packets.

    Feel free to use this code in any of your projects - whether you keep them private or upload them to BukkitDev. However, I would appreciate it if you would give me some credit where necessary ;)
     
  2. Offline

    Compressions

    chasechocolate Great job with this fantastic resource! This would be perfect for creating a message board that would render mandatory viewing of it.
     
  3. Offline

    chasechocolate

    Also, some of the variable types have changed in the 1.6 update, I'll update this tomorrow.
     
  4. Offline

    SoThatsIt

    now just to change this all to use reflection and work on different versions

    added using reflection, havent tested but i think it will work

    PacketUtils Class:

    Code:
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
     
    import org.bukkit.Location;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.scheduler.BukkitRunnable;
     
    public class PacketUtils {
        public static final Integer ENTITY_ID = 1234;
     
        //Accessing packets
        public static Object getMobPacket(String text, Location loc){
            Class<?> mob_class = General.getCraftClass("Packet24MobSpawn");
            Object mobPacket = null;
            try {
                mobPacket = mob_class.newInstance();
               
                Field a = General.getField(mob_class, "a");
                a.setAccessible(true);
                a.set(mobPacket, ENTITY_ID);//Entity ID
                Field b = General.getField(mob_class, "b");
                b.setAccessible(true);
                b.set(mobPacket, EntityType.WITHER.getTypeId());//Mob type (ID: 64)
                Field c = General.getField(mob_class, "c");
                c.setAccessible(true);
                c.set(mobPacket, Math.floor(loc.getBlockX() * 32.0D));//X position
                Field d = General.getField(mob_class, "d");
                d.setAccessible(true);
                d.set(mobPacket, Math.floor(loc.getBlockY() * 32.0D));//Y position
                Field e = General.getField(mob_class, "e");
                e.setAccessible(true);
                e.set(mobPacket, Math.floor(loc.getBlockZ() * 32.0D));//Z position
                Field f = General.getField(mob_class, "f");
                f.setAccessible(true);
                f.set(mobPacket, 0);//Pitch
                Field g = General.getField(mob_class, "g");
                g.setAccessible(true);
                g.set(mobPacket, 0);//Head Pitch
                Field h = General.getField(mob_class, "h");
                h.setAccessible(true);
                h.set(mobPacket, 0);//Yaw
                Field i = General.getField(mob_class, "i");
                i.setAccessible(true);
                i.set(mobPacket, 0);//X velocity
                Field j = General.getField(mob_class, "j");
                j.setAccessible(true);
                j.set(mobPacket, 0);//Y velocity
                Field k = General.getField(mob_class, "k");
                k.setAccessible(true);
                k.set(mobPacket, 0);//Z velocity
       
                Object watcher = getWatcher(text, 300);
                Field t = General.getField(General.getCraftClass("Packet24MobSpawn"), "t");
                t.setAccessible(true);
                t.set(mobPacket, watcher);
            } catch (InstantiationException e1) {
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                e1.printStackTrace();
            }
     
            return mobPacket;
        }
     
        public static Object getDestroyEntityPacket(){
            Class<?> packet_class = General.getCraftClass("Packet29DestroyEntity");
            Object packet = null;
            try {
                packet = packet_class.newInstance();
               
                Field a = General.getField(packet_class, "a");
                a.setAccessible(true);
                a.set(packet, new int[]{ENTITY_ID});
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
     
            return packet;
        }
     
        public static Object getMetadataPacket(Object watcher){
            Class<?> packet_class = General.getCraftClass("Packet40EntityMetadata");
            Object packet = null;
            try {
                packet = packet_class.newInstance();
               
                Field a = General.getField(packet_class, "a");
                a.setAccessible(true);
                a.set(packet, ENTITY_ID);
               
                Method watcher_c = General.getMethod(watcher.getClass(), "c");
               
                Field b = General.getField(packet_class, "b");
                b.setAccessible(true);
                b.set(packet, watcher_c.invoke(watcher));
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
     
            return packet;
        }
     
        public static Object getRespawnPacket(){
            Class<?> packet_class = General.getCraftClass("Packet205ClientCommand");
            Object packet = null;
            try {
                packet = packet_class.newInstance();
               
                Field a = General.getField(packet_class, "a");
                a.setAccessible(true);
                a.set(packet, 1);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
     
            return packet;
        }
     
        public static Object getWatcher(String text, int health){
            Class<?> watcher_class = General.getCraftClass("DataWatcher");
            Object watcher = null;
            try {
                watcher = watcher_class.newInstance();
               
                Method a = General.getMethod(watcher_class, "a");
                a.setAccessible(true);
                a.invoke(watcher, (Byte) (byte) 0x20);
                a.invoke(watcher, (String) text);
                a.invoke(watcher, (Byte) (byte) 1);
                a.invoke(watcher, (Integer) (int) 0);
                a.invoke(watcher, (Byte) (byte) 0);
                a.invoke(watcher, (Integer) (int) health);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
           
            return watcher;
        }
     
        //Other methods
        public static void displayTextBar(Plugin plugin, String text, final Player player){
            Object mobPacket = getMobPacket(text, player.getLocation());
     
            General.sendPacket(player, mobPacket);
     
            new BukkitRunnable(){
                @Override
                public void run(){
                    Object destroyEntityPacket = getDestroyEntityPacket();
     
                    General.sendPacket(player, destroyEntityPacket);
                }
            }.runTaskLater(plugin, 120L);
        }
     
        public static void displayLoadingBar(final Plugin plugin, final String text, final String completeText, final Player player, final int healthAdd, final long delay, final boolean loadUp){
            Object mobPacket = getMobPacket(text, player.getLocation());
     
            General.sendPacket(player, mobPacket);
     
            new BukkitRunnable(){
                int health = (loadUp ? 0 : 300);
     
                @Override
                public void run(){
                    if((loadUp ? health < 300 : health > 0)){
                        Object watcher = getWatcher(text, health);
                        Object metaPacket = getMetadataPacket(watcher);
     
                        General.sendPacket(player, metaPacket);
     
                        if(loadUp){
                            health += healthAdd;
                        } else {
                            health -= healthAdd;
                        }
                    } else {
                        Object watcher = getWatcher(text, (loadUp ? 300 : 0));
                        Object metaPacket = getMetadataPacket(watcher);
     
                        General.sendPacket(player, metaPacket);
     
                        Object destroyEntityPacket = getDestroyEntityPacket();
     
                        General.sendPacket(player, destroyEntityPacket);
     
                        //Complete text
                        Object mobPacket = getMobPacket(completeText, player.getLocation());
     
                        General.sendPacket(player, mobPacket);
     
                        Object watcher2 = getWatcher(completeText, 300);
                        Object metaPacket2 = getMetadataPacket(watcher2);
     
                        General.sendPacket(player, metaPacket2);
     
                        new BukkitRunnable(){
                            @Override
                            public void run(){
                                Object destroyEntityPacket = getDestroyEntityPacket();
     
                                General.sendPacket(player, destroyEntityPacket);
                            }
                        }.runTaskLater(plugin, 40L);
     
                        this.cancel();
                    }
                }
            }.runTaskTimer(plugin, delay, delay);
        }
     
        public static void displayLoadingBar(final Plugin plugin, final String text, final String completeText, final Player player, final int secondsDelay, final boolean loadUp){
            final int healthChangePerSecond = 300 / secondsDelay;
     
            displayLoadingBar(plugin, text, completeText, player, healthChangePerSecond, 20L, loadUp);
        }
    }
    General Class:

    Code:
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
     
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
     
    public class General {
     
        public static void sendPacket(Player p, Object packet){
            try {
                Object nmsPlayer = getHandle(p);
                Field con_field = nmsPlayer.getClass().getField("playerConnection");
                Object con = con_field.get(nmsPlayer);
                Method packet_method = getMethod(con.getClass(), "sendPacket");
                packet_method.invoke(con, packet);
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
     
        public static Class<?> getCraftClass(String ClassName){
            String name = Bukkit.getServer().getClass().getPackage().getName();
            String version = name.substring(name.lastIndexOf('.') + 1)+".";
            String className = version+ClassName;
            Class<?> c = null;
            try {
                c = Class.forName(className);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            return c;
        }
     
        public static Object getHandle(Entity entity){
            Object nms_entity = null;
            Method entity_getHandle = getMethod(entity.getClass(), "getHandle");
            try {
                nms_entity = entity_getHandle.invoke(entity);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            return nms_entity;
        }
     
        public static Field getField(Class<?> cl, String field_name){
            try {
                Field field = cl.getDeclaredField(field_name);
                return field;
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            return null;
        }
     
        public static Method getMethod(Class<?> cl, String method, Integer args) {
            for(Method m : cl.getMethods()) {
                if(m.getName().equals(method) && args.equals(new Integer(m.getParameterTypes().length))) {
                    return m;
                }
            }
            return null;
        }
     
        public static Method getMethod(Class<?> cl, String method) {
            for(Method m : cl.getMethods()) {
                if(m.getName().equals(method)) {
                    return m;
                }
            }
            return null;
        }
     
    }
    
    Edit: saw that i made a mistake, edited, if you used before this edit then please re-copy the classes

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  5. Offline

    Comphenix

    You can, just you have to make sure it doesn't conflict with an existing entity ID. You can also send a different ID to each player, though I don't know if that's useful in any way.

    Way ahead of you:
    https://gist.github.com/aadnk/5801424

    It uses ProtocolLib, however, but I think that's a small price to pay to get version independence.
     
    hawkfalcon, KingFaris11 and Minecrell like this.
  6. Offline

    SoThatsIt

    i tested my old Code and it did not work, here is some working code.

    FakeWither Class:

    Code:
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
     
    import org.bukkit.Location;
    import org.bukkit.entity.EntityType;
     
    public class FakeWither {
       
        public static final int MAX_HEALTH = 25;
        public boolean visible;
        public int EntityID;
        public int x;
        public int y;
        public int z;
        public int pitch = 0;
        public int head_pitch = 0;
        public int yaw = 0;
        public byte xvel = 0;
        public byte yvel = 0;
        public byte zvel = 0;
        public float health;
        public String name;
       
        public FakeWither(String name, int EntityID, Location loc){
            this(name, EntityID, (int) Math.floor(loc.getBlockX() * 32.0D), (int) Math.floor(loc.getBlockY() * 32.0D), (int) Math.floor(loc.getBlockZ() * 32.0D));
        }
       
        public FakeWither(String name, int EntityID, Location loc, float health, boolean visible){
            this(name, EntityID, (int) Math.floor(loc.getBlockX() * 32.0D), (int) Math.floor(loc.getBlockY() * 32.0D), (int) Math.floor(loc.getBlockZ() * 32.0D), health, visible);
        }
       
        public FakeWither(String name, int EntityID, int x, int y, int z){
            this(name, EntityID, x, y, z, MAX_HEALTH, false);
        }
       
        public FakeWither(String name, int EntityID, int x, int y, int z, float health, boolean visible){
            this.name=name;
            this.EntityID=EntityID;
            this.x=x;
            this.y=y;
            this.z=z;
            this.health=health;
            this.visible=visible;
        }
       
        public Object getMobPacket(){
            Class<?> mob_class = General.getCraftClass("Packet24MobSpawn");
            Object mobPacket = null;
            try {
                mobPacket = mob_class.newInstance();
               
                Field a = General.getField(mob_class, "a");
                a.setAccessible(true);
                a.set(mobPacket, EntityID);//Entity ID
                Field b = General.getField(mob_class, "b");
                b.setAccessible(true);
                b.set(mobPacket, EntityType.WITHER.getTypeId());//Mob type (ID: 64)
                Field c = General.getField(mob_class, "c");
                c.setAccessible(true);
                c.set(mobPacket, x);//X position
                Field d = General.getField(mob_class, "d");
                d.setAccessible(true);
                d.set(mobPacket, y);//Y position
                Field e = General.getField(mob_class, "e");
                e.setAccessible(true);
                e.set(mobPacket, z);//Z position
                Field f = General.getField(mob_class, "f");
                f.setAccessible(true);
                f.set(mobPacket, pitch);//Pitch
                Field g = General.getField(mob_class, "g");
                g.setAccessible(true);
                g.set(mobPacket, head_pitch);//Head Pitch
                Field h = General.getField(mob_class, "h");
                h.setAccessible(true);
                h.set(mobPacket, yaw);//Yaw
                Field i = General.getField(mob_class, "i");
                i.setAccessible(true);
                i.set(mobPacket, xvel);//X velocity
                Field j = General.getField(mob_class, "j");
                j.setAccessible(true);
                j.set(mobPacket, yvel);//Y velocity
                Field k = General.getField(mob_class, "k");
                k.setAccessible(true);
                k.set(mobPacket, zvel);//Z velocity
       
                Object watcher = getWatcher();
                Field t = General.getField(mob_class, "t");
                t.setAccessible(true);
                t.set(mobPacket, watcher);
            } catch (InstantiationException e1) {
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                e1.printStackTrace();
            }
     
            return mobPacket;
        }
     
        public Object getDestroyEntityPacket(){
            Class<?> packet_class = General.getCraftClass("Packet29DestroyEntity");
            Object packet = null;
            try {
                packet = packet_class.newInstance();
               
                Field a = General.getField(packet_class, "a");
                a.setAccessible(true);
                a.set(packet, new int[]{EntityID});
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
     
            return packet;
        }
     
        public Object getMetadataPacket(Object watcher){
            Class<?> packet_class = General.getCraftClass("Packet40EntityMetadata");
            Object packet = null;
            try {
                packet = packet_class.newInstance();
               
                Field a = General.getField(packet_class, "a");
                a.setAccessible(true);
                a.set(packet, EntityID);
               
                Method watcher_c = General.getMethod(watcher.getClass(), "c");
                Field b = General.getField(packet_class, "b");
                b.setAccessible(true);
                b.set(packet, watcher_c.invoke(watcher));
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
     
            return packet;
        }
     
        public Object getRespawnPacket(){
            Class<?> packet_class = General.getCraftClass("Packet205ClientCommand");
            Object packet = null;
            try {
                packet = packet_class.newInstance();
               
                Field a = General.getField(packet_class, "a");
                a.setAccessible(true);
                a.set(packet, 1);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
     
            return packet;
        }
     
        public Object getWatcher(){
            Class<?> watcher_class = General.getCraftClass("DataWatcher");
            Object watcher = null;
            try {
                watcher = watcher_class.newInstance();
               
                Method a = General.getMethod(watcher_class, "a", new Class<?>[] {int.class, Object.class});
                a.setAccessible(true);
               
                a.invoke(watcher, 0, visible ? (byte)0 : (byte)0x20);
                a.invoke(watcher, 6, (Float) (float) health);
                a.invoke(watcher, 7, (Integer) (int) 0);
                a.invoke(watcher, 8, (Byte) (byte) 0);
                a.invoke(watcher, 10, (String) name);
                a.invoke(watcher, 11, (Byte) (byte) 1);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
           
            return watcher;
        }
       
    }
    
    General Class:

    Code:
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
     
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
     
    public class General {
     
        public static void sendPacket(Player p, Object packet){
            try {
                Object nmsPlayer = getHandle(p);
                Field con_field = nmsPlayer.getClass().getField("playerConnection");
                Object con = con_field.get(nmsPlayer);
                Method packet_method = getMethod(con.getClass(), "sendPacket");
                packet_method.invoke(con, packet);
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
     
        public static Class<?> getCraftClass(String ClassName){
            String name = Bukkit.getServer().getClass().getPackage().getName();
            String version = name.substring(name.lastIndexOf('.') + 1)+".";
            String className = "net.minecraft.server."+version+ClassName;
            Class<?> c = null;
            try {
                c = Class.forName(className);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            return c;
        }
     
        public static Object getHandle(Entity entity){
            Object nms_entity = null;
            Method entity_getHandle = getMethod(entity.getClass(), "getHandle");
            try {
                nms_entity = entity_getHandle.invoke(entity);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            return nms_entity;
        }
     
        public static Field getField(Class<?> cl, String field_name){
            try {
                Field field = cl.getDeclaredField(field_name);
                return field;
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            return null;
        }
     
        public static Method getMethod(Class<?> cl, String method, Class<?>[] args) {
                for(Method m : cl.getMethods()) {
                    if(m.getName().equals(method) && ClassListEqual(args, m.getParameterTypes())) {
                        return m;
                    }
                }
                return null;
            }
     
        public static Method getMethod(Class<?> cl, String method, Integer args) {
            for(Method m : cl.getMethods()) {
                if(m.getName().equals(method) && args.equals(new Integer(m.getParameterTypes().length))) {
                    return m;
                }
            }
            return null;
        }
     
        public static Method getMethod(Class<?> cl, String method) {
            for(Method m : cl.getMethods()) {
                if(m.getName().equals(method)) {
                    return m;
                }
            }
            return null;
        }
     
        public static boolean ClassListEqual(Class<?>[] l1, Class<?>[] l2){
            boolean equal = true;
       
            if(l1.length != l2.length)return false;
            for(int i=0; i<l1.length; i++){
                if(l1[i] != l2[i]){equal=false;break;}
            }
       
            return equal;
        }
     
    }
    
    PacketUtils class:

    Code:
    package me.sothatsit.lgpvp;
     
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.scheduler.BukkitRunnable;
     
    public class PacketUtils {
        public static Integer ENTITY_ID = 6000;
     
        //Other methods
        public static void displayTextBar(Plugin plugin, String text, final Player player, long length){
            final FakeWither wither = new FakeWither(text, ENTITY_ID++, player.getLocation());
            Object mobPacket = wither.getMobPacket();
     
            General.sendPacket(player, mobPacket);
     
            new BukkitRunnable(){
                @Override
                public void run(){
                    Object destroyEntityPacket = wither.getDestroyEntityPacket();
     
                    General.sendPacket(player, destroyEntityPacket);
                }
            }.runTaskLater(plugin, length);
        }
     
        public static void displayLoadingBar(final Plugin plugin, final String text, final Player player, final int healthAdd, final long delay, final boolean loadUp){
            final FakeWither wither = new FakeWither(text, ENTITY_ID++, player.getLocation(), (loadUp ? 1 : FakeWither.MAX_HEALTH), false);
            Object mobPacket = wither.getMobPacket();
     
            General.sendPacket(player, mobPacket);
     
            new BukkitRunnable(){
                int health = (loadUp ? 0 : FakeWither.MAX_HEALTH);
     
                @Override
                public void run(){
                    if((loadUp ? health < FakeWither.MAX_HEALTH : health > 1)){
                        wither.name=text;
                        wither.health=health;
                        Object watcher = wither.getWatcher();
                        Object metaPacket = wither.getMetadataPacket(watcher);
     
                        General.sendPacket(player, metaPacket);
     
                        if(loadUp){
                            health += healthAdd;
                        } else {
                            health -= healthAdd;
                        }
                    } else {
                        wither.name = text;
                        wither.health = (loadUp ? FakeWither.MAX_HEALTH : 1);
                        Object watcher = wither.getWatcher();
                        Object metaPacket = wither.getMetadataPacket(watcher);
     
                        General.sendPacket(player, metaPacket);
     
                        Object destroyEntityPacket = wither.getDestroyEntityPacket();
     
                        General.sendPacket(player, destroyEntityPacket);
     
                        this.cancel();
                    }
                }
            }.runTaskTimer(plugin, delay, delay);
        }
     
        public static void displayLoadingBar(final Plugin plugin, final String text, final Player player, final int secondsDelay, final boolean loadUp){
            final int healthChangePerSecond = FakeWither.MAX_HEALTH / secondsDelay / 2;
     
            displayLoadingBar(plugin, text, player, healthChangePerSecond, 10L, loadUp);
        }
    }
    Note, when you set the health to below a certain value, not sure exactly, but when the wither becomes enraged, you can see the electrical effect that appears around its body? maybe someone knows how to fix this.

    Turns out that the max health for the wither is actually 25 for the sakes of the health bar. i will update the code above to accomodate this

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
    Darkchaosknight, Axe2760 and Alex3543 like this.
  7. Offline

    NathanG_

    Pictures? :3
     
  8. Offline

    SoThatsIt

    [​IMG]
    there you go :) note, in my version i removed the glitched health bar in the top right although if you are using the loading bar it will glitch when the withers health gets low.
     
    Minecrell and NathanG_ like this.
  9. Offline

    iTidez

    I am working on the announcement plugin you suggested. Seems like a good idea. Now we just need to get the overlay to go away with the low health!
     
  10. Offline

    SoThatsIt

    yeah, i dont think thats possible D: but anyway you can just instead set the withers health to 25 and then it wont show the glitch
     
  11. Offline

    Omerrg

    SoThatsIt
    Tell me what's not working, cause there's no error and it still doesn't work :
    Code:java
    1. loadMessages();
    2. getDelay();
    3. getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable()
    4. {
    5.  
    6. public void run()
    7. {
    8.  
    9. for(String s : messages){
    10. if(s != null){
    11. for(Player p : Bukkit.getOnlinePlayers()){
    12. PacketUtils.displayLoadingBar(plugin, s, p, 3,true);
    13. }
    14. }
    15. }
    16. }
    17. },0,delay*20L);
    18.  

    Those are the methods:
    Code:java
    1. public void getDelay(){
    2. for(String delayId : announcmentsfile.getKeys(false)){
    3. if(delayId.equals("delay")){
    4. this.delay = Integer.parseInt(announcmentsfile.getString(delayId));
    5. }
    6. }
    7. }
    8. public void loadMessages(){
    9. for(String messageId : announcmentsfile.getKeys(false)){
    10. if(announcmentsfile.isString(messageId) && messageId!="delay"){
    11. this.messages.add(announcmentsfile.getString(messageId));
    12. }
    13. }
    14. }

    and this is the config:
    Code:
    delay: 60
    message1: test
    message2: test2
     
  12. Offline

    microgeek

    Knew it wouldn't be long until it was released. I felt proud of myself when I got this working last month :3
    Nice job, none the less
    This is what I did:
    Code:
      @SuppressWarnings("unchecked")
        public static void setStatus(Player player, String message, int percent) {
            if(percent > 100) {
                percent = 100;
            }else if(percent < 0) {
                percent = 0;
            }
            //Works out nicely, the EnderDragon's max health is 200
            float computedHealth = 2 * percent;
            if(spawnedDragons.containsKey(player.getName())) {
                EntityUtil.removeEntitiesForPlayer(player, ENDER_DRAGON_ID);
            }
            spawnedDragons.put(player.getName(), message);
            HashMap<Integer, Object> meta = (HashMap<Integer, Object>) ENDER_DRAGON_META.clone();
            meta.put(5, ChatColor.translateAlternateColorCodes('&', message));
            meta.put(16, computedHealth);
            EntityUtil.spawnFakeMobForPlayer((byte) 63, player, player.getLocation().add(0, -200, 0), ENDER_DRAGON_ID, meta);
        }
     
  13. Offline

    Omerrg

    Hi, i've used your code but when i set teh text bar it shows like a white wither shield, what should i do to fix it?
     
  14. Offline

    SgtPunishment

    Random noob question, is there any way of using this class/method as a health bar for other mobs? say for example the Giant Zombie?
     
  15. Offline

    microgeek

    Not directly. You will need to update the bar based on the Zombie's health.
     
  16. Offline

    SgtPunishment

    So in theory I could... but I would need a damage event handler to update the health bar?
     
    bobacadodl likes this.
  17. Offline

    phips99

    How do I even set the health to the bar?
    SgtPunishment only listen on the entitydamageevent check if the entity is a instanceof a zombie and then set the bar to the health of it. but i still dont know how to set something to the bar
     
  18. Offline

    chasechocolate

    phips99 check out my getWatcher() and getMetaPacket() methods. For anyone wondering, I will most likely be updating this tomorrow! Some of the stuff changed in the 1.6 update.
     
  19. Offline

    XD 3VIL M0NKEY

    I wonder where that is from :D
     
  20. Offline

    Omerrg

    Guys can you help me?..
     
  21. Offline

    chasechocolate

    Omerrg I am not entirely sure about that either. I think the white shield will appear when the wither's health gets below 0, maybe Comphenix knows if there is a packet that can be modified to block it?

    EDIT: Just saw your other question. If you are displaying a message, then just use displayTextBar() rather then displayLoadingBar(). A better way to load messages would be to have a List<String> in the file and then use <variable> = <config>.getStringList("messages");
     
  22. Offline

    phips99

    chasechocolate Which method should I use if I want like to display the health of a normal pig? displayLoadingBar or displayTextBar? And with which method do I update the health bar?
     
  23. Offline

    chasechocolate

    phips99 you would have to create your own method. Take a look on how I made the displayLoadingBar() method. You will need a mob spawn packet, a data watcher and eventually a destroy entity packet. I provide simple methods to easily access them, you'll just need to find out how to use them :)
     
  24. Offline

    etaxi341

    This is Awesome! Thank you very much!
     
  25. Offline

    Omerrg

    chasechocolate Hey, thx for responding, but how can i set the wither's health so he will not have the white shield?
     
  26. Offline

    chasechocolate

    Omerrg I am not entirely sure how (if you can) to prevent it. For now, just keep the wither's health above 50% (150) and it won't display the shield.
     
  27. Offline

    Omerrg

    Wow thx for the quick answer, But i dont understand the whole code and i can't find out how to set his health to 150 xD

    (sorry for double)
    ok, i figured out how to remove the white thingie, but now i have smokie effect. like black thingies, you know how can i fix it?. I thought maybe because im using it for a kind of broadcast, i will make 1 wither in a place none can see and set his name when i need, but maybe using the simple way is better, just help me remove the smoke please :) and if there's no way to remove it tell me how to do the alternative i thought about xD

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  28. Offline

    phips99

    Omerrg and how did you do this to remove the white thingi?

    chasechocolate btw when I try to activate the displaytextbar the client crashes. and in the console of the server i cant see any error...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  29. Offline

    chasechocolate

    phips99 I haven't updated the code yet. I will tomorrow.
     
  30. Offline

    phips99

Thread Status:
Not open for further replies.

Share This Page