Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-12-27 00:22:51 +01:00
Move a few methods out of AbstractProtocol
Dieser Commit ist enthalten in:
Ursprung
3c0930c62f
Commit
56b82b049a
@ -34,6 +34,7 @@ public interface EntityTracker {
|
||||
*
|
||||
* @return user connection
|
||||
*/
|
||||
@Deprecated
|
||||
UserConnection user();
|
||||
|
||||
/**
|
||||
|
@ -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<CU extends ClientboundPacketType, CM exte
|
||||
for (Map.Entry<State, PacketTypeMap<M>> entry : mappedPacketTypes.entrySet()) {
|
||||
PacketTypeMap<M> mappedTypes = entry.getValue();
|
||||
PacketTypeMap<U> 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 <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()) {
|
||||
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<CU extends ClientboundPacketType, CM exte
|
||||
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() {
|
||||
final PacketTypeMap<SU> 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<CU> 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<SU> 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<CU> 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<CU extends ClientboundPacketType, CM exte
|
||||
try {
|
||||
handler.handle(packetWrapper);
|
||||
} catch (CancelException e) {
|
||||
// Pass through CancelExceptions
|
||||
throw e;
|
||||
throw e; // Pass through CancelExceptions
|
||||
} catch (InformativeException e) {
|
||||
// Catch InformativeExceptions
|
||||
e.addSource(handler.getClass());
|
||||
throwRemapError(direction, state, unmappedId, packetWrapper.getId(), e);
|
||||
return;
|
||||
printRemapError(direction, state, unmappedId, packetWrapper.getId(), e);
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
// Wrap other exceptions during packet handling
|
||||
InformativeException ex = new InformativeException(e);
|
||||
ex.addSource(handler.getClass());
|
||||
throwRemapError(direction, state, unmappedId, packetWrapper.getId(), ex);
|
||||
return;
|
||||
printRemapError(direction, state, unmappedId, packetWrapper.getId(), ex);
|
||||
throw ex;
|
||||
}
|
||||
|
||||
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
|
||||
if (state != State.PLAY && direction == Direction.SERVERBOUND && !Via.getManager().debugHandler().enabled()) {
|
||||
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) {
|
||||
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 {
|
||||
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);
|
||||
}
|
||||
|
||||
protected PacketHandler setServerStateHandler(final State state) {
|
||||
private PacketHandler setServerStateHandler(final State state) {
|
||||
return wrapper -> wrapper.user().getProtocolInfo().setServerState(state);
|
||||
}
|
||||
|
||||
|
@ -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<CU extends ClientboundPacketType, CM extends Clientbou
|
||||
*
|
||||
* @return the packet types provider
|
||||
*/
|
||||
@Beta
|
||||
PacketTypesProvider<CU, CM, SM, SU> getPacketTypesProvider();
|
||||
|
||||
/**
|
||||
@ -267,6 +265,7 @@ public interface Protocol<CU extends ClientboundPacketType, CM extends Clientbou
|
||||
* @param <T> type
|
||||
* @return object if present, else null
|
||||
*/
|
||||
@Deprecated
|
||||
@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
|
||||
*/
|
||||
@Deprecated
|
||||
void put(Object object);
|
||||
|
||||
/**
|
||||
|
@ -22,11 +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;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
/**
|
||||
* Provides unmapped and mapped packet types for a Protocol.
|
||||
@ -37,7 +37,6 @@ import java.util.Map;
|
||||
* @param <SU> unmapped serverbound packet type
|
||||
* @see SimplePacketTypesProvider
|
||||
*/
|
||||
@Beta
|
||||
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
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -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<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<CM>> mappedClientboundPacketTypes;
|
||||
|
72
api/src/main/java/com/viaversion/viaversion/util/ProtocolUtil.java
Normale Datei
72
api/src/main/java/com/viaversion/viaversion/util/ProtocolUtil.java
Normale Datei
@ -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;
|
||||
}
|
||||
}
|
@ -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
|
||||
});
|
||||
|
@ -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<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");
|
||||
|
@ -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<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");
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren