3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-19 14:30:16 +01:00

Move a few methods out of AbstractProtocol

Dieser Commit ist enthalten in:
Nassim Jahnke 2024-02-19 22:37:51 +01:00
Ursprung 3c0930c62f
Commit 56b82b049a
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: EF6771C01F6EF02F
9 geänderte Dateien mit 135 neuen und 88 gelöschten Zeilen

Datei anzeigen

@ -34,6 +34,7 @@ public interface EntityTracker {
* *
* @return user connection * @return user connection
*/ */
@Deprecated
UserConnection user(); UserConnection user();
/** /**

Datei anzeigen

@ -41,8 +41,7 @@ import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
import com.viaversion.viaversion.api.rewriter.Rewriter; import com.viaversion.viaversion.api.rewriter.Rewriter;
import com.viaversion.viaversion.exception.CancelException; import com.viaversion.viaversion.exception.CancelException;
import com.viaversion.viaversion.exception.InformativeException; import com.viaversion.viaversion.exception.InformativeException;
import java.util.Collections; import com.viaversion.viaversion.util.ProtocolUtil;
import java.util.EnumMap;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
@ -50,6 +49,8 @@ import java.util.function.Predicate;
import java.util.logging.Level; import java.util.logging.Level;
import org.checkerframework.checker.nullness.qual.Nullable; 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. * Abstract protocol class to handle packet transformation between two protocol versions.
* *
@ -171,11 +172,6 @@ public abstract class AbstractProtocol<CU extends ClientboundPacketType, CM exte
for (Map.Entry<State, PacketTypeMap<M>> entry : mappedPacketTypes.entrySet()) { for (Map.Entry<State, PacketTypeMap<M>> entry : mappedPacketTypes.entrySet()) {
PacketTypeMap<M> mappedTypes = entry.getValue(); PacketTypeMap<M> mappedTypes = entry.getValue();
PacketTypeMap<U> unmappedTypes = unmappedPacketTypes.get(entry.getKey()); PacketTypeMap<U> unmappedTypes = unmappedPacketTypes.get(entry.getKey());
registerPacketIdChanges(unmappedTypes, mappedTypes, registeredPredicate, registerConsumer);
}
}
protected <U extends PacketType, M extends PacketType> void registerPacketIdChanges(PacketTypeMap<U> unmappedTypes, PacketTypeMap<M> mappedTypes, Predicate<U> registeredPredicate, BiConsumer<U, M> registerConsumer) {
for (U unmappedType : unmappedTypes.types()) { for (U unmappedType : unmappedTypes.types()) {
M mappedType = mappedTypes.typeByName(unmappedType.getName()); M mappedType = mappedTypes.typeByName(unmappedType.getName());
if (mappedType == null) { if (mappedType == null) {
@ -187,6 +183,8 @@ public abstract class AbstractProtocol<CU extends ClientboundPacketType, CM exte
// Register if no custom handler exists and ids are different // Register if no custom handler exists and ids are different
if (unmappedType.getId() != mappedType.getId() && !registeredPredicate.test(unmappedType)) { if (unmappedType.getId() != mappedType.getId() && !registeredPredicate.test(unmappedType)) {
registerConsumer.accept(unmappedType, mappedType); registerConsumer.accept(unmappedType, mappedType);
}
} }
} }
} }
@ -248,50 +246,20 @@ public abstract class AbstractProtocol<CU extends ClientboundPacketType, CM exte
return PacketMappings.arrayMappings(); return PacketMappings.arrayMappings();
} }
/**
* 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 <P> packet type class
* @return map of packet types by state
*/
@SafeVarargs
protected final <P extends PacketType> Map<State, PacketTypeMap<P>> packetTypeMap(@Nullable Class<P> parent, Class<? extends P>... packetTypeClasses) {
if (parent == null) {
return Collections.emptyMap();
}
final Map<State, PacketTypeMap<P>> map = new EnumMap<>(State.class);
for (final Class<? extends P> 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() { protected @Nullable SU configurationAcknowledgedPacket() {
final PacketTypeMap<SU> packetTypeMap = packetTypesProvider.unmappedServerboundPacketTypes().get(State.PLAY); return packetTypesProvider.unmappedServerboundType(State.PLAY, "CONFIGURATION_ACKNOWLEDGED");
return packetTypeMap != null ? packetTypeMap.typeByName("CONFIGURATION_ACKNOWLEDGED") : null;
} }
protected @Nullable CU startConfigurationPacket() { protected @Nullable CU startConfigurationPacket() {
final PacketTypeMap<CU> packetTypeMap = packetTypesProvider.unmappedClientboundPacketTypes().get(State.PLAY); return packetTypesProvider.unmappedClientboundType(State.PLAY, "START_CONFIGURATION");
return packetTypeMap != null ? packetTypeMap.typeByName("START_CONFIGURATION") : null;
} }
protected @Nullable SU serverboundFinishConfigurationPacket() { protected @Nullable SU serverboundFinishConfigurationPacket() {
final PacketTypeMap<SU> packetTypeMap = packetTypesProvider.unmappedServerboundPacketTypes().get(State.CONFIGURATION); return packetTypesProvider.unmappedServerboundType(State.CONFIGURATION, "FINISH_CONFIGURATION");
return packetTypeMap != null ? packetTypeMap.typeByName("FINISH_CONFIGURATION") : null;
} }
protected @Nullable CU clientboundFinishConfigurationPacket() { protected @Nullable CU clientboundFinishConfigurationPacket() {
final PacketTypeMap<CU> packetTypeMap = packetTypesProvider.unmappedClientboundPacketTypes().get(State.CONFIGURATION); return packetTypesProvider.unmappedClientboundType(State.CONFIGURATION, "FINISH_CONFIGURATION");
return packetTypeMap != null ? packetTypeMap.typeByName("FINISH_CONFIGURATION") : null;
} }
// --------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------
@ -426,19 +394,17 @@ public abstract class AbstractProtocol<CU extends ClientboundPacketType, CM exte
try { try {
handler.handle(packetWrapper); handler.handle(packetWrapper);
} catch (CancelException e) { } catch (CancelException e) {
// Pass through CancelExceptions throw e; // Pass through CancelExceptions
throw e;
} catch (InformativeException e) { } catch (InformativeException e) {
// Catch InformativeExceptions
e.addSource(handler.getClass()); e.addSource(handler.getClass());
throwRemapError(direction, state, unmappedId, packetWrapper.getId(), e); printRemapError(direction, state, unmappedId, packetWrapper.getId(), e);
return; throw e;
} catch (Exception e) { } catch (Exception e) {
// Wrap other exceptions during packet handling // Wrap other exceptions during packet handling
InformativeException ex = new InformativeException(e); InformativeException ex = new InformativeException(e);
ex.addSource(handler.getClass()); ex.addSource(handler.getClass());
throwRemapError(direction, state, unmappedId, packetWrapper.getId(), ex); printRemapError(direction, state, unmappedId, packetWrapper.getId(), ex);
return; throw ex;
} }
if (packetWrapper.isCancelled()) { if (packetWrapper.isCancelled()) {
@ -447,36 +413,22 @@ public abstract class AbstractProtocol<CU extends ClientboundPacketType, CM exte
} }
} }
protected void throwRemapError(Direction direction, State state, int unmappedPacketId, int mappedPacketId, InformativeException e) throws InformativeException { private void printRemapError(Direction direction, State state, int unmappedPacketId, int mappedPacketId, InformativeException e) {
// Don't print errors during handshake/login/status // Don't print errors during handshake/login/status
if (state != State.PLAY && direction == Direction.SERVERBOUND && !Via.getManager().debugHandler().enabled()) { if (state != State.PLAY && direction == Direction.SERVERBOUND && !Via.getManager().debugHandler().enabled()) {
e.setShouldBePrinted(false); e.setShouldBePrinted(false);
throw e; return;
} }
PacketType packetType = direction == Direction.CLIENTBOUND ? unmappedClientboundPacketType(state, unmappedPacketId) : unmappedServerboundPacketType(state, unmappedPacketId); PacketType packetType = direction == Direction.CLIENTBOUND
? packetTypesProvider.unmappedClientboundType(state, unmappedPacketId)
: packetTypesProvider.unmappedServerboundType(state, unmappedPacketId);
if (packetType != null) { if (packetType != null) {
Via.getPlatform().getLogger().warning("ERROR IN " + getClass().getSimpleName() + " IN REMAP OF " + packetType + " (" + toNiceHex(unmappedPacketId) + ")"); Via.getPlatform().getLogger().warning("ERROR IN " + getClass().getSimpleName() + " IN REMAP OF " + packetType + " (" + ProtocolUtil.toNiceHex(unmappedPacketId) + ")");
} else { } else {
Via.getPlatform().getLogger().warning("ERROR IN " + getClass().getSimpleName() Via.getPlatform().getLogger().warning("ERROR IN " + getClass().getSimpleName()
+ " IN REMAP OF " + state + " " + toNiceHex(unmappedPacketId) + "->" + 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<CU> map = packetTypesProvider.unmappedClientboundPacketTypes().get(state);
return map != null ? map.typeById(packetId) : null;
}
private @Nullable SU unmappedServerboundPacketType(final State state, final int packetId) {
PacketTypeMap<SU> 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<CU extends ClientboundPacketType, CM exte
} }
} }
protected PacketHandler setClientStateHandler(final State state) { private PacketHandler setClientStateHandler(final State state) {
return wrapper -> wrapper.user().getProtocolInfo().setClientState(state); return wrapper -> wrapper.user().getProtocolInfo().setClientState(state);
} }
protected PacketHandler setServerStateHandler(final State state) { private PacketHandler setServerStateHandler(final State state) {
return wrapper -> wrapper.user().getProtocolInfo().setServerState(state); return wrapper -> wrapper.user().getProtocolInfo().setServerState(state);
} }

Datei anzeigen

@ -22,7 +22,6 @@
*/ */
package com.viaversion.viaversion.api.protocol; package com.viaversion.viaversion.api.protocol;
import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.data.MappingData; import com.viaversion.viaversion.api.data.MappingData;
@ -257,7 +256,6 @@ public interface Protocol<CU extends ClientboundPacketType, CM extends Clientbou
* *
* @return the packet types provider * @return the packet types provider
*/ */
@Beta
PacketTypesProvider<CU, CM, SM, SU> getPacketTypesProvider(); PacketTypesProvider<CU, CM, SM, SU> getPacketTypesProvider();
/** /**
@ -267,6 +265,7 @@ public interface Protocol<CU extends ClientboundPacketType, CM extends Clientbou
* @param <T> type * @param <T> type
* @return object if present, else null * @return object if present, else null
*/ */
@Deprecated
@Nullable <T> T get(Class<T> objectClass); @Nullable <T> T get(Class<T> objectClass);
/** /**
@ -274,6 +273,7 @@ public interface Protocol<CU extends ClientboundPacketType, CM extends Clientbou
* *
* @param object object to cache * @param object object to cache
*/ */
@Deprecated
void put(Object object); void put(Object object);
/** /**

Datei anzeigen

@ -22,11 +22,11 @@
*/ */
package com.viaversion.viaversion.api.protocol.packet.provider; 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.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.State; import com.viaversion.viaversion.api.protocol.packet.State;
import java.util.Map; import java.util.Map;
import org.checkerframework.checker.nullness.qual.Nullable;
/** /**
* Provides unmapped and mapped packet types for a Protocol. * Provides unmapped and mapped packet types for a Protocol.
@ -37,7 +37,6 @@ import java.util.Map;
* @param <SU> unmapped serverbound packet type * @param <SU> unmapped serverbound packet type
* @see SimplePacketTypesProvider * @see SimplePacketTypesProvider
*/ */
@Beta
public interface PacketTypesProvider<CU extends ClientboundPacketType, CM extends ClientboundPacketType, SM extends ServerboundPacketType, SU extends ServerboundPacketType> { public interface PacketTypesProvider<CU extends ClientboundPacketType, CM extends ClientboundPacketType, SM extends ServerboundPacketType, SU extends ServerboundPacketType> {
/** /**
@ -69,4 +68,24 @@ public interface PacketTypesProvider<CU extends ClientboundPacketType, CM extend
* @return map of mapped serverbound packet types * @return map of mapped serverbound packet types
*/ */
Map<State, PacketTypeMap<SM>> mappedServerboundPacketTypes(); Map<State, PacketTypeMap<SM>> mappedServerboundPacketTypes();
default @Nullable CU unmappedClientboundType(final State state, final String typeName) {
PacketTypeMap<CU> map = unmappedClientboundPacketTypes().get(state);
return map != null ? map.typeByName(typeName) : null;
}
default @Nullable SU unmappedServerboundType(final State state, final String typeName) {
PacketTypeMap<SU> map = unmappedServerboundPacketTypes().get(state);
return map != null ? map.typeByName(typeName) : null;
}
default @Nullable CU unmappedClientboundType(final State state, final int packetId) {
PacketTypeMap<CU> map = unmappedClientboundPacketTypes().get(state);
return map != null ? map.typeById(packetId) : null;
}
default @Nullable SU unmappedServerboundType(final State state, final int packetId) {
PacketTypeMap<SU> map = unmappedServerboundPacketTypes().get(state);
return map != null ? map.typeById(packetId) : null;
}
} }

Datei anzeigen

@ -22,13 +22,11 @@
*/ */
package com.viaversion.viaversion.api.protocol.packet.provider; 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.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.State; import com.viaversion.viaversion.api.protocol.packet.State;
import java.util.Map; import java.util.Map;
@Beta
public final class SimplePacketTypesProvider<CU extends ClientboundPacketType, CM extends ClientboundPacketType, SM extends ServerboundPacketType, SU extends ServerboundPacketType> implements PacketTypesProvider<CU, CM, SM, SU> { public final class SimplePacketTypesProvider<CU extends ClientboundPacketType, CM extends ClientboundPacketType, SM extends ServerboundPacketType, SU extends ServerboundPacketType> implements PacketTypesProvider<CU, CM, SM, SU> {
private final Map<State, PacketTypeMap<CU>> unmappedClientboundPacketTypes; private final Map<State, PacketTypeMap<CU>> unmappedClientboundPacketTypes;
private final Map<State, PacketTypeMap<CM>> mappedClientboundPacketTypes; private final Map<State, PacketTypeMap<CM>> mappedClientboundPacketTypes;

Datei anzeigen

@ -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 <P> packet type class
* @return map of packet types by state
*/
@SafeVarargs
public static <P extends PacketType> Map<State, PacketTypeMap<P>> packetTypeMap(@Nullable Class<P> parent, Class<? extends P>... packetTypeClasses) {
if (parent == null) {
return Collections.emptyMap();
}
final Map<State, PacketTypeMap<P>> map = new EnumMap<>(State.class);
for (final Class<? extends P> 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;
}
}

Datei anzeigen

@ -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.Direction;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.packet.State; import com.viaversion.viaversion.api.protocol.packet.State;
import com.viaversion.viaversion.util.ProtocolUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
@ -138,9 +139,9 @@ public class ProtocolPipelineImpl extends AbstractSimpleProtocol implements Prot
direction, direction,
state, state,
originalID, originalID,
AbstractSimpleProtocol.toNiceHex(originalID), ProtocolUtil.toNiceHex(originalID),
packetWrapper.getId(), packetWrapper.getId(),
AbstractSimpleProtocol.toNiceHex(packetWrapper.getId()), ProtocolUtil.toNiceHex(packetWrapper.getId()),
protocolInfo.protocolVersion().getName(), protocolInfo.protocolVersion().getName(),
packetWrapper packetWrapper
}); });

Datei anzeigen

@ -54,6 +54,8 @@ import java.nio.charset.StandardCharsets;
import java.util.BitSet; import java.util.BitSet;
import java.util.UUID; import java.util.UUID;
import static com.viaversion.viaversion.util.ProtocolUtil.packetTypeMap;
public final class Protocol1_20_3To1_20_2 extends AbstractProtocol<ClientboundPacket1_20_2, ClientboundPacket1_20_3, ServerboundPacket1_20_2, ServerboundPacket1_20_3> { public final class Protocol1_20_3To1_20_2 extends AbstractProtocol<ClientboundPacket1_20_2, ClientboundPacket1_20_3, ServerboundPacket1_20_2, ServerboundPacket1_20_3> {
public static final MappingData MAPPINGS = new MappingDataBase("1.20.2", "1.20.3"); public static final MappingData MAPPINGS = new MappingDataBase("1.20.2", "1.20.3");

Datei anzeigen

@ -50,6 +50,8 @@ import com.viaversion.viaversion.rewriter.SoundRewriter;
import com.viaversion.viaversion.rewriter.StatisticsRewriter; import com.viaversion.viaversion.rewriter.StatisticsRewriter;
import com.viaversion.viaversion.rewriter.TagRewriter; import com.viaversion.viaversion.rewriter.TagRewriter;
import static com.viaversion.viaversion.util.ProtocolUtil.packetTypeMap;
public final class Protocol1_20_5To1_20_3 extends AbstractProtocol<ClientboundPacket1_20_3, ClientboundPacket1_20_5, ServerboundPacket1_20_3, ServerboundPacket1_20_5> { public final class Protocol1_20_5To1_20_3 extends AbstractProtocol<ClientboundPacket1_20_3, ClientboundPacket1_20_5, ServerboundPacket1_20_3, ServerboundPacket1_20_5> {
public static final MappingData MAPPINGS = new MappingDataBase("1.20.3", "1.20.5"); public static final MappingData MAPPINGS = new MappingDataBase("1.20.3", "1.20.5");