From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: ayunami2000 Date: Mon, 28 Mar 2022 17:02:21 -0400 Subject: [PATCH] Block server-side chunkbans diff --git a/src/main/java/net/minecraft/network/PacketEncoder.java b/src/main/java/net/minecraft/network/PacketEncoder.java index b039a32b805fc02033fa862a1c40c4a51639e69a..b2227f01bc90351a4a3f73fa835dd89bde15632e 100644 --- a/src/main/java/net/minecraft/network/PacketEncoder.java +++ b/src/main/java/net/minecraft/network/PacketEncoder.java @@ -5,8 +5,22 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder; import io.papermc.paper.adventure.PaperAdventure; // Paper import java.io.IOException; +import java.util.Collections; +import net.minecraft.ChatFormatting; +import net.minecraft.core.NonNullList; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.chat.Component; import net.minecraft.network.protocol.Packet; import net.minecraft.network.protocol.PacketFlow; +import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket; +import net.minecraft.network.protocol.game.ClientboundChatPacket; +import net.minecraft.network.protocol.game.ClientboundContainerSetContentPacket; +import net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket; +import net.minecraft.network.protocol.game.ClientboundLevelChunkPacket; +import net.minecraft.network.protocol.game.ClientboundMapItemDataPacket; +import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket; +import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket; +import net.minecraft.world.item.ItemStack; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Marker; @@ -21,6 +35,23 @@ public class PacketEncoder extends MessageToByteEncoder> { this.flow = side; } + // Scissors start + private static int tryWrite(Packet packet, FriendlyByteBuf friendlyByteBuf, ChannelHandlerContext channelHandlerContext, int i) { + friendlyByteBuf.writeVarInt(i); + friendlyByteBuf.adventure$locale = channelHandlerContext.channel().attr(PaperAdventure.LOCALE_ATTRIBUTE).get(); + + int j = friendlyByteBuf.writerIndex(); + packet.write(friendlyByteBuf); + int k = friendlyByteBuf.writerIndex() - j; + int packetLength = friendlyByteBuf.readableBytes(); + if (k > 8388608 || packetLength > MAX_PACKET_SIZE) { + throw new SkipPacketException(new IllegalArgumentException("Packet too big (is " + k + "): " + packet)); + } + + return k; + } + // Scissors end + @Override protected void encode(ChannelHandlerContext channelHandlerContext, Packet packet, ByteBuf byteBuf) throws Exception { ConnectionProtocol connectionProtocol = channelHandlerContext.channel().attr(Connection.ATTRIBUTE_PROTOCOL).get(); @@ -36,35 +67,68 @@ public class PacketEncoder extends MessageToByteEncoder> { throw new IOException("Can't serialize unregistered packet"); } else { FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(byteBuf); - friendlyByteBuf.writeVarInt(integer); - friendlyByteBuf.adventure$locale = channelHandlerContext.channel().attr(PaperAdventure.LOCALE_ATTRIBUTE).get(); // Paper - + // Scissors start + int k; try { - int i = friendlyByteBuf.writerIndex(); - packet.write(friendlyByteBuf); - int j = friendlyByteBuf.writerIndex() - i; - if (j > 8388608) { - throw new IllegalArgumentException("Packet too big (is " + j + ", should be less than 8388608): " + packet); - } + k = tryWrite(packet, friendlyByteBuf, channelHandlerContext, integer); } catch (Throwable var9) { - LOGGER.error("Packet encoding of packet ID {} threw (skippable? {})", integer, packet.isSkippable(), var9); // Paper - WHAT WAS IT? WHO DID THIS TO YOU? WHAT DID YOU SEE? - if (packet.isSkippable()) { - throw new SkipPacketException(var9); - } else { - throw var9; + packet = capPacket(packet, integer); + if (packet == null) { + throw new SkipPacketException(new IllegalArgumentException("Packet too big: " + packet)); } + friendlyByteBuf.clear(); + k = tryWrite(packet, friendlyByteBuf, channelHandlerContext, integer); } - - // Paper start - int packetLength = friendlyByteBuf.readableBytes(); - if (packetLength > MAX_PACKET_SIZE) { - throw new PacketTooLargeException(packet, packetLength); - } - // Paper end + // Scissors end } } } + // Scissors start + private static Packet capPacket(Packet packet, int i) + { + if (packet instanceof ClientboundBlockEntityDataPacket blockEntityDataPacket) + { + packet = new ClientboundBlockEntityDataPacket(blockEntityDataPacket.getPos(), blockEntityDataPacket.getType(), new CompoundTag()); + } + else if (packet instanceof ClientboundLevelChunkPacket chunkPacket) + { + chunkPacket.clearNBT(); + } + else if (packet instanceof ClientboundSetEntityDataPacket) + { + return null; // Skip + } + else if (packet instanceof ClientboundContainerSetContentPacket containerSetContentPacket) + { + packet = new ClientboundContainerSetContentPacket(containerSetContentPacket.getContainerId(), containerSetContentPacket.getStateId(), NonNullList.create(), ItemStack.EMPTY); + } + else if (packet instanceof ClientboundSetEquipmentPacket setEquipmentPacket) + { + packet = new ClientboundSetEquipmentPacket(setEquipmentPacket.getEntity(), Collections.emptyList()); + } + else if (packet instanceof ClientboundContainerSetSlotPacket containerSetSlotPacket) + { + packet = new ClientboundContainerSetSlotPacket(containerSetSlotPacket.getContainerId(), containerSetSlotPacket.getStateId(), containerSetSlotPacket.getSlot(), ItemStack.EMPTY); + } + else if (packet instanceof ClientboundMapItemDataPacket mapItemDataPacket) + { + packet = new ClientboundMapItemDataPacket(mapItemDataPacket.getMapId(), mapItemDataPacket.getScale(), mapItemDataPacket.isLocked(), null, null); + } + else if (packet instanceof ClientboundChatPacket chatPacket) + { + return null; + } + else + { + LOGGER.error("Packet with ID {} was too large and was not caught. Please report this to the Scissors developers.", i); + return null; + } + + return packet; + } + // Scissors end + // Paper start private static int MAX_PACKET_SIZE = 2097152; diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacket.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacket.java index 60d72e488bc77cd913328be400ca374a873b4561..1af2260e7bab81fca24784172dd31e06e9adb1e1 100644 --- a/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacket.java +++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacket.java @@ -50,6 +50,13 @@ public class ClientboundLevelChunkPacket implements Packet