Getting maxHealth and health of EntityLiving using craftbukkit

Discussion in 'Plugin Development' started by nielsbwashere, May 14, 2015.

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

    nielsbwashere

    Hey guys, nielsbwashere here.
    For a while I have been wondering how to fix the error 'method getMaxHealth is ambiguous for type EntityLiving' and so I came up with a way WITHOUT using bukkit instead of craftbukkit.
    So what I do down here, is I look for the class of the entityliving (argument 1) I get its methods using reflection and I check if the name is equal to "getMaxHealth" and the return type is a double. Down here there's code for getMaxHealth and getHealth:

    Code:
    public static double getHealth(LivingEntity hitEntity) {
            Method[] met = hitEntity.getClass().getMethods();
            for(Method m : met){
                if(m.getName().equals("getHealth")&&m.getReturnType()==double.class)
                    try {
                        return (double) m.invoke(hitEntity);
                    } catch (Exception e) {
                        System.out.println("Couldn't get the health of the entity!");
                    }
            }
            return 0;
        }
        public static double getMaxHealth(LivingEntity hitEntity) {
            Method[] met = hitEntity.getClass().getMethods();
            for(Method m : met){
                if(m.getName().equals("getMaxHealth")&&m.getReturnType()==double.class)
                    try {
                        return (double) m.invoke(hitEntity);
                    } catch (Exception e) {
                        System.out.println("Couldn't get the max health of the entity!");
                    }
            }
            return 0;
        }
    Of course since you have A LOT of methods that are ambiguous in the craftbukkit project, I decided to make a version of this that allows you to get ANY ambiguous double method invoked.

    Code:
    public static double getAmbiguous(Object o, Class<?> c, String methodName) {
            Method[] met = c.getMethods();
            for(Method m : met){
                if(m.getName().equals(methodName)&&m.getReturnType()==double.class)
                    try {
                        return (double) m.invoke(o);
                    } catch (Exception e) {
                        System.out.println("Couldn't get the ambiguous method!");
                    }
            }
            return 0;
        }
    Hopefully this code will help some people, as it helped me. No longer you have to worry about getting ambiguous methods invoked! Just use "getAmbiguous(entity, entity.getClass(), "getHealth")" and you are good to go! It even works with getting the damage of the EntityDamagedByEntityEvent! "getAmbiguous(event, event.getClass(), "getDamage")"!

    Thanks for looking at this thread! :D

    P.S. I am sorry moderators... I didn't know where to post this :S
     
  2. Online

    timtower Moderator Moderator

    Moved to plugin development.
    @nielsbwashere Isn't this a bit overkill?
     
  3. Offline

    nielsbwashere

    Well, if people want to use craftbukkit, which has stuff like CraftPlayer and stuff (if I'm right bukkit doesn't), then this could be the way. (Or you would use NMS, which is literally not readable at all)
    I don't really know, at least this has helped for me.
     
  4. Offline

    Zombie_Striker

    Just cast the Entity to a Damageable and use the .getMaxHealth() and .setMaxHealth() from there. This seems a bit overkill instead of using those two lines.
     
  5. Offline

    nielsbwashere

    Well, the point is that this is a fix for ambiguous methods. How are you going to do it for EntityDamageByEntityEvent? The getDamage() is ambiguous in there...
     
  6. Online

    timtower Moderator Moderator

  7. Offline

    nielsbwashere

  8. Offline

    AdamQpzm

    @nielsbwashere This is a wonderful way to waste resources for a problem that doesn't exist. There is no justification for this 'solution' above depending on both Bukkit and CraftBukkit, other than laziness to the point at which one shouldn't be programming at all.
     
  9. Offline

    Zombie_Striker

    @nielsbwashere How does this fix the ambiguous method? It will still be ambiguous even if you add this. All this is really is a giant way to avoid Bukkit's code with a method that extracts data from other classes. Damageable is already apart of Bukkit's code and can be casted to any Entity.
     
  10. Offline

    nielsbwashere

    Calm down everybody, I was just trying to be helpful, geez :p
     
  11. Offline

    Zombie_Striker

    @nielsbwashere The only reasons why we are posting here is because this is supposed to be a tutorial, and this is a bad way to get the health of an entity.
     
  12. Offline

    RingOfStorms

    Yea not to be overly mean, but your ambiguous "error" is literally just your IDE complaining. It still compiles fine and will work regardless of the ambiguous warning, so from the get go there is no reason to try and avoid it. And the only good way to fix that warning is to simply set up proper dependencies and then your IDE will no longer show you the warning. And even after that reflection should be the LAST resort to fix something of this nature. People could be calling the health methods incredibly often and reflection will slow down the process immensely. So in the end this tutorial is simply a longer/harder/and performance eating solution to a problem that isn't even a problem.
     
Thread Status:
Not open for further replies.

Share This Page