Spawning Custom Entity

Discussion in 'Spigot Plugin Development' started by tarpers, May 31, 2017.

  1. Hello,
    So I am creating a custom Zombie, I have done all the code to create the mob (and I believe this is fine) my only problem is when I spawn it. The code I'm using to spawn it is:

    Code (Text):
        public static Zombie spawn(Location loc){
            World mcWorld = (World) ((CraftWorld) loc.getWorld()).getHandle();
            final Flash customEntity = new Flash(mcWorld);
            customEntity.setLocation(loc.getX(),loc.getY(),loc.getZ(),loc.getYaw(),loc.getPitch());
            ((CraftLivingEntity) customEntity.getBukkitEntity()).setRemoveWhenFarAway(false);
            mcWorld.addEntity(customEntity, CreatureSpawnEvent.SpawnReason.CUSTOM);

            ((CraftWorld) loc.getWorld()).getHandle().addEntity(new Flash(mcWorld));
            return (Zombie) customEntity.getBukkitEntity();
        }

     
    I'm trying to spawn it by simply calling the Class then method and passing through players location. I can give the other code if needed.
    There is no errors, it's just doing nothing ingame.

    Thanks,
     
  2. did you actually register the entity into the server?
     
    #2 ChefJava, May 31, 2017
    Last edited: May 31, 2017
  3. Code (Text):
            NMSUtil nmsu = new NMSUtil();    
      nmsu.registerEntity("Flash",54, EntityZombie.class,Flash.class);


    Then in class:

        public void registerEntity(String name, int id, Class<? extends EntityInsentient> nmsClass, Class<? extends EntityInsentient> customClass){
            try{


                List<Map<?,?>> dataMap = new ArrayList<Map<?,?>>();
                /**
                 * Go through fields
                 */
                for(Field f : EntityTypes.class.getDeclaredFields()){
                    if(f.getType().getSimpleName().equals(Map.class.getSimpleName())){
                        f.setAccessible(true);
                        dataMap.add((Map<?,?>) f.get(null));
                    }
                }
                /**
                 * If something in the datamap that contains id then:
                 */
                if(dataMap.get(2).containsKey(id)){
                    dataMap.get(0).remove(name);
                    dataMap.get(2).remove(id);
                }

                Method method = EntityTypes.class.getDeclaredMethod("a", Class.class, String.class,int.class);
                method.setAccessible(true);
                method.invoke(null,customClass,name,id);

            }catch(Exception e){
                e.printStackTrace();
            }

        }