Controlling arrow using NMS

Discussion in 'Plugin Development' started by Woef2001, Jun 18, 2014.

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

    Woef2001

    I had an idea so when you shoot an arrow you would ride it and also control it.
    I got the riding stuff already done but now the controls...

    there are some tutorials on this forum like this one but they are about LivingEntities like zombies.
    i've gotten this code already but it gives me an error.

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


    CustomEntityArrow:
    Code:java
    1. package nl.woef2001.game.util.customentities;
    2.  
    3. import net.minecraft.server.v1_7_R3.EntityArrow;
    4. import net.minecraft.server.v1_7_R3.World;
    5.  
    6. public class CustomEntityArrow extends EntityArrow {
    7.  
    8. public CustomEntityArrow(World world) {
    9. super(world);
    10. }
    11.  
    12. }
    13.  


    I don't know what i did wrong maybe because it isn't an LivingEntity.
    Then i also wanna know which thingy i have to use to control the arrow.
     
  2. Offline

    thepaperboy99

    Where's the error?
     
  3. Offline

    Woef2001

    Code:java
    1. //it sais that 'ARROW' is undefined.
    2. ARROW("Arrow", 10, EntityType.ARROW, EntityArrow.class, CustomEntityArrow.class),
     
  4. Offline

    thepaperboy99

    But where's the stack trace?
     
  5. Offline

    blablubbabc

    The arrow class doesn't extend EntityInsentient.
     
  6. Offline

    Woef2001

    okay i've removed the implement and solved the errors but now i wanna know how to control the arrow.
    because it does it in an total different way than living entities

    Do i have to use the a(float f, float f1, float f2) in the Entity class?

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
Thread Status:
Not open for further replies.

Share This Page