static { final String[] parts = Bukkit.class.getName().split("."); if (parts.length == 4) { InventoryUtil.version = ""; } else { InventoryUtil.version = "." + parts[4]; } Field tiinv = null; Field ttitle = null; Field thandle = null; Field tcontainerCounter = null; Constructor<?> topenWindowPacket = null; Field tplayerConnection = null; Method tsendPacket; try { tiinv = getVersionedClass("org.bukkit.craftbukkit.inventory.CraftInventory") .getDeclaredField("inventory"); tiinv.setAccessible(true); ttitle = getVersionedClass( "org.bukkit.craftbukkit.inventory.CraftInventoryCustom$MinecraftInventory") .getDeclaredField("title"); ttitle.setAccessible(true); thandle = getVersionedClass("org.bukkit.craftbukkit.entity.CraftEntity").getDeclaredField("handle"); thandle.setAccessible(true); tcontainerCounter = getVersionedClass("net.minecraft.server.EntityPlayer") .getDeclaredField("containerCounter"); tcontainerCounter.setAccessible(true); thandle = getVersionedClass("org.bukkit.craftbukkit.entity.CraftEntity").getDeclaredField("handle"); thandle.setAccessible(true); topenWindowPacket = getVersionedClass("net.minecraft.server.PacketPlayOutOpenWindow") .getDeclaredConstructor( Integer.TYPE, Integer.TYPE, String.class, Integer.TYPE, Boolean.TYPE); topenWindowPacket.setAccessible(true); tplayerConnection = getVersionedClass("net.minecraft.server.EntityPlayer") .getDeclaredField("playerConnection"); tplayerConnection.setAccessible(true); tsendPacket = getVersionedClass("net.minecraft.server.PlayerConnection") .getDeclaredMethod( "sendPacket", topenWindowPacket.getDeclaringClass().getSuperclass()); tsendPacket.setAccessible(true); } catch (Exception ex) { throw new ExceptionInInitializerError(ex); } iinvField = tiinv; titleField = ttitle; handleField = thandle; containerCounterField = tcontainerCounter; openWindowPacketConstructor = topenWindowPacket; playerConnectionField = tplayerConnection; sendPacket = tsendPacket; }
private static Method getMethod(final Class<?> clazz, final String name, final Class<?>... args) { Method[] methods; for (int length = (methods = clazz.getMethods()).length, i = 0; i < length; ++i) { final Method m = methods[i]; if (m.getName().equals(name) && (args.length == 0 || ClassListEqual(args, m.getParameterTypes()))) { m.setAccessible(true); return m; } } return null; }