From b17af6871690bfbe0c0c76644d27d91218605978 Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Tue, 24 Nov 2015 15:35:23 -0500 Subject: [PATCH] Clean up some less-than-conventional code --- .../comphenix/protocol/CommandProtocol.java | 4 +-- .../comphenix/protocol/ProtocolConfig.java | 4 --- .../independent/NettyChannelInjector.java | 2 +- .../concurrency/SortedCopyOnWriteArray.java | 2 +- .../protocol/events/PacketAdapter.java | 10 +++---- .../injector/DelayedPacketManager.java | 3 +- .../injector/PacketFilterManager.java | 3 +- .../injector/packet/InterceptWritePacket.java | 4 +-- .../injector/packet/ProxyPacketInjector.java | 2 +- .../injector/spigot/DummyPacketInjector.java | 3 +- .../reflect/cloning/SerializableCloner.java | 28 ++++++++----------- .../compiler/CompiledStructureModifier.java | 2 +- .../reflect/compiler/MethodDescriptor.java | 2 +- .../reflect/fuzzy/AbstractFuzzyMatcher.java | 7 +---- .../reflect/fuzzy/FuzzyClassContract.java | 2 +- .../protocol/utility/SnapshotVersion.java | 4 +-- .../protocol/wrappers/WrappedGameProfile.java | 4 ++- .../wrappers/nbt/WrappedCompound.java | 4 +-- .../protocol/wrappers/nbt/WrappedList.java | 2 +- .../protocol/SimpleCraftBukkitITCase.java | 1 - .../protocol/SimpleMinecraftClient.java | 6 +--- .../protocol/wrappers/PlayerInfoDataTest.java | 2 +- .../comphenix/tinyprotocol/TinyProtocol.java | 1 + 23 files changed, 41 insertions(+), 61 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java b/ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java index b73c64a7..d74016c7 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java @@ -75,7 +75,7 @@ class CommandProtocol extends CommandBase { } else if (subCommand.equalsIgnoreCase("timings")) { toggleTimings(sender, args); } else if (subCommand.equalsIgnoreCase("listeners")) { - printListeners(sender, args); + printListeners(sender); } else if (subCommand.equalsIgnoreCase("version")) { printVersion(sender); } else if (subCommand.equalsIgnoreCase("dump")) { @@ -96,7 +96,7 @@ class CommandProtocol extends CommandBase { } // Display every listener on the server - private void printListeners(final CommandSender sender, String[] args) { + private void printListeners(final CommandSender sender) { ProtocolManager manager = ProtocolLibrary.getProtocolManager(); sender.sendMessage(ChatColor.GOLD + "Packet listeners:"); diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolConfig.java b/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolConfig.java index 9cf6430a..68663f76 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolConfig.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolConfig.java @@ -77,10 +77,6 @@ public class ProtocolConfig { private int modCount; public ProtocolConfig(Plugin plugin) { - this(plugin, plugin.getConfig()); - } - - public ProtocolConfig(Plugin plugin, Configuration config) { this.plugin = plugin; reloadConfig(); } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/compat/netty/independent/NettyChannelInjector.java b/ProtocolLib/src/main/java/com/comphenix/protocol/compat/netty/independent/NettyChannelInjector.java index 3a1eb41b..ddf8a910 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/compat/netty/independent/NettyChannelInjector.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/compat/netty/independent/NettyChannelInjector.java @@ -388,7 +388,7 @@ public class NettyChannelInjector extends ByteToMessageDecoder implements Channe * @return TRUE if it is, FALSE if not or unknown. */ private boolean guessCompression(ChannelHandler handler) { - String className = handler != null ? handler.getClass().getCanonicalName() : null; + String className = handler != null ? handler.getClass().getCanonicalName() : ""; return className.contains("Compressor") || className.contains("Decompressor"); } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/concurrency/SortedCopyOnWriteArray.java b/ProtocolLib/src/main/java/com/comphenix/protocol/concurrency/SortedCopyOnWriteArray.java index 8f9e3f60..ed1d84c8 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/concurrency/SortedCopyOnWriteArray.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/concurrency/SortedCopyOnWriteArray.java @@ -30,7 +30,7 @@ import com.google.common.collect.Iterables; * An implicitly sorted array list that preserves insertion order and maintains duplicates. * @param - type of the elements in the list. */ -public class SortedCopyOnWriteArray> implements Iterable, Collection { +public class SortedCopyOnWriteArray> implements Collection { // Prevent reordering private volatile List list; diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketAdapter.java b/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketAdapter.java index 079e4deb..a5204268 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketAdapter.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketAdapter.java @@ -346,13 +346,11 @@ public abstract class PacketAdapter implements PacketListener { * @return Name of the given plugin. */ public static String getPluginName(Plugin plugin) { - // Try to get the plugin name + if (plugin == null) + return "UNKNOWN"; + try { - if (plugin == null) - return "UNKNOWN"; - else - return plugin.getName(); - + return plugin.getName(); } catch (NoSuchMethodError e) { return plugin.toString(); } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/DelayedPacketManager.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/DelayedPacketManager.java index d96fbcac..85126ea1 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/DelayedPacketManager.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/DelayedPacketManager.java @@ -18,7 +18,6 @@ import org.bukkit.plugin.PluginManager; import com.comphenix.protocol.AsynchronousManager; import com.comphenix.protocol.PacketType; import com.comphenix.protocol.PacketType.Sender; -import com.comphenix.protocol.ProtocolManager; import com.comphenix.protocol.error.ErrorReporter; import com.comphenix.protocol.error.Report; import com.comphenix.protocol.error.ReportType; @@ -43,7 +42,7 @@ import com.google.common.collect.Sets; * * @author Kristian */ -public class DelayedPacketManager implements ProtocolManager, InternalManager { +public class DelayedPacketManager implements InternalManager { // Registering packet IDs that are not supported public static final ReportType REPORT_CANNOT_SEND_QUEUED_PACKET = new ReportType("Cannot send queued packet %s."); public static final ReportType REPORT_CANNOT_SEND_QUEUED_WIRE_PACKET = new ReportType("Cannot send queued wire packet %s."); diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java index cd302f55..e8a1b5f8 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java @@ -51,7 +51,6 @@ import org.bukkit.plugin.PluginManager; import com.comphenix.protocol.AsynchronousManager; import com.comphenix.protocol.PacketType; import com.comphenix.protocol.PacketType.Sender; -import com.comphenix.protocol.ProtocolManager; import com.comphenix.protocol.async.AsyncFilterManager; import com.comphenix.protocol.async.AsyncMarker; import com.comphenix.protocol.compat.netty.Netty; @@ -91,7 +90,7 @@ import com.google.common.base.Predicate; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; -public final class PacketFilterManager implements ProtocolManager, ListenerInvoker, InternalManager { +public final class PacketFilterManager implements ListenerInvoker, InternalManager { public static final ReportType REPORT_CANNOT_LOAD_PACKET_LIST = new ReportType("Cannot load server and client packet list."); public static final ReportType REPORT_CANNOT_INITIALIZE_PACKET_INJECTOR = new ReportType("Unable to initialize packet injector"); diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/packet/InterceptWritePacket.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/packet/InterceptWritePacket.java index 67332f7b..d3ecb2cd 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/packet/InterceptWritePacket.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/packet/InterceptWritePacket.java @@ -54,7 +54,7 @@ public class InterceptWritePacket { } // TODO: PacketId should probably do something... - private Class createProxyClass(int packetId) { + private Class createProxyClass() { // Construct the proxy object Enhancer ex = EnhancerFactory.getInstance().createEnhancer(); @@ -102,7 +102,7 @@ public class InterceptWritePacket { // Concurrent pattern if (stored == null) { - final Class created = createProxyClass(packetId); + final Class created = createProxyClass(); stored = proxyClasses.putIfAbsent(packetId, created); // We won! diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/packet/ProxyPacketInjector.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/packet/ProxyPacketInjector.java index 49191078..b047ac9c 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/packet/ProxyPacketInjector.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/packet/ProxyPacketInjector.java @@ -348,7 +348,7 @@ class ProxyPacketInjector implements PacketInjector { @Override public PacketEvent packetRecieved(PacketContainer packet, Player client, byte[] buffered) { NetworkMarker marker = buffered != null ? new LegacyNetworkMarker(ConnectionSide.CLIENT_SIDE, buffered, packet.getType()) : null; - PacketEvent event = PacketEvent.fromClient((Object) manager, packet, marker, client); + PacketEvent event = PacketEvent.fromClient(manager, packet, marker, client); manager.invokePacketRecieving(event); return event; diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/spigot/DummyPacketInjector.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/spigot/DummyPacketInjector.java index aa1e65c5..645d1abc 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/spigot/DummyPacketInjector.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/spigot/DummyPacketInjector.java @@ -8,7 +8,6 @@ import com.comphenix.protocol.PacketType; import com.comphenix.protocol.concurrency.PacketTypeSet; import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketEvent; -import com.comphenix.protocol.injector.packet.PacketInjector; import com.google.common.collect.Sets; /** @@ -16,7 +15,7 @@ import com.google.common.collect.Sets; * * @author Kristian */ -class DummyPacketInjector extends AbstractPacketInjector implements PacketInjector { +class DummyPacketInjector extends AbstractPacketInjector { private SpigotPacketInjector injector; private PacketTypeSet lastBufferedPackets = new PacketTypeSet(); diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/cloning/SerializableCloner.java b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/cloning/SerializableCloner.java index 58819caa..1dd4c74a 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/cloning/SerializableCloner.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/cloning/SerializableCloner.java @@ -31,21 +31,17 @@ public class SerializableCloner implements Cloner { * @return The cloned object. * @throws RuntimeException If we were unable to clone the object. */ - @SuppressWarnings("unchecked") - public static T clone(final T obj) { - try { - if (obj instanceof Serializable) { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - ObjectOutputStream oout = new ObjectOutputStream(out); + @SuppressWarnings("unchecked") + public static T clone(final T obj) { + try { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ObjectOutputStream oout = new ObjectOutputStream(out); - oout.writeObject(obj); - ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())); - return (T) in.readObject(); - } else { - throw new RuntimeException("Object " + obj + " is not serializable!"); - } - } catch (Exception e) { - throw new RuntimeException("Unable to clone object " + obj + " (" + obj.getClass().getName() + ")", e); - } - } + oout.writeObject(obj); + ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())); + return (T) in.readObject(); + } catch (Exception e) { + throw new RuntimeException("Unable to clone object " + obj + " (" + obj.getClass().getName() + ")", e); + } + } } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/compiler/CompiledStructureModifier.java b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/compiler/CompiledStructureModifier.java index dfd3710d..f2365289 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/compiler/CompiledStructureModifier.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/compiler/CompiledStructureModifier.java @@ -79,7 +79,7 @@ public abstract class CompiledStructureModifier extends StructureModifier implements Comparable obj) { - if (obj instanceof AbstractFuzzyMatcher) { - AbstractFuzzyMatcher matcher = (AbstractFuzzyMatcher) obj; - return Ints.compare(getRoundNumber(), matcher.getRoundNumber()); - } - // No match - return -1; + return Ints.compare(getRoundNumber(), obj.getRoundNumber()); } /** diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/fuzzy/FuzzyClassContract.java b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/fuzzy/FuzzyClassContract.java index a8236da0..e3f076fb 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/fuzzy/FuzzyClassContract.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/fuzzy/FuzzyClassContract.java @@ -251,7 +251,7 @@ public class FuzzyClassContract extends AbstractFuzzyMatcher> { (baseclassContracts.size() == 0 || processValue(value.getSuperclass(), parent, baseclassContracts)) && (interfaceContracts.size() == 0 || - processContracts(Arrays.asList(value.getInterfaces()), (Class) parent, interfaceContracts)); + processContracts(Arrays.asList(value.getInterfaces()), parent, interfaceContracts)); } private boolean processContracts(Collection values, Object parent, List> matchers) { diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/utility/SnapshotVersion.java b/ProtocolLib/src/main/java/com/comphenix/protocol/utility/SnapshotVersion.java index d23aba23..9644165e 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/utility/SnapshotVersion.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/utility/SnapshotVersion.java @@ -33,7 +33,7 @@ public class SnapshotVersion implements Comparable, Serializabl if (matcher.matches()) { try { this.snapshotDate = getDateFormat().parse(matcher.group(1)); - this.snapshotWeekVersion = (int)matcher.group(2).charAt(0) - (int)'a'; + this.snapshotWeekVersion = matcher.group(2).charAt(0) - 'a'; this.rawString = version; } catch (ParseException e) { throw new IllegalArgumentException("Date implied by snapshot version is invalid.", e); @@ -83,7 +83,7 @@ public class SnapshotVersion implements Comparable, Serializabl rawString = String.format("%02dw%02d%s", current.get(Calendar.YEAR) % 100, current.get(Calendar.WEEK_OF_YEAR), - (char) ((int)'a' + snapshotWeekVersion)); + (char) ('a' + snapshotWeekVersion)); } return rawString; } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedGameProfile.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedGameProfile.java index 1a98cf0c..f5090faf 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedGameProfile.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedGameProfile.java @@ -165,8 +165,10 @@ public class WrappedGameProfile extends AbstractWrapper { * @throws IllegalArgumentException If we cannot parse the text. */ private static UUID parseUUID(String id) { + if (id == null) return null; + try { - return id != null ? UUID.fromString(id) : null; + return UUID.fromString(id); } catch (IllegalArgumentException e) { // Warn once every hour (per plugin) ProtocolLibrary.getErrorReporter() diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedCompound.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedCompound.java index e861e41d..058bb585 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedCompound.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedCompound.java @@ -31,7 +31,7 @@ import com.comphenix.protocol.wrappers.nbt.io.NbtBinarySerializer; * * @author Kristian */ -class WrappedCompound implements NbtWrapper>>, Iterable>, NbtCompound { +class WrappedCompound implements NbtWrapper>>, NbtCompound { // A list container private WrappedElement> container; @@ -636,7 +636,7 @@ class WrappedCompound implements NbtWrapper>>, Iterable NbtBase remove(String key) { + public NbtBase remove(String key) { return getValue().remove(key); } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedList.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedList.java index 44adc534..39f65f51 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedList.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedList.java @@ -36,7 +36,7 @@ import com.google.common.collect.Iterables; * * @param - the type of the value in each NBT sub element. */ -class WrappedList implements NbtWrapper>>, Iterable, NbtList { +class WrappedList implements NbtWrapper>>, NbtList { // A list container private WrappedElement> container; diff --git a/ProtocolLib/src/test/java/com/comphenix/integration/protocol/SimpleCraftBukkitITCase.java b/ProtocolLib/src/test/java/com/comphenix/integration/protocol/SimpleCraftBukkitITCase.java index ebfcea50..d9f14a48 100644 --- a/ProtocolLib/src/test/java/com/comphenix/integration/protocol/SimpleCraftBukkitITCase.java +++ b/ProtocolLib/src/test/java/com/comphenix/integration/protocol/SimpleCraftBukkitITCase.java @@ -103,7 +103,6 @@ public class SimpleCraftBukkitITCase { * Copy ProtocolLib into the plugins folder. * @throws IOException If anything went wrong. */ - @SuppressWarnings("deprecation") private static void setupPlugins() throws IOException { File pluginDirectory = new File("plugins/"); File srcDirectory = new File("../"); diff --git a/ProtocolLib/src/test/java/com/comphenix/integration/protocol/SimpleMinecraftClient.java b/ProtocolLib/src/test/java/com/comphenix/integration/protocol/SimpleMinecraftClient.java index 6de6edab..e4e43028 100644 --- a/ProtocolLib/src/test/java/com/comphenix/integration/protocol/SimpleMinecraftClient.java +++ b/ProtocolLib/src/test/java/com/comphenix/integration/protocol/SimpleMinecraftClient.java @@ -6,7 +6,6 @@ import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.OutputStream; import java.net.InetSocketAddress; import java.net.Socket; @@ -50,7 +49,6 @@ public class SimpleMinecraftClient { Socket socket = null; OutputStream output = null; InputStream input = null; - InputStreamReader reader = null; try { socket = new Socket(); @@ -76,10 +74,7 @@ public class SimpleMinecraftClient { socket.close(); return ((ResponsePacket) packet).getPingJson(); - } finally { - if (reader != null) - reader.close(); if (input != null) input.close(); if (output != null) @@ -214,6 +209,7 @@ public class SimpleMinecraftClient { serializer.serializeVarInt(output, type.getCurrentId()); } + @SuppressWarnings("unused") public void read(PacketType type, DataInputStream input) throws IOException { // Note - we don't read the packet id if (this.type != type) { diff --git a/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/PlayerInfoDataTest.java b/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/PlayerInfoDataTest.java index bb9d6c80..d86fedb2 100644 --- a/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/PlayerInfoDataTest.java +++ b/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/PlayerInfoDataTest.java @@ -33,7 +33,7 @@ import com.comphenix.protocol.wrappers.EnumWrappers.NativeGameMode; public class PlayerInfoDataTest { @BeforeClass - public static void initializeBukkit() throws IllegalAccessException { + public static void initializeBukkit() { BukkitInitialization.initializePackage(); } diff --git a/modules/TinyProtocol/src/main/java/com/comphenix/tinyprotocol/TinyProtocol.java b/modules/TinyProtocol/src/main/java/com/comphenix/tinyprotocol/TinyProtocol.java index 7158222f..3ef458f9 100644 --- a/modules/TinyProtocol/src/main/java/com/comphenix/tinyprotocol/TinyProtocol.java +++ b/modules/TinyProtocol/src/main/java/com/comphenix/tinyprotocol/TinyProtocol.java @@ -257,6 +257,7 @@ public abstract class TinyProtocol { * Note that this is not executed on the main thread. * * @param reciever - the receiving player, NULL for early login/status packets. + * @param channel - the channel that received the packet. Never NULL. * @param remoteAddress - remote address of the sending client. Never NULL. * @param packet - the packet being sent. * @return The packet to send instead, or NULL to cancel the transmission.