From 56b82b049a7c7b292b2170daa57cf8eaf138622d Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Mon, 19 Feb 2024 22:37:51 +0100 Subject: [PATCH] Move a few methods out of AbstractProtocol --- .../api/data/entity/EntityTracker.java | 1 + .../api/protocol/AbstractProtocol.java | 112 +++++------------- .../viaversion/api/protocol/Protocol.java | 4 +- .../packet/provider/PacketTypesProvider.java | 23 +++- .../provider/SimplePacketTypesProvider.java | 2 - .../viaversion/util/ProtocolUtil.java | 72 +++++++++++ .../protocol/ProtocolPipelineImpl.java | 5 +- .../Protocol1_20_3To1_20_2.java | 2 + .../Protocol1_20_5To1_20_3.java | 2 + 9 files changed, 135 insertions(+), 88 deletions(-) create mode 100644 api/src/main/java/com/viaversion/viaversion/util/ProtocolUtil.java diff --git a/api/src/main/java/com/viaversion/viaversion/api/data/entity/EntityTracker.java b/api/src/main/java/com/viaversion/viaversion/api/data/entity/EntityTracker.java index 56770754a..11f194e49 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/data/entity/EntityTracker.java +++ b/api/src/main/java/com/viaversion/viaversion/api/data/entity/EntityTracker.java @@ -34,6 +34,7 @@ public interface EntityTracker { * * @return user connection */ + @Deprecated UserConnection user(); /** diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/AbstractProtocol.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/AbstractProtocol.java index 06faadd21..1a8e313d9 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/AbstractProtocol.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/AbstractProtocol.java @@ -41,8 +41,7 @@ import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; import com.viaversion.viaversion.api.rewriter.Rewriter; import com.viaversion.viaversion.exception.CancelException; import com.viaversion.viaversion.exception.InformativeException; -import java.util.Collections; -import java.util.EnumMap; +import com.viaversion.viaversion.util.ProtocolUtil; import java.util.HashMap; import java.util.Map; import java.util.function.BiConsumer; @@ -50,6 +49,8 @@ import java.util.function.Predicate; import java.util.logging.Level; import org.checkerframework.checker.nullness.qual.Nullable; +import static com.viaversion.viaversion.util.ProtocolUtil.packetTypeMap; + /** * Abstract protocol class to handle packet transformation between two protocol versions. * @@ -171,22 +172,19 @@ public abstract class AbstractProtocol> entry : mappedPacketTypes.entrySet()) { PacketTypeMap mappedTypes = entry.getValue(); PacketTypeMap unmappedTypes = unmappedPacketTypes.get(entry.getKey()); - registerPacketIdChanges(unmappedTypes, mappedTypes, registeredPredicate, registerConsumer); - } - } + for (U unmappedType : unmappedTypes.types()) { + M mappedType = mappedTypes.typeByName(unmappedType.getName()); + if (mappedType == null) { + // No mapped packet of the same name exists + Preconditions.checkArgument(registeredPredicate.test(unmappedType), "Packet %s in %s has no mapping - it needs to be manually cancelled or remapped", unmappedType, getClass()); + continue; + } - protected void registerPacketIdChanges(PacketTypeMap unmappedTypes, PacketTypeMap mappedTypes, Predicate registeredPredicate, BiConsumer registerConsumer) { - for (U unmappedType : unmappedTypes.types()) { - M mappedType = mappedTypes.typeByName(unmappedType.getName()); - if (mappedType == null) { - // No mapped packet of the same name exists - Preconditions.checkArgument(registeredPredicate.test(unmappedType), "Packet %s in %s has no mapping - it needs to be manually cancelled or remapped", unmappedType, getClass()); - continue; - } + // Register if no custom handler exists and ids are different + if (unmappedType.getId() != mappedType.getId() && !registeredPredicate.test(unmappedType)) { + registerConsumer.accept(unmappedType, mappedType); - // Register if no custom handler exists and ids are different - if (unmappedType.getId() != mappedType.getId() && !registeredPredicate.test(unmappedType)) { - registerConsumer.accept(unmappedType, mappedType); + } } } } @@ -248,50 +246,20 @@ public abstract class AbstractProtocol packet type class - * @return map of packet types by state - */ - @SafeVarargs - protected final

Map> packetTypeMap(@Nullable Class

parent, Class... packetTypeClasses) { - if (parent == null) { - return Collections.emptyMap(); - } - - final Map> map = new EnumMap<>(State.class); - for (final Class packetTypeClass : packetTypeClasses) { - // Get state from first enum type - final P[] types = packetTypeClass.getEnumConstants(); - Preconditions.checkArgument(types != null, "%s not an enum", packetTypeClass); - Preconditions.checkArgument(types.length > 0, "Enum %s has no types", packetTypeClass); - final State state = types[0].state(); - map.put(state, PacketTypeMap.of(packetTypeClass)); - } - return map; - } - protected @Nullable SU configurationAcknowledgedPacket() { - final PacketTypeMap packetTypeMap = packetTypesProvider.unmappedServerboundPacketTypes().get(State.PLAY); - return packetTypeMap != null ? packetTypeMap.typeByName("CONFIGURATION_ACKNOWLEDGED") : null; + return packetTypesProvider.unmappedServerboundType(State.PLAY, "CONFIGURATION_ACKNOWLEDGED"); } protected @Nullable CU startConfigurationPacket() { - final PacketTypeMap packetTypeMap = packetTypesProvider.unmappedClientboundPacketTypes().get(State.PLAY); - return packetTypeMap != null ? packetTypeMap.typeByName("START_CONFIGURATION") : null; + return packetTypesProvider.unmappedClientboundType(State.PLAY, "START_CONFIGURATION"); } protected @Nullable SU serverboundFinishConfigurationPacket() { - final PacketTypeMap packetTypeMap = packetTypesProvider.unmappedServerboundPacketTypes().get(State.CONFIGURATION); - return packetTypeMap != null ? packetTypeMap.typeByName("FINISH_CONFIGURATION") : null; + return packetTypesProvider.unmappedServerboundType(State.CONFIGURATION, "FINISH_CONFIGURATION"); } protected @Nullable CU clientboundFinishConfigurationPacket() { - final PacketTypeMap packetTypeMap = packetTypesProvider.unmappedClientboundPacketTypes().get(State.CONFIGURATION); - return packetTypeMap != null ? packetTypeMap.typeByName("FINISH_CONFIGURATION") : null; + return packetTypesProvider.unmappedClientboundType(State.CONFIGURATION, "FINISH_CONFIGURATION"); } // --------------------------------------------------------------------------------- @@ -426,19 +394,17 @@ public abstract class AbstractProtocol" + toNiceHex(mappedPacketId)); + + " IN REMAP OF " + state + " " + ProtocolUtil.toNiceHex(unmappedPacketId) + "->" + ProtocolUtil.toNiceHex(mappedPacketId)); } - throw e; - } - - private @Nullable CU unmappedClientboundPacketType(final State state, final int packetId) { - PacketTypeMap map = packetTypesProvider.unmappedClientboundPacketTypes().get(state); - return map != null ? map.typeById(packetId) : null; - } - - private @Nullable SU unmappedServerboundPacketType(final State state, final int packetId) { - PacketTypeMap map = packetTypesProvider.unmappedServerboundPacketTypes().get(state); - return map != null ? map.typeById(packetId) : null; - } - - public static String toNiceHex(int id) { - String hex = Integer.toHexString(id).toUpperCase(); - return (hex.length() == 1 ? "0x0" : "0x") + hex; } /** @@ -490,11 +442,11 @@ public abstract class AbstractProtocol wrapper.user().getProtocolInfo().setClientState(state); } - protected PacketHandler setServerStateHandler(final State state) { + private PacketHandler setServerStateHandler(final State state) { return wrapper -> wrapper.user().getProtocolInfo().setServerState(state); } diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/Protocol.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/Protocol.java index d5c8088e6..58bc62ee0 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/Protocol.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/Protocol.java @@ -22,7 +22,6 @@ */ package com.viaversion.viaversion.api.protocol; -import com.google.common.annotations.Beta; import com.google.common.base.Preconditions; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.data.MappingData; @@ -257,7 +256,6 @@ public interface Protocol getPacketTypesProvider(); /** @@ -267,6 +265,7 @@ public interface Protocol type * @return object if present, else null */ + @Deprecated @Nullable T get(Class objectClass); /** @@ -274,6 +273,7 @@ public interface Protocol unmapped serverbound packet type * @see SimplePacketTypesProvider */ -@Beta public interface PacketTypesProvider { /** @@ -69,4 +68,24 @@ public interface PacketTypesProvider> mappedServerboundPacketTypes(); + + default @Nullable CU unmappedClientboundType(final State state, final String typeName) { + PacketTypeMap map = unmappedClientboundPacketTypes().get(state); + return map != null ? map.typeByName(typeName) : null; + } + + default @Nullable SU unmappedServerboundType(final State state, final String typeName) { + PacketTypeMap map = unmappedServerboundPacketTypes().get(state); + return map != null ? map.typeByName(typeName) : null; + } + + default @Nullable CU unmappedClientboundType(final State state, final int packetId) { + PacketTypeMap map = unmappedClientboundPacketTypes().get(state); + return map != null ? map.typeById(packetId) : null; + } + + default @Nullable SU unmappedServerboundType(final State state, final int packetId) { + PacketTypeMap map = unmappedServerboundPacketTypes().get(state); + return map != null ? map.typeById(packetId) : null; + } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/packet/provider/SimplePacketTypesProvider.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/packet/provider/SimplePacketTypesProvider.java index 06953c252..646bdb08f 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/packet/provider/SimplePacketTypesProvider.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/packet/provider/SimplePacketTypesProvider.java @@ -22,13 +22,11 @@ */ package com.viaversion.viaversion.api.protocol.packet.provider; -import com.google.common.annotations.Beta; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; import com.viaversion.viaversion.api.protocol.packet.State; import java.util.Map; -@Beta public final class SimplePacketTypesProvider implements PacketTypesProvider { private final Map> unmappedClientboundPacketTypes; private final Map> mappedClientboundPacketTypes; diff --git a/api/src/main/java/com/viaversion/viaversion/util/ProtocolUtil.java b/api/src/main/java/com/viaversion/viaversion/util/ProtocolUtil.java new file mode 100644 index 000000000..2a61e7393 --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/util/ProtocolUtil.java @@ -0,0 +1,72 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.viaversion.viaversion.util; + +import com.google.common.base.Preconditions; +import com.viaversion.viaversion.api.protocol.packet.PacketType; +import com.viaversion.viaversion.api.protocol.packet.State; +import com.viaversion.viaversion.api.protocol.packet.provider.PacketTypeMap; +import java.util.Collections; +import java.util.EnumMap; +import java.util.Map; +import org.checkerframework.checker.nullness.qual.Nullable; + +public final class ProtocolUtil { + + /** + * Returns a map of packet types by state. + * + * @param parent parent packet type class as given by the Protocol generics + * @param packetTypeClasses packet type enum classes belonging to the parent type + * @param

packet type class + * @return map of packet types by state + */ + @SafeVarargs + public static

Map> packetTypeMap(@Nullable Class

parent, Class... packetTypeClasses) { + if (parent == null) { + return Collections.emptyMap(); + } + + final Map> map = new EnumMap<>(State.class); + for (final Class packetTypeClass : packetTypeClasses) { + // Get state from first enum type + final P[] types = packetTypeClass.getEnumConstants(); + Preconditions.checkArgument(types != null, "%s not an enum", packetTypeClass); + Preconditions.checkArgument(types.length > 0, "Enum %s has no types", packetTypeClass); + final State state = types[0].state(); + map.put(state, PacketTypeMap.of(packetTypeClass)); + } + return map; + } + + /** + * Returns a hex string of a packet id. + * + * @param id packet id + * @return packet id as a nice hex string + */ + public static String toNiceHex(int id) { + final String hex = Integer.toHexString(id).toUpperCase(); + return (hex.length() == 1 ? "0x0" : "0x") + hex; + } +} diff --git a/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolPipelineImpl.java b/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolPipelineImpl.java index 97bce8082..fcffa1b6e 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolPipelineImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolPipelineImpl.java @@ -27,6 +27,7 @@ import com.viaversion.viaversion.api.protocol.ProtocolPipeline; import com.viaversion.viaversion.api.protocol.packet.Direction; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.packet.State; +import com.viaversion.viaversion.util.ProtocolUtil; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -138,9 +139,9 @@ public class ProtocolPipelineImpl extends AbstractSimpleProtocol implements Prot direction, state, originalID, - AbstractSimpleProtocol.toNiceHex(originalID), + ProtocolUtil.toNiceHex(originalID), packetWrapper.getId(), - AbstractSimpleProtocol.toNiceHex(packetWrapper.getId()), + ProtocolUtil.toNiceHex(packetWrapper.getId()), protocolInfo.protocolVersion().getName(), packetWrapper }); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_3to1_20_2/Protocol1_20_3To1_20_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_3to1_20_2/Protocol1_20_3To1_20_2.java index ce9acd502..ff1ae6e39 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_3to1_20_2/Protocol1_20_3To1_20_2.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_3to1_20_2/Protocol1_20_3To1_20_2.java @@ -54,6 +54,8 @@ import java.nio.charset.StandardCharsets; import java.util.BitSet; import java.util.UUID; +import static com.viaversion.viaversion.util.ProtocolUtil.packetTypeMap; + public final class Protocol1_20_3To1_20_2 extends AbstractProtocol { public static final MappingData MAPPINGS = new MappingDataBase("1.20.2", "1.20.3"); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/Protocol1_20_5To1_20_3.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/Protocol1_20_5To1_20_3.java index 5ab4973f8..d920f5398 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/Protocol1_20_5To1_20_3.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/Protocol1_20_5To1_20_3.java @@ -50,6 +50,8 @@ import com.viaversion.viaversion.rewriter.SoundRewriter; import com.viaversion.viaversion.rewriter.StatisticsRewriter; import com.viaversion.viaversion.rewriter.TagRewriter; +import static com.viaversion.viaversion.util.ProtocolUtil.packetTypeMap; + public final class Protocol1_20_5To1_20_3 extends AbstractProtocol { public static final MappingData MAPPINGS = new MappingDataBase("1.20.3", "1.20.5");