3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-28 17:10:13 +01:00

Allow bypassing of packet type restriction in Protocol, amend packet creator

Dieser Commit ist enthalten in:
kennytv 2021-07-31 23:17:52 +02:00
Ursprung b81109f512
Commit 2094397bca
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 6BE3B555EBC5982B
6 geänderte Dateien mit 155 neuen und 50 gelöschten Zeilen

Datei anzeigen

@ -87,14 +87,14 @@ public abstract class AbstractProtocol<C1 extends ClientboundPacketType, C2 exte
} }
protected void registerClientboundChannelIdChanges() { protected void registerClientboundChannelIdChanges() {
ClientboundPacketType[] newConstants = newClientboundPacketEnum.getEnumConstants(); C2[] newConstants = newClientboundPacketEnum.getEnumConstants();
Map<String, ClientboundPacketType> newClientboundPackets = new HashMap<>(newConstants.length); Map<String, C2> newClientboundPackets = new HashMap<>(newConstants.length);
for (ClientboundPacketType newConstant : newConstants) { for (C2 newConstant : newConstants) {
newClientboundPackets.put(newConstant.getName(), newConstant); newClientboundPackets.put(newConstant.getName(), newConstant);
} }
for (C1 packet : oldClientboundPacketEnum.getEnumConstants()) { for (C1 packet : oldClientboundPacketEnum.getEnumConstants()) {
C2 mappedPacket = (C2) newClientboundPackets.get(packet.getName()); C2 mappedPacket = newClientboundPackets.get(packet.getName());
if (mappedPacket == null) { if (mappedPacket == null) {
// Packet doesn't exist on new client // Packet doesn't exist on new client
Preconditions.checkArgument(hasRegisteredClientbound(packet), Preconditions.checkArgument(hasRegisteredClientbound(packet),
@ -109,14 +109,14 @@ public abstract class AbstractProtocol<C1 extends ClientboundPacketType, C2 exte
} }
protected void registerServerboundChannelIdChanges() { protected void registerServerboundChannelIdChanges() {
ServerboundPacketType[] oldConstants = oldServerboundPacketEnum.getEnumConstants(); S1[] oldConstants = oldServerboundPacketEnum.getEnumConstants();
Map<String, ServerboundPacketType> oldServerboundConstants = new HashMap<>(oldConstants.length); Map<String, S1> oldServerboundConstants = new HashMap<>(oldConstants.length);
for (ServerboundPacketType oldConstant : oldConstants) { for (S1 oldConstant : oldConstants) {
oldServerboundConstants.put(oldConstant.getName(), oldConstant); oldServerboundConstants.put(oldConstant.getName(), oldConstant);
} }
for (S2 packet : newServerboundPacketEnum.getEnumConstants()) { for (S2 packet : newServerboundPacketEnum.getEnumConstants()) {
S1 mappedPacket = (S1) oldServerboundConstants.get(packet.getName()); S1 mappedPacket = oldServerboundConstants.get(packet.getName());
if (mappedPacket == null) { if (mappedPacket == null) {
// Packet doesn't exist on old server // Packet doesn't exist on old server
Preconditions.checkArgument(hasRegisteredServerbound(packet), Preconditions.checkArgument(hasRegisteredServerbound(packet),
@ -200,13 +200,13 @@ public abstract class AbstractProtocol<C1 extends ClientboundPacketType, C2 exte
@Override @Override
public void registerClientbound(C1 packetType, @Nullable PacketRemapper packetRemapper) { public void registerClientbound(C1 packetType, @Nullable PacketRemapper packetRemapper) {
checkPacketType(packetType, packetType.getClass() == oldClientboundPacketEnum); checkPacketType(packetType, oldClientboundPacketEnum == null || packetType.getClass() == oldClientboundPacketEnum);
ClientboundPacketType mappedPacket = oldClientboundPacketEnum == newClientboundPacketEnum ? packetType C2 mappedPacket = oldClientboundPacketEnum == newClientboundPacketEnum ? (C2) packetType
: Arrays.stream(newClientboundPacketEnum.getEnumConstants()).filter(en -> en.getName().equals(packetType.getName())).findAny().orElse(null); : Arrays.stream(newClientboundPacketEnum.getEnumConstants()).filter(en -> en.getName().equals(packetType.getName())).findAny().orElse(null);
Preconditions.checkNotNull(mappedPacket, "Packet type " + packetType + " in " + packetType.getClass().getSimpleName() + " could not be automatically mapped!"); Preconditions.checkNotNull(mappedPacket, "Packet type " + packetType + " in " + packetType.getClass().getSimpleName() + " could not be automatically mapped!");
registerClientbound(packetType, (C2) mappedPacket, packetRemapper); registerClientbound(packetType, mappedPacket, packetRemapper);
} }
@Override @Override
@ -226,13 +226,13 @@ public abstract class AbstractProtocol<C1 extends ClientboundPacketType, C2 exte
@Override @Override
public void registerServerbound(S2 packetType, @Nullable PacketRemapper packetRemapper) { public void registerServerbound(S2 packetType, @Nullable PacketRemapper packetRemapper) {
checkPacketType(packetType, packetType.getClass() == newServerboundPacketEnum); checkPacketType(packetType, newServerboundPacketEnum == null || packetType.getClass() == newServerboundPacketEnum);
ServerboundPacketType mappedPacket = oldServerboundPacketEnum == newServerboundPacketEnum ? packetType S1 mappedPacket = oldServerboundPacketEnum == newServerboundPacketEnum ? (S1) packetType
: Arrays.stream(oldServerboundPacketEnum.getEnumConstants()).filter(en -> en.getName().equals(packetType.getName())).findAny().orElse(null); : Arrays.stream(oldServerboundPacketEnum.getEnumConstants()).filter(en -> en.getName().equals(packetType.getName())).findAny().orElse(null);
Preconditions.checkNotNull(mappedPacket, "Packet type " + packetType + " in " + packetType.getClass().getSimpleName() + " could not be automatically mapped!"); Preconditions.checkNotNull(mappedPacket, "Packet type " + packetType + " in " + packetType.getClass().getSimpleName() + " could not be automatically mapped!");
registerServerbound(packetType, (S1) mappedPacket, packetRemapper); registerServerbound(packetType, mappedPacket, packetRemapper);
} }
@Override @Override
@ -253,8 +253,8 @@ public abstract class AbstractProtocol<C1 extends ClientboundPacketType, C2 exte
private void register(Map<Packet, ProtocolPacket> packetMap, PacketType packetType, @Nullable PacketType mappedPacketType, private void register(Map<Packet, ProtocolPacket> packetMap, PacketType packetType, @Nullable PacketType mappedPacketType,
Class<? extends PacketType> unmappedPacketEnum, Class<? extends PacketType> mappedPacketEnum, Class<? extends PacketType> unmappedPacketEnum, Class<? extends PacketType> mappedPacketEnum,
@Nullable PacketRemapper remapper, boolean override) { @Nullable PacketRemapper remapper, boolean override) {
checkPacketType(packetType, packetType.getClass() == unmappedPacketEnum); checkPacketType(packetType, unmappedPacketEnum == null || packetType.getClass() == unmappedPacketEnum);
checkPacketType(mappedPacketType, mappedPacketType == null || mappedPacketType.getClass() == mappedPacketEnum); checkPacketType(mappedPacketType, mappedPacketType == null || mappedPacketEnum == null || mappedPacketType.getClass() == mappedPacketEnum);
Preconditions.checkArgument(mappedPacketType == null || packetType.state() == mappedPacketType.state(), "Packet type state does not match mapped packet type state"); Preconditions.checkArgument(mappedPacketType == null || packetType.state() == mappedPacketType.state(), "Packet type state does not match mapped packet type state");
ProtocolPacket protocolPacket = new ProtocolPacket(packetType.state(), packetType, mappedPacketType, remapper); ProtocolPacket protocolPacket = new ProtocolPacket(packetType.state(), packetType, mappedPacketType, remapper);
@ -277,8 +277,8 @@ public abstract class AbstractProtocol<C1 extends ClientboundPacketType, C2 exte
} }
@Override @Override
public boolean hasRegisteredClientbound(State state, int unmappedPacketid) { public boolean hasRegisteredClientbound(State state, int unmappedPacketId) {
Packet packet = new Packet(state, unmappedPacketid); Packet packet = new Packet(state, unmappedPacketId);
return clientbound.containsKey(packet); return clientbound.containsKey(packet);
} }
@ -291,7 +291,7 @@ public abstract class AbstractProtocol<C1 extends ClientboundPacketType, C2 exte
@Override @Override
public void transform(Direction direction, State state, PacketWrapper packetWrapper) throws Exception { public void transform(Direction direction, State state, PacketWrapper packetWrapper) throws Exception {
Packet statePacket = new Packet(state, packetWrapper.getId()); Packet statePacket = new Packet(state, packetWrapper.getId());
Map<Packet, ProtocolPacket> packetMap = (direction == Direction.CLIENTBOUND ? this.clientbound : serverbound); Map<Packet, ProtocolPacket> packetMap = direction == Direction.CLIENTBOUND ? clientbound : serverbound;
ProtocolPacket protocolPacket = packetMap.get(statePacket); ProtocolPacket protocolPacket = packetMap.get(statePacket);
if (protocolPacket == null) { if (protocolPacket == null) {
return; return;

Datei anzeigen

@ -28,7 +28,7 @@ import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.PacketType; import com.viaversion.viaversion.api.protocol.packet.PacketType;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.VersionedPacketCreator; import com.viaversion.viaversion.api.protocol.packet.VersionedPacketTransformer;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import com.viaversion.viaversion.api.protocol.version.ServerProtocolVersion; import com.viaversion.viaversion.api.protocol.version.ServerProtocolVersion;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
@ -147,18 +147,34 @@ public interface ProtocolManager {
@Nullable List<ProtocolPathEntry> getProtocolPath(int clientVersion, int serverVersion); @Nullable List<ProtocolPathEntry> getProtocolPath(int clientVersion, int serverVersion);
/** /**
* Returns a versioned packet creator to send packets from a given base version to any client version supported by Via. * Returns a versioned packet transformer to transform and send packets from a given base version to any client version supported by Via.
* The used packet types have to match the given protocol version. * The used packet types have to match the given protocol version.
* <p>
* It is important the correct packet type classes are passed. The ViaVersion given packet type enums
* are found in the common module. Examples for correct invocations are:
* <pre>
* createPacketTransformer(ProtocolVersion.v1_17_1, ClientboundPackets1_17_1.class, ServerboundPackets1_17.class);
* createPacketTransformer(ProtocolVersion.v1_12_2, ClientboundPackets1_12_1.class, ServerboundPackets1_12_1.class);
* createPacketTransformer(ProtocolVersion.v1_8, ClientboundPackets1_8.class, ServerboundPackets1_8.class);
* </pre>
* If only clientbound <b>or</b> serverbound packets are used, the other class can be passed as null, see:
* <pre>
* VersionedPacketTransformer&lt;?, ServerboundHandshakePackets&gt; creator
* = createPacketTransformer(ProtocolVersion.v1_17_1, null, ServerboundHandshakePackets.class);
* </pre>
* *
* @param inputVersion input protocol version * @param inputVersion input protocol version
* @param clientboundPacketsClass clientbound packets class * @param clientboundPacketsClass clientbound packets class, or null if no clientbound packets will be sent or transformed with this
* @param serverboundPacketsClass serverbound packets class * @param serverboundPacketsClass serverbound packets class, or null if no serverbound packets will be sent or transformed with this
* @return versioned packet creator * @return versioned packet transformer to transform and send packets from a given protocol version
* @throws IllegalArgumentException if either of the packet classes are the base {@link ClientboundPacketType} or {@link ServerboundPacketType} interfaces * @throws IllegalArgumentException if either of the packet classes are the base {@link ClientboundPacketType} or {@link ServerboundPacketType} interfaces
* @throws IllegalArgumentException if both packet classes are null
*/ */
VersionedPacketCreator createVersionedPacketCreator(ProtocolVersion inputVersion, <C extends ClientboundPacketType,
Class<? extends ClientboundPacketType> clientboundPacketsClass, S extends ServerboundPacketType
Class<? extends ServerboundPacketType> serverboundPacketsClass); > VersionedPacketTransformer<C, S> createPacketTransformer(ProtocolVersion inputVersion,
@Nullable Class<C> clientboundPacketsClass,
@Nullable Class<S> serverboundPacketsClass);
/** /**
* Returns whether protocol path calculation expects the path to come closer to the expected version with each entry, true by default. * Returns whether protocol path calculation expects the path to come closer to the expected version with each entry, true by default.

Datei anzeigen

@ -219,8 +219,10 @@ public interface PacketWrapper {
*/ */
ChannelFuture sendFuture(Class<? extends Protocol> packetProtocol) throws Exception; ChannelFuture sendFuture(Class<? extends Protocol> packetProtocol) throws Exception;
/**
@Deprecated * @deprecated misleading; use {@link #sendRaw()}. This method will be removed in 5.0.0
*/
@Deprecated/*(forRemoval = true)*/
default void send() throws Exception { default void send() throws Exception {
sendRaw(); sendRaw();
} }
@ -328,8 +330,9 @@ public interface PacketWrapper {
* (Ensure the ID is suitable for viaversion) * (Ensure the ID is suitable for viaversion)
* *
* @throws Exception If it failed to write * @throws Exception If it failed to write
* @deprecated misleading; use {@link #sendToServerRaw()}. This method will be removed in 5.0.0
*/ */
@Deprecated @Deprecated/*(forRemoval = true)*/
default void sendToServer() throws Exception { default void sendToServer() throws Exception {
sendToServerRaw(); sendToServerRaw();
} }

Datei anzeigen

@ -23,12 +23,18 @@
package com.viaversion.viaversion.api.protocol.packet; package com.viaversion.viaversion.api.protocol.packet;
import com.viaversion.viaversion.api.connection.ProtocolInfo; import com.viaversion.viaversion.api.connection.ProtocolInfo;
import com.viaversion.viaversion.api.connection.UserConnection;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.function.Consumer;
/** /**
* Utility to send packets from a given base version to or from any client version supported by Via. * Utility to send packets from a given base version to or from any client version supported by Via.
*
* @param <C> clientbound packet type
* @param <S> serverbound packet type
*/ */
public interface VersionedPacketCreator { public interface VersionedPacketTransformer<C extends ClientboundPacketType, S extends ServerboundPacketType> {
/** /**
* Sends a packet to the user or server, depending on the packet type given by {@link PacketWrapper#getPacketType()}. * Sends a packet to the user or server, depending on the packet type given by {@link PacketWrapper#getPacketType()}.
@ -38,33 +44,63 @@ public interface VersionedPacketCreator {
* @throws IllegalArgumentException if the packet type is not of the expected clientbound or serverbound packets class * @throws IllegalArgumentException if the packet type is not of the expected clientbound or serverbound packets class
* @throws IllegalArgumentException if {@link PacketWrapper#user()} returns null * @throws IllegalArgumentException if {@link PacketWrapper#user()} returns null
* @throws RuntimeException if no path from the input version to the required client version exists * @throws RuntimeException if no path from the input version to the required client version exists
* @throws Exception if an error occurred while constructing the packet or sending it * @throws Exception if an error occurred while transforming or sending the packet
*/ */
boolean send(PacketWrapper packet) throws Exception; boolean send(PacketWrapper packet) throws Exception;
/**
* @see #send(PacketWrapper)
*/
boolean send(UserConnection connection, C packetType, Consumer<PacketWrapper> packetWriter) throws Exception;
/**
* @see #send(PacketWrapper)
*/
boolean send(UserConnection connection, S packetType, Consumer<PacketWrapper> packetWriter) throws Exception;
/** /**
* Sends a packet to the user or server, depending on the packet type given by {@link PacketWrapper#getPacketType()}, submitted to the netty event loop. * Sends a packet to the user or server, depending on the packet type given by {@link PacketWrapper#getPacketType()}, submitted to the netty event loop.
* Returns false if the packet has been cancelled at some point, but does not indicate whether a replacement has been constructed. * Returns false if the packet has been cancelled at some point, but does not indicate whether a replacement has been constructed.
* *
* @param packet packet wrapper * @param packet packet wrapper
* @return whether this packet specifically has been sent, false if cancelled * @return whether this packet specifically has been sent, false if cancelled
* @throws IllegalArgumentException if the packet type is not of the expected clientbound packets class * @throws IllegalArgumentException if the packet type is not of the expected clientbound or serverbound packets class
* @throws IllegalArgumentException if {@link PacketWrapper#user()} returns null * @throws IllegalArgumentException if {@link PacketWrapper#user()} returns null
* @throws RuntimeException if no path from the input version to the required client version exists * @throws RuntimeException if no path from the input version to the required client version exists
* @throws Exception if an error occurred while constructing the packet or sending it * @throws Exception if an error occurred while transforming or sending the packet
*/ */
boolean scheduleSend(PacketWrapper packet) throws Exception; boolean scheduleSend(PacketWrapper packet) throws Exception;
/**
* @see #scheduleSend(PacketWrapper)
*/
boolean scheduleSend(UserConnection connection, C packetType, Consumer<PacketWrapper> packetWriter) throws Exception;
/**
* @see #scheduleSend(PacketWrapper)
*/
boolean scheduleSend(UserConnection connection, S packetType, Consumer<PacketWrapper> packetWriter) throws Exception;
/** /**
* Transforms a packet to the protocol version of the given connection or server, or null if cancelled at some point. * Transforms a packet to the protocol version of the given connection or server, or null if cancelled at some point.
* The target version is given by {@link ProtocolInfo#getProtocolVersion()} or {@link ProtocolInfo#getServerProtocolVersion()}. * The target version is given by {@link ProtocolInfo#getProtocolVersion()} or {@link ProtocolInfo#getServerProtocolVersion()}.
* *
* @param packet packet wrapper * @param packet packet wrapper
* @return created and transformed packet wrapper, or null if cancelled at some point * @return created and transformed packet wrapper, or null if cancelled at some point
* @throws IllegalArgumentException if the packet type is not of the expected clientbound packets class * @throws IllegalArgumentException if the packet type is not of the expected clientbound or serverbound packets class
* @throws IllegalArgumentException if {@link PacketWrapper#user()} returns null * @throws IllegalArgumentException if {@link PacketWrapper#user()} returns null
* @throws RuntimeException if no path from the input version to the required client version exists * @throws RuntimeException if no path from the input version to the required client version exists
* @throws Exception if an error occurred while constructing the packet * @throws Exception if an error occurred while transforming the packet
*/ */
@Nullable PacketWrapper transform(PacketWrapper packet) throws Exception; @Nullable PacketWrapper transform(PacketWrapper packet) throws Exception;
/**
* @see #transform(PacketWrapper)
*/
@Nullable PacketWrapper transform(UserConnection connection, C packetType, Consumer<PacketWrapper> packetWriter) throws Exception;
/**
* @see #transform(PacketWrapper)
*/
@Nullable PacketWrapper transform(UserConnection connection, S packetType, Consumer<PacketWrapper> packetWriter) throws Exception;
} }

Datei anzeigen

@ -32,11 +32,11 @@ import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.PacketType; import com.viaversion.viaversion.api.protocol.packet.PacketType;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.VersionedPacketCreator; import com.viaversion.viaversion.api.protocol.packet.VersionedPacketTransformer;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import com.viaversion.viaversion.api.protocol.version.ServerProtocolVersion; import com.viaversion.viaversion.api.protocol.version.ServerProtocolVersion;
import com.viaversion.viaversion.protocol.packet.PacketWrapperImpl; import com.viaversion.viaversion.protocol.packet.PacketWrapperImpl;
import com.viaversion.viaversion.protocol.packet.VersionedPacketCreatorImpl; import com.viaversion.viaversion.protocol.packet.VersionedPacketTransformerImpl;
import com.viaversion.viaversion.protocols.base.BaseProtocol; import com.viaversion.viaversion.protocols.base.BaseProtocol;
import com.viaversion.viaversion.protocols.base.BaseProtocol1_16; import com.viaversion.viaversion.protocols.base.BaseProtocol1_16;
import com.viaversion.viaversion.protocols.base.BaseProtocol1_7; import com.viaversion.viaversion.protocols.base.BaseProtocol1_7;
@ -267,11 +267,13 @@ public class ProtocolManagerImpl implements ProtocolManager {
} }
@Override @Override
public VersionedPacketCreator createVersionedPacketCreator(ProtocolVersion inputVersion, public <C extends ClientboundPacketType,
Class<? extends ClientboundPacketType> clientboundPacketsClass, S extends ServerboundPacketType
Class<? extends ServerboundPacketType> serverboundPacketsClass) { > VersionedPacketTransformer<C, S> createPacketTransformer(ProtocolVersion inputVersion,
@Nullable Class<C> clientboundPacketsClass,
@Nullable Class<S> serverboundPacketsClass) {
Preconditions.checkArgument(clientboundPacketsClass != ClientboundPacketType.class && serverboundPacketsClass != ServerboundPacketType.class); Preconditions.checkArgument(clientboundPacketsClass != ClientboundPacketType.class && serverboundPacketsClass != ServerboundPacketType.class);
return new VersionedPacketCreatorImpl(inputVersion, clientboundPacketsClass, serverboundPacketsClass); return new VersionedPacketTransformerImpl<>(inputVersion, clientboundPacketsClass, serverboundPacketsClass);
} }
/** /**

Datei anzeigen

@ -28,24 +28,24 @@ import com.viaversion.viaversion.api.protocol.packet.PacketType;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
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 com.viaversion.viaversion.api.protocol.packet.VersionedPacketCreator; import com.viaversion.viaversion.api.protocol.packet.VersionedPacketTransformer;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Consumer;
public class VersionedPacketCreatorImpl implements VersionedPacketCreator { public class VersionedPacketTransformerImpl<C extends ClientboundPacketType, S extends ServerboundPacketType> implements VersionedPacketTransformer<C, S> {
private final int inputProtocolVersion; private final int inputProtocolVersion;
private final Class<? extends ClientboundPacketType> clientboundPacketsClass; private final Class<C> clientboundPacketsClass;
private final Class<? extends ServerboundPacketType> serverboundPacketsClass; private final Class<S> serverboundPacketsClass;
public VersionedPacketCreatorImpl(ProtocolVersion inputVersion, public VersionedPacketTransformerImpl(ProtocolVersion inputVersion, @Nullable Class<C> clientboundPacketsClass, @Nullable Class<S> serverboundPacketsClass) {
Class<? extends ClientboundPacketType> clientboundPacketsClass, Class<? extends ServerboundPacketType> serverboundPacketsClass) {
Preconditions.checkNotNull(inputVersion); Preconditions.checkNotNull(inputVersion);
Preconditions.checkNotNull(clientboundPacketsClass); Preconditions.checkArgument(clientboundPacketsClass != null || serverboundPacketsClass != null,
Preconditions.checkNotNull(serverboundPacketsClass); "Either the clientbound or serverbound packets class has to be non-null");
this.inputProtocolVersion = inputVersion.getVersion(); this.inputProtocolVersion = inputVersion.getVersion();
this.clientboundPacketsClass = clientboundPacketsClass; this.clientboundPacketsClass = clientboundPacketsClass;
this.serverboundPacketsClass = serverboundPacketsClass; this.serverboundPacketsClass = serverboundPacketsClass;
@ -57,12 +57,32 @@ public class VersionedPacketCreatorImpl implements VersionedPacketCreator {
return transformAndSendPacket(packet, true); return transformAndSendPacket(packet, true);
} }
@Override
public boolean send(UserConnection connection, C packetType, Consumer<PacketWrapper> packetWriter) throws Exception {
return createAndSend(connection, packetType, packetWriter);
}
@Override
public boolean send(UserConnection connection, S packetType, Consumer<PacketWrapper> packetWriter) throws Exception {
return createAndSend(connection, packetType, packetWriter);
}
@Override @Override
public boolean scheduleSend(PacketWrapper packet) throws Exception { public boolean scheduleSend(PacketWrapper packet) throws Exception {
validatePacket(packet); validatePacket(packet);
return transformAndSendPacket(packet, false); return transformAndSendPacket(packet, false);
} }
@Override
public boolean scheduleSend(UserConnection connection, C packetType, Consumer<PacketWrapper> packetWriter) throws Exception {
return scheduleCreateAndSend(connection, packetType, packetWriter);
}
@Override
public boolean scheduleSend(UserConnection connection, S packetType, Consumer<PacketWrapper> packetWriter) throws Exception {
return scheduleCreateAndSend(connection, packetType, packetWriter);
}
@Override @Override
public @Nullable PacketWrapper transform(PacketWrapper packet) throws Exception { public @Nullable PacketWrapper transform(PacketWrapper packet) throws Exception {
validatePacket(packet); validatePacket(packet);
@ -70,6 +90,16 @@ public class VersionedPacketCreatorImpl implements VersionedPacketCreator {
return packet.isCancelled() ? null : packet; return packet.isCancelled() ? null : packet;
} }
@Override
public @Nullable PacketWrapper transform(UserConnection connection, C packetType, Consumer<PacketWrapper> packetWriter) throws Exception {
return createAndTransform(connection, packetType, packetWriter);
}
@Override
public @Nullable PacketWrapper transform(UserConnection connection, S packetType, Consumer<PacketWrapper> packetWriter) throws Exception {
return createAndTransform(connection, packetType, packetWriter);
}
private void validatePacket(PacketWrapper packet) { private void validatePacket(PacketWrapper packet) {
if (packet.user() == null) { if (packet.user() == null) {
throw new IllegalArgumentException("PacketWrapper does not have a targetted UserConnection"); throw new IllegalArgumentException("PacketWrapper does not have a targetted UserConnection");
@ -140,4 +170,22 @@ public class VersionedPacketCreatorImpl implements VersionedPacketCreator {
} }
} }
} }
private boolean createAndSend(UserConnection connection, PacketType packetType, Consumer<PacketWrapper> packetWriter) throws Exception {
PacketWrapper packet = PacketWrapper.create(packetType, connection);
packetWriter.accept(packet);
return send(packet);
}
private boolean scheduleCreateAndSend(UserConnection connection, PacketType packetType, Consumer<PacketWrapper> packetWriter) throws Exception {
PacketWrapper packet = PacketWrapper.create(packetType, connection);
packetWriter.accept(packet);
return scheduleSend(packet);
}
private @Nullable PacketWrapper createAndTransform(UserConnection connection, PacketType packetType, Consumer<PacketWrapper> packetWriter) throws Exception {
PacketWrapper packet = PacketWrapper.create(packetType, connection);
packetWriter.accept(packet);
return transform(packet);
}
} }