From b5e5118450edfd7dc0635c78cf9c6d88c18e0ada Mon Sep 17 00:00:00 2001 From: KennyTV Date: Sun, 22 Mar 2020 21:57:41 +0100 Subject: [PATCH] Minor protocol cleanup Removed unneeded integer boxing and empty method implementations of the user init --- .../ViaVersion/api/protocol/Protocol.java | 94 +++++++++++++------ .../protocols/base/BaseProtocol1_7.java | 6 -- .../Protocol1_11_1To1_11.java | 6 -- .../Protocol1_12_1To1_12.java | 6 -- .../Protocol1_12_2To1_12_1.java | 6 -- .../Protocol1_13_2To1_13_1.java | 6 -- .../Protocol1_14_3To1_14_2.java | 5 - .../Protocol1_14_4To1_14_3.java | 5 - .../Protocol1_15_1To1_15.java | 5 - .../Protocol1_15_2To1_15_1.java | 5 - .../Protocol1_9_1To1_9.java | 6 -- .../Protocol1_9To1_9_1.java | 6 -- 12 files changed, 67 insertions(+), 89 deletions(-) diff --git a/common/src/main/java/us/myles/ViaVersion/api/protocol/Protocol.java b/common/src/main/java/us/myles/ViaVersion/api/protocol/Protocol.java index bcf25e9ea..719f54c70 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/protocol/Protocol.java +++ b/common/src/main/java/us/myles/ViaVersion/api/protocol/Protocol.java @@ -1,7 +1,6 @@ package us.myles.ViaVersion.api.protocol; import us.myles.ViaVersion.api.PacketWrapper; -import us.myles.ViaVersion.api.Pair; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.platform.providers.ViaProviders; @@ -13,14 +12,12 @@ import us.myles.ViaVersion.packets.State; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; public abstract class Protocol { - private final Map, ProtocolPacket> incoming = new HashMap<>(); - private final Map, ProtocolPacket> outgoing = new HashMap<>(); - - private final Map storedObjects = new ConcurrentHashMap<>(); + private final Map incoming = new HashMap<>(); + private final Map outgoing = new HashMap<>(); + private final Map storedObjects = new HashMap<>(); // currently only used for MetadataRewriters public Protocol() { registerPackets(); @@ -50,26 +47,30 @@ public abstract class Protocol { } /** - * Handle protocol registration phase, use this to register providers / tasks. - * - * @param providers The current providers - */ - protected void register(ViaProviders providers) { - - } - - /** - * Register the packets for this protocol + * Register the packets for this protocol. */ protected abstract void registerPackets(); + /** + * Handle protocol registration phase, use this to register providers / tasks. + *

+ * To be overridden if needed. + * + * @param providers The current providers + */ + protected void register(ViaProviders providers) { + } + /** * Initialise a user for this protocol setting up objects. * /!\ WARNING - May be called more than once in a single {@link UserConnection} + *

+ * To be overridden if needed. * * @param userConnection The user to initialise */ - public abstract void init(UserConnection userConnection); + public void init(UserConnection userConnection) { + } /** * Register an incoming packet, with simple id transformation. @@ -96,12 +97,12 @@ public abstract class Protocol { public void registerIncoming(State state, int oldPacketID, int newPacketID, PacketRemapper packetRemapper, boolean override) { ProtocolPacket protocolPacket = new ProtocolPacket(state, oldPacketID, newPacketID, packetRemapper); - Pair pair = new Pair<>(state, newPacketID); - if (!override && incoming.containsKey(pair)) { - Via.getPlatform().getLogger().log(Level.WARNING, pair + " already registered!" + + Packet packet = new Packet(state, newPacketID); + if (!override && incoming.containsKey(packet)) { + Via.getPlatform().getLogger().log(Level.WARNING, packet + " already registered!" + " If this override is intentional, set override to true. Stacktrace: ", new Exception()); } - incoming.put(pair, protocolPacket); + incoming.put(packet, protocolPacket); } public void cancelIncoming(State state, int oldPacketID, int newPacketID) { @@ -142,12 +143,12 @@ public abstract class Protocol { public void registerOutgoing(State state, int oldPacketID, int newPacketID, PacketRemapper packetRemapper, boolean override) { ProtocolPacket protocolPacket = new ProtocolPacket(state, oldPacketID, newPacketID, packetRemapper); - Pair pair = new Pair<>(state, oldPacketID); - if (!override && outgoing.containsKey(pair)) { - Via.getPlatform().getLogger().log(Level.WARNING, pair + " already registered!" + + Packet packet = new Packet(state, oldPacketID); + if (!override && outgoing.containsKey(packet)) { + Via.getPlatform().getLogger().log(Level.WARNING, packet + " already registered!" + " If override is intentional, set override to true. Stacktrace: ", new Exception()); } - outgoing.put(pair, protocolPacket); + outgoing.put(packet, protocolPacket); } public void cancelOutgoing(State state, int oldPacketID, int newPacketID) { @@ -172,12 +173,13 @@ public abstract class Protocol { * @throws Exception Throws exception if it fails to transform */ public void transform(Direction direction, State state, PacketWrapper packetWrapper) throws Exception { - Pair statePacket = new Pair<>(state, packetWrapper.getId()); - Map, ProtocolPacket> packetMap = (direction == Direction.OUTGOING ? outgoing : incoming); + Packet statePacket = new Packet(state, packetWrapper.getId()); + Map packetMap = (direction == Direction.OUTGOING ? outgoing : incoming); ProtocolPacket protocolPacket = packetMap.get(statePacket); if (protocolPacket == null) { return; } + // write packet id int newID = direction == Direction.OUTGOING ? protocolPacket.getNewID() : protocolPacket.getOldID(); packetWrapper.setId(newID); @@ -203,6 +205,44 @@ public abstract class Protocol { return "Protocol:" + getClass().getSimpleName(); } + public static class Packet { + private final State state; + private final int packetId; + + public Packet(State state, int packetId) { + this.state = state; + this.packetId = packetId; + } + + public State getState() { + return state; + } + + public int getPacketId() { + return packetId; + } + + @Override + public String toString() { + return "Packet{" + "state=" + state + ", packetId=" + packetId + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Packet that = (Packet) o; + return packetId == that.packetId && state == that.state; + } + + @Override + public int hashCode() { + int result = state != null ? state.hashCode() : 0; + result = 31 * result + packetId; + return result; + } + } + public static class ProtocolPacket { private final State state; private final int oldID; diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol1_7.java b/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol1_7.java index a71399a66..8ad74afde 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol1_7.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol1_7.java @@ -9,7 +9,6 @@ import net.md_5.bungee.api.ChatColor; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Pair; import us.myles.ViaVersion.api.Via; -import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.protocol.ProtocolRegistry; import us.myles.ViaVersion.api.protocol.ProtocolVersion; @@ -190,11 +189,6 @@ public class BaseProtocol1_7 extends Protocol { registerIncoming(State.LOGIN, 0x02, 0x02); // Plugin Response (1.13) } - @Override - public void init(UserConnection userConnection) { - - } - public static String addDashes(String trimmedUUID) { StringBuilder idBuff = new StringBuilder(trimmedUUID); idBuff.insert(20, '-'); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11_1to1_11/Protocol1_11_1To1_11.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11_1to1_11/Protocol1_11_1To1_11.java index 2cb6d5a66..cbedf97c9 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11_1to1_11/Protocol1_11_1To1_11.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_11_1to1_11/Protocol1_11_1To1_11.java @@ -1,6 +1,5 @@ package us.myles.ViaVersion.protocols.protocol1_11_1to1_11; -import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.Protocol; public class Protocol1_11_1To1_11 extends Protocol { @@ -8,9 +7,4 @@ public class Protocol1_11_1To1_11 extends Protocol { protected void registerPackets() { // Only had metadata changes, see wiki.vg for full info. } - - @Override - public void init(UserConnection userConnection) { - - } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12_1to1_12/Protocol1_12_1To1_12.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12_1to1_12/Protocol1_12_1To1_12.java index 49e886f24..d4d26efdf 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12_1to1_12/Protocol1_12_1To1_12.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12_1to1_12/Protocol1_12_1To1_12.java @@ -1,7 +1,6 @@ package us.myles.ViaVersion.protocols.protocol1_12_1to1_12; import us.myles.ViaVersion.api.PacketWrapper; -import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; @@ -87,9 +86,4 @@ public class Protocol1_12_1To1_12 extends Protocol { } }); } - - @Override - public void init(UserConnection userConnection) { - - } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12_2to1_12_1/Protocol1_12_2To1_12_1.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12_2to1_12_1/Protocol1_12_2To1_12_1.java index 9e46ef1e4..263d6008f 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12_2to1_12_1/Protocol1_12_2To1_12_1.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_12_2to1_12_1/Protocol1_12_2To1_12_1.java @@ -1,6 +1,5 @@ package us.myles.ViaVersion.protocols.protocol1_12_2to1_12_1; -import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.type.Type; @@ -26,9 +25,4 @@ public class Protocol1_12_2To1_12_1 extends Protocol { } }); } - - @Override - public void init(UserConnection userConnection) { - - } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_2to1_13_1/Protocol1_13_2To1_13_1.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_2to1_13_1/Protocol1_13_2To1_13_1.java index 166c3becf..f5dd2533d 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_2to1_13_1/Protocol1_13_2To1_13_1.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_2to1_13_1/Protocol1_13_2To1_13_1.java @@ -1,7 +1,6 @@ package us.myles.ViaVersion.protocols.protocol1_13_2to1_13_1; import us.myles.ViaVersion.api.PacketWrapper; -import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketHandler; @@ -71,9 +70,4 @@ public class Protocol1_13_2To1_13_1 extends Protocol { } }); } - - @Override - public void init(UserConnection userConnection) { - - } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_3to1_14_2/Protocol1_14_3To1_14_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_3to1_14_2/Protocol1_14_3To1_14_2.java index 5bfe35315..16fb87047 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_3to1_14_2/Protocol1_14_3To1_14_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_3to1_14_2/Protocol1_14_3To1_14_2.java @@ -1,7 +1,6 @@ package us.myles.ViaVersion.protocols.protocol1_14_3to1_14_2; import us.myles.ViaVersion.api.PacketWrapper; -import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; @@ -43,8 +42,4 @@ public class Protocol1_14_3To1_14_2 extends Protocol { } }); } - - @Override - public void init(UserConnection userConnection) { - } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_4to1_14_3/Protocol1_14_4To1_14_3.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_4to1_14_3/Protocol1_14_4To1_14_3.java index 8c571275e..9b8eda613 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_4to1_14_3/Protocol1_14_4To1_14_3.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14_4to1_14_3/Protocol1_14_4To1_14_3.java @@ -1,7 +1,6 @@ package us.myles.ViaVersion.protocols.protocol1_14_4to1_14_3; import us.myles.ViaVersion.api.PacketWrapper; -import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; @@ -40,8 +39,4 @@ public class Protocol1_14_4To1_14_3 extends Protocol { } }); } - - @Override - public void init(UserConnection userConnection) { - } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15_1to1_15/Protocol1_15_1To1_15.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15_1to1_15/Protocol1_15_1To1_15.java index c0f74c3fb..f5773ba77 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15_1to1_15/Protocol1_15_1To1_15.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15_1to1_15/Protocol1_15_1To1_15.java @@ -1,6 +1,5 @@ package us.myles.ViaVersion.protocols.protocol1_15_1to1_15; -import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.Protocol; public class Protocol1_15_1To1_15 extends Protocol { @@ -8,8 +7,4 @@ public class Protocol1_15_1To1_15 extends Protocol { @Override protected void registerPackets() { } - - @Override - public void init(UserConnection user) { - } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15_2to1_15_1/Protocol1_15_2To1_15_1.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15_2to1_15_1/Protocol1_15_2To1_15_1.java index 8a6b08b40..370817009 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15_2to1_15_1/Protocol1_15_2To1_15_1.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15_2to1_15_1/Protocol1_15_2To1_15_1.java @@ -1,6 +1,5 @@ package us.myles.ViaVersion.protocols.protocol1_15_2to1_15_1; -import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.Protocol; public class Protocol1_15_2To1_15_1 extends Protocol { @@ -8,8 +7,4 @@ public class Protocol1_15_2To1_15_1 extends Protocol { @Override protected void registerPackets() { } - - @Override - public void init(UserConnection user) { - } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1to1_9/Protocol1_9_1To1_9.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1to1_9/Protocol1_9_1To1_9.java index e6f21be97..8fd1ab5cc 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1to1_9/Protocol1_9_1To1_9.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1to1_9/Protocol1_9_1To1_9.java @@ -1,7 +1,6 @@ package us.myles.ViaVersion.protocols.protocol1_9_1to1_9; import us.myles.ViaVersion.api.PacketWrapper; -import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; @@ -45,9 +44,4 @@ public class Protocol1_9_1To1_9 extends Protocol { } }); } - - @Override - public void init(UserConnection userConnection) { - - } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_9_1/Protocol1_9To1_9_1.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_9_1/Protocol1_9To1_9_1.java index a06b58302..30ad7c794 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_9_1/Protocol1_9To1_9_1.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_9_1/Protocol1_9To1_9_1.java @@ -1,7 +1,6 @@ package us.myles.ViaVersion.protocols.protocol1_9to1_9_1; import us.myles.ViaVersion.api.PacketWrapper; -import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; @@ -47,9 +46,4 @@ public class Protocol1_9To1_9_1 extends Protocol { } }); } - - @Override - public void init(UserConnection userConnection) { - - } }