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

Further safety checks for correct packets types

Dieser Commit ist enthalten in:
KennyTV 2020-06-06 15:54:51 +02:00
Ursprung cc3dae127b
Commit f117239184
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 6BE3B555EBC5982B
4 geänderte Dateien mit 42 neuen und 23 gelöschten Zeilen

Datei anzeigen

@ -4,15 +4,5 @@ package us.myles.ViaVersion.api.protocol;
* Interface to be implemented by server outgoing packet type enums,
* representing PLAY state packets, ordered by their packet id.
*/
public interface ClientboundPacketType {
/**
* @return name of the packet, to be consistent over multiple versions
*/
String name();
/**
* @return ordinal, being the packet id for the implemented protocol
*/
int ordinal();
public interface ClientboundPacketType extends PacketType {
}

Datei anzeigen

@ -0,0 +1,20 @@
package us.myles.ViaVersion.api.protocol;
/**
* Interface representing PLAY state packets, ordered by their packet id.
*
* @see ClientboundPacketType
* @see ServerboundPacketType
*/
public interface PacketType {
/**
* @return name of the packet, to be consistent over multiple versions
*/
String name();
/**
* @return ordinal, being the packet id for the implemented protocol
*/
int ordinal();
}

Datei anzeigen

@ -284,6 +284,8 @@ public abstract class Protocol<C1 extends ClientboundPacketType, C2 extends Clie
* @param packetRemapper remapper
*/
public void registerOutgoing(C1 packetType, @Nullable PacketRemapper packetRemapper) {
checkPacketType(packetType, packetType.getClass() == oldClientboundPacketEnum);
ClientboundPacketType mappedPacket = oldClientboundPacketEnum == newClientboundPacketEnum ? packetType
: Arrays.stream(newClientboundPacketEnum.getEnumConstants()).filter(en -> en.name().equals(packetType.name())).findAny().orElse(null);
Preconditions.checkNotNull(mappedPacket, "Packet type " + packetType + " in " + packetType.getClass().getSimpleName() + " could not be automatically mapped!");
@ -301,6 +303,9 @@ public abstract class Protocol<C1 extends ClientboundPacketType, C2 extends Clie
* @param packetRemapper remapper
*/
public void registerOutgoing(C1 packetType, @Nullable C2 mappedPacketType, @Nullable PacketRemapper packetRemapper) {
checkPacketType(packetType, packetType.getClass() == oldClientboundPacketEnum);
checkPacketType(mappedPacketType, mappedPacketType == null || mappedPacketType.getClass() == newClientboundPacketEnum);
registerOutgoing(State.PLAY, packetType.ordinal(), mappedPacketType != null ? mappedPacketType.ordinal() : -1, packetRemapper);
}
@ -326,7 +331,7 @@ public abstract class Protocol<C1 extends ClientboundPacketType, C2 extends Clie
* @param packetRemapper remapper
*/
public void registerIncoming(S2 packetType, @Nullable PacketRemapper packetRemapper) {
Preconditions.checkArgument(packetType.getClass() == newServerboundPacketEnum);
checkPacketType(packetType, packetType.getClass() == newServerboundPacketEnum);
ServerboundPacketType mappedPacket = oldServerboundPacketEnum == newServerboundPacketEnum ? packetType
: Arrays.stream(oldServerboundPacketEnum.getEnumConstants()).filter(en -> en.name().equals(packetType.name())).findAny().orElse(null);
@ -345,6 +350,9 @@ public abstract class Protocol<C1 extends ClientboundPacketType, C2 extends Clie
* @param packetRemapper remapper
*/
public void registerIncoming(S2 packetType, @Nullable S1 mappedPacketType, @Nullable PacketRemapper packetRemapper) {
checkPacketType(packetType, packetType.getClass() == newServerboundPacketEnum);
checkPacketType(mappedPacketType, mappedPacketType == null || mappedPacketType.getClass() == oldServerboundPacketEnum);
registerIncoming(State.PLAY, mappedPacketType != null ? mappedPacketType.ordinal() : -1, packetType.ordinal(), packetRemapper);
}
@ -410,6 +418,17 @@ public abstract class Protocol<C1 extends ClientboundPacketType, C2 extends Clie
}
}
/**
* @param packetType packet type
* @param isValid expression to check the packet's validity
* @throws IllegalArgumentException if the given expression is not met
*/
private void checkPacketType(PacketType packetType, boolean isValid) {
if (!isValid) {
throw new IllegalArgumentException("Packet type " + packetType + " in " + packetType.getClass().getSimpleName() + " is taken from the wrong enum");
}
}
public @Nullable <T> T get(Class<T> objectClass) {
return (T) storedObjects.get(objectClass);
}

Datei anzeigen

@ -4,15 +4,5 @@ package us.myles.ViaVersion.api.protocol;
* Interface to be implemented by server incoming packet type enums,
* representing PLAY state packets, ordered by their packet id.
*/
public interface ServerboundPacketType {
/**
* @return name of the packet, to be consistent over multiple versions
*/
String name();
/**
* @return ordinal, being the packet id for the implemented protocol
*/
int ordinal();
public interface ServerboundPacketType extends PacketType {
}