Skip to content

Commit 50dfe9f

Browse files
Implement ViaPlatform#sendCustomPayload - Add PacketTypeUtil for getting packet ids
1 parent 47728fe commit 50dfe9f

File tree

6 files changed

+157
-29
lines changed

6 files changed

+157
-29
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ repositories {
5050
}
5151

5252
dependencies {
53-
compileOnly "com.viaversion:viaversion-common:5.2.1"
54-
compileOnly "com.viaversion:viabackwards-common:5.2.1"
53+
compileOnly "com.viaversion:viaversion-common:5.2.2-SNAPSHOT"
54+
compileOnly "com.viaversion:viabackwards-common:5.2.2-SNAPSHOT"
5555
compileOnly "com.viaversion:viarewind-common:4.0.5"
5656
compileOnly "net.raphimc:ViaLegacy:3.0.7"
5757
compileOnly "com.viaversion:viaaprilfools-common:4.0.0"

src/main/java/com/viaversion/vialoader/impl/platform/ViaVersionPlatformImpl.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,19 @@
2525
import com.viaversion.vialoader.impl.viaversion.VLApiBase;
2626
import com.viaversion.vialoader.impl.viaversion.VLViaConfig;
2727
import com.viaversion.vialoader.util.JLoggerToSLF4J;
28+
import com.viaversion.vialoader.util.PacketTypeUtil;
2829
import com.viaversion.vialoader.util.VLTask;
2930
import com.viaversion.viaversion.api.Via;
3031
import com.viaversion.viaversion.api.ViaAPI;
3132
import com.viaversion.viaversion.api.command.ViaCommandSender;
3233
import com.viaversion.viaversion.api.configuration.ViaVersionConfig;
3334
import com.viaversion.viaversion.api.connection.UserConnection;
3435
import com.viaversion.viaversion.api.platform.ViaPlatform;
36+
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
37+
import com.viaversion.viaversion.api.type.Types;
3538
import com.viaversion.viaversion.configuration.AbstractViaConfig;
3639
import com.viaversion.viaversion.libs.gson.JsonObject;
40+
import com.viaversion.viaversion.protocols.base.InitialBaseProtocol;
3741
import com.viaversion.viaversion.util.VersionInfo;
3842
import org.slf4j.LoggerFactory;
3943

@@ -120,6 +124,23 @@ public boolean kickPlayer(UUID uuid, String s) {
120124
return false;
121125
}
122126

127+
@Override
128+
public void sendCustomPayload(UUID uuid, String channel, String message) {
129+
UserConnection connection = Via.getManager().getConnectionManager().getConnectedClient(uuid);
130+
if (connection == null) {
131+
// The connection field will always be null on clientside platforms, get the first connection instead
132+
connection = Via.getManager().getConnectionManager().getConnections().stream().findFirst().orElse(null);
133+
}
134+
135+
if (connection != null) {
136+
final PacketWrapper packet = PacketWrapper.create(PacketTypeUtil.getServerboundPacketType("CUSTOM_PAYLOAD", connection), connection);
137+
packet.write(Types.STRING, channel);
138+
packet.write(Types.REMAINING_BYTES, message.getBytes());
139+
140+
packet.sendToServer(InitialBaseProtocol.class);
141+
}
142+
}
143+
123144
@Override
124145
public boolean hasPlugin(String s) {
125146
return false; // Used for ViaPlatform#getUnsupportedSoftwareClasses

src/main/java/com/viaversion/vialoader/netty/ViaCodec.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,34 @@
3434

3535
public class ViaCodec extends ByteToMessageCodec<ByteBuf> {
3636

37-
protected final UserConnection user;
37+
protected final UserConnection connection;
3838

39-
public ViaCodec(final UserConnection user) {
40-
this.user = user;
39+
public ViaCodec(final UserConnection connection) {
40+
this.connection = connection;
4141
}
4242

4343
@Override
44-
protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception {
45-
if (!this.user.checkOutgoingPacket()) throw CancelEncoderException.generate(null);
44+
protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) {
45+
if (!this.connection.checkOutgoingPacket()) {
46+
throw CancelEncoderException.generate(null);
47+
}
4648

4749
out.writeBytes(in);
48-
if (this.user.shouldTransformPacket()) {
49-
this.user.transformOutgoing(out, CancelEncoderException::generate);
50+
if (this.connection.shouldTransformPacket()) {
51+
this.connection.transformOutgoing(out, CancelEncoderException::generate);
5052
}
5153
}
5254

5355
@Override
5456
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
55-
if (!this.user.checkIncomingPacket()) throw CancelDecoderException.generate(null);
57+
if (!this.connection.checkIncomingPacket()) {
58+
throw CancelDecoderException.generate(null);
59+
}
5660

5761
final ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(in);
5862
try {
59-
if (this.user.shouldTransformPacket()) {
60-
this.user.transformIncoming(transformedBuf, CancelDecoderException::generate);
63+
if (this.connection.shouldTransformPacket()) {
64+
this.connection.transformIncoming(transformedBuf, CancelDecoderException::generate);
6165
}
6266
out.add(transformedBuf.retain());
6367
} finally {
@@ -93,7 +97,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
9397
public boolean isSharable() {
9498
// Netty doesn't allow codecs to be shared, but we need it to be shared because of the pipeline reordering.
9599
// The check if it is sharable is done in the constructor and can be bypassed by returning false during that check.
96-
return this.user != null;
100+
return this.connection != null;
97101
}
98102

99103
}

src/main/java/com/viaversion/vialoader/netty/ViaDecoder.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,25 @@
3232

3333
public class ViaDecoder extends MessageToMessageDecoder<ByteBuf> {
3434

35-
protected final UserConnection user;
35+
protected final UserConnection connection;
3636

37-
public ViaDecoder(final UserConnection user) {
38-
this.user = user;
37+
public ViaDecoder(final UserConnection connection) {
38+
this.connection = connection;
3939
}
4040

4141
@Override
42-
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
43-
if (!this.user.checkIncomingPacket()) throw CancelDecoderException.generate(null);
44-
if (!this.user.shouldTransformPacket()) {
42+
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
43+
if (!this.connection.checkIncomingPacket()) {
44+
throw CancelDecoderException.generate(null);
45+
}
46+
if (!this.connection.shouldTransformPacket()) {
4547
out.add(in.retain());
4648
return;
4749
}
4850

4951
final ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(in);
5052
try {
51-
this.user.transformIncoming(transformedBuf, CancelDecoderException::generate);
53+
this.connection.transformIncoming(transformedBuf, CancelDecoderException::generate);
5254
out.add(transformedBuf.retain());
5355
} finally {
5456
transformedBuf.release();
@@ -70,6 +72,6 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
7072
public boolean isSharable() {
7173
// Netty doesn't allow codecs to be shared, but we need it to be shared because of the pipeline reordering.
7274
// The check if it is sharable is done in the constructor and can be bypassed by returning false during that check.
73-
return this.user != null;
75+
return this.connection != null;
7476
}
7577
}

src/main/java/com/viaversion/vialoader/netty/ViaEncoder.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,25 @@
3333

3434
public class ViaEncoder extends MessageToMessageEncoder<ByteBuf> {
3535

36-
protected final UserConnection user;
36+
protected final UserConnection connection;
3737

38-
public ViaEncoder(final UserConnection user) {
39-
this.user = user;
38+
public ViaEncoder(final UserConnection connection) {
39+
this.connection = connection;
4040
}
4141

4242
@Override
43-
protected void encode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
44-
if (!this.user.checkOutgoingPacket()) throw CancelEncoderException.generate(null);
45-
if (!this.user.shouldTransformPacket()) {
43+
protected void encode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
44+
if (!this.connection.checkOutgoingPacket()) {
45+
throw CancelEncoderException.generate(null);
46+
}
47+
if (!this.connection.shouldTransformPacket()) {
4648
out.add(in.retain());
4749
return;
4850
}
4951

5052
final ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(in);
5153
try {
52-
this.user.transformOutgoing(transformedBuf, CancelEncoderException::generate);
54+
this.connection.transformOutgoing(transformedBuf, CancelEncoderException::generate);
5355
out.add(transformedBuf.retain());
5456
} finally {
5557
transformedBuf.release();
@@ -73,6 +75,6 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
7375
public boolean isSharable() {
7476
// Netty doesn't allow codecs to be shared, but we need it to be shared because of the pipeline reordering.
7577
// The check if it is sharable is done in the constructor and can be bypassed by returning false during that check.
76-
return this.user != null;
78+
return this.connection != null;
7779
}
7880
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* This file is part of ViaLoader - https://github.com/ViaVersion/ViaLoader
3+
* Copyright (C) 2020-2025 the original authors
4+
* - RK_01/RaphiMC
5+
* - FlorianMichael/EnZaXD <[email protected]>
6+
* Copyright (C) 2023-2025 ViaVersion and contributors
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
package com.viaversion.vialoader.util;
22+
23+
import com.viaversion.viaversion.api.Via;
24+
import com.viaversion.viaversion.api.connection.ProtocolInfo;
25+
import com.viaversion.viaversion.api.connection.UserConnection;
26+
import com.viaversion.viaversion.api.protocol.ProtocolPathEntry;
27+
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
28+
import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
29+
import com.viaversion.viaversion.api.protocol.packet.State;
30+
import com.viaversion.viaversion.api.protocol.packet.provider.PacketTypeMap;
31+
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
32+
33+
import java.util.List;
34+
35+
public class PacketTypeUtil {
36+
37+
public static ServerboundPacketType getServerboundPacketType(final String packetName, final UserConnection connection) {
38+
return getServerboundPacketType(State.PLAY, packetName, connection);
39+
}
40+
41+
public static ServerboundPacketType getServerboundPacketType(final State state, final String packetName, final UserConnection connection) {
42+
final ProtocolInfo protocolInfo = connection.getProtocolInfo();
43+
return getServerboundPacketType(state, packetName, protocolInfo.protocolVersion(), protocolInfo.serverProtocolVersion());
44+
}
45+
46+
/**
47+
* Returns the {@link ServerboundPacketType} for the given packet name, state, client version and server version. Note that this
48+
* function does not support packet mappings and will simply return null if the packet is not found in the server version.
49+
*
50+
* @param state The {@link State} of the packet
51+
* @param packetName The name of the packet
52+
* @param clientVersion The client version
53+
* @param serverVersion The server version
54+
* @return The {@link ServerboundPacketType} for the given packet name, state, client version and server version
55+
*/
56+
public static ServerboundPacketType getServerboundPacketType(final State state, final String packetName, final ProtocolVersion clientVersion, final ProtocolVersion serverVersion) {
57+
final List<ProtocolPathEntry> protocols = Via.getManager().getProtocolManager().getProtocolPath(clientVersion, serverVersion);
58+
if (protocols != null) {
59+
final ProtocolPathEntry last = protocols.get(protocols.size() - 1);
60+
final PacketTypeMap<? extends ServerboundPacketType> map = last.protocol().getPacketTypesProvider().mappedServerboundPacketTypes().get(state);
61+
if (map != null) {
62+
return map.typeByName(packetName);
63+
}
64+
}
65+
return null;
66+
}
67+
68+
public static ClientboundPacketType getClientboundPacketType(final String packetName, final UserConnection connection) {
69+
return getClientboundPacketType(State.PLAY, packetName, connection);
70+
}
71+
72+
public static ClientboundPacketType getClientboundPacketType(final State state, final String packetName, final UserConnection connection) {
73+
final ProtocolInfo protocolInfo = connection.getProtocolInfo();
74+
return getClientboundPacketType(state, packetName, protocolInfo.protocolVersion(), protocolInfo.serverProtocolVersion());
75+
}
76+
77+
/**
78+
* Returns the {@link ClientboundPacketType} for the given packet name, state, client version and server version. Note that this
79+
* function does not support packet mappings and will simply return null if the packet is not found in the server version.
80+
*
81+
* @param state The {@link State} of the packet
82+
* @param packetName The name of the packet
83+
* @param clientVersion The client version
84+
* @param serverVersion The server version
85+
* @return The {@link ClientboundPacketType} for the given packet name, state, client version and server version
86+
*/
87+
public static ClientboundPacketType getClientboundPacketType(final State state, final String packetName, final ProtocolVersion clientVersion, final ProtocolVersion serverVersion) {
88+
final List<ProtocolPathEntry> protocols = Via.getManager().getProtocolManager().getProtocolPath(clientVersion, serverVersion);
89+
if (protocols != null) {
90+
final ProtocolPathEntry last = protocols.get(protocols.size() - 1);
91+
final PacketTypeMap<? extends ClientboundPacketType> map = last.protocol().getPacketTypesProvider().mappedClientboundPacketTypes().get(state);
92+
if (map != null) {
93+
return map.typeByName(packetName);
94+
}
95+
}
96+
return null;
97+
}
98+
99+
}

0 commit comments

Comments
 (0)