So far, I have 3 classes. The main Code: package me.leothepro555.apocalypse; import java.lang.reflect.Field; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntitySpawnEvent; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; public class ApocalypsePlugin extends JavaPlugin implements Listener{ @Override public void onEnable(){ PluginManager manager = Bukkit.getPluginManager(); manager.registerEvents(this, this); getConfig().options().copyDefaults(true); saveDefaultConfig(); } @EventHandler @SuppressWarnings("unused") public void onEntitySpawn(EntitySpawnEvent event){ if(isValidWorld(event.getEntity().getWorld())){ // Player p = getNearestPlayer(event.getEntity().getLocation()); spawnEntity(new Infected(event.getLocation().getWorld()), event.getEntity.getLocation()); event.setCancelled(true); } } public static Player getNearestPlayer(Location origin){ Player closest = null; int i = 0; for(Player p: origin.getWorld().getPlayers()){ if(i == 0){ i++; closest = p; } Location loc = p.getLocation(); if(loc.distance(origin) < closest.getLocation().distance(origin)){ closest = p; } } return closest; } public boolean isValidWorld(World w){ if(getConfig().getStringList("enabled-worlds").contains(w.getName())){ return true; } return false; } @SuppressWarnings("rawtypes") public static Object getPrivateField(String fieldName, Class clazz, Object object) { Field field; Object o = null; try { field = clazz.getDeclaredField(fieldName); field.setAccessible(true); o = field.get(object); } catch(NoSuchFieldException e) { e.printStackTrace(); } catch(IllegalAccessException e) { e.printStackTrace(); } return o; } } Infected.java (The custom mob class) Code: package me.leothepro555.apocalypse; import java.util.List; import net.minecraft.server.v1_8_R2.EntityHuman; import net.minecraft.server.v1_8_R2.EntityVillager; import net.minecraft.server.v1_8_R2.EntityZombie; import net.minecraft.server.v1_8_R2.PathfinderGoalFloat; import net.minecraft.server.v1_8_R2.PathfinderGoalHurtByTarget; import net.minecraft.server.v1_8_R2.PathfinderGoalLookAtPlayer; import net.minecraft.server.v1_8_R2.PathfinderGoalMeleeAttack; import net.minecraft.server.v1_8_R2.PathfinderGoalMoveThroughVillage; import net.minecraft.server.v1_8_R2.PathfinderGoalMoveTowardsRestriction; import net.minecraft.server.v1_8_R2.PathfinderGoalNearestAttackableTarget; import net.minecraft.server.v1_8_R2.PathfinderGoalRandomLookaround; import net.minecraft.server.v1_8_R2.PathfinderGoalRandomStroll; import net.minecraft.server.v1_8_R2.PathfinderGoalSelector; import org.bukkit.craftbukkit.v1_8_R2.CraftWorld; public class Infected extends EntityZombie { @SuppressWarnings({ "rawtypes", "unchecked" }) public Infected(org.bukkit.World world) { super(((CraftWorld)world).getHandle()); List goalB = (List)ApocalypsePlugin.getPrivateField("b", PathfinderGoalSelector.class, goalSelector); goalB.clear(); List goalC = (List)ApocalypsePlugin.getPrivateField("c", PathfinderGoalSelector.class, goalSelector); goalC.clear(); List targetB = (List)ApocalypsePlugin.getPrivateField("b", PathfinderGoalSelector.class, targetSelector); targetB.clear(); List targetC = (List)ApocalypsePlugin.getPrivateField("c", PathfinderGoalSelector.class, targetSelector); targetC.clear(); this.goalSelector.a(0, new PathfinderGoalFloat(this)); this.goalSelector.a(2, new PathfinderGoalMeleeAttack(this, EntityHuman.class, 1.0D, false)); this.goalSelector.a(4, new PathfinderGoalMeleeAttack(this, EntityVillager.class, 1.0D, true)); this.goalSelector.a(5, new PathfinderGoalMoveTowardsRestriction(this, 1.0D)); this.goalSelector.a(6, new PathfinderGoalMoveThroughVillage(this, 1.0D, false)); this.goalSelector.a(7, new PathfinderGoalRandomStroll(this, 1.0D)); this.goalSelector.a(8, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, 8.0F)); this.goalSelector.a(8, new PathfinderGoalRandomLookaround(this)); this.targetSelector.a(1, new PathfinderGoalHurtByTarget(this, true)); this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityHuman.class,true)); this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityVillager.class,false)); } } EntityTypes.java (The supposed entity register-er) Code: package me.leothepro555.apocalypse import java.lang.reflect.Field; import java.util.Map; import net.minecraft.server.v1_8_R2.Entity; import me.leothepro555.titans.CustomZombie; import org.bukkit.Location; import org.bukkit.craftbukkit.v1_8_R2.CraftWorld; public enum EntityTypes { //NAME("Entity name", Entity ID, yourcustomclass.class); INFECTED("Infected", 60, Infected.class); //You can add as many as you want. private EntityTypes(String name, int id, Class<? extends Entity> custom) { addToMaps(custom, name, id); } public static void spawnEntity(Entity entity, Location loc) { entity.setLocation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch()); ((CraftWorld)loc.getWorld()).getHandle().addEntity(entity); } @SuppressWarnings({ "unchecked", "rawtypes" }) private static void addToMaps(Class clazz, String name, int id) { //getPrivateField is the method from above. //Remove the lines with // in front of them if you want to override default entities (You'd have to remove the default entity from the map first though). ((Map)getPrivateField("c", EntityTypes.class, null)).put(name, clazz); ((Map)getPrivateField("d", EntityTypes.class, null)).put(clazz, name); //((Map)getPrivateField("e", net.minecraft.server.v1_7_R4.EntityTypes.class, null)).put(Integer.valueOf(id), clazz); ((Map)getPrivateField("f", EntityTypes.class, null)).put(clazz, Integer.valueOf(id)); //((Map)getPrivateField("g", net.minecraft.server.v1_7_R4.EntityTypes.class, null)).put(name, Integer.valueOf(id)); } @SuppressWarnings("rawtypes") public static Object getPrivateField(String fieldName, Class clazz, Object object) { Field field; Object o = null; try { field = clazz.getDeclaredField(fieldName); field.setAccessible(true); o = field.get(object); } catch(NoSuchFieldException e) { e.printStackTrace(); } catch(IllegalAccessException e) { e.printStackTrace(); } return o; } } It doesn't seem to work. Is there something I'm missing?
@Hex_27 What is not working? It looks to me that in your EntitySpawnEvent listener you just cancel the event and do nothing after.
Sorry this code was edited. I updated the post (main class, the spawnevent) Bump EDIT by Moderator: merged posts, please use the edit button instead of double posting.
@ShadowLAX he console spams random, unidentified errors and the Custom zombie spawned does not act customly. As in unidentified errors, means that the stack trace does not show the type of error (ex NPE) created .
@ShadowLAX Well, I couldn't get it back easily, so I retrieved this error from a spigot post I made there. It isn't the error I'm talking about, but I guess it would do. Code: [00:03:56 ERROR]: Could not pass event CreatureSpawnEvent to Titans v1 org.bukkit.event.EventException at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja va:305) ~[spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428] at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav a:62) ~[spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428] at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j ava:502) [spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428] at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j ava:487) [spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428] at org.bukkit.craftbukkit.v1_8_R2.event.CraftEventFactory.callCreatureSp awnEvent(CraftEventFactory.java:276) [spigot-1.8.3.jar:git-Spigot-2ec6f06-772242 8] at net.minecraft.server.v1_8_R2.World.addEntity(World.java:1015) [spigot -1.8.3.jar:git-Spigot-2ec6f06-7722428] at net.minecraft.server.v1_8_R2.SpawnerCreature.a(SpawnerCreature.java:1 82) [spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428] at net.minecraft.server.v1_8_R2.WorldServer.doTick(WorldServer.java:227) [spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428] at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:7 67) [spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428] at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:3 68) [spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428] at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:6 51) [spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428] at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java :554) [spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428] at java.lang.Thread.run(Thread.java:745) [?:1.8.0_11] Caused by: java.lang.[B]NoClassDefFoundError[/B]: Could not initialize class me.leothep ro555.titans.EntityTypes at me.leothepro555.titans.Titans.onMobSpawn(Titans.java:45) ~[?:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0 _11] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl. java:62) ~[?:1.8.0_11] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces sorImpl.java:43) ~[?:1.8.0_11] at java.lang.reflect.Method.invoke(Method.java:483) ~[?:1.8.0_11] at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja va:301) ~[spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428] ... 12 more > Titans.java:45 is spawnEntity(new Infected(event.getLocation().getWorld()), event.getEntity.getLocation());
@Hex_27 That error is caused by you calling a static method from another class without calling it properly. spawnEntity is a static method of EntityTypes class and should be referred to statically. Not sure how that line is not giving you errors in your IDE at that line, as there isn't even a local duplicate method.
So how do I fix it? I think I reproduced that unidentified error Code: at net.minecraft.server.v1_8_R2.World.addEntity(World.java:1015) ~[spigo t-1.8.3.jar:git-Spigot-2ec6f06-7722428] at net.minecraft.server.v1_8_R2.World.addEntity(World.java:987) ~[spigot -1.8.3.jar:git-Spigot-2ec6f06-7722428] at me.leothepro555.titans.Titans.spawnEntity(Titans.java:170) ~[?:?] at me.leothepro555.titans.Titans.onMobSpawn(Titans.java:45) ~[?:?] at sun.reflect.GeneratedMethodAccessor110.invoke(Unknown Source) ~[?:?] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces sorImpl.java:43) ~[?:1.8.0_11] at java.lang.reflect.Method.invoke(Method.java:483) ~[?:1.8.0_11] at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja va:301) ~[spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428] at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav a:62) ~[spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428] at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j ava:502) ~[spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428] at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j ava:487) ~[spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428] at org.bukkit.craftbukkit.v1_8_R2.event.CraftEventFactory.callCreatureSp awnEvent(CraftEventFactory.java:276) ~[spigot-1.8.3.jar:git-Spigot-2ec6f06-77224 28] This was after i moved spawnEntity to the main class instead of EntityTypes EDIT by Moderator: merged posts, please use the edit button instead of double posting.