Rename Protocol generics to be clearer

This commit is contained in:
Nassim Jahnke 2023-02-12 21:14:10 +01:00
parent f5ddc6550d
commit 45fcc9ef7d
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
4 changed files with 62 additions and 58 deletions

View File

@ -52,18 +52,18 @@ import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Abstract protocol class to handle packet transformation between two protocol versions.
*
* @param <C1> unmapped clientbound packet type
* @param <C2> mapped clientbound packet type
* @param <S1> mapped serverbound packet type
* @param <S2> unmapped serverbound packet type
* @param <CU> unmapped clientbound packet type
* @param <CM> mapped clientbound packet type
* @param <SM> mapped serverbound packet type
* @param <SU> unmapped serverbound packet type
*/
public abstract class AbstractProtocol<C1 extends ClientboundPacketType, C2 extends ClientboundPacketType,
S1 extends ServerboundPacketType, S2 extends ServerboundPacketType> implements Protocol<C1, C2, S1, S2> {
protected final Class<C1> unmappedClientboundPacketType;
protected final Class<C2> mappedClientboundPacketType;
protected final Class<S1> mappedServerboundPacketType;
protected final Class<S2> unmappedServerboundPacketType;
private final PacketTypesProvider<C1, C2, S1, S2> packetTypesProvider;
public abstract class AbstractProtocol<CU extends ClientboundPacketType, CM extends ClientboundPacketType,
SM extends ServerboundPacketType, SU extends ServerboundPacketType> implements Protocol<CU, CM, SM, SU> {
protected final Class<CU> unmappedClientboundPacketType;
protected final Class<CM> mappedClientboundPacketType;
protected final Class<SM> mappedServerboundPacketType;
protected final Class<SU> unmappedServerboundPacketType;
private final PacketTypesProvider<CU, CM, SM, SU> packetTypesProvider;
private final PacketMappings serverboundMappings = PacketMappings.arrayMappings();
private final PacketMappings clientboundMappings = PacketMappings.arrayMappings();
private final Map<Class<?>, Object> storedObjects = new HashMap<>();
@ -76,8 +76,8 @@ public abstract class AbstractProtocol<C1 extends ClientboundPacketType, C2 exte
/**
* Creates a protocol with automated id mapping if the respective packet type classes are not null.
*/
protected AbstractProtocol(@Nullable Class<C1> unmappedClientboundPacketType, @Nullable Class<C2> mappedClientboundPacketType,
@Nullable Class<S1> mappedServerboundPacketType, @Nullable Class<S2> unmappedServerboundPacketType) {
protected AbstractProtocol(@Nullable Class<CU> unmappedClientboundPacketType, @Nullable Class<CM> mappedClientboundPacketType,
@Nullable Class<SM> mappedServerboundPacketType, @Nullable Class<SU> unmappedServerboundPacketType) {
this.unmappedClientboundPacketType = unmappedClientboundPacketType;
this.mappedClientboundPacketType = mappedClientboundPacketType;
this.mappedServerboundPacketType = mappedServerboundPacketType;
@ -162,7 +162,7 @@ public abstract class AbstractProtocol<C1 extends ClientboundPacketType, C2 exte
connection.addEntityTracker(this.getClass(), tracker);
}
protected PacketTypesProvider<C1, C2, S1, S2> createPacketTypesProvider() {
protected PacketTypesProvider<CU, CM, SM, SU> createPacketTypesProvider() {
return new SimplePacketTypesProvider<>(
packetTypeMap(unmappedClientboundPacketType),
packetTypeMap(mappedClientboundPacketType),
@ -217,36 +217,36 @@ public abstract class AbstractProtocol<C1 extends ClientboundPacketType, C2 exte
// ---------------------------------------------------------------------------------
@Override
public void registerClientbound(C1 packetType, @Nullable PacketHandler handler) {
PacketTypeMap<C2> mappedPacketTypes = packetTypesProvider.mappedClientboundPacketTypes().get(packetType.state());
C2 mappedPacketType = mappedPacketType(packetType, mappedPacketTypes, unmappedClientboundPacketType, mappedClientboundPacketType);
public void registerClientbound(CU packetType, @Nullable PacketHandler handler) {
PacketTypeMap<CM> mappedPacketTypes = packetTypesProvider.mappedClientboundPacketTypes().get(packetType.state());
CM mappedPacketType = mappedPacketType(packetType, mappedPacketTypes, unmappedClientboundPacketType, mappedClientboundPacketType);
registerClientbound(packetType, mappedPacketType, handler);
}
@Override
public void registerClientbound(C1 packetType, @Nullable C2 mappedPacketType, @Nullable PacketHandler handler, boolean override) {
public void registerClientbound(CU packetType, @Nullable CM mappedPacketType, @Nullable PacketHandler handler, boolean override) {
register(clientboundMappings, packetType, mappedPacketType, unmappedClientboundPacketType, mappedClientboundPacketType, handler, override);
}
@Override
public void cancelClientbound(C1 packetType) {
public void cancelClientbound(CU packetType) {
registerClientbound(packetType, null, PacketWrapper::cancel);
}
@Override
public void registerServerbound(S2 packetType, @Nullable PacketHandler handler) {
PacketTypeMap<S1> mappedPacketTypes = packetTypesProvider.mappedServerboundPacketTypes().get(packetType.state());
S1 mappedPacketType = mappedPacketType(packetType, mappedPacketTypes, unmappedServerboundPacketType, mappedServerboundPacketType);
public void registerServerbound(SU packetType, @Nullable PacketHandler handler) {
PacketTypeMap<SM> mappedPacketTypes = packetTypesProvider.mappedServerboundPacketTypes().get(packetType.state());
SM mappedPacketType = mappedPacketType(packetType, mappedPacketTypes, unmappedServerboundPacketType, mappedServerboundPacketType);
registerServerbound(packetType, mappedPacketType, handler);
}
@Override
public void registerServerbound(S2 packetType, @Nullable S1 mappedPacketType, @Nullable PacketHandler handler, boolean override) {
public void registerServerbound(SU packetType, @Nullable SM mappedPacketType, @Nullable PacketHandler handler, boolean override) {
register(serverboundMappings, packetType, mappedPacketType, unmappedServerboundPacketType, mappedServerboundPacketType, handler, override);
}
@Override
public void cancelServerbound(S2 packetType) {
public void cancelServerbound(SU packetType) {
registerServerbound(packetType, null, PacketWrapper::cancel);
}
@ -350,13 +350,13 @@ public abstract class AbstractProtocol<C1 extends ClientboundPacketType, C2 exte
throw e;
}
private @Nullable C1 unmappedClientboundPacketType(final State state, final int packetId) {
PacketTypeMap<C1> map = packetTypesProvider.unmappedClientboundPacketTypes().get(state);
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 S2 unmappedServerboundPacketType(final State state, final int packetId) {
PacketTypeMap<S2> map = packetTypesProvider.unmappedServerboundPacketTypes().get(state);
private @Nullable SU unmappedServerboundPacketType(final State state, final int packetId) {
PacketTypeMap<SU> map = packetTypesProvider.unmappedServerboundPacketTypes().get(state);
return map != null ? map.typeById(packetId) : null;
}
@ -387,7 +387,7 @@ public abstract class AbstractProtocol<C1 extends ClientboundPacketType, C2 exte
storedObjects.put(object.getClass(), object);
}
public PacketTypesProvider<C1, C2, S1, S2> packetTypesProvider() {
public PacketTypesProvider<CU, CM, SM, SU> packetTypesProvider() {
return packetTypesProvider;
}

View File

@ -40,13 +40,13 @@ import org.checkerframework.checker.nullness.qual.Nullable;
* Abstract protocol class handling packet transformation between two protocol versions.
* Clientbound and serverbount packet types can be set to enforce correct usage of them.
*
* @param <C1> unmapped ("old") clientbound packet types
* @param <C2> mapped ("new") clientbound packet types
* @param <S1> mapped ("old") serverbound packet types
* @param <S2> unmapped ("new") serverbound packet types
* @see SimpleProtocol for a helper class if you do not want to define any of the types above
* @param <CU> unmapped clientbound packet types
* @param <CM> mapped clientbound packet types
* @param <SM> mapped serverbound packet types
* @param <SU> unmapped serverbound packet types
* @see SimpleProtocol for a helper class if you do not need to define any of the types above
*/
public interface Protocol<C1 extends ClientboundPacketType, C2 extends ClientboundPacketType, S1 extends ServerboundPacketType, S2 extends ServerboundPacketType> {
public interface Protocol<CU extends ClientboundPacketType, CM extends ClientboundPacketType, SM extends ServerboundPacketType, SU extends ServerboundPacketType> {
default void registerServerbound(State state, int unmappedPacketId, int mappedPacketId) {
registerServerbound(state, unmappedPacketId, mappedPacketId, (PacketHandler) null);
@ -100,7 +100,7 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
* @param packetType clientbound packet type the server sends
* @param handler packet handler
*/
void registerClientbound(C1 packetType, @Nullable PacketHandler handler);
void registerClientbound(CU packetType, @Nullable PacketHandler handler);
/**
* Maps a packet type to another packet type without a packet handler.
@ -109,7 +109,7 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
* @param packetType clientbound packet type the server initially sends
* @param mappedPacketType clientbound packet type after transforming for the client
*/
default void registerClientbound(C1 packetType, @Nullable C2 mappedPacketType) {
default void registerClientbound(CU packetType, @Nullable CM mappedPacketType) {
registerClientbound(packetType, mappedPacketType, (PacketHandler) null);
}
@ -120,7 +120,7 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
* @param mappedPacketType clientbound packet type after transforming for the client
* @param handler packet handler
*/
default void registerClientbound(C1 packetType, @Nullable C2 mappedPacketType, @Nullable PacketHandler handler) {
default void registerClientbound(CU packetType, @Nullable CM mappedPacketType, @Nullable PacketHandler handler) {
registerClientbound(packetType, mappedPacketType, handler, false);
}
@ -132,14 +132,14 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
* @param handler packet handler
* @param override whether an existing mapping should be overridden if present
*/
void registerClientbound(C1 packetType, @Nullable C2 mappedPacketType, @Nullable PacketHandler handler, boolean override);
void registerClientbound(CU packetType, @Nullable CM mappedPacketType, @Nullable PacketHandler handler, boolean override);
/**
* Cancels any clientbound packets from the given type.
*
* @param packetType clientbound packet type to cancel
*/
void cancelClientbound(C1 packetType);
void cancelClientbound(CU packetType);
/**
* Maps a packet type to another packet type without a packet handler.
@ -148,7 +148,7 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
* @param packetType serverbound packet type the client initially sends
* @param mappedPacketType serverbound packet type after transforming for the client
*/
default void registerServerbound(S2 packetType, @Nullable S1 mappedPacketType) {
default void registerServerbound(SU packetType, @Nullable SM mappedPacketType) {
registerServerbound(packetType, mappedPacketType, (PacketHandler) null);
}
@ -158,7 +158,7 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
* @param packetType serverbound packet type the client sends
* @param handler packet handler
*/
void registerServerbound(S2 packetType, @Nullable PacketHandler handler);
void registerServerbound(SU packetType, @Nullable PacketHandler handler);
/**
* Registers a serverbound protocol.
@ -167,7 +167,7 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
* @param mappedPacketType serverbound packet type after transforming for the server
* @param handler packet handler
*/
default void registerServerbound(S2 packetType, @Nullable S1 mappedPacketType, @Nullable PacketHandler handler) {
default void registerServerbound(SU packetType, @Nullable SM mappedPacketType, @Nullable PacketHandler handler) {
registerServerbound(packetType, mappedPacketType, handler, false);
}
@ -179,15 +179,14 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
* @param handler packet handler
* @param override whether an existing mapping should be overridden if present
*/
void registerServerbound(S2 packetType, @Nullable S1 mappedPacketType, @Nullable PacketHandler handler, boolean override);
void registerServerbound(SU packetType, @Nullable SM mappedPacketType, @Nullable PacketHandler handler, boolean override);
/**
* Cancels any serverbound packets from the given type.
*
* @param packetType serverbound packet type to cancel
*/
void cancelServerbound(S2 packetType);
void cancelServerbound(SU packetType);
/**
* Checks if a clientbound packet has already been registered.
@ -195,7 +194,7 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
* @param packetType clientbound packet type
* @return true if already registered
*/
default boolean hasRegisteredClientbound(C1 packetType) {
default boolean hasRegisteredClientbound(CU packetType) {
return hasRegisteredClientbound(packetType.state(), packetType.getId());
}
@ -205,7 +204,7 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
* @param packetType serverbound packet type
* @return true if already registered
*/
default boolean hasRegisteredServerbound(S2 packetType) {
default boolean hasRegisteredServerbound(SU packetType) {
return hasRegisteredServerbound(packetType.state(), packetType.getId());
}
@ -365,17 +364,17 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
}
@Deprecated/*(forRemoval = true)*/
default void registerClientbound(C1 packetType, @Nullable PacketRemapper packetRemapper) {
default void registerClientbound(CU packetType, @Nullable PacketRemapper packetRemapper) {
registerClientbound(packetType, packetRemapper.asPacketHandler());
}
@Deprecated/*(forRemoval = true)*/
default void registerClientbound(C1 packetType, @Nullable C2 mappedPacketType, @Nullable PacketRemapper packetRemapper) {
default void registerClientbound(CU packetType, @Nullable CM mappedPacketType, @Nullable PacketRemapper packetRemapper) {
registerClientbound(packetType, mappedPacketType, packetRemapper.asPacketHandler(), false);
}
@Deprecated/*(forRemoval = true)*/
default void registerClientbound(C1 packetType, @Nullable C2 mappedPacketType, @Nullable PacketRemapper packetRemapper, boolean override) {
default void registerClientbound(CU packetType, @Nullable CM mappedPacketType, @Nullable PacketRemapper packetRemapper, boolean override) {
registerClientbound(packetType, mappedPacketType, packetRemapper.asPacketHandler(), override);
}
@ -390,17 +389,17 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
}
@Deprecated/*(forRemoval = true)*/
default void registerServerbound(S2 packetType, @Nullable PacketRemapper packetRemapper) {
default void registerServerbound(SU packetType, @Nullable PacketRemapper packetRemapper) {
registerServerbound(packetType, packetRemapper.asPacketHandler());
}
@Deprecated/*(forRemoval = true)*/
default void registerServerbound(S2 packetType, @Nullable S1 mappedPacketType, @Nullable PacketRemapper packetRemapper) {
default void registerServerbound(SU packetType, @Nullable SM mappedPacketType, @Nullable PacketRemapper packetRemapper) {
registerServerbound(packetType, mappedPacketType, packetRemapper.asPacketHandler(), false);
}
@Deprecated/*(forRemoval = true)*/
default void registerServerbound(S2 packetType, @Nullable S1 mappedPacketType, @Nullable PacketRemapper packetRemapper, boolean override) {
default void registerServerbound(SU packetType, @Nullable SM mappedPacketType, @Nullable PacketRemapper packetRemapper, boolean override) {
registerServerbound(packetType, mappedPacketType, packetRemapper.asPacketHandler(), override);
}
}

View File

@ -48,7 +48,3 @@ final class PacketTypeMapping implements PacketMapping {
return handler;
}
}

View File

@ -27,6 +27,15 @@ import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.State;
import java.util.Map;
/**
* Provides unmapped and mapped packet types for a Protocol.
*
* @param <CU> unmapped clientbound packet type
* @param <CM> mapped clientbound packet type
* @param <SM> mapped serverbound packet type
* @param <SU> unmapped serverbound packet type
* @see SimplePacketTypesProvider
*/
public interface PacketTypesProvider<CU extends ClientboundPacketType, CM extends ClientboundPacketType, SM extends ServerboundPacketType, SU extends ServerboundPacketType> {
/**