Solved Custom NMS entity problem

Discussion in 'Plugin Help/Development/Requests' started by 53ninja53, Apr 2, 2015.

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

    53ninja53

    Hello, I try to create custom nms entity, but when I spawn it, so it is invisible, but hitting me, walking to me, making sounds, etc. Im using CraftBukkit 1.8.3 from spigot. In console is not error. Please excuse my bad English :)

    Here is my main class:
    Code:
    public class CastleConqueror extends JavaPlugin{
        @Override
        public void onEnable(){
            registerEntity(CustomEntity.class, "CustomEntity", 253);
        }
       
        @Override
        public void onDisable(){
        }
       
       
        private void registerEntity(Class entityClass, String entityName, int entityId){
            try {
                Method a = EntityTypes.class.getDeclaredMethod("a", Class.class, String.class, int.class);
                a.setAccessible(true);
                a.invoke(a, entityClass, entityName, entityId);
            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
                e.printStackTrace();
            }
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("spawnmyentity") && sender instanceof Player) {
                try{
                CustomEntity e = new CustomEntity(((CraftWorld)((Player)sender).getWorld()).getHandle());
                e.setPosition(((Player)sender).getLocation().getX(), ((Player)sender).getLocation().getY(), ((Player)sender).getLocation().getZ());
                ((CraftWorld)((Player)sender).getWorld()).getHandle().addEntity(e);
                }
                catch(Exception e){
                    e.printStackTrace();
                }
                return true;
        }
        return false;
    }
    }
    and here is CustomEntity class:
    Code:
    public class CustomEntity extends EntityZombie{
        public CustomEntity(World world) {
            super(world);
        }
    }
    
     
  2. Moved to Bukkit Alternates.
     
  3. Offline

    Europia79

    @53ninja53

    253 isn't a valid EntityId. Therefore, you get the invisible entity.

    Code:java
    1.  
    2. static
    3. {
    4. a(EntityItem.class, "Item", 1);
    5. a(EntityExperienceOrb.class, "XPOrb", 2);
    6.  
    7. a(EntityLeash.class, "LeashKnot", 8);
    8. a(EntityPainting.class, "Painting", 9);
    9. a(EntityArrow.class, "Arrow", 10);
    10. a(EntitySnowball.class, "Snowball", 11);
    11. a(EntityLargeFireball.class, "Fireball", 12);
    12. a(EntitySmallFireball.class, "SmallFireball", 13);
    13. a(EntityEnderPearl.class, "ThrownEnderpearl", 14);
    14. a(EntityEnderSignal.class, "EyeOfEnderSignal", 15);
    15. a(EntityPotion.class, "ThrownPotion", 16);
    16. a(EntityThrownExpBottle.class, "ThrownExpBottle", 17);
    17. a(EntityItemFrame.class, "ItemFrame", 18);
    18. a(EntityWitherSkull.class, "WitherSkull", 19);
    19.  
    20. a(EntityTNTPrimed.class, "PrimedTnt", 20);
    21. a(EntityFallingBlock.class, "FallingSand", 21);
    22.  
    23. a(EntityFireworks.class, "FireworksRocketEntity", 22);
    24.  
    25. a(EntityArmorStand.class, "ArmorStand", 30);
    26.  
    27. a(EntityBoat.class, "Boat", 41);
    28. a(EntityMinecartRideable.class, EnumMinecartType.RIDEABLE.b(), 42);
    29. a(EntityMinecartChest.class, EnumMinecartType.CHEST.b(), 43);
    30. a(EntityMinecartFurnace.class, EnumMinecartType.FURNACE.b(), 44);
    31. a(EntityMinecartTNT.class, EnumMinecartType.TNT.b(), 45);
    32. a(EntityMinecartHopper.class, EnumMinecartType.HOPPER.b(), 46);
    33. a(EntityMinecartMobSpawner.class, EnumMinecartType.SPAWNER.b(), 47);
    34. a(EntityMinecartCommandBlock.class, EnumMinecartType.COMMAND_BLOCK.b(), 40);
    35.  
    36. a(EntityInsentient.class, "Mob", 48);
    37. a(EntityMonster.class, "Monster", 49);
    38.  
    39. a(EntityCreeper.class, "Creeper", 50, 894731, 0);
    40. a(EntitySkeleton.class, "Skeleton", 51, 12698049, 4802889);
    41. a(EntitySpider.class, "Spider", 52, 3419431, 11013646);
    42. a(EntityGiantZombie.class, "Giant", 53);
    43. a(EntityZombie.class, "Zombie", 54, 44975, 7969893);
    44. a(EntitySlime.class, "Slime", 55, 5349438, 8306542);
    45. a(EntityGhast.class, "Ghast", 56, 16382457, 12369084);
    46. a(EntityPigZombie.class, "PigZombie", 57, 15373203, 5009705);
    47. a(EntityEnderman.class, "Enderman", 58, 1447446, 0);
    48. a(EntityCaveSpider.class, "CaveSpider", 59, 803406, 11013646);
    49. a(EntitySilverfish.class, "Silverfish", 60, 7237230, 3158064);
    50. a(EntityBlaze.class, "Blaze", 61, 16167425, 16775294);
    51. a(EntityMagmaCube.class, "LavaSlime", 62, 3407872, 16579584);
    52. a(EntityEnderDragon.class, "EnderDragon", 63);
    53. a(EntityWither.class, "WitherBoss", 64);
    54. a(EntityBat.class, "Bat", 65, 4996656, 986895);
    55. a(EntityWitch.class, "Witch", 66, 3407872, 5349438);
    56. a(EntityEndermite.class, "Endermite", 67, 1447446, 7237230);
    57. a(EntityGuardian.class, "Guardian", 68, 5931634, 15826224);
    58.  
    59. a(EntityPig.class, "Pig", 90, 15771042, 14377823);
    60. a(EntitySheep.class, "Sheep", 91, 15198183, 16758197);
    61. a(EntityCow.class, "Cow", 92, 4470310, 10592673);
    62. a(EntityChicken.class, "Chicken", 93, 10592673, 16711680);
    63. a(EntitySquid.class, "Squid", 94, 2243405, 7375001);
    64. a(EntityWolf.class, "Wolf", 95, 14144467, 13545366);
    65. a(EntityMushroomCow.class, "MushroomCow", 96, 10489616, 12040119);
    66. a(EntitySnowman.class, "SnowMan", 97);
    67. a(EntityOcelot.class, "Ozelot", 98, 15720061, 5653556);
    68. a(EntityIronGolem.class, "VillagerGolem", 99);
    69. a(EntityHorse.class, "EntityHorse", 100, 12623485, 15656192);
    70. a(EntityRabbit.class, "Rabbit", 101, 10051392, 7555121);
    71.  
    72. a(EntityVillager.class, "Villager", 120, 5651507, 12422002);
    73.  
    74. a(EntityEnderCrystal.class, "EnderCrystal", 200);
    75. }


    Choose an EntityId from this list in order to get the corresponding graphic.

    The EntityId is the 3rd argument. For example, Villager has an id of 120. And Zombie is 54.
     
  4. Offline

    53ninja53

    But I dont want to replace existing entity, I want to make new entity, which will look like a zombie.
     
  5. Offline

    Europia79

    @53ninja53

    Code:java
    1.  
    2. package net.minecraft.server.v1_8_R1;
    3.  
    4. public class EntityTypes
    5. {
    6. private static final Logger b = ;
    7. private static final Map c = Maps.newHashMap();
    8. private static final Map d = Maps.newHashMap();
    9. private static final Map e = Maps.newHashMap();
    10. private static final Map f = Maps.newHashMap();
    11. private static final Map g = Maps.newHashMap();
    12. public static final Map eggInfo = Maps.newLinkedHashMap();
    13. private static void a(Class paramClass, String paramString, int paramInt)
    14. {
    15. if (c.containsKey(paramString)) {
    16. throw new IllegalArgumentException("ID is already registered: " + paramString);
    17. }
    18. if (e.containsKey(Integer.valueOf(paramInt))) {
    19. throw new IllegalArgumentException("ID is already registered: " + paramInt);
    20. }
    21. if (paramInt == 0) {
    22. throw new IllegalArgumentException("Cannot register to reserved id: " + paramInt);
    23. }
    24. if (paramClass == null) {
    25. throw new IllegalArgumentException("Cannot register null clazz for id: " + paramInt);
    26. }
    27. c.put(paramString, paramClass);
    28. d.put(paramClass, paramString);
    29. e.put(Integer.valueOf(paramInt), paramClass);
    30. f.put(paramClass, Integer.valueOf(paramInt));
    31. g.put(paramString, Integer.valueOf(paramInt));
    32. }


    just write to Map d and Map f

    Also, open up your 1.8.3 JAR and verify the names of the Map fields for d + f are correct.
     
  6. Offline

    53ninja53

    Thank you my problem is solved. :)
    Here is solution:
    Code:
    public class CastleConqueror extends JavaPlugin{
        @Override
        public void onEnable(){
            registerEntity(CustomEntity.class, "CustomEntity", 54);
        }
       
        @Override
        public void onDisable(){
        }
       
       
        private void registerEntity(Class entityClass, String entityName, int entityId){
            ((Map)ReflectionUtil.getPrivateField("c", EntityTypes.class, null)).put(entityName, entityClass);
            ((Map)ReflectionUtil.getPrivateField("d", EntityTypes.class, null)).put(entityClass, entityName);
            //((Map)ReflectionUtil.getPrivateField("e", EntityTypes.class, null)).put(entityId, entityClass);
            ((Map)ReflectionUtil.getPrivateField("f", EntityTypes.class, null)).put(entityClass, entityId);
            ((Map)ReflectionUtil.getPrivateField("g", EntityTypes.class, null)).put(entityName, entityId);
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("spawnmyentity") && sender instanceof Player) {
                CustomEntity e = new CustomEntity(((CraftWorld)((Player)sender).getWorld()).getHandle());
                e.setPosition(((Player)sender).getLocation().getX(), ((Player)sender).getLocation().getY(), ((Player)sender).getLocation().getZ());
                ((CraftWorld)((Player)sender).getWorld()).getHandle().addEntity(e);
                return true;
        }
        return false;
    }
    }
    Code:
    public class CustomEntity extends EntityZombie{
        public CustomEntity(World world) {
            super(world);
            this.setCustomName("my entity");
            this.setCustomNameVisible(true);
            List goalB = (List)ReflectionUtil.getPrivateField("b", PathfinderGoalSelector.class, goalSelector); goalB.clear();
            List goalC = (List)ReflectionUtil.getPrivateField("c", PathfinderGoalSelector.class, goalSelector); goalC.clear();
            List targetB = (List)ReflectionUtil.getPrivateField("b", PathfinderGoalSelector.class, targetSelector); targetB.clear();
            List targetC = (List)ReflectionUtil.getPrivateField("c", PathfinderGoalSelector.class, targetSelector); targetC.clear();
    
        }
    }
    Code:
    public class ReflectionUtil {
        public static Object getPrivateField(String fieldName, Class oclass, Object object){
            Field field = null;
            try {
                field = oclass.getDeclaredField(fieldName);
                field.setAccessible(true);
                return field.get(object);
            } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
                e.printStackTrace();
            }
            return field;
        }
    }
     
    Europia79 likes this.
Thread Status:
Not open for further replies.

Share This Page