mardi 28 mars 2017

Array with datatype of a reflected class

I want to adress this constructor:

public PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction paramEnumPlayerInfoAction, EntityPlayer[] paramArrayOfEntityPlayer) {
    [...]
}

using the code below.

ReflectionHandler.getNMSClass(String) is a Method utilizing the Class.forName()-Method to get the class in a shared package, hardcoded into the .getNMSClass().

01 Constructor<?> npcConstructor = ReflectionHandler.getNMSClass("EntityPlayer").getConstructor(server, world.getClass(), GameProfile.class, ReflectionHandler.getNMSClass("PlayerInteractManager"));
02 Constructor<?> playerInteractManagerConstructor = ReflectionHandler.getNMSClass("PlayerInteractManager").getConstructor(world.getClass().getSuperclass());
03                  
04 playerInteractManager = playerInteractManagerConstructor.newInstance(world);
05 npc = new Object[]{npcConstructor.newInstance(craftServer.getMethod("getServer").invoke(Bukkit.getServer()), world, new GameProfile(UUID.randomUUID(), args[1]), playerInteractManager)};
06                  
07 Constructor<?> packetPlayerInfoConstructor = ReflectionHandler.getNMSClass("PacketPlayOutPlayerInfo").getConstructor(ReflectionHandler.getNMSClass("PacketPlayOutPlayerInfo$EnumPlayerInfoAction"), 
08    Class.forName("[Lnet.minecraft.server." + Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3] + ".EntityPlayer;"));
09 Constructor<?> packetEntitySpawnConstructor = ReflectionHandler.getNMSClass("PacketPlayOutNamedEntitySpawn").getConstructor(npc[0].getClass().getSuperclass());
10
11 packetPlayerInfo = packetPlayerInfoConstructor.newInstance(ReflectionHandler.getNMSClass("PacketPlayOutPlayerInfo$EnumPlayerInfoAction").getField("ADD_PLAYER"), npc);
12 packetEntitySpawn = packetEntitySpawnConstructor.newInstance(npc[0]);

I want to instantiate the constructor in line 11 but get a java.lang.IllegalArgumentException: argument type mismatch, for that line, (propably) because npc is an array declared like this Object[] npc; as an object, even if the constructor requires an EntityPlayer[].

My question is how you solve that problem or rather how you declare npc as an EntityPlayer[], without importing EntityPlayer. I'd use reflection if I knew the greater details, wich I unfortunately don't.

Thanks in advance and have a wonderful day

rapt0r





Aucun commentaire:

Enregistrer un commentaire