Spawn custom entity help.

Discussion in 'Plugin Development' started by Gamesareme, Jul 27, 2014.

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

    Gamesareme

    I am following this tutorial, on how to make a custom entity.
    https://forums.bukkit.org/threads/tutorial-how-to-customize-the-behaviour-of-a-mob-or-entity.54547/
    I am having problems though. Bellow is my code.
    Code:java
    1. import net.minecraft.server.v1_7_R3.EntityInsentient;
    2. import net.minecraft.server.v1_7_R3.EntitySkeleton;
    3.  
    4. import org.bukkit.entity.EntityType;
    5.  
    6. public enum CustomEntityType {
    7.  
    8. SKELETON("Skeleton", 51, EntityType.SKELETON, EntitySkeleton.class, CustomEntitySkeleton.class);
    9.  
    10. private String name;
    11. private int id;
    12. private EntityType entityType;
    13. private Class<? extends EntityInsentient> nmsClass;
    14. private Class<? extends EntityInsentient> customClass;
    15.  
    16. public String getName(){
    17. return this.name;
    18. }
    19.  
    20. public int getID(){
    21. return this.id;
    22. }
    23.  
    24. public EntityType getEntityType(){
    25. return this.entityType;
    26. }
    27.  
    28. public Class<? extends EntityInsentient> getNMSClass(){
    29. return this.nmsClass;
    30. }
    31.  
    32. public Class<? extends EntityInsentient> getCustomClass(){
    33. return this.customClass;
    34. }
    35.  
    36. }

    In the tutorial, on the end of "SKELETON" there is a comma. I am removed this and added ";". Would that be right? When I do this "SKELETON" becomes deprecated. If I make it a comma again "SKELETON" gets highlighted red, and so does "private String name;"
    Can some one tell me what is wrong.
     
  2. Offline

    chasechocolate

    Are you sure it's deprecation warnings? It could just be because it's not being used. Anyways, it works fine for me.
     
  3. Offline

    Drew1080

    Gamesareme
    Missing the constructor
    Code:java
    1. private CustomEntityType(String name, int id, EntityType entityType, Class<? extends EntityInsentient> nmsClass, Class<? extends EntityInsentient> customClass){
    2. this.name = name;
    3. this.id = id;
    4. this.entityType = entityType;
    5. this.nmsClass = nmsClass;
    6. this.customClass = customClass;
    7. }
     
  4. Offline

    Gamesareme

    chasechocolate I am sure. The code I game you did not work for me.
    Drew1080 Will this do the same thing as the one in the tutorial does?
     
  5. Offline

    Drew1080

    Gamesareme
    It's the constructor from the tutorial.. I just copied and pasted it here.
     
  6. Offline

    Gamesareme

    Drew1080 It does not look like the same one, but I will try it.
     
  7. Offline

    NonameSL

    Next times, please do not copy & paste.
     
  8. Offline

    Gamesareme

    Drew1080 I added that. :( sorry I missed it. It still does not work though. I get this error on "Skeleton"
    Code:
    The constructor CustomEntityType(String, int, EntityType, Class<EntitySkeleton>, Class<CustomEntitySkeleton>) is undefined
    NonameSL I did not. I have been editing and changing things for quite some time now. I know what most the stuff in the code does. I just happened to forget to add that bit of code here. Please do not criticise people like that. :/ It is the same code as of the tutorial, because I am starting from the beginning again.

    chasechocolate I used the wrong word. No it is throwing the error like I said above.

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

    Drew1080

    Gamesareme
    Sorry about the late reply.
    Can you paste your CustomEntitySkeleton class.
     
  10. Offline

    Gamesareme

    Drew1080 This is my code.
    Code:
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    import java.util.List;
     
    import net.minecraft.server.v1_7_R3.BiomeBase;
    import net.minecraft.server.v1_7_R3.BiomeMeta;
    import net.minecraft.server.v1_7_R3.EntityTypes;
     
    public class CustomEntitySkeleton {
       
        public static void registerEntities(){
            for (CustomEntityType entity : values()){
                try{
                    Method a = EntityTypes.class.getDeclaredMethod("a", new Class<?>[]{Class.class, String.class, int.class});
                    a.setAccessible(true);
                    a.invoke(null, entity.getCustomClass(), entity.getName(), entity.getID());
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
       
            for (BiomeBase biomeBase : BiomeBase.biomes){
                if (biomeBase == null){
                    break;
                }
           
                for (String field : new String[]{"K", "J", "L", "M"}){
                    try{
                        Field list = BiomeBase.class.getDeclaredField(field);
                        list.setAccessible(true);
                        @SuppressWarnings("unchecked")
                        List<BiomeMeta> mobList = (List<BiomeMeta>) list.get(biomeBase);
                   
                        for (BiomeMeta meta : mobList){
                            for (CustomEntityType entity : values()){
                                if (entity.getNMSClass().equals(meta.b)){
                                    meta.b = entity.getCustomClass();
                                }
                            }
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }
     
    }
    
    I am getting errors on, values(), and "BiomeBase.biomes". I think that I need to change BiomeBase.biomes to BiomeBase.n().
    values() on the other hand, when I hover over it, it asks me if I want to make a method. Sorry there are so many mistakes. This is my first time enums. Note I am a rather good programer, but until you know how to do something you have to hunt around for the answer.

    any one?

    please. :(

    I am got the 'Skeleton' party working. I still have one more problem though. It is the values() that still gives me errors. What is wrong?

    All right, I worked out my blunder. I was meant to add most of the stuff to the enum, but I did not. I have fixed that now, and I am getting no errors. Now all I need to do is register it in my main.class. I tried doing what I do for my other codes, but it does not work. Is there a different way to register an enum?

    Any one? can someone tell me how to register the enum please.

    <merged by Iroh>
     
  11. Gamesareme Think: Where are you trying to get values() from? Because at the moment you're trying to get it from the CustomEntitySkeleton class, hence why you're getting errors.
     
  12. Offline

    Gamesareme

    AdamQpzm I found that error all ready. At the moment I am trying to get it to register in the main class. This is my updated code.
    Code:
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    import java.util.List;
     
    import net.minecraft.server.v1_7_R3.BiomeBase;
    import net.minecraft.server.v1_7_R3.BiomeMeta;
    import net.minecraft.server.v1_7_R3.EntityInsentient;
    import net.minecraft.server.v1_7_R3.EntitySkeleton;
    import net.minecraft.server.v1_7_R3.EntityTypes;
     
    import org.bukkit.entity.EntityType;
     
    public enum CustomEntityType {
     
        SKELETON("mySkeleton", 51, EntityType.SKELETON, EntitySkeleton.class, CustomEntitySkeleton.class);
     
        private String name;
        private int id;
        private EntityType entityType;
        private Class<? extends EntityInsentient> nmsClass;
        private Class<? extends EntityInsentient> customClass;
     
        private CustomEntityType(String name, int id, EntityType entityType, Class<? extends EntityInsentient> nmsClass, Class<? extends EntityInsentient> customClass){
            this.name = name;
            this.id = id;
            this.entityType = entityType;
            this.nmsClass = nmsClass;
            this.customClass = customClass;
        }
     
        public String getName(){
            return this.name;
        }
     
        public int getID(){
            return this.id;
        }
     
        public EntityType getEntityType(){
            return this.entityType;
        }
     
        public Class<? extends EntityInsentient> getNMSClass(){
            return this.nmsClass;
        }
     
        public Class<? extends EntityInsentient> getCustomClass(){
            return this.customClass;
        }
     
        public static void registerEntities(){
            for (CustomEntityType entity : values()){
                try{
                    Method a = EntityTypes.class.getDeclaredMethod("a", new Class<?>[]{Class.class, String.class, int.class});
                    a.setAccessible(true);
                    a.invoke(null, entity.getCustomClass(), entity.getName(), entity.getID());
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
     
            for (BiomeBase biomeBase : BiomeBase.n()){
                if (biomeBase == null){
                    break;
                }
         
                for (String field : new String[]{"K", "J", "L", "M"}){
                    try{
                        Field list = BiomeBase.class.getDeclaredField(field);
                        list.setAccessible(true);
                        @SuppressWarnings("unchecked")
                        List<BiomeMeta> mobList = (List<BiomeMeta>) list.get(biomeBase);
                 
                        for (BiomeMeta meta : mobList){
                            for (CustomEntityType entity : values()){
                                if (entity.getNMSClass().equals(meta.b)){
                                    meta.b = entity.getCustomClass();
                                }
                            }
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    And this is my other one.
    Code:
    import net.minecraft.server.v1_7_R3.EntityLiving;
    import net.minecraft.server.v1_7_R3.EntitySkeleton;
    import net.minecraft.server.v1_7_R3.World;
     
     
    public class CustomEntitySkeleton extends EntitySkeleton{
     
        public CustomEntitySkeleton(World world) {
            super(world);
        }
     
        @Override
        public void a(EntityLiving entityliving, float f){
            for (int i = 0; i < 100; ++i){
                super.a(entityliving, f);
            }
        }
    }
    
     
    AdamQpzm likes this.
  13. Offline

    Gamesareme

    AdamQpzm how do I register this in the main class.
     
  14. Gamesareme Inside your onEnable() call the registerEntities() method.
     
  15. Offline

    Gamesareme

    AdamQpzm This is my onEnable()
    Code:
    public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info("[PXHub] " + pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled!");
            this.logger.info("[PXHub] Plugin Made By " + pdfFile.getAuthors());
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this.pl, this);
            pm.registerEvents(this.hl, this);
            pm.registerEvents(this.jm, this);
            pm.registerEvents(this.bg, this);
            pm.registerEvents(this.pg, this);
            pm.registerEvents(this.gm, this);
            pm.registerEvents(this.tpv, this);
            pm.registerEvents(this.op, this);
            pm.addPermission(new permissions().canhub);
            pm.addPermission(new permissions().pass);
            pm.addPermission(new permissions().sethub);
            getCommand("HUB").setExecutor(mc);
            getCommand("HUBSet").setExecutor(mc);
            getCommand("HUBTo").setExecutor(mc);
            this.saveDefaultConfig();
            this.saveConfig();
            this.getConfig();
        }
    I would have thought that I would register it by this. pm. registerEntities(); but this does not work. What is the proper method of doing this?

    please someone :oops:

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  16. Gamesareme No need to be so impatient :)

    And registerEntities() is a method in your CustomEntityType enum, not the PluginManager class. You call registerEntities() the same way you'd call any other public static method.
     
  17. Offline

    Gamesareme

    AdamQpzm I think that I may have already done that. In the tutorial I was following it said this.
    I am un sure on how to do this.
     
  18. Offline

    Gamesareme

    AdamQpzm I think I do. I have called all the other ones, if in fact they are methods. It would be thismethod(); But this would not work since it is in another class, or enum. I have never done this before. :( How would you do this. if I had to make a guess it would be like this plugin.somemethod. registerEntities()
    But I do not know.
     
  19. Offline

    Gamesareme

    AdamQpzm Thanks, but I think I worked it out any way. Does this look right?
    Code:
    CustomEntityType.registerEntities();
     
  20. Offline

    Gamesareme

    AdamQpzm Every thing seamed to work, so I uploaded it to my server. When it loaded it had a lot of errors, like this one.
    Code:
    [08:36:52 WARN]: java.lang.NoSuchFieldException: M
    [08:36:52 WARN]:at java.lang.Class.getDeclaredField(Class.java:2057)
    [08:36:52 WARN]:at me.joxboyz.hubpets.CustomEntityType.registerEntities(CustomEntityType.java:71)
    [08:36:52 WARN]:at me.joxboyz.pxhub.Main.onEnable(Main.java:52)
    [08:36:52 WARN]:at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:250)
    [08:36:52 WARN]:at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:324)
    [08:36:52 WARN]:at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:404)
    [08:36:52 WARN]:at org.bukkit.craftbukkit.v1_7_R3.CraftServer.loadPlugin(CraftServer.java:448)
    [08:36:52 WARN]:at org.bukkit.craftbukkit.v1_7_R3.CraftServer.enablePlugins(CraftServer.java:382)
    [08:36:52 WARN]:at net.minecraft.server.v1_7_R3.MinecraftServer.n(MinecraftServer.java:352)
    [08:36:52 WARN]:at net.minecraft.server.v1_7_R3.MinecraftServer.g(MinecraftServer.java:326)
    [08:36:52 WARN]:at net.minecraft.server.v1_7_R3.MinecraftServer.a(MinecraftServer.java:282)
    [08:36:52 WARN]:at net.minecraft.server.v1_7_R3.DedicatedServer.init(DedicatedServer.java:182)
    [08:36:52 WARN]:at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:436)
    [08:36:52 WARN]:at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628)
    I believe that the error is coming from here.
    Code:java
    1. for (BiomeBase biomeBase : BiomeBase.n()){
    2. if (biomeBase == null){
    3. break;
    4. }
    5.  
    6. for (String field : new String[]{"K", "J", "L", "M"}){
    7. try{
    8. Field list = BiomeBase.class.getDeclaredField(field);
    9. list.setAccessible(true);
    10. @SuppressWarnings("unchecked")
    11. List<BiomeMeta> mobList = (List<BiomeMeta>) list.get(biomeBase);
    12.  
    13. for (BiomeMeta meta : mobList){
    14. for (CustomEntityType entity : values()){
    15. if (entity.getNMSClass().equals(meta.b)){
    16. meta.b = entity.getCustomClass();
    17. }
    18. }
    19. }
    20. }catch (Exception e){
    21. e.printStackTrace();
    22. }
    23. }
    24. }

    At the top here 'BiomeBase.n()' I change BiomeBase.biomes to BiomeBase.n(), because BiomeBase.biomes Threw an error. Is n() the right field to use?

    So I worked out why it was doing that.
    Code:
    for (String field : new String[]{"K", "J", "L", "M"}){
    Needed to be this.
    Code:
    for (String field : new String[]{"as", "at", "au", "av"}){
    When I load this into my server I get this error. :(
    Code:
    [11:09:31 WARN]: java.lang.reflect.InvocationTargetException
    [11:09:31 WARN]:    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [11:09:31 WARN]:    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    [11:09:31 WARN]:    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    [11:09:31 WARN]:    at java.lang.reflect.Method.invoke(Method.java:483)
    [11:09:31 WARN]:    at me.joxboyz.hubpets.CustomEntityType.registerEntities(CustomEntityType.java:58)
    [11:09:31 WARN]:    at me.joxboyz.pxhub.Main.onEnable(Main.java:52)
    [11:09:31 WARN]:    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:250)
    [11:09:31 WARN]:    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:324)
    [11:09:31 WARN]:    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:404)
    [11:09:31 WARN]:    at org.bukkit.craftbukkit.v1_7_R3.CraftServer.loadPlugin(CraftServer.java:448)
    [11:09:31 WARN]:    at org.bukkit.craftbukkit.v1_7_R3.CraftServer.enablePlugins(CraftServer.java:382)
    [11:09:31 WARN]:    at org.bukkit.craftbukkit.v1_7_R3.CraftServer.reload(CraftServer.java:801)
    [11:09:31 WARN]:    at org.bukkit.Bukkit.reload(Bukkit.java:288)
    [11:09:31 WARN]:    at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23)
    [11:09:31 WARN]:    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180)
    [11:09:31 WARN]:    at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:703)
    [11:09:31 WARN]:    at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchServerCommand(CraftServer.java:690)
    [11:09:31 WARN]:    at net.minecraft.server.v1_7_R3.DedicatedServer.aB(DedicatedServer.java:296)
    [11:09:31 WARN]:    at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:261)
    [11:09:31 WARN]:    at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558)
    [11:09:31 WARN]:    at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469)
    [11:09:31 WARN]:    at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628)
    [11:09:31 WARN]: Caused by: java.lang.IllegalArgumentException: ID is already registered: Skeleton
    [11:09:31 WARN]:    at net.minecraft.server.v1_7_R3.EntityTypes.a(SourceFile:30)
    [11:09:31 WARN]:    ... 22 more
    
    I changed the id to some random thing and reran the code it loaded fine, but when I try and log into the server it crashes the minecraft.app. :/ What should I try.

    So I fixed the problem above. This is my new code.
    Code:java
    1. public enum CustomEntityType {
    2.  
    3. SKELETON("Skeleton", 54, EntityType.SKELETON, EntitySkeleton.class, CustomEntitySkeleton.class);
    4.  
    5. private String name;
    6. private int id;
    7. private EntityType entityType;
    8. private Class<? extends EntityInsentient> nmsClass;
    9. private Class<? extends EntityInsentient> customClass;
    10.  
    11. private CustomEntityType(String name, int id, EntityType entityType, Class<? extends EntityInsentient> nmsClass,
    12. Class<? extends EntityInsentient> customClass) {
    13. this.name = name;
    14. this.id = id;
    15. this.entityType = entityType;
    16. this.nmsClass = nmsClass;
    17. this.customClass = customClass;
    18. }
    19.  
    20. public String getName() {
    21. return name;
    22. }
    23.  
    24. public int getID() {
    25. return id;
    26. }
    27.  
    28. public EntityType getEntityType() {
    29. return entityType;
    30. }
    31.  
    32. public Class<? extends EntityInsentient> getNMSClass() {
    33. return nmsClass;
    34. }
    35.  
    36. public Class<? extends EntityInsentient> getCustomClass() {
    37. return customClass;
    38. }
    39.  
    40. public static void registerEntities() {
    41. for (CustomEntityType entity : values())
    42. a(entity.getCustomClass(), entity.getName(), entity.getID());
    43.  
    44. BiomeBase[] biomes;
    45. try {
    46. biomes = (BiomeBase[]) getPrivateStatic(BiomeBase.class, "biomes");
    47. } catch (Exception exc) {
    48. return;
    49. }
    50. for (BiomeBase biomeBase : biomes) {
    51. if (biomeBase == null)
    52. break;
    53.  
    54. for (String field : new String[] { "as", "at", "au", "av" })
    55. try {
    56. Field list = BiomeBase.class.getDeclaredField(field);
    57. list.setAccessible(true);
    58. @SuppressWarnings("unchecked")
    59. List<BiomeMeta> mobList = (List<BiomeMeta>) list.get(biomeBase);
    60.  
    61. for (BiomeMeta meta : mobList)
    62. for (CustomEntityType entity : values())
    63. if (entity.getNMSClass().equals(meta.b))
    64. meta.b = entity.getCustomClass();
    65. } catch (Exception e) {
    66. e.printStackTrace();
    67. }
    68. }
    69. }
    70.  
    71. @SuppressWarnings("rawtypes")
    72. private static Object getPrivateStatic(Class clazz, String f) throws Exception {
    73. Field field = clazz.getDeclaredField(f);
    74. field.setAccessible(true);
    75. return field.get(null);
    76. }
    77.  
    78. @SuppressWarnings({ "unchecked", "rawtypes" })
    79. private static void a(Class paramClass, String paramString, int paramInt) {
    80. try {
    81. ((Map) getPrivateStatic(EntityTypes.class, "c")).put(paramString, paramClass);
    82. ((Map) getPrivateStatic(EntityTypes.class, "d")).put(paramClass, paramString);
    83. ((Map) getPrivateStatic(EntityTypes.class, "f")).put(paramClass, Integer.valueOf(paramInt));
    84. ((Map) getPrivateStatic(EntityTypes.class, "g")).put(paramString, Integer.valueOf(paramInt));
    85. } catch (Exception exc) {
    86. }
    87. }
    88. }

    But when I log into minecraft it crashes. Can someone please Help!!!!!!!!!!!!!!!!!!!!!! :'(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  21. Hmm... It looks like you registered your entity twice on your onEnable()?
    Could you please post it's code.
     
  22. Offline

    Gamesareme

    XXLuigiMario sorry that this is late.
    Hang on :confused: so silly of me. I hade this in the method.
    Code:java
    1. CustomEntityType.registerEntities();
    2. CustomEntityType.values();

    Thanks so much for pointing this out. [zombie]

    How would I then spawn my costume entity?
    I have tried this but it does not work.
    Code:java
    1. CustomEntityZombie zombie = new CustomEntityZombie(((CraftWorld)player.getWorld()).getHandle());
    2. zombie.setLocation(player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ(), player.getLocation().getPitch(), player.getLocation().getYaw());
    3. ((CraftWorld)player.getWorld()).getHandle().addEntity(zombie, SpawnReason.CUSTOM);

    I know that it is not working because of this bit of code.
    Code:java
    1. @EventHandler
    2. public void onPlayerHitEntity(EntityDamageByEntityEvent event){
    3. if(event.getDamager() instanceof Player){
    4. Player player = (Player)event.getDamager();
    5. player.sendMessage("You Are A Player!");
    6. }
    7. if(event.getEntity() instanceof CustomEntityZombie){
    8. Player player = (Player)event.getDamager();
    9. player.sendMessage("You Clicked Great!!");
    10. }
    11. if(!(event.getEntity() instanceof CustomEntityZombie)){
    12. Player player = (Player)event.getDamager();
    13. player.sendMessage("Not good!!");
    14. }
    15. }

    When ever I hit one of my zombies it says Not good!!.
    What have I done wrong?

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

Share This Page