I am currently developing a Minecraft plugin that should be able to manage mobs. In short, in any case, I want my plugin to be a maximum multi-version, so I use Reflector. but when I use Class.cast(Object o) I get unwanted classes.
This is what I have without Reflector:
WorldServer nms = ((CraftWorld) entity.getWorld()).getHandle();
nms.addEntity((net.minecraft.server.v1_13_R2.Entity) ((CraftEntity) entity).getHandle(), SpawnReason.CUSTOM);
With Reflector:
try {
// We get the craftworld class with nms so it can be used in multiple versions
Class<?> craftWorldClass = getNMSClass("org.bukkit.craftbukkit.", "CraftWorld");
// Cast the bukkit world to the craftworld
Object craftWorldObject = craftWorldClass.cast(entity.getWorld());
// Create variable with the method that get handle
// https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/browse/src/main/java/org/bukkit/craftbukkit/CraftWorld.java#580
Method getHandleMethod = craftWorldObject.getClass().getMethod("getHandle");
// Attempt to invoke the method that creates the entity itself. This returns a net.minecraft.server entity
Object worldServerObject = getHandleMethod.invoke(craftWorldObject);
// We get the CraftEntity class
Class<?> craftEntityClass = getNMSClass("org.bukkit.craftbukkit.", "entity.CraftEntity");
//cast org.bukkit.entity.Entity to CraftEntity
Object craftEntityObject = craftEntityClass.cast(entity);
//get the method getHandle
Method entityGetHandleMethod = craftEntityClass.getMethod("getHandle");
//Attempt to invoke the method
Object entityTypeObject = entityGetHandleMethod.invoke(craftEntityObject);
// We get the Entity class of NMS
Class<?> entityClass = getNMSClass("net.minecraft.server.", "Entity");
System.out.println(entityClass);
//cast CraftEntity to NMS Entity
Object entityObject = entityClass.cast(entityTypeObject);
System.out.println(entityTypeObject.getClass());
System.out.println(entityObject.getClass());
//get the method to add mob in world
Method addEntityMethod = worldServerObject.getClass().getMethod("addEntity", entityClass, SpawnReason.CUSTOM.getClass());
//Attempt to invoke the method
addEntityMethod.invoke(entityObject, SpawnReason.CUSTOM);
} catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException exception) {
exception.printStackTrace();
}
private static Class<?> getNMSClass(String prefix, String nmsClassString) throws ClassNotFoundException {
// Getting the version by splitting the package
String version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3] + ".";
// Combining the prefix + version + nmsClassString for the full class path
String name = prefix + version + nmsClassString;
return Class.forName(name);
}
So in my logic I find myself at the output with an object of type NMS Entity, but in my tests I'm getting by with a NMS EntityZombie
Output:
[15:43:01 INFO]: class net.minecraft.server.v1_13_R2.Entity
[15:43:01 INFO]: class net.minecraft.server.v1_13_R2.EntityZombie
[15:43:01 INFO]: class net.minecraft.server.v1_13_R2.EntityZombie
So how do I use Reflector to get the right type?
Thx for your help ^^
ZombieEntity inherits from Entity indirectly (ZombieEntity -> EntityCreature -> EntityCreature -> EntityInsentient -> EntityLiving -> Entity), that means it is already an Entity.
You cast it, but the cast doesn't change the class type. Quick example:
Your code is correct.