Simplify custom payload handling (#12347)

This commit is contained in:
Nassim Jahnke
2025-03-27 14:22:38 +01:00
committed by GitHub
parent c467df95a2
commit 9b1798d643
5 changed files with 59 additions and 55 deletions

View File

@@ -1,21 +1,26 @@
--- a/net/minecraft/network/protocol/common/custom/DiscardedPayload.java
+++ b/net/minecraft/network/protocol/common/custom/DiscardedPayload.java
@@ -4,13 +_,14 @@
@@ -4,13 +_,19 @@
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.ResourceLocation;
-public record DiscardedPayload(ResourceLocation id) implements CustomPacketPayload {
+public record DiscardedPayload(ResourceLocation id, io.netty.buffer.ByteBuf data) implements CustomPacketPayload { // CraftBukkit - store data
+public record DiscardedPayload(ResourceLocation id, byte[] data) implements CustomPacketPayload { // Paper - store data
public static <T extends FriendlyByteBuf> StreamCodec<T, DiscardedPayload> codec(ResourceLocation id, int maxSize) {
- return CustomPacketPayload.codec((value, output) -> {}, buffer -> {
+ return CustomPacketPayload.codec((value, output) -> {
+ output.writeBytes(value.data); // CraftBukkit - serialize
+ // Paper start
+ // Always write data
+ output.writeBytes(value.data);
+ }, buffer -> {
int i = buffer.readableBytes();
if (i >= 0 && i <= maxSize) {
- buffer.skipBytes(i);
- return new DiscardedPayload(id);
+ return new DiscardedPayload(id, buffer.readBytes(i)); // CraftBukkit
+ final byte[] data = new byte[i];
+ buffer.readBytes(data);
+ return new DiscardedPayload(id, data);
+ // Paper end
} else {
throw new IllegalArgumentException("Payload may not be larger than " + maxSize + " bytes");
}

View File

@@ -95,7 +95,7 @@
}
}
@@ -88,30 +_,119 @@
@@ -88,30 +_,123 @@
public void handlePong(ServerboundPongPacket packet) {
}
@@ -105,64 +105,68 @@
@Override
public void handleCustomPayload(ServerboundCustomPayloadPacket packet) {
- }
+ // CraftBukkit start
+ // Paper start - Brand support
+ // Paper start
+ if (packet.payload() instanceof net.minecraft.network.protocol.common.custom.BrandPayload(String brand)) {
+ this.player.clientBrandName = brand;
+ }
+ // Paper end - Brand support
+
+ if (!(packet.payload() instanceof final net.minecraft.network.protocol.common.custom.DiscardedPayload discardedPayload)) {
+ return;
+ }
+ PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel());
+ net.minecraft.resources.ResourceLocation identifier = packet.payload().type().id();
+ io.netty.buffer.ByteBuf payload = discardedPayload.data();
+
+ if (identifier.equals(ServerCommonPacketListenerImpl.CUSTOM_REGISTER)) {
+ try {
+ String channels = payload.toString(com.google.common.base.Charsets.UTF_8);
+ for (String channel : channels.split("\0")) {
+ this.getCraftPlayer().addChannel(channel);
+ }
+ } catch (Exception ex) {
+ ServerGamePacketListenerImpl.LOGGER.error("Couldn't register custom payload", ex);
+ this.disconnect(Component.literal("Invalid payload REGISTER!"), org.bukkit.event.player.PlayerKickEvent.Cause.INVALID_PAYLOAD); // Paper - kick event cause
+ }
+ } else if (identifier.equals(ServerCommonPacketListenerImpl.CUSTOM_UNREGISTER)) {
+ try {
+ String channels = payload.toString(com.google.common.base.Charsets.UTF_8);
+ for (String channel : channels.split("\0")) {
+ this.getCraftPlayer().removeChannel(channel);
+ }
+ } catch (Exception ex) {
+ ServerGamePacketListenerImpl.LOGGER.error("Couldn't unregister custom payload", ex);
+ this.disconnect(Component.literal("Invalid payload UNREGISTER!"), org.bukkit.event.player.PlayerKickEvent.Cause.INVALID_PAYLOAD); // Paper - kick event cause
+ }
+ } else {
+ try {
+ byte[] data = new byte[payload.readableBytes()];
+ payload.readBytes(data);
+ // Paper start - Brand support; Retain this incase upstream decides to 'break' the new mechanism in favour of backwards compat...
+ if (identifier.equals(MINECRAFT_BRAND)) {
+ try {
+ this.player.clientBrandName = new net.minecraft.network.FriendlyByteBuf(io.netty.buffer.Unpooled.copiedBuffer(data)).readUtf(256);
+ } catch (StringIndexOutOfBoundsException ex) {
+ this.player.clientBrandName = "illegal";
+ PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel());
+
+ final net.minecraft.resources.ResourceLocation identifier = packet.payload().type().id();
+ final byte[] data = discardedPayload.data();
+ try {
+ final boolean registerChannel = ServerCommonPacketListenerImpl.CUSTOM_REGISTER.equals(identifier);
+ if (registerChannel || ServerCommonPacketListenerImpl.CUSTOM_UNREGISTER.equals(identifier)) {
+ // Strings separated by zeros instead of length prefixes
+ int startIndex = 0;
+ for (int i = 0; i < data.length; i++) {
+ final byte b = data[i];
+ if (b != 0) {
+ continue;
+ }
+
+ readChannelIdentifier(data, startIndex, i, registerChannel);
+ startIndex = i + 1;
+ }
+ // Paper end - Brand support
+ this.cserver.getMessenger().dispatchIncomingMessage(this.player.getBukkitEntity(), identifier.toString(), data);
+ } catch (Exception ex) {
+ ServerGamePacketListenerImpl.LOGGER.error("Couldn't dispatch custom payload", ex);
+ this.disconnect(Component.literal("Invalid custom payload!"), org.bukkit.event.player.PlayerKickEvent.Cause.INVALID_PAYLOAD); // Paper - kick event cause
+
+ // Read the last one
+ readChannelIdentifier(data, startIndex, data.length, registerChannel);
+ return;
+ }
+
+ if (identifier.equals(MINECRAFT_BRAND)) {
+ this.player.clientBrandName = new net.minecraft.network.FriendlyByteBuf(io.netty.buffer.Unpooled.wrappedBuffer(data)).readUtf(256);
+ }
+
+ this.cserver.getMessenger().dispatchIncomingMessage(this.player.getBukkitEntity(), identifier.toString(), data);
+ } catch (final Exception e) {
+ ServerGamePacketListenerImpl.LOGGER.error("Couldn't handle custom payload on channel {}", identifier, e);
+ this.disconnect(Component.literal("Invalid custom payload payload!"), org.bukkit.event.player.PlayerKickEvent.Cause.INVALID_PAYLOAD); // Paper - kick event cause
+ }
+ }
+
+ private void readChannelIdentifier(final byte[] data, final int from, final int to, final boolean register) {
+ final int length = to - from;
+ if (length == 0) {
+ return;
+ }
+
+ final String channel = new String(data, from, length, java.nio.charset.StandardCharsets.US_ASCII);
+ if (register) {
+ this.getCraftPlayer().addChannel(channel);
+ } else {
+ this.getCraftPlayer().removeChannel(channel);
+ }
+ }
+
+ public final boolean isDisconnected() {
+ return (!this.player.joining && !this.connection.isConnected()) || this.processedDisconnect; // Paper - Fix duplication bugs
+ }
+ // CraftBukkit end
+ // Paper end
@Override
public void handleResourcePackResponse(ServerboundResourcePackPacket packet) {

View File

@@ -1,7 +1,7 @@
--- a/net/minecraft/world/inventory/HorseInventoryMenu.java
+++ b/net/minecraft/world/inventory/HorseInventoryMenu.java
@@ -19,9 +_,23 @@
private final AbstractHorse horse;
public final AbstractHorse horse;
public static final int SLOT_BODY_ARMOR = 1;
private static final int SLOT_HORSE_INVENTORY_START = 2;
+ // CraftBukkit start