[NMS] Stop Custom Entity being Pushable

Discussion in 'Plugin Development' started by CrazyGuy3000, Apr 8, 2014.

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

    CrazyGuy3000

    Ok, so to save everybody's eyes here I went through and just hastebin'd the code I currently have. I was wondering how in the world I can @Override the event for this custom entity so that it cant be pushed around by players.

    Arcade.java
    Code:java
    1. package com.Twipply.ArcadeZombie;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4.  
    5. public class Arcade extends JavaPlugin {
    6.  
    7. @Override
    8. public void onEnable(){
    9. CustomEntityType.registerEntities();
    10. }
    11.  
    12. @Override
    13. public void onDisable(){
    14. CustomEntityType.unregisterEntities();
    15. }
    16.  
    17. }


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


    CustomEntityZombie.java
    Code:java
    1. package com.Twipply.ArcadeZombie;
    2.  
    3. import java.lang.reflect.Field;
    4.  
    5. import net.minecraft.server.v1_7_R1.EntityHuman;
    6. import net.minecraft.server.v1_7_R1.EntitySkeleton;
    7. import net.minecraft.server.v1_7_R1.EntityVillager;
    8. import net.minecraft.server.v1_7_R1.EntityZombie;
    9. import net.minecraft.server.v1_7_R1.GenericAttributes;
    10. import net.minecraft.server.v1_7_R1.PathfinderGoalFloat;
    11. import net.minecraft.server.v1_7_R1.PathfinderGoalHurtByTarget;
    12. import net.minecraft.server.v1_7_R1.PathfinderGoalLookAtPlayer;
    13. import net.minecraft.server.v1_7_R1.PathfinderGoalMeleeAttack;
    14. import net.minecraft.server.v1_7_R1.PathfinderGoalMoveThroughVillage;
    15. import net.minecraft.server.v1_7_R1.PathfinderGoalMoveTowardsRestriction;
    16. import net.minecraft.server.v1_7_R1.PathfinderGoalNearestAttackableTarget;
    17. import net.minecraft.server.v1_7_R1.PathfinderGoalRandomLookaround;
    18. import net.minecraft.server.v1_7_R1.PathfinderGoalRandomStroll;
    19. import net.minecraft.server.v1_7_R1.PathfinderGoalSelector;
    20. import net.minecraft.server.v1_7_R1.World;
    21.  
    22. import org.bukkit.craftbukkit.v1_7_R1.util.UnsafeList;
    23. import org.bukkit.entity.EntityType;
    24.  
    25. public class CustomEntityZombie extends EntityZombie {
    26.  
    27. public CustomEntityZombie(World world) {
    28. super(world);
    29. super.setCustomName("Zombie");
    30. super.setCustomNameVisible(true);
    31.  
    32. try {
    33. Field bField = PathfinderGoalSelector.class.getDeclaredField("b");
    34. bField.setAccessible(true);
    35. Field cField = PathfinderGoalSelector.class.getDeclaredField("c");
    36. cField.setAccessible(true);
    37. bField.set(goalSelector, new UnsafeList<PathfinderGoalSelector>());
    38. bField.set(targetSelector, new UnsafeList<PathfinderGoalSelector>());
    39. cField.set(goalSelector, new UnsafeList<PathfinderGoalSelector>());
    40. cField.set(targetSelector, new UnsafeList<PathfinderGoalSelector>());
    41. } catch (Exception exc) {
    42. exc.printStackTrace();
    43. // This means that the name of one of the fields changed names or declaration and will have to be re-examined.
    44. }
    45.  
    46. this.goalSelector.a(0, new PathfinderGoalFloat(this));
    47. this.goalSelector.a(2, new PathfinderGoalMeleeAttack(this, EntityHuman.class, 0.0D, false));
    48. this.goalSelector.a(4, new PathfinderGoalMeleeAttack(this, EntitySkeleton.class, 0.0D, true));
    49. this.goalSelector.a(5, new PathfinderGoalMoveTowardsRestriction(this, 0.0D));
    50. this.goalSelector.a(6, new PathfinderGoalMoveThroughVillage(this, 0.0D, false));
    51. this.goalSelector.a(7, new PathfinderGoalRandomStroll(this, 0.0D));
    52. this.goalSelector.a(8, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, 8.0F));
    53. this.goalSelector.a(8, new PathfinderGoalRandomLookaround(this));
    54. this.targetSelector.a(1, new PathfinderGoalHurtByTarget(this, true));
    55. this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityHuman.class, 0, false));
    56. this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntitySkeleton.class, 0, false));
    57. }
    58.  
    59. @Override
    60. protected void aD() {
    61. super.aD();
    62. this.getAttributeInstance(GenericAttributes.e).setValue(300.0D); // Original 3.0D
    63. }
    64.  
    65. }
    66.  
     
  2. Offline

    RawCode

    what your code does
    what you code not does
    what you want from this code

    ?
     
  3. Offline

    CrazyGuy3000

    If you cant read it, it spawns in a custom entity with a custom name and I want to make this entity no pushable by players as stated at the top "@Override the event for this custom entity so that it cant be pushed around by players.".
     
  4. Offline

    RawCode

    if this code does what you want...

    what is your question?

    i perfectly able to read, but unable to find what you want from community.
     
  5. Offline

    CrazyGuy3000

    @Override the event for this custom entity so that it cant be pushed around by players.
    In nowhere in my message have I stated that this does what I want. I have simply placed code so I don't get the people yelling at me "Can we see what you have so far" or "Have you even tried making this?"

    Sorry if this message sounds a little annoyed/angry. Im not annoyed or angry at you I just cant find a way to put that nicely.


    chasechocolate I hear your good at NMS, any halp x3?

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

    xTrollxDudex

  7. Offline

    RawCode

    You did literally NOTHING to solve your issue, there are ZERO debug messages in your code, you ever can't tell what method you override does and when that method called, since you not outputting any message from that method.

    NMS coding requires some skill at original research and that skill includes ability to place debug output.
     
  8. Offline

    CrazyGuy3000

    I wasn't trying to solve my issue, I didn't know the method nor did the people who I have contacted yet so far. I am still learning NMS so theres no need to get angry/annoyed at me, I cant just tell people 8 and expect them to know i'm speaking about 4 + 4

    xTrollxDudex

    Code:
      @Override
      public void collide(Entity arg0) {
            if (arg0 instanceof Zombie) {
                super.collide((EntityZombie) arg0);
            }
        }
    Gives me a error asking if I want to delete @Override
    Error: The method collide(Entity) of type CustomEntityZombie must override or implement a supertype method

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

    RawCode

    System.out.println("message") will provide you with viable information about codeflow.

    type error you got to google and you will get reason for this and solution.

    i will provide you a spoiler
    Code:
        public double f(Entity entity) {
            double d0 = this.locX - entity.locX;
            double d1 = this.locY - entity.locY;
            double d2 = this.locZ - entity.locZ;
    
            return d0 * d0 + d1 * d1 + d2 * d2;
        }
    
        public void b_(EntityHuman entityhuman) {}
    
        public void collide(Entity entity) {
            if (entity.passenger != this && entity.vehicle != this) {
                double d0 = entity.locX - this.locX;
                double d1 = entity.locZ - this.locZ;
                double d2 = MathHelper.a(d0, d1);
    
                if (d2 >= 0.009999999776482582D) {
                    d2 = (double) MathHelper.sqrt(d2);
                    d0 /= d2;
                    d1 /= d2;
                    double d3 = 1.0D / d2;
    
                    if (d3 > 1.0D) {
                        d3 = 1.0D;
                    }
    
                    d0 *= d3;
                    d1 *= d3;
                    d0 *= 0.05000000074505806D;
                    d1 *= 0.05000000074505806D;
                    d0 *= (double) (1.0F - this.Y);
                    d1 *= (double) (1.0F - this.Y);
                    this.g(-d0, 0.0D, -d1);
                    entity.g(d0, 0.0D, d1);
                }
            }
        }
    
    This is superclass method, you can't skip it, but, you allowed to alter object state before calling it, soo some conditions wont pass.
     
  10. Offline

    CrazyGuy3000

    If you wont give me a "You need to learn yourself" or a "Just google it, there will be a answer". How do I stop this? I am still super nooby at NMS,
     
  11. Offline

    RawCode

    this is generic java knowlage about methods and inheritance and not related to bukkit or NMS.
    try reading about inheritance and method signatures and try again.

    if i just give you solution now (or anyone else, its literally two lines of code) you wont learn anything.
     
    Thury likes this.
Thread Status:
Not open for further replies.

Share This Page