From 166c6157259e2d7496e5249e67fa540f1cc16ead Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Thu, 16 Apr 2015 21:13:58 -0400 Subject: [PATCH] Fix getProtocolVersion() Fixes #74 --- .../comphenix/protocol/CommandProtocol.java | 8 +++---- .../injector/netty/ChannelInjector.java | 23 +++++++++++++++---- .../utility/MinecraftProtocolVersion.java | 3 --- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java b/ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java index a8255d7f..1eebd6a4 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java @@ -74,13 +74,13 @@ class CommandProtocol extends CommandBase { sender.sendMessage(ChatColor.GOLD + "Packet listeners:"); for (PacketListener listener : manager.getPacketListeners()) { - sender.sendMessage(ChatColor.GOLD + " " + listener); + sender.sendMessage(ChatColor.GOLD + " - " + listener); } // Along with every asynchronous listener sender.sendMessage(ChatColor.GOLD + "Asynchronous listeners:"); for (PacketListener listener : manager.getAsynchronousManager().getAsyncHandlers()) { - sender.sendMessage(ChatColor.GOLD + " " + listener); + sender.sendMessage(ChatColor.GOLD + " - " + listener); } } @@ -136,8 +136,8 @@ class CommandProtocol extends CommandBase { PluginDescriptionFile desc = plugin.getDescription(); sender.sendMessage(ChatColor.GREEN + desc.getName() + ChatColor.WHITE + " v" + ChatColor.GREEN + desc.getVersion()); - sender.sendMessage("Authors: " + ChatColor.GREEN + "dmulloy2 " + ChatColor.WHITE + " and " + ChatColor.GREEN + "Comphenix"); - sender.sendMessage("Issues: " + ChatColor.GREEN + "https://github.com/dmulloy2/ProtocolLib/issues"); + sender.sendMessage(ChatColor.WHITE + "Authors: " + ChatColor.GREEN + "dmulloy2" + ChatColor.WHITE + " and " + ChatColor.GREEN + "Comphenix"); + sender.sendMessage(ChatColor.WHITE + "Issues: " + ChatColor.GREEN + "https://github.com/dmulloy2/ProtocolLib/issues"); } /** diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/ChannelInjector.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/ChannelInjector.java index 1577965c..e6434a4c 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/ChannelInjector.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/ChannelInjector.java @@ -82,6 +82,9 @@ class ChannelInjector extends ByteToMessageDecoder implements Injector { // For retrieving the protocol private static FieldAccessor PROTOCOL_ACCESSOR; + // For retrieving the protocol version + private static MethodAccessor PROTOCOL_VERSION; + // The factory that created this injector private InjectionFactory factory; @@ -156,10 +159,8 @@ class ChannelInjector extends ByteToMessageDecoder implements Injector { this.processor = new NetworkProcessor(ProtocolLibrary.getErrorReporter()); // Get the channel field - this.channelField = new VolatileField( - FuzzyReflection.fromObject(networkManager, true). - getFieldByType("channel", Channel.class), - networkManager, true); + this.channelField = new VolatileField(FuzzyReflection.fromObject(networkManager, true).getFieldByType("channel", Channel.class), + networkManager, true); } /** @@ -168,7 +169,19 @@ class ChannelInjector extends ByteToMessageDecoder implements Injector { */ @Override public int getProtocolVersion() { - return MinecraftProtocolVersion.getCurrentVersion(); + MethodAccessor accessor = PROTOCOL_VERSION; + if (accessor == null) { + try { + accessor = Accessors.getMethodAccessor(networkManager.getClass(), "getVersion"); + } catch (Throwable ex) { + } + } + + if (accessor != null) { + return (Integer) accessor.invoke(networkManager); + } else { + return MinecraftProtocolVersion.getCurrentVersion(); + } } @Override diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/utility/MinecraftProtocolVersion.java b/ProtocolLib/src/main/java/com/comphenix/protocol/utility/MinecraftProtocolVersion.java index 865b6d27..82969274 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/utility/MinecraftProtocolVersion.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/utility/MinecraftProtocolVersion.java @@ -39,9 +39,6 @@ public class MinecraftProtocolVersion { map.put(new MinecraftVersion(1, 7, 1), 4); map.put(new MinecraftVersion(1, 7, 6), 5); map.put(new MinecraftVersion(1, 8, 0), 47); - - // Unknown number - map.put(new MinecraftVersion(1, 8, 1), Integer.MIN_VALUE); return map; }