Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-12-26 16:12:42 +01:00
More packet sending API
Allows for easy packet sending from a specific version to or from any client version supported by Via. For example, you can send packets in the 1.17.1 format to both client and server, and it will be transformed accordingly if executed on another server version or sent to a different client version.
Dieser Commit ist enthalten in:
Ursprung
46ca469a4a
Commit
874dbafe26
@ -52,7 +52,7 @@ public interface ViaAPI<T> {
|
|||||||
* @return API version incremented with meaningful API changes
|
* @return API version incremented with meaningful API changes
|
||||||
*/
|
*/
|
||||||
default int apiVersion() {
|
default int apiVersion() {
|
||||||
return 3;
|
return 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -24,8 +24,11 @@ package com.viaversion.viaversion.api.protocol;
|
|||||||
|
|
||||||
import com.google.common.collect.Range;
|
import com.google.common.collect.Range;
|
||||||
import com.viaversion.viaversion.api.connection.UserConnection;
|
import com.viaversion.viaversion.api.connection.UserConnection;
|
||||||
|
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.VersionedPacketCreator;
|
||||||
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;
|
||||||
@ -143,6 +146,22 @@ 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.
|
||||||
|
* The used packet types have to match the given protocol version.
|
||||||
|
*
|
||||||
|
* @param inputVersion input protocol version
|
||||||
|
* @param <C> clientbound packet for the given protocol version
|
||||||
|
* @param <S> serverbound packet for the given protocol version
|
||||||
|
* @param clientboundPacketsClass clientbound packets class
|
||||||
|
* @param serverboundPacketsClass serverbound packets class
|
||||||
|
* @return versioned packet creator
|
||||||
|
* @throws IllegalArgumentException if either of the packet classes are the base {@link ClientboundPacketType} or {@link ServerboundPacketType} interfaces
|
||||||
|
*/
|
||||||
|
<C extends ClientboundPacketType,
|
||||||
|
S extends ServerboundPacketType
|
||||||
|
> VersionedPacketCreator<C, S> createVersionedPacketCreator(ProtocolVersion inputVersion, Class<C> clientboundPacketsClass, 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.
|
||||||
* <p>
|
* <p>
|
||||||
|
@ -23,6 +23,7 @@
|
|||||||
package com.viaversion.viaversion.api.protocol;
|
package com.viaversion.viaversion.api.protocol;
|
||||||
|
|
||||||
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
|
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
|
||||||
|
import com.viaversion.viaversion.api.protocol.packet.Direction;
|
||||||
import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
|
import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -45,5 +46,10 @@ public interface SimpleProtocol extends Protocol<SimpleProtocol.DummyPacketTypes
|
|||||||
public String getName() {
|
public String getName() {
|
||||||
return name();
|
return name();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Direction direction() {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,4 +27,9 @@ package com.viaversion.viaversion.api.protocol.packet;
|
|||||||
* representing PLAY state packets, ordered by their packet id.
|
* representing PLAY state packets, ordered by their packet id.
|
||||||
*/
|
*/
|
||||||
public interface ClientboundPacketType extends PacketType {
|
public interface ClientboundPacketType extends PacketType {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
default Direction direction() {
|
||||||
|
return Direction.CLIENTBOUND;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,4 +43,11 @@ public interface PacketType {
|
|||||||
* @return name of the packet
|
* @return name of the packet
|
||||||
*/
|
*/
|
||||||
String getName();
|
String getName();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clientbound or serverbound direction.
|
||||||
|
*
|
||||||
|
* @return direction
|
||||||
|
*/
|
||||||
|
Direction direction();
|
||||||
}
|
}
|
||||||
|
@ -217,16 +217,27 @@ public interface PacketWrapper {
|
|||||||
*/
|
*/
|
||||||
ChannelFuture sendFuture(Class<? extends Protocol> packetProtocol) throws Exception;
|
ChannelFuture sendFuture(Class<? extends Protocol> packetProtocol) throws Exception;
|
||||||
|
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
|
default void send() throws Exception {
|
||||||
|
sendRaw();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send this packet to the associated user.
|
* Sends this packet to the connection.
|
||||||
* Be careful not to send packets twice.
|
* <b>Unlike {@link #send(Class)}, this method does not handle the pipeline with packet id and data changes.</b>
|
||||||
* (Sends it after current)
|
|
||||||
* <b>This method is no longer used, it's favoured to use {@link #send(Class)} as it will handle the pipeline properly.</b>
|
|
||||||
*
|
*
|
||||||
* @throws Exception if it fails to write
|
* @throws Exception if it fails to write
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
void sendRaw() throws Exception;
|
||||||
void send() throws Exception;
|
|
||||||
|
/**
|
||||||
|
* Sends this packet to the associated user, submitted to netty's event loop.
|
||||||
|
* <b>Unlike {@link #send(Class)}, this method does not handle the pipeline with packet id and data changes.</b>
|
||||||
|
*
|
||||||
|
* @throws Exception if it fails to write
|
||||||
|
*/
|
||||||
|
void scheduleSendRaw() throws Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new packet for the target of this packet.
|
* Creates a new packet for the target of this packet.
|
||||||
@ -317,7 +328,25 @@ public interface PacketWrapper {
|
|||||||
* @throws Exception If it failed to write
|
* @throws Exception If it failed to write
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
void sendToServer() throws Exception;
|
default void sendToServer() throws Exception {
|
||||||
|
sendToServerRaw();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends this packet to the server.
|
||||||
|
* <b>Unlike {@link #sendToServer(Class)}, this method does not handle the pipeline with packet id and data changes.</b>
|
||||||
|
*
|
||||||
|
* @throws Exception if it fails to write
|
||||||
|
*/
|
||||||
|
void sendToServerRaw() throws Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends this packet to the server, submitted to netty's event loop.
|
||||||
|
* <b>Unlike {@link #sendToServer(Class)}, this method does not handle the pipeline with packet id and data changes.</b>
|
||||||
|
*
|
||||||
|
* @throws Exception if it fails to write
|
||||||
|
*/
|
||||||
|
void scheduleSendToServerRaw() throws Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send this packet to the server on the current thread, skipping the current protocol.
|
* Send this packet to the server on the current thread, skipping the current protocol.
|
||||||
|
@ -27,4 +27,9 @@ package com.viaversion.viaversion.api.protocol.packet;
|
|||||||
* representing PLAY state packets, ordered by their packet id.
|
* representing PLAY state packets, ordered by their packet id.
|
||||||
*/
|
*/
|
||||||
public interface ServerboundPacketType extends PacketType {
|
public interface ServerboundPacketType extends PacketType {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
default Direction direction() {
|
||||||
|
return Direction.SERVERBOUND;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,122 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
|
||||||
|
* Copyright (C) 2016-2021 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.api.protocol.packet;
|
||||||
|
|
||||||
|
import com.viaversion.viaversion.api.connection.ProtocolInfo;
|
||||||
|
import com.viaversion.viaversion.api.connection.UserConnection;
|
||||||
|
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.
|
||||||
|
*
|
||||||
|
* @param <C> clientbound packet type
|
||||||
|
* @param <S> serverbound packet type
|
||||||
|
*/
|
||||||
|
public interface VersionedPacketCreator<C extends ClientboundPacketType, S extends ServerboundPacketType> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a packet to the given user.
|
||||||
|
* Returns false if the packet has been cancelled at some point, but does not indicate whether a replacement has been constructed.
|
||||||
|
*
|
||||||
|
* @param connection user connection
|
||||||
|
* @param packetType clientbound packet type
|
||||||
|
* @param packetWriter consumer filling the packet with data
|
||||||
|
* @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 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
|
||||||
|
*/
|
||||||
|
boolean send(UserConnection connection, C packetType, Consumer<PacketWrapper> packetWriter) throws Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a packet to the server.
|
||||||
|
* Returns false if the packet has been cancelled at some point, but does not indicate whether a replacement has been constructed.
|
||||||
|
*
|
||||||
|
* @param connection user connection
|
||||||
|
* @param packetType serverbound packet type
|
||||||
|
* @param packetWriter consumer filling the packet with data
|
||||||
|
* @return whether this packet specifically has been sent, false if cancelled
|
||||||
|
* @throws IllegalArgumentException if the packet type is not of the expected serverbound packets class
|
||||||
|
* @throws RuntimeException if no path from the input version to the required server version exists
|
||||||
|
* @throws Exception if an error occurred while constructing the packet or sending it
|
||||||
|
*/
|
||||||
|
boolean send(UserConnection connection, S packetType, Consumer<PacketWrapper> packetWriter) throws Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a packet to the given user, 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.
|
||||||
|
*
|
||||||
|
* @param connection user connection
|
||||||
|
* @param packetType clientbound packet type
|
||||||
|
* @param packetWriter consumer filling the packet with data
|
||||||
|
* @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 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
|
||||||
|
*/
|
||||||
|
boolean scheduleSend(UserConnection connection, C packetType, Consumer<PacketWrapper> packetWriter) throws Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a packet to the server, 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.
|
||||||
|
*
|
||||||
|
* @param connection user connection
|
||||||
|
* @param packetType serverbound packet type
|
||||||
|
* @param packetWriter consumer filling the packet with data
|
||||||
|
* @return whether this packet specifically has been sent, false if cancelled
|
||||||
|
* @throws IllegalArgumentException if the packet type is not of the expected serverbound packets class
|
||||||
|
* @throws RuntimeException if no path from the input version to the required server version exists
|
||||||
|
* @throws Exception if an error occurred while constructing the packet or sending it
|
||||||
|
*/
|
||||||
|
boolean scheduleSend(UserConnection connection, S packetType, Consumer<PacketWrapper> packetWriter) throws Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transforms a packet to the protocol version of the given connection, or null if cancelled at some point.
|
||||||
|
* The target version is given by {@link ProtocolInfo#getProtocolVersion()} with the connection as the receiver.
|
||||||
|
*
|
||||||
|
* @param connection user connection
|
||||||
|
* @param packetType clientbound packet type
|
||||||
|
* @param packetWriter consumer filling the packet with data
|
||||||
|
* @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 RuntimeException if no path from the input version to the required client version exists
|
||||||
|
* @throws Exception if an error occurred while constructing the packet
|
||||||
|
*/
|
||||||
|
@Nullable PacketWrapper transform(UserConnection connection, C packetType, Consumer<PacketWrapper> packetWriter) throws Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transforms a packet to the server protocol version the connection is on, or null if cancelled at some point.
|
||||||
|
* The target version is given by {@link ProtocolInfo#getServerProtocolVersion()} with the connection as the sender.
|
||||||
|
*
|
||||||
|
* @param connection user connection
|
||||||
|
* @param packetType serverbound packet type
|
||||||
|
* @param packetWriter consumer filling the packet with data
|
||||||
|
* @return created and transformed packet wrapper, or null if cancelled at some point
|
||||||
|
* @throws IllegalArgumentException if the packet type is not of the expected serverbound packets class
|
||||||
|
* @throws RuntimeException if no path from the input version to the required server version exists
|
||||||
|
* @throws Exception if an error occurred while constructing the packet
|
||||||
|
*/
|
||||||
|
@Nullable PacketWrapper transform(UserConnection connection, S packetType, Consumer<PacketWrapper> packetWriter) throws Exception;
|
||||||
|
}
|
@ -200,12 +200,17 @@ public class UserConnectionImpl implements UserConnection {
|
|||||||
// We'll use passing through because there are some encoder wrappers
|
// We'll use passing through because there are some encoder wrappers
|
||||||
ChannelHandlerContext context = PipelineUtil
|
ChannelHandlerContext context = PipelineUtil
|
||||||
.getPreviousContext(Via.getManager().getInjector().getDecoderName(), channel.pipeline());
|
.getPreviousContext(Via.getManager().getInjector().getDecoderName(), channel.pipeline());
|
||||||
try {
|
|
||||||
Type.VAR_INT.writePrimitive(buf, PacketWrapper.PASSTHROUGH_ID);
|
if (shouldTransformPacket()) {
|
||||||
Type.UUID.write(buf, generatePassthroughToken());
|
// Bypass serverbound packet decoder transforming
|
||||||
} catch (Exception shouldNotHappen) {
|
try {
|
||||||
throw new RuntimeException(shouldNotHappen);
|
Type.VAR_INT.writePrimitive(buf, PacketWrapper.PASSTHROUGH_ID);
|
||||||
|
Type.UUID.write(buf, generatePassthroughToken());
|
||||||
|
} catch (Exception shouldNotHappen) {
|
||||||
|
throw new RuntimeException(shouldNotHappen);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
buf.writeBytes(packet);
|
buf.writeBytes(packet);
|
||||||
Runnable act = () -> {
|
Runnable act = () -> {
|
||||||
if (context != null) {
|
if (context != null) {
|
||||||
|
@ -28,10 +28,14 @@ import com.viaversion.viaversion.api.protocol.Protocol;
|
|||||||
import com.viaversion.viaversion.api.protocol.ProtocolManager;
|
import com.viaversion.viaversion.api.protocol.ProtocolManager;
|
||||||
import com.viaversion.viaversion.api.protocol.ProtocolPathEntry;
|
import com.viaversion.viaversion.api.protocol.ProtocolPathEntry;
|
||||||
import com.viaversion.viaversion.api.protocol.ProtocolPathKey;
|
import com.viaversion.viaversion.api.protocol.ProtocolPathKey;
|
||||||
|
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
|
||||||
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.VersionedPacketCreator;
|
||||||
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.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;
|
||||||
@ -261,6 +265,14 @@ public class ProtocolManagerImpl implements ProtocolManager {
|
|||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <C extends ClientboundPacketType,
|
||||||
|
S extends ServerboundPacketType
|
||||||
|
> VersionedPacketCreator<C, S> createVersionedPacketCreator(ProtocolVersion inputVersion, Class<C> clientboundPacketsClass, Class<S> serverboundPacketsClass) {
|
||||||
|
Preconditions.checkArgument(clientboundPacketsClass != ClientboundPacketType.class && serverboundPacketsClass != ServerboundPacketType.class);
|
||||||
|
return new VersionedPacketCreatorImpl<>(inputVersion, clientboundPacketsClass, serverboundPacketsClass);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates a path to get from an input protocol to the server's protocol.
|
* Calculates a path to get from an input protocol to the server's protocol.
|
||||||
*
|
*
|
||||||
@ -270,7 +282,7 @@ public class ProtocolManagerImpl implements ProtocolManager {
|
|||||||
* @return path that has been generated, null if failed
|
* @return path that has been generated, null if failed
|
||||||
*/
|
*/
|
||||||
private @Nullable Int2ObjectSortedMap<Protocol> getProtocolPath(Int2ObjectSortedMap<Protocol> current, int clientVersion, int serverVersion) {
|
private @Nullable Int2ObjectSortedMap<Protocol> getProtocolPath(Int2ObjectSortedMap<Protocol> current, int clientVersion, int serverVersion) {
|
||||||
if (current.size() > maxProtocolPathSize) return null; // Fail safe, protocol too complicated.
|
if (current.size() > maxProtocolPathSize) return null; // Fail-safe, protocol too complicated.
|
||||||
|
|
||||||
// First, check if there is any protocols for this
|
// First, check if there is any protocols for this
|
||||||
Int2ObjectMap<Protocol> toServerProtocolMap = registryMap.get(clientVersion);
|
Int2ObjectMap<Protocol> toServerProtocolMap = registryMap.get(clientVersion);
|
||||||
|
@ -306,15 +306,26 @@ public class PacketWrapperImpl implements PacketWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Deprecated
|
public void sendRaw() throws Exception {
|
||||||
public void send() throws Exception {
|
sendRaw(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void scheduleSendRaw() throws Exception {
|
||||||
|
sendRaw(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendRaw(boolean currentThread) throws Exception {
|
||||||
if (isCancelled()) return;
|
if (isCancelled()) return;
|
||||||
|
|
||||||
// Send
|
|
||||||
ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer();
|
ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer();
|
||||||
try {
|
try {
|
||||||
writeToBuffer(output);
|
writeToBuffer(output);
|
||||||
user().sendRawPacket(output.retain());
|
if (currentThread) {
|
||||||
|
user().sendRawPacket(output.retain());
|
||||||
|
} else {
|
||||||
|
user().scheduleSendRawPacket(output.retain());
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
output.release();
|
output.release();
|
||||||
}
|
}
|
||||||
@ -384,14 +395,26 @@ public class PacketWrapperImpl implements PacketWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Deprecated
|
public void sendToServerRaw() throws Exception {
|
||||||
public void sendToServer() throws Exception {
|
sendToServerRaw(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void scheduleSendToServerRaw() throws Exception {
|
||||||
|
sendToServerRaw(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendToServerRaw(boolean currentThread) throws Exception {
|
||||||
if (isCancelled()) return;
|
if (isCancelled()) return;
|
||||||
|
|
||||||
ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer();
|
ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer();
|
||||||
try {
|
try {
|
||||||
writeToBuffer(output);
|
writeToBuffer(output);
|
||||||
user().sendRawPacketToServer(output.retain());
|
if (currentThread) {
|
||||||
|
user().sendRawPacketToServer(output.retain());
|
||||||
|
} else {
|
||||||
|
user().scheduleSendRawPacketToServer(output.retain());
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
output.release();
|
output.release();
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,148 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
|
||||||
|
* Copyright (C) 2016-2021 ViaVersion and contributors
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package com.viaversion.viaversion.protocol.packet;
|
||||||
|
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
|
import com.viaversion.viaversion.api.Via;
|
||||||
|
import com.viaversion.viaversion.api.connection.UserConnection;
|
||||||
|
import com.viaversion.viaversion.api.protocol.Protocol;
|
||||||
|
import com.viaversion.viaversion.api.protocol.ProtocolPathEntry;
|
||||||
|
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
|
||||||
|
import com.viaversion.viaversion.api.protocol.packet.Direction;
|
||||||
|
import com.viaversion.viaversion.api.protocol.packet.PacketType;
|
||||||
|
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
|
||||||
|
import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
|
||||||
|
import com.viaversion.viaversion.api.protocol.packet.State;
|
||||||
|
import com.viaversion.viaversion.api.protocol.packet.VersionedPacketCreator;
|
||||||
|
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
|
||||||
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
public class VersionedPacketCreatorImpl<C extends ClientboundPacketType, S extends ServerboundPacketType> implements VersionedPacketCreator<C, S> {
|
||||||
|
|
||||||
|
private final int inputProtocolVersion;
|
||||||
|
private final Class<C> clientboundPacketsClass;
|
||||||
|
private final Class<S> serverboundPacketsClass;
|
||||||
|
|
||||||
|
public VersionedPacketCreatorImpl(ProtocolVersion inputVersion, Class<C> clientboundPacketsClass, Class<S> serverboundPacketsClass) {
|
||||||
|
Preconditions.checkNotNull(inputVersion);
|
||||||
|
Preconditions.checkNotNull(clientboundPacketsClass);
|
||||||
|
Preconditions.checkNotNull(serverboundPacketsClass);
|
||||||
|
this.inputProtocolVersion = inputVersion.getVersion();
|
||||||
|
this.clientboundPacketsClass = clientboundPacketsClass;
|
||||||
|
this.serverboundPacketsClass = serverboundPacketsClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean send(UserConnection connection, C packetType, Consumer<PacketWrapper> packetWriter) throws Exception {
|
||||||
|
Preconditions.checkArgument(packetType.getClass() == clientboundPacketsClass);
|
||||||
|
return createAndSend(connection, packetType, packetWriter, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean send(UserConnection connection, S packetType, Consumer<PacketWrapper> packetWriter) throws Exception {
|
||||||
|
Preconditions.checkArgument(packetType.getClass() == serverboundPacketsClass);
|
||||||
|
return createAndSend(connection, packetType, packetWriter, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean scheduleSend(UserConnection connection, C packetType, Consumer<PacketWrapper> packetWriter) throws Exception {
|
||||||
|
Preconditions.checkArgument(packetType.getClass() == clientboundPacketsClass);
|
||||||
|
return createAndSend(connection, packetType, packetWriter, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean scheduleSend(UserConnection connection, S packetType, Consumer<PacketWrapper> packetWriter) throws Exception {
|
||||||
|
Preconditions.checkArgument(packetType.getClass() == serverboundPacketsClass);
|
||||||
|
return createAndSend(connection, packetType, packetWriter, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable PacketWrapper transform(UserConnection connection, C packetType, Consumer<PacketWrapper> packetWriter) throws Exception {
|
||||||
|
Preconditions.checkArgument(packetType.getClass() == clientboundPacketsClass);
|
||||||
|
PacketWrapper packet = createAndTransform(connection, packetType, packetWriter);
|
||||||
|
return packet.isCancelled() ? null : packet;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable PacketWrapper transform(UserConnection connection, S packetType, Consumer<PacketWrapper> packetWriter) throws Exception {
|
||||||
|
Preconditions.checkArgument(packetType.getClass() == serverboundPacketsClass);
|
||||||
|
PacketWrapper packet = createAndTransform(connection, packetType, packetWriter);
|
||||||
|
return packet.isCancelled() ? null : packet;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean createAndSend(UserConnection connection, PacketType packetType, Consumer<PacketWrapper> packetWriter, boolean currentThread) throws Exception {
|
||||||
|
PacketWrapper packet = createAndTransform(connection, packetType, packetWriter);
|
||||||
|
if (!packet.isCancelled()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentThread) {
|
||||||
|
if (packetType.direction() == Direction.CLIENTBOUND) {
|
||||||
|
packet.sendRaw();
|
||||||
|
} else {
|
||||||
|
packet.sendToServerRaw();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (packetType.direction() == Direction.CLIENTBOUND) {
|
||||||
|
packet.scheduleSendRaw();
|
||||||
|
} else {
|
||||||
|
packet.scheduleSendToServerRaw();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private PacketWrapper createAndTransform(UserConnection connection, PacketType packetType, Consumer<PacketWrapper> packetWriter) throws Exception {
|
||||||
|
// If clientbound: Constructor given inputProtocolVersion → Client version
|
||||||
|
// If serverbound: Constructor given inputProtocolVersion → Server version
|
||||||
|
boolean clientbound = packetType.direction() == Direction.CLIENTBOUND;
|
||||||
|
int serverProtocolVersion = clientbound ? this.inputProtocolVersion : connection.getProtocolInfo().getServerProtocolVersion();
|
||||||
|
int clientProtocolVersion = clientbound ? connection.getProtocolInfo().getProtocolVersion() : this.inputProtocolVersion;
|
||||||
|
|
||||||
|
// Construct protocol pipeline
|
||||||
|
List<ProtocolPathEntry> path = Via.getManager().getProtocolManager().getProtocolPath(clientProtocolVersion, serverProtocolVersion);
|
||||||
|
List<Protocol> protocolList = null;
|
||||||
|
if (path != null) {
|
||||||
|
protocolList = new ArrayList<>(path.size());
|
||||||
|
for (ProtocolPathEntry entry : path) {
|
||||||
|
protocolList.add(entry.getProtocol());
|
||||||
|
}
|
||||||
|
} else if (serverProtocolVersion != clientProtocolVersion) {
|
||||||
|
throw new RuntimeException("No protocol path between client version " + clientProtocolVersion + " and server version " + serverProtocolVersion);
|
||||||
|
}
|
||||||
|
|
||||||
|
PacketWrapper packet = PacketWrapper.create(packetType, connection);
|
||||||
|
packetWriter.accept(packet);
|
||||||
|
if (protocolList != null) {
|
||||||
|
// Reset reader and apply pipeline
|
||||||
|
packet.resetReader();
|
||||||
|
|
||||||
|
try {
|
||||||
|
packet.apply(packetType.direction(), State.PLAY, 0, protocolList, clientbound);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new Exception("Exception trying to transform packet between client version " + clientProtocolVersion
|
||||||
|
+ " and server version " + serverProtocolVersion + ". Are you sure you used the correct input version and packet write types?", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return packet;
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards
|
* This file is part of ViaVerion - https://github.com/ViaVersion/ViaBackwards
|
||||||
* Copyright (C) 2016-2021 ViaVersion and contributors
|
* Copyright (C) 2016-2021 ViaVersion and contributors
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards
|
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaBackwards
|
||||||
* Copyright (C) 2016-2021 ViaVersion and contributors
|
* Copyright (C) 2016-2021 ViaVersion and contributors
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards
|
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaBackwards
|
||||||
* Copyright (C) 2016-2021 ViaVersion and contributors
|
* Copyright (C) 2016-2021 ViaVersion and contributors
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@ -1,5 +1,5 @@
|
|||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1.1-bin.zip
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren