3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-27 08:30:09 +01:00

Minor protocol cleanup

Removed unneeded integer boxing and empty method implementations of the user init
Dieser Commit ist enthalten in:
KennyTV 2020-03-22 21:57:41 +01:00
Ursprung 95db675de5
Commit b5e5118450
12 geänderte Dateien mit 67 neuen und 89 gelöschten Zeilen

Datei anzeigen

@ -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<Pair<State, Integer>, ProtocolPacket> incoming = new HashMap<>();
private final Map<Pair<State, Integer>, ProtocolPacket> outgoing = new HashMap<>();
private final Map<Class, Object> storedObjects = new ConcurrentHashMap<>();
private final Map<Packet, ProtocolPacket> incoming = new HashMap<>();
private final Map<Packet, ProtocolPacket> outgoing = new HashMap<>();
private final Map<Class, Object> 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.
* <p>
* 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}
* <p>
* 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<State, Integer> 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<State, Integer> 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<State, Integer> statePacket = new Pair<>(state, packetWrapper.getId());
Map<Pair<State, Integer>, ProtocolPacket> packetMap = (direction == Direction.OUTGOING ? outgoing : incoming);
Packet statePacket = new Packet(state, packetWrapper.getId());
Map<Packet, ProtocolPacket> 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;

Datei anzeigen

@ -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, '-');

Datei anzeigen

@ -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) {
}
}

Datei anzeigen

@ -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) {
}
}

Datei anzeigen

@ -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) {
}
}

Datei anzeigen

@ -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) {
}
}

Datei anzeigen

@ -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) {
}
}

Datei anzeigen

@ -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) {
}
}

Datei anzeigen

@ -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) {
}
}

Datei anzeigen

@ -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) {
}
}

Datei anzeigen

@ -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) {
}
}

Datei anzeigen

@ -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) {
}
}