Further safety checks for correct packets types

This commit is contained in:
KennyTV 2020-06-06 15:54:51 +02:00
parent cc3dae127b
commit f117239184
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
4 changed files with 42 additions and 23 deletions

View File

@ -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 {
}

View File

@ -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();
}

View File

@ -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);
}

View File

@ -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 {
}