From fee986d21558e63d5ef9b33eb525a433cec48062 Mon Sep 17 00:00:00 2001 From: Myles Date: Thu, 3 Mar 2016 18:12:10 +0000 Subject: [PATCH] Make the injectors work in any 1.8 version. Now looks through fields for suitable in case of different field names also construct slot using known arguments. --- .../us/myles/ViaVersion/ViaVersionPlugin.java | 37 ++++++++++++------- .../transformers/IncomingTransformer.java | 7 +++- .../transformers/OutgoingTransformer.java | 5 ++- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java index 0bdaf7820..ae03f1cd1 100644 --- a/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java +++ b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java @@ -12,12 +12,12 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; - import us.myles.ViaVersion.api.ViaVersion; import us.myles.ViaVersion.api.ViaVersionAPI; import us.myles.ViaVersion.handlers.ViaVersionInitializer; import us.myles.ViaVersion.util.ReflectionUtil; +import java.lang.reflect.Field; import java.util.Collections; import java.util.List; import java.util.Set; @@ -33,7 +33,7 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI { @Override public void onEnable() { ViaVersion.setInstance(this); - if(System.getProperty("ViaVersion") != null){ + if (System.getProperty("ViaVersion") != null) { getLogger().severe("ViaVersion is already loaded, we don't support reloads. Please reboot if you wish to update."); return; } @@ -58,18 +58,29 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI { Class serverClazz = ReflectionUtil.nms("MinecraftServer"); Object server = ReflectionUtil.invokeStatic(serverClazz, "getServer"); Object connection = serverClazz.getDeclaredMethod("getServerConnection").invoke(server); - - List futures = ReflectionUtil.get(connection, "g", List.class); - if (futures.size() == 0) { - throw new Exception("Could not find server to inject (Please ensure late-bind in your spigot.yml is false)"); + // loop through all fields checking if list + boolean injected = false; + for (Field field : connection.getClass().getDeclaredFields()) { + field.setAccessible(true); + Object value = field.get(connection); + if (value instanceof List) { + for (Object o : (List) value) { + if (o instanceof ChannelFuture) { + ChannelFuture future = (ChannelFuture) o; + ChannelPipeline pipeline = future.channel().pipeline(); + ChannelHandler bootstrapAcceptor = pipeline.first(); + ChannelInitializer oldInit = ReflectionUtil.get(bootstrapAcceptor, "childHandler", ChannelInitializer.class); + ChannelInitializer newInit = new ViaVersionInitializer(oldInit); + ReflectionUtil.set(bootstrapAcceptor, "childHandler", newInit); + injected = true; + } else { + break; // not the right list. + } + } + } } - - for (ChannelFuture future : futures) { - ChannelPipeline pipeline = future.channel().pipeline(); - ChannelHandler bootstrapAcceptor = pipeline.first(); - ChannelInitializer oldInit = ReflectionUtil.get(bootstrapAcceptor, "childHandler", ChannelInitializer.class); - ChannelInitializer newInit = new ViaVersionInitializer(oldInit); - ReflectionUtil.set(bootstrapAcceptor, "childHandler", newInit); + if (!injected) { + throw new Exception("Could not find server to inject (Please ensure late-bind in your spigot.yml is false)"); } } diff --git a/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java b/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java index b7c99669d..579297062 100644 --- a/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java +++ b/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java @@ -10,6 +10,7 @@ import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.util.PacketUtil; import us.myles.ViaVersion.util.ReflectionUtil; +import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -102,7 +103,9 @@ public class IncomingTransformer { if (slot == 45 && windowID == 0) { try { Class setSlot = ReflectionUtil.nms("PacketPlayOutSetSlot"); - Object setSlotPacket = setSlot.getConstructors()[1].newInstance(windowID, slot, null); + Constructor setSlotConstruct = setSlot.getDeclaredConstructor(int.class, int.class, ReflectionUtil.nms("ItemStack")); + // properly construct + Object setSlotPacket = setSlotConstruct.newInstance(windowID, slot, null); info.getChannel().pipeline().writeAndFlush(setSlotPacket); // slot is empty slot = -999; // we're evil, they'll throw item on the ground } catch (ClassNotFoundException e) { @@ -113,6 +116,8 @@ public class IncomingTransformer { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); } } diff --git a/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java b/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java index 9fcc91970..90cf3967a 100644 --- a/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java +++ b/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java @@ -50,7 +50,7 @@ public class OutgoingTransformer { if (packet == null) { throw new RuntimeException("Outgoing Packet not found? " + packetID + " State: " + info.getState() + " Version: " + info.getProtocol()); } -// if (packet != PacketType.PLAY_CHUNK_DATA && packet != PacketType.PLAY_KEEP_ALIVE && packet != PacketType.PLAY_TIME_UPDATE && (!packet.name().toLowerCase().contains("move") && !packet.name().contains("look"))) +// if (packet != PacketType.PLAY_CHUNK_DATA && packet != PacketType.PLAY_KEEP_ALIVE && packet != PacketType.PLAY_TIME_UPDATE && (!packet.name().toLowerCase().contains("move") && !packet.name().toLowerCase().contains("look"))) // System.out.println("Packet Type: " + packet + " Original ID: " + packetID + " State:" + info.getState()); if (packet.getPacketID() != -1) { packetID = packet.getNewPacketID(); @@ -60,6 +60,7 @@ public class OutgoingTransformer { PacketUtil.writeVarInt(packetID, output); if (packet == PacketType.PLAY_NAMED_SOUND_EFFECT) { String name = PacketUtil.readString(input); + SoundEffect effect = SoundEffect.getByName(name); int catid = 0; String newname = name; @@ -511,7 +512,7 @@ public class OutgoingTransformer { output.writeBytes(input); } - private String fixJson(String line) { + public static String fixJson(String line) { if (line == null || line.equalsIgnoreCase("null")) { line = "{\"text\":\"\"}"; } else {