[FRAGMENT] The method getHealth() is ambiguous for the type <EntityName>

Discussion in 'Resources' started by Cirno, Feb 2, 2014.

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

    Cirno

    As of 1.6.x, Minecraft changed how it handled health from int to float. While this is a great thing, since now we can work with decimal health, Bukkit had opted to go a step further and make it a double, essentially making it compatible with later changes. However, as many have been noticing, this is causing problems in terms of getting health or any of the changed methods that went from int to float.

    Solution?
    The most common one is to build with Bukkit before CraftBukkit. However, there are those who don't use Maven or any dependency manager, so it can be quite cumbersome redownloading Bukkit and CraftBukkit every time a new major or even minor update comes out.

    The second solution is a rather unknown one. Take this code for example:
    Code:java
    1. double healthLeft = entity.getHealth();

    Instead of directly calling getHealth() on entity, cast entity to Damageable.
    Code:java
    1. Damageable dEntity = (Damageable)entity;
    2. double healthLeft = dEntity.getHealth();

    Boom. Problem gone.

    I hope this tutorial has helped someone. Please leave comments, flames, and concerns!
     
  2. Offline

    Desle

    Or
    Code:java
    1. ((CraftEntity) entity).getHandle().getHealth();
     
  3. Or remove CraftBukkit as a dependency and add Bukkit
    Or add Bukkit as a dependency whilst keeping CraftBukkit and move Bukkit to the top of the hierarchy

    Boom.
     
    mbaxter likes this.
  4. Offline

    Cirno

     
  5. Offline

    Tirelessly

    Cirno You only need to update your bukkit version once in a blue moon, and it really isn't cumbersome to update your craftbukkit once every major update
     
  6. Offline

    Cirno

    But that takes effort :(
    Anyways, I do see your point. However, I would still prefer this because instead of downloading Bukkit and adding it to the build path, it's just an extra line of code.
     
  7. Offline

    Tirelessly

    Cirno That's true I guess.
     
  8. Offline

    RawCode

    bukkit team intentionally "broken" some stuff to discourage noobs from using NMS\cbukkit.

    this tutorial shoud not be here.
     
  9. Offline

    Cirno

    1:
    2: It's a resource section for a reason.

    3: Why discourage noobs? We were all noobs at some point; point and case, I tried to declare a private variable within a method.
     
  10. Offline

    RawCode

    NMS is not place to learn basics.

    I already explained few times why this happens, decompile class from cbukkit jar and compare with "source" you will notice "difference".
     
  11. Offline

    Conarnar

    Uhhhhhhhhhhh... what?
    Edit: ^^^^^^^^^^^^^^^^^^
     
  12. Offline

    Cirno

    I agree, NMS/OBC isn't a properly place to learn; but Resources section is named "Resources" and not "Basics".
    and I don't understand what you mean by "compare with 'source' and you will notice 'difference'". Of course it's going to be different; for one thing, the compiler had likely done optimization on some of the code, maybe added some of it's own (take enums for example; they have code shoved in post-soruce/pre-compile/somewhere in the compiling process).
     
  13. Offline

    RawCode

    learn2code, in best case visit oracle. Cirno

    my final lesson to you:
    classes compiled from same source text always same (as long as compiler same), if compiled classes NOT same, source text also not same.

    you see enums differently only becouse good old JAD was made before enums added and unable to decompile it properly (99% of tools use it for decompilation), raw bytecode looks properly.
     
  14. Offline

    Cirno

    To be completely honest, I don't understand what your "final lesson" is trying to say.
    I'm not the only one who sees enums differently; javap sees it too.
    Code:java
    1. public final class abc.SomeEnum extends java.lang.Enum<abc.SomeEnum> {
    2. public static final abc.SomeEnum A;
    3.  
    4. public static final abc.SomeEnum B;
    5.  
    6. public static final abc.SomeEnum C;
    7.  
    8. static {};
    9. Code:
    10. 0: new #1 // class abc/SomeEnum
    11. 3: dup
    12. 4: ldc #14 // String A
    13. 6: iconst_0
    14. 7: invokespecial #15 // Method "<init>":(Ljava/lang/String;I)V
    15. 10: putstatic #19 // Field A:Labc/SomeEnum;
    16. 13: new #1 // class abc/SomeEnum
    17. 16: dup
    18. 17: ldc #21 // String B
    19. 19: iconst_1
    20. 20: invokespecial #15 // Method "<init>":(Ljava/lang/String;I)V
    21. 23: putstatic #22 // Field B:Labc/SomeEnum;
    22. 26: new #1 // class abc/SomeEnum
    23. 29: dup
    24. 30: ldc #24 // String C
    25. 32: iconst_2
    26. 33: invokespecial #15 // Method "<init>":(Ljava/lang/String;I)V
    27. 36: putstatic #25 // Field C:Labc/SomeEnum;
    28. 39: iconst_3
    29. 40: anewarray #1 // class abc/SomeEnum
    30. 43: dup
    31. 44: iconst_0
    32. 45: getstatic #19 // Field A:Labc/SomeEnum;
    33. 48: aastore
    34. 49: dup
    35. 50: iconst_1
    36. 51: getstatic #22 // Field B:Labc/SomeEnum;
    37. 54: aastore
    38. 55: dup
    39. 56: iconst_2
    40. 57: getstatic #25 // Field C:Labc/SomeEnum;
    41. 60: aastore
    42. 61: putstatic #27 // Field ENUM$VALUES:[Labc/SomeEnum;
    43. 64: return
    44.  
    45. public void doThings();
    46. Code:
    47. 0: getstatic #34 // Field java/lang/System.out:Ljava/io/PrintStream;
    48. 3: aload_0
    49. 4: invokevirtual #40 // Method name:()Ljava/lang/String;
    50. 7: invokevirtual #44 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
    51. 10: return
    52.  
    53. public static abc.SomeEnum[] values();
    54. Code:
    55. 0: getstatic #27 // Field ENUM$VALUES:[Labc/SomeEnum;
    56. 3: dup
    57. 4: astore_0
    58. 5: iconst_0
    59. 6: aload_0
    60. 7: arraylength
    61. 8: dup
    62. 9: istore_1
    63. 10: anewarray #1 // class abc/SomeEnum
    64. 13: dup
    65. 14: astore_2
    66. 15: iconst_0
    67. 16: iload_1
    68. 17: invokestatic #52 // Method java/lang/System.arraycopy:(Ljava/lang/Object;ILjava/lang/Object;II)V
    69. 20: aload_2
    70. 21: areturn
    71.  
    72. public static abc.SomeEnum valueOf(java.lang.String);
    73. Code:
    74. 0: ldc #1 // class abc/SomeEnum
    75. 2: aload_0
    76. 3: invokestatic #58 // Method java/lang/Enum.valueOf:(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Enum;
    77. 6: checkcast #1 // class abc/SomeEnum
    78. 9: areturn


    Code:java
    1. package abc;
    2.  
    3. public enum SomeEnum {
    4.  
    5. A, B, C;
    6.  
    7. public void doThings(){
    8. System.out.println(this.name());
    9. }
    10.  
    11. }


    JAD decompiles successfully omits the compiler-generated code:
    Code:java
    1. package abc;
    2.  
    3. import java.io.PrintStream;
    4.  
    5. public enum SomeEnum { A, B, C;
    6.  
    7. public void doThings() {
    8. System.out.println(name());
    9. }
    10. }


    I'm not claiming to be a genius/prodigy programmer who knows the JVM backwards and forwards, sideways and diagonally, but if you're going to comment "learn2code" and then tell me to "visit" Oracle, at least backup your statement with some facts (though, may be hypocritical)
     
  15. Offline

    RawCode

    You can't claim to be genius becouse you can't ever read simple statements properly, i will direct you to right direction one more time:

    Get class hosting getHealth() method from bukkit
    Get class hosting getHealth() method from craftbukkit

    COMPARE THEM
    Take in account implementation class, interface does nothing by it's own.

    You will notice difference (if did everything properly).

    Then compare your decompiled classes with source and javadocs.

    Use my statement:
    classes compiled from same source text always same (as long as compiler same), if compiled classes NOT same, source text also not same.

    Make conclusion.

    You asking me to verify statement similar to "Sky is blue", i will reply "go outside and look upside", go oracle and read javaspecs, i wont read it for you.
     
  16. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    RawCode Are you suggesting the source creating org/bukkit/entity/Entity.java is different for Bukkit and CraftBukkit?
     
  17. Offline

    RawCode

Thread Status:
Not open for further replies.

Share This Page