[Tutorial] Utilizing the Boss Health Bar

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

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

    darkness1999

    It's now working fine for me. Thanks! <3
     
  2. Offline

    DevRosemberg

    chasechocolate i tried some more times to create a health bar like mineplex's one but it failed and i dont think im the only one hating the particles spawning each time a ender dragon spawns. Eitherway, can you provide some code to do this please?
     
  3. Offline

    darkness1999

    chasechocolate DevRosemberg

    I think the loading bar (v2) is not working. My code:
    Code:
                        BarAPI.displayLoadingBar(p, chosenbossbarmessage, chosenbossbarmessage, 20, true, false);
    The Bar does show up (but too short). But the health of the enderdragon does not change. Maybe you should have a look at this.

    darkness19
     
  4. Offline

    chasechocolate

    darkness1999 I think it has to do with the override argument. Try changing the last argument from false to true.
     
  5. Offline

    darkness1999

  6. Offline

    Welite

    There is some problem with packets in 1.7.2, I think they are renamed or something ? Where can I find list of packets to get this working in 1.7.2 ?
     
  7. Offline

    BungeeTheCookie

    This doesn't work anymore. 1.7.2 = renaming of all packets + rewrite of all code. :( chasechocolate When can you update this?
     
  8. Offline

    Minnymin3

    BungeeTheCookie
    I'm working on an update for it since I utilize the boss health bar
     
  9. To update this:
    The mob spawn packet, known as Packet24MobSpawn, is now PacketPlayOutSpawnEntityLiving.
    If you change this then it should work for 1.7.
     
  10. Offline

    Quaro

    Version2 is awesome and working, i even improved it. Thanks.
     
  11. Offline

    MrOAriO

    CaptainBern nope, The field PacketPlayOutSpawnEntityLiving.c is not visible that the error
     
  12. Offline

    Minnymin3

    All the packets changed and a few things changed in the way entities are spawned with the packet etc. The lib requires an entire overhaul. I'm working on a version dependant one (as I don't need it to be version independent) that I'll post when its done.
     
  13. MrOAriO That doesn't change the fact that that is the required packet. The field not being accessible can easily be by-passed with reflection.
     
  14. Offline

    Minnymin3

  15. Offline

    SoThatsIt

    Heres a 1.7 compatible version which uses reflection :)

    Code:
    package me.sothatsit.minigame.utils;
     
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.HashMap;
    import java.util.Map;
     
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
     
    public class MobBarAPI
    {
        private static MobBarAPI instance;
        private Map< String , Dragon > dragonMap = new HashMap< String , Dragon >();
       
        public static MobBarAPI getInstance()
        {
            if ( MobBarAPI.instance == null )
                MobBarAPI.instance = new MobBarAPI();
           
            return MobBarAPI.instance;
        }
       
        public void setStatus(Player player, String text, int percent, boolean reset)
                throws IllegalArgumentException, SecurityException, InstantiationException,
                IllegalAccessException, InvocationTargetException, NoSuchMethodException
        {
            Dragon dragon = null;
           
            if ( dragonMap.containsKey(player.getName()) && !reset )
            {
                dragon = dragonMap.get(player.getName());
            }
            else
            {
                dragon = new Dragon(text, player.getLocation().add(0 , -200 , 0), percent);
                Object mobPacket = dragon.getSpawnPacket();
                sendPacket(player , mobPacket);
                dragonMap.put(player.getName() , dragon);
            }
           
            if ( text == "" )
            {
                Object destroyPacket = dragon.getDestroyPacket();
                sendPacket(player , destroyPacket);
                dragonMap.remove(player.getName());
            }
            else
            {
                dragon.setName(text);
                dragon.setHealth(percent);
                Object metaPacket = dragon.getMetaPacket(dragon.getWatcher());
                Object teleportPacket = dragon
                        .getTeleportPacket(player.getLocation().add(0 , -200 , 0));
                sendPacket(player , metaPacket);
                sendPacket(player , teleportPacket);
            }
        }
       
        private void sendPacket(Player player, Object packet)
        {
            try
            {
                Object nmsPlayer = ReflectionUtils.getHandle(player);
                Field con_field = nmsPlayer.getClass().getField("playerConnection");
                Object con = con_field.get(nmsPlayer);
                Method packet_method = ReflectionUtils.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();
            }
        }
       
        private class Dragon
        {
           
            private static final int MAX_HEALTH = 200;
            private int id;
            private int x;
            private int y;
            private int z;
            private int pitch = 0;
            private int yaw = 0;
            private byte xvel = 0;
            private byte yvel = 0;
            private byte zvel = 0;
            private float health;
            private boolean visible = false;
            private String name;
            private Object world;
           
            private Object dragon;
           
            public Dragon( String name , Location loc , int percent )
            {
                this.name = name;
                this.x = loc.getBlockX();
                this.y = loc.getBlockY();
                this.z = loc.getBlockZ();
                this.health = percent / 100F * MAX_HEALTH;
                this.world = ReflectionUtils.getHandle(loc.getWorld());
            }
           
            public void setHealth(int percent)
            {
                this.health = percent / 100F * MAX_HEALTH;
            }
           
            public void setName(String name)
            {
                this.name = name;
            }
           
            public Object getSpawnPacket() throws IllegalArgumentException, SecurityException,
                    InstantiationException, IllegalAccessException, InvocationTargetException,
                    NoSuchMethodException
            {
                Class< ? > Entity = ReflectionUtils.getCraftClass("Entity");
                Class< ? > EntityLiving = ReflectionUtils.getCraftClass("EntityLiving");
                Class< ? > EntityEnderDragon = ReflectionUtils.getCraftClass("EntityEnderDragon");
                dragon = EntityEnderDragon.getConstructor(ReflectionUtils.getCraftClass("World"))
                        .newInstance(world);
               
                Method setLocation = ReflectionUtils.getMethod(EntityEnderDragon , "setLocation" ,
                        new Class< ? >[]
                        { double.class , double.class , double.class , float.class , float.class });
                setLocation.invoke(dragon , x , y , z , pitch , yaw);
               
                Method setInvisible = ReflectionUtils.getMethod(EntityEnderDragon , "setInvisible" ,
                        new Class< ? >[]
                        { boolean.class });
                setInvisible.invoke(dragon , visible);
               
                Method setCustomName = ReflectionUtils.getMethod(EntityEnderDragon , "setCustomName" ,
                        new Class< ? >[]
                        { String.class });
                setCustomName.invoke(dragon , name);
               
                Method setHealth = ReflectionUtils.getMethod(EntityEnderDragon , "setHealth" ,
                        new Class< ? >[]
                        { float.class });
                setHealth.invoke(dragon , health);
               
                Field motX = ReflectionUtils.getField(Entity , "motX");
                motX.set(dragon , xvel);
               
                Field motY = ReflectionUtils.getField(Entity , "motX");
                motY.set(dragon , yvel);
               
                Field motZ = ReflectionUtils.getField(Entity , "motX");
                motZ.set(dragon , zvel);
               
                Method getId = ReflectionUtils.getMethod(EntityEnderDragon , "getId" ,
                        new Class< ? >[] { });
                this.id = (Integer) getId.invoke(dragon);
               
                Class< ? > PacketPlayOutSpawnEntityLiving = ReflectionUtils
                        .getCraftClass("PacketPlayOutSpawnEntityLiving");
               
                Object packet = PacketPlayOutSpawnEntityLiving.getConstructor(new Class< ? >[]
                { EntityLiving }).newInstance(dragon);
               
                return packet;
            }
           
            public Object getDestroyPacket() throws IllegalArgumentException, SecurityException,
                    InstantiationException, IllegalAccessException, InvocationTargetException,
                    NoSuchMethodException
            {
                Class< ? > PacketPlayOutEntityDestroy = ReflectionUtils
                        .getCraftClass("PacketPlayOutEntityDestroy");
               
                Object packet = PacketPlayOutEntityDestroy.getConstructors()[0].newInstance(id);
               
                return packet;
            }
           
            public Object getMetaPacket(Object watcher) throws IllegalArgumentException,
                    SecurityException, InstantiationException, IllegalAccessException,
                    InvocationTargetException, NoSuchMethodException
            {
                Class< ? > DataWatcher = ReflectionUtils.getCraftClass("DataWatcher");
               
                Class< ? > PacketPlayOutEntityMetadata = ReflectionUtils
                        .getCraftClass("PacketPlayOutEntityMetadata");
               
                Object packet = PacketPlayOutEntityMetadata.getConstructor(new Class< ? >[]
                { int.class , DataWatcher , boolean.class }).newInstance(id , watcher , true);
               
                return packet;
            }
           
            public Object getTeleportPacket(Location loc) throws IllegalArgumentException,
                    SecurityException, InstantiationException, IllegalAccessException,
                    InvocationTargetException, NoSuchMethodException
            {
                Class< ? > PacketPlayOutEntityTeleport = ReflectionUtils
                        .getCraftClass("PacketPlayOutEntityTeleport");
               
                Object packet = PacketPlayOutEntityTeleport.getConstructor(new Class< ? >[]
                { int.class , int.class , int.class , int.class , byte.class , byte.class })
                        .newInstance(this.id , loc.getBlockX() * 32 , loc.getBlockY() * 32 ,
                                loc.getBlockZ() * 32 , (byte) ( (int) loc.getYaw() * 256 / 360 ) ,
                                (byte) ( (int) loc.getPitch() * 256 / 360 ));
               
                return packet;
            }
           
            public Object getWatcher() throws IllegalArgumentException, SecurityException,
                    InstantiationException, IllegalAccessException, InvocationTargetException,
                    NoSuchMethodException
            {
                Class< ? > Entity = ReflectionUtils.getCraftClass("Entity");
                Class< ? > DataWatcher = ReflectionUtils.getCraftClass("DataWatcher");
               
                Object watcher = DataWatcher.getConstructor(new Class< ? >[]
                { Entity }).newInstance(dragon);
               
                Method a = ReflectionUtils.getMethod(DataWatcher , "a" , new Class< ? >[]
                { int.class , Object.class });
               
                a.invoke(watcher , 0 , visible ? (byte) 0 : (byte) 0x20);
                a.invoke(watcher , 6 , (Float) health);
                a.invoke(watcher , 7 , (Integer) 0);
                a.invoke(watcher , 8 , (Byte) (byte) 0);
                a.invoke(watcher , 10 , name);
                a.invoke(watcher , 11 , (Byte) (byte) 1);
                return watcher;
            }
           
        }
       
    }
    
    Code:
    package me.sothatsit.minigame.utils;
     
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.List;
     
    import org.bukkit.Bukkit;
    import org.bukkit.World;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
     
    public class ReflectionUtils
    {
        public static void sendPacket(List< Player > players, Object packet)
        {
            for ( Player p : players )
            {
                sendPacket(p , packet);
            }
        }
       
        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 Object getHandle(World world)
        {
            Object nms_entity = null;
            Method entity_getHandle = getMethod(world.getClass() , "getHandle");
            try
            {
                nms_entity = entity_getHandle.invoke(world);
            }
            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(Integer.valueOf(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;
        }
    }
    
    Enjoy

    Edit: also if anyone has a, or knows where a really good reflectionutils class is, i would be very appreciative (sorry off topic)
     
  16. SoThatsIt It's good that you use CamelCase to name you bariables but better don't capitalize the first character. This may cause problems and doesn't look nice either.
     
  17. Offline

    Minnymin3

    CaptainBern
    It won't cause problems. It will just confuse and piss off people.
     
  18. Offline

    xLoGiiKzZo

    SoThatsIt
    Code:java
    1. Object destroyPacket = dragon.getDestroyPacket();

    Throws an IllegelArgumentException everytime.
     
  19. Offline

    SoThatsIt

    if you have made plugins you would know its useful to see the whole error so i can evaluate the error properly
     
  20. Offline

    xLoGiiKzZo

    SoThatsIt
    Sorry was on my phone.
    Here's the error:
    Code:java
    1. [14:36:27 WARN]: java.lang.IllegalArgumentException
    2. [14:36:27 WARN]: at sun.reflect.GeneratedConstructorAccessor41.newInstance(Unknown Source)
    3. [14:36:27 WARN]: at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    4. [14:36:27 WARN]: at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    5. [14:36:27 WARN]: at net.desiredcraft.soulwars.utils.MobBarAPI$Dragon.getDestroyPacket(MobBarAPI.java:196)
    6. [14:36:27 WARN]: at net.desiredcraft.soulwars.utils.MobBarAPI.setStatus(MobBarAPI.java:60)
    7. [14:36:27 WARN]: at net.desiredcraft.soulwars.timers.ObeliskCaptureTimer$1.run(ObeliskCaptureTimer.java:416)
    8. [14:36:27 WARN]: at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftTask.run(CraftTask.java:58)
    9. [14:36:27 WARN]: at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345)
    10. [14:36:27 WARN]: at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:573)
    11. [14:36:27 WARN]: at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:259)
    12. [14:36:27 WARN]: at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:530)
    13. [14:36:27 WARN]: at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:442)
    14. [14:36:27 WARN]: at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617)
    15.  
     
  21. Offline

    SoThatsIt

    k, np i've fixed the issue, here is the new code

    Code:
    package me.sothatsit.minigame.utils;
     
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.HashMap;
    import java.util.Map;
     
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
     
    public class MobBarAPI
    {
        private static MobBarAPI instance;
        private Map< String , Dragon > dragonMap = new HashMap< String , Dragon >();
     
        public static MobBarAPI getInstance()
        {
            if ( MobBarAPI.instance == null )
                MobBarAPI.instance = new MobBarAPI();
         
            return MobBarAPI.instance;
        }
     
        public void setStatus(Player player, String text, int percent, boolean reset)
                throws IllegalArgumentException, SecurityException, InstantiationException,
                IllegalAccessException, InvocationTargetException, NoSuchMethodException,
                NoSuchFieldException
        {
            Dragon dragon = null;
         
            if ( dragonMap.containsKey(player.getName()) && !reset )
            {
                dragon = dragonMap.get(player.getName());
            }
            else
            {
                dragon = new Dragon(text, player.getLocation().add(0 , -200 , 0), percent);
                Object mobPacket = dragon.getSpawnPacket();
                sendPacket(player , mobPacket);
                dragonMap.put(player.getName() , dragon);
            }
         
            if ( text == "" )
            {
                Object destroyPacket = dragon.getDestroyPacket();
                sendPacket(player , destroyPacket);
                dragonMap.remove(player.getName());
            }
            else
            {
                dragon.setName(text);
                dragon.setHealth(percent);
                Object metaPacket = dragon.getMetaPacket(dragon.getWatcher());
                Object teleportPacket = dragon
                        .getTeleportPacket(player.getLocation().add(0 , -200 , 0));
                sendPacket(player , metaPacket);
                sendPacket(player , teleportPacket);
            }
        }
     
        private void sendPacket(Player player, Object packet)
        {
            try
            {
                Object nmsPlayer = ReflectionUtils.getHandle(player);
                Field con_field = nmsPlayer.getClass().getField("playerConnection");
                Object con = con_field.get(nmsPlayer);
                Method packet_method = ReflectionUtils.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();
            }
        }
     
        private class Dragon
        {
         
            private static final int MAX_HEALTH = 200;
            private int id;
            private int x;
            private int y;
            private int z;
            private int pitch = 0;
            private int yaw = 0;
            private byte xvel = 0;
            private byte yvel = 0;
            private byte zvel = 0;
            private float health;
            private boolean visible = false;
            private String name;
            private Object world;
         
            private Object dragon;
         
            public Dragon( String name , Location loc , int percent )
            {
                this.name = name;
                this.x = loc.getBlockX();
                this.y = loc.getBlockY();
                this.z = loc.getBlockZ();
                this.health = percent / 100F * MAX_HEALTH;
                this.world = ReflectionUtils.getHandle(loc.getWorld());
            }
         
            public void setHealth(int percent)
            {
                this.health = percent / 100F * MAX_HEALTH;
            }
         
            public void setName(String name)
            {
                this.name = name;
            }
         
            public Object getSpawnPacket() throws IllegalArgumentException, SecurityException,
                    InstantiationException, IllegalAccessException, InvocationTargetException,
                    NoSuchMethodException
            {
                Class< ? > Entity = ReflectionUtils.getCraftClass("Entity");
                Class< ? > EntityLiving = ReflectionUtils.getCraftClass("EntityLiving");
                Class< ? > EntityEnderDragon = ReflectionUtils.getCraftClass("EntityEnderDragon");
                dragon = EntityEnderDragon.getConstructor(ReflectionUtils.getCraftClass("World"))
                        .newInstance(world);
             
                Method setLocation = ReflectionUtils.getMethod(EntityEnderDragon , "setLocation" ,
                        new Class< ? >[]
                        { double.class , double.class , double.class , float.class , float.class });
                setLocation.invoke(dragon , x , y , z , pitch , yaw);
             
                Method setInvisible = ReflectionUtils.getMethod(EntityEnderDragon , "setInvisible" ,
                        new Class< ? >[]
                        { boolean.class });
                setInvisible.invoke(dragon , visible);
             
                Method setCustomName = ReflectionUtils.getMethod(EntityEnderDragon , "setCustomName" ,
                        new Class< ? >[]
                        { String.class });
                setCustomName.invoke(dragon , name);
             
                Method setHealth = ReflectionUtils.getMethod(EntityEnderDragon , "setHealth" ,
                        new Class< ? >[]
                        { float.class });
                setHealth.invoke(dragon , health);
             
                Field motX = ReflectionUtils.getField(Entity , "motX");
                motX.set(dragon , xvel);
             
                Field motY = ReflectionUtils.getField(Entity , "motX");
                motY.set(dragon , yvel);
             
                Field motZ = ReflectionUtils.getField(Entity , "motX");
                motZ.set(dragon , zvel);
             
                Method getId = ReflectionUtils.getMethod(EntityEnderDragon , "getId" ,
                        new Class< ? >[] { });
                this.id = (Integer) getId.invoke(dragon);
             
                Class< ? > PacketPlayOutSpawnEntityLiving = ReflectionUtils
                        .getCraftClass("PacketPlayOutSpawnEntityLiving");
             
                Object packet = PacketPlayOutSpawnEntityLiving.getConstructor(new Class< ? >[]
                { EntityLiving }).newInstance(dragon);
             
                return packet;
            }
         
            public Object getDestroyPacket() throws IllegalArgumentException, SecurityException,
                    InstantiationException, IllegalAccessException, InvocationTargetException,
                    NoSuchMethodException, NoSuchFieldException
            {
                Class< ? > PacketPlayOutEntityDestroy = ReflectionUtils
                        .getCraftClass("PacketPlayOutEntityDestroy");
             
                Object packet = PacketPlayOutEntityDestroy.newInstance();
             
                Field a = PacketPlayOutEntityDestroy.getDeclaredField("a");
                a.setAccessible(true);
                a.set(packet , new int[]
                { id });
             
                return packet;
            }
         
            public Object getMetaPacket(Object watcher) throws IllegalArgumentException,
                    SecurityException, InstantiationException, IllegalAccessException,
                    InvocationTargetException, NoSuchMethodException
            {
                Class< ? > DataWatcher = ReflectionUtils.getCraftClass("DataWatcher");
             
                Class< ? > PacketPlayOutEntityMetadata = ReflectionUtils
                        .getCraftClass("PacketPlayOutEntityMetadata");
             
                Object packet = PacketPlayOutEntityMetadata.getConstructor(new Class< ? >[]
                { int.class , DataWatcher , boolean.class }).newInstance(id , watcher , true);
             
                return packet;
            }
         
            public Object getTeleportPacket(Location loc) throws IllegalArgumentException,
                    SecurityException, InstantiationException, IllegalAccessException,
                    InvocationTargetException, NoSuchMethodException
            {
                Class< ? > PacketPlayOutEntityTeleport = ReflectionUtils
                        .getCraftClass("PacketPlayOutEntityTeleport");
             
                Object packet = PacketPlayOutEntityTeleport.getConstructor(new Class< ? >[]
                { int.class , int.class , int.class , int.class , byte.class , byte.class })
                        .newInstance(this.id , loc.getBlockX() * 32 , loc.getBlockY() * 32 ,
                                loc.getBlockZ() * 32 , (byte) ( (int) loc.getYaw() * 256 / 360 ) ,
                                (byte) ( (int) loc.getPitch() * 256 / 360 ));
             
                return packet;
            }
         
            public Object getWatcher() throws IllegalArgumentException, SecurityException,
                    InstantiationException, IllegalAccessException, InvocationTargetException,
                    NoSuchMethodException
            {
                Class< ? > Entity = ReflectionUtils.getCraftClass("Entity");
                Class< ? > DataWatcher = ReflectionUtils.getCraftClass("DataWatcher");
             
                Object watcher = DataWatcher.getConstructor(new Class< ? >[]
                { Entity }).newInstance(dragon);
             
                Method a = ReflectionUtils.getMethod(DataWatcher , "a" , new Class< ? >[]
                { int.class , Object.class });
             
                a.invoke(watcher , 0 , visible ? (byte) 0 : (byte) 0x20);
                a.invoke(watcher , 6 , (Float) health);
                a.invoke(watcher , 7 , (Integer) 0);
                a.invoke(watcher , 8 , (Byte) (byte) 0);
                a.invoke(watcher , 10 , name);
                a.invoke(watcher , 11 , (Byte) (byte) 1);
                return watcher;
            }
         
        }
     
    }
    
    this also requires the reflection util from my post above
     
    chasechocolate and Zughy like this.
  22. Offline

    Zughy

    SoThatsIt 2 questions:
    1 - If I wanna to decrease dragon health value I need to create a scheduler repeating task where I edit the dragon health percent? Because (ok, I'm half asleep now but) I don't see any "pre-writed" method in your classes
    2 - Oh my gosh, when I call the instance it's normal to have 2 filled lines of multicatching exceptions? Or there's a way to avoid them?

    Anyway, you and chasechocolate are doing an excellent work (I tried both)
     
  23. Offline

    xLoGiiKzZo

  24. Offline

    SoThatsIt

    yes, you will have to create a scheduler.

    you could just do
    catch(Exception e){e.printStackTrace();}
    otherwise, no there is no way to avoid them.
     
    Zughy likes this.
  25. Offline

    imaboy321

    So how would I get rid of the status after a certain amount of time?
     
  26. Offline

    SoThatsIt

    set it to "", just an empty string
     
  27. Offline

    Peary

    So I've been trying to update this is to the latest bukkit api and I've found theirs an error:
    Code:java
    1. mobPacket.a = (int) ENTITY_ID; //Entity ID
    2. /* The Field PacketPlayOutEntity.a is not visible */

    so the .a is not visible just like all the other letters and I've heard of some ways of breaking in with reflections but non have worked for me so far. Once I figure this out and test it i will upload the updated code
     
  28. Offline

    imaboy321

    I did that in a scheduler but it does not remove the bar.

    Code:java
    1. try {
    2. BarAPI.getInstance().setStatus(
    3. p,
    4. "§8§l[§f§lLegacyUndead§8§l]: §a§lZombies Killed§8: §c§l§n"
    5. + KillGettingAPI.getPlayerKills(p
    6. .getName()), 100, true);
    7. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin,
    8. new Runnable() {
    9. @Override
    10. public void run() {
    11. try {
    12. BarAPI.getInstance().setStatus(p,
    13. "", 100, true);
    14. // TODO Auto-generated catch block
    15. e.printStackTrace();
    16. } catch (SecurityException e) {
    17. // TODO Auto-generated catch block
    18. e.printStackTrace();
    19. } catch (InstantiationException e) {
    20. // TODO Auto-generated catch block
    21. e.printStackTrace();
    22. } catch (IllegalAccessException e) {
    23. // TODO Auto-generated catch block
    24. e.printStackTrace();
    25. // TODO Auto-generated catch block
    26. e.printStackTrace();
    27. } catch (NoSuchMethodException e) {
    28. // TODO Auto-generated catch block
    29. e.printStackTrace();
    30. } catch (NoSuchFieldException e) {
    31. // TODO Auto-generated catch block
    32. e.printStackTrace();
    33. }
    34. }
    35. }, 5 * 20);
    36. // TODO Auto-generated catch block
    37. e.printStackTrace();
    38. } catch (SecurityException e) {
    39. // TODO Auto-generated catch block
    40. e.printStackTrace();
    41. } catch (InstantiationException e) {
    42. // TODO Auto-generated catch block
    43. e.printStackTrace();
    44. } catch (IllegalAccessException e) {
    45. // TODO Auto-generated catch block
    46. e.printStackTrace();
    47. // TODO Auto-generated catch block
    48. e.printStackTrace();
    49. } catch (NoSuchMethodException e) {
    50. // TODO Auto-generated catch block
    51. e.printStackTrace();
    52. } catch (NoSuchFieldException e) {
    53. // TODO Auto-generated catch block
    54. e.printStackTrace();
    55. }


    SoThatsIt

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

    SoThatsIt

    im not sure why that wouldnt work, it should, try changing the last parameter to false instead of true
     
  30. Offline

    imaboy321

    Didn't do anything. Also the heal thing doesn't work. It always puts it as 0 instead of putting the bar part way
     
Thread Status:
Not open for further replies.

Share This Page