NMS How to override default Minecraft mobs

Discussion in 'Resources' started by TeeePeee, Jan 6, 2014.

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

    hsndmrts98

    Code:
    @Override
        protected void aD(){
            super.aD();
            getAttributeInstance(GenericAttributes.maxHealth).setValue(48.0D);
        }
    why void have under red line?
    http://snag.gy/4icp3.jpg that error have, why? what should i do?
     
  2. Online

    timtower Moderator Moderator

    @hsndmrts98 Probably due to the fact that this is an old tutorial and might be outdated. NMS changes a lot.
     
  3. Offline

    hsndmrts98

    yes but how i can?
     
  4. Online

    timtower Moderator Moderator

    Not something that I know, this is not something that I stay up to date with.
     
  5. Offline

    hsndmrts98

    edit: okey i founded this:
    Code:
    @Override
        protected void initAttributes(){
            super.aD();
            getAttributeMap().b(GenericAttributes.ATTACK_DAMAGE).setValue(100.D);
            getAttributeMap().b(GenericAttributes.maxHealth).setValue(1000.D);
            getAttributeMap().b(GenericAttributes.MOVEMENT_SPEED).setValue(1.0D);
            getAttributeMap().b(GenericAttributes.c).setValue(0.0D);
        }
    location: net mineraftserver 1.8... entityzombie 49. line
     
  6. Offline

    Europia79

    Here's the updated code for 1_9_R1

    Code:java
    1. package mc.euro.extraction.nms.v1_9_R1;
    2.  
    3. import java.lang.reflect.Field;
    4. import java.util.List;
    5. import java.util.Map;
    6.  
    7. import net.minecraft.server.v1_9_R1.BiomeBase;
    8. import net.minecraft.server.v1_9_R1.BiomeBase.BiomeMeta;
    9. import net.minecraft.server.v1_9_R1.EntityInsentient;
    10. import net.minecraft.server.v1_9_R1.EntityTypes;
    11. import net.minecraft.server.v1_9_R1.EntityVillager;
    12.  
    13. import org.bukkit.entity.EntityType;
    14.  
    15. /**
    16. * [URL]http://forums.bukkit.org/threads/nms-tutorial-how-to-override-default-minecraft-mobs.216788/[/URL]
    17. *
    18. * @author TeeePeee
    19. */
    20. public enum CustomEntityType {
    21.  
    22. HOSTAGE("Villager", 120, EntityType.VILLAGER, EntityVillager.class, CraftHostage.class);
    23.  
    24. public String author = "TeeePeee";
    25. public String source = "[URL]http://forums.bukkit.org/threads/nms-tutorial-how-to-override-default-minecraft-mobs.216788/[/URL]";
    26.  
    27. private String name;
    28. private int id;
    29. private EntityType entityType;
    30. private Class<? extends EntityInsentient> nmsClass;
    31. private Class<? extends EntityInsentient> customClass;
    32.  
    33. private CustomEntityType(String name, int id, EntityType entityType,
    34. Class<? extends EntityInsentient> nmsClass,
    35. Class<? extends EntityInsentient> customClass) {
    36. this.name = name;
    37. this.id = id;
    38. this.entityType = entityType;
    39. this.nmsClass = nmsClass;
    40. this.customClass = customClass;
    41. }
    42.  
    43. public String getName() {
    44. return name;
    45. }
    46.  
    47. public int getID() {
    48. return id;
    49. }
    50.  
    51. public EntityType getEntityType() {
    52. return entityType;
    53. }
    54.  
    55. public Class<? extends EntityInsentient> getNMSClass() {
    56. return nmsClass;
    57. }
    58.  
    59. public Class<? extends EntityInsentient> getCustomClass() {
    60. return customClass;
    61. }
    62.  
    63. /**
    64.   * Register our entities.
    65.   */
    66. public static void registerEntities() {
    67. for (CustomEntityType entity : values()) {
    68. a(entity.getCustomClass(), entity.getName(), entity.getID());
    69. }
    70.  
    71. for (BiomeBase biomeBase : BiomeBase.REGISTRY_ID) {
    72. if (biomeBase == null) {
    73. break;
    74. }
    75.  
    76. // Names changed from J, K, L and M.
    77. // Names changed from as, at, au, av
    78. // Names changed from aw, at, au, av
    79. for (String field : new String[]{"u", "v", "w", "x"}) {
    80. try {
    81. Field list = BiomeBase.class.getDeclaredField(field);
    82. list.setAccessible(true);
    83. @SuppressWarnings("unchecked")
    84. List<BiomeMeta> mobList = (List<BiomeMeta>) list.get(biomeBase);
    85.  
    86. // Write in our custom class.
    87. for (BiomeMeta meta : mobList) {
    88. for (CustomEntityType entity : values()) {
    89. if (entity.getNMSClass().equals(meta.b)) {
    90. meta.b = entity.getCustomClass();
    91. }
    92. }
    93. }
    94. } catch (Exception ex) {
    95. ex.printStackTrace();
    96. }
    97. }
    98. }
    99. }
    100.  
    101. /**
    102.   * Unregister our entities to prevent memory leaks. Call on disable.
    103.   */
    104. public static void unregisterEntities() {
    105. for (CustomEntityType entity : values()) {
    106. // Remove our class references.
    107. try {
    108. ((Map) getPrivateStatic(EntityTypes.class, "d")).remove(entity.getCustomClass());
    109. } catch (Exception ex) {
    110. ex.printStackTrace();
    111. }
    112.  
    113. try {
    114. ((Map) getPrivateStatic(EntityTypes.class, "f")).remove(entity.getCustomClass());
    115. } catch (Exception e) {
    116. e.printStackTrace();
    117. }
    118. }
    119.  
    120. for (CustomEntityType entity : values()) {
    121. try {
    122. // Unregister each entity by writing the NMS back in place of the custom class.
    123. a(entity.getNMSClass(), entity.getName(), entity.getID());
    124. } catch (Exception ex) {
    125. ex.printStackTrace();
    126. }
    127. }
    128.  
    129. for (BiomeBase biomeBase : BiomeBase.REGISTRY_ID) {
    130. if (biomeBase == null) {
    131. break;
    132. }
    133.  
    134. // The list fields changed names but update the meta regardless.
    135. for (String field : new String[]{"u", "v", "w", "x"}) {
    136. try {
    137. Field list = BiomeBase.class.getDeclaredField(field);
    138. list.setAccessible(true);
    139. @SuppressWarnings("unchecked")
    140. List<BiomeMeta> mobList = (List<BiomeMeta>) list.get(biomeBase);
    141.  
    142. // Make sure the NMS class is written back over our custom class.
    143. for (BiomeMeta meta : mobList) {
    144. for (CustomEntityType entity : values()) {
    145. if (entity.getCustomClass().equals(meta.b)) {
    146. meta.b = entity.getNMSClass();
    147. }
    148. }
    149. }
    150. } catch (Exception ex) {
    151. ex.printStackTrace();
    152. }
    153. }
    154. }
    155. }
    156.  
    157. /**
    158.   * A convenience method.
    159.   *
    160.   * @param clazz The class.
    161.   * @param f The string representation of the private static field.
    162.   * @return The object found
    163.   * @throws Exception if unable to get the object.
    164.   */
    165. private static Object getPrivateStatic(Class clazz, String f) throws Exception {
    166. Field field = clazz.getDeclaredField(f);
    167. field.setAccessible(true);
    168. return field.get(null);
    169. }
    170.  
    171. /*
    172.   * Since 1.7.2 added a check in their entity registration, simply bypass it and write to the maps ourself.
    173.   */
    174. private static void a(Class paramClass, String paramString, int paramInt) {
    175. try {
    176. ((Map) getPrivateStatic(EntityTypes.class, "c")).put(paramString, paramClass);
    177. ((Map) getPrivateStatic(EntityTypes.class, "d")).put(paramClass, paramString);
    178. ((Map) getPrivateStatic(EntityTypes.class, "e")).put(Integer.valueOf(paramInt), paramClass);
    179. ((Map) getPrivateStatic(EntityTypes.class, "f")).put(paramClass, Integer.valueOf(paramInt));
    180. ((Map) getPrivateStatic(EntityTypes.class, "g")).put(paramString, Integer.valueOf(paramInt));
    181. } catch (Exception ex) {
    182. // Unable to register the new class.
    183. ex.printStackTrace();
    184. }
    185. }
    186. }
    187.  
    188.  
     
    ChipDev likes this.
  7. Offline

    Sillyman

    This was very helpful

    Thank you!
     
    Last edited: May 19, 2016
Thread Status:
Not open for further replies.

Share This Page