Scissors/patches/server/0013-Block-server-side-chunkbans.patch

181 lines
10 KiB
Diff
Raw Normal View History

2022-07-27 23:57:50 -04:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: ayunami2000 <spwilliamsiam@gmail.com>
Date: Mon, 28 Mar 2022 17:02:21 -0400
Subject: [PATCH] Block server-side chunkbans
2022-07-27 23:57:50 -04:00
diff --git a/src/main/java/net/minecraft/network/PacketEncoder.java b/src/main/java/net/minecraft/network/PacketEncoder.java
2023-11-04 22:36:47 -04:00
index d364bd57b1675c8b21d781c2bc16c3e65800455c..f2e7585630ff724f966c7bcedc17e502d786870d 100644
2022-07-27 23:57:50 -04:00
--- a/src/main/java/net/minecraft/network/PacketEncoder.java
+++ b/src/main/java/net/minecraft/network/PacketEncoder.java
2023-10-15 18:45:13 -04:00
@@ -7,8 +7,16 @@ import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;
2022-07-27 23:57:50 -04:00
import java.io.IOException;
+import java.util.Collections; // Scissors
+import net.minecraft.ChatFormatting; // Scissors
+import net.minecraft.core.NonNullList; // Scissors
+import net.minecraft.nbt.CompoundTag; // Scissors
+import net.minecraft.network.chat.Component; // Scissors
+import net.minecraft.network.chat.SignedMessageBody; // Scissors
2022-07-27 23:57:50 -04:00
import net.minecraft.network.protocol.Packet;
+import net.minecraft.network.protocol.game.*; // Scissors
2022-07-27 23:57:50 -04:00
import net.minecraft.util.profiling.jfr.JvmProfiler;
+import net.minecraft.world.item.ItemStack; // Scissors
2022-07-27 23:57:50 -04:00
import org.slf4j.Logger;
public class PacketEncoder extends MessageToByteEncoder<Packet<?>> {
@@ -19,6 +27,23 @@ public class PacketEncoder extends MessageToByteEncoder<Packet<?>> {
2023-10-15 18:45:13 -04:00
this.codecKey = protocolKey;
2023-04-08 15:10:07 -04:00
}
+ // Scissors start
2023-10-15 18:45:13 -04:00
+ private int tryWrite(Packet packet, FriendlyByteBuf friendlyByteBuf, ChannelHandlerContext channelHandlerContext, int i) {
2023-04-08 15:10:07 -04:00
+ friendlyByteBuf.writeVarInt(i);
2023-10-15 18:45:13 -04:00
+ friendlyByteBuf.adventure$locale = channelHandlerContext.channel().attr(io.papermc.paper.adventure.PaperAdventure.LOCALE_ATTRIBUTE).get(); // Paper
2023-04-08 15:10:07 -04:00
+
+ int j = friendlyByteBuf.writerIndex();
+ packet.write(friendlyByteBuf);
+ int k = friendlyByteBuf.writerIndex() - j;
+ int packetLength = friendlyByteBuf.readableBytes();
2023-10-15 18:45:13 -04:00
+ if (packetLength > MAX_PACKET_SIZE) {
+ throw new PacketTooLargeException(packet, this.codecKey, packetLength);
2023-04-08 15:10:07 -04:00
+ }
+
+ return k;
+ }
+ // Scissors end
+
protected void encode(ChannelHandlerContext channelHandlerContext, Packet<?> packet, ByteBuf byteBuf) throws Exception {
2023-10-15 18:45:13 -04:00
Attribute<ConnectionProtocol.CodecData<?>> attribute = channelHandlerContext.channel().attr(this.codecKey);
ConnectionProtocol.CodecData<?> codecData = attribute.get();
2023-11-04 22:36:47 -04:00
@@ -34,46 +59,59 @@ public class PacketEncoder extends MessageToByteEncoder<Packet<?>> {
2023-04-08 15:10:07 -04:00
throw new IOException("Can't serialize unregistered packet");
} else {
FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(byteBuf);
- friendlyByteBuf.writeVarInt(i);
2023-10-15 18:45:13 -04:00
- friendlyByteBuf.adventure$locale = channelHandlerContext.channel().attr(io.papermc.paper.adventure.PaperAdventure.LOCALE_ATTRIBUTE).get(); // Paper
2023-06-08 20:58:46 -04:00
2023-04-08 15:10:07 -04:00
+ // Scissors start
+ int k;
try {
- int j = friendlyByteBuf.writerIndex();
- packet.write(friendlyByteBuf);
- int k = friendlyByteBuf.writerIndex() - j;
2023-04-28 22:43:33 -04:00
- if (false && k > 8388608) { // Paper - disable
2023-03-15 23:57:35 -04:00
- throw new IllegalArgumentException("Packet too big (is " + k + ", should be less than 8388608): " + packet);
2023-04-08 15:10:07 -04:00
- }
2023-10-15 18:45:13 -04:00
-
- JvmProfiler.INSTANCE.onPacketSent(codecData.protocol(), i, channelHandlerContext.channel().remoteAddress(), k);
+ k = this.tryWrite(packet, friendlyByteBuf, channelHandlerContext, i);
} catch (Throwable var13) {
2023-11-04 22:36:47 -04:00
- // Paper start - Give proper error message
- String packetName = io.papermc.paper.util.ObfHelper.INSTANCE.deobfClassName(packet.getClass().getName());
- if (packetName.contains(".")) {
- packetName = packetName.substring(packetName.lastIndexOf(".") + 1);
- }
-
- LOGGER.error("Packet encoding of packet {} (ID: {}) threw (skippable? {})", packetName, i, packet.isSkippable(), var13);
- // Paper end
2022-07-27 23:57:50 -04:00
- if (packet.isSkippable()) {
2023-10-15 18:45:13 -04:00
- throw new SkipPacketException(var13);
2023-11-04 22:36:47 -04:00
- }
2023-10-15 18:45:13 -04:00
-
- throw var13;
- } finally {
- // Paper start
- int packetLength = friendlyByteBuf.readableBytes();
- if (packetLength > MAX_PACKET_SIZE) {
- throw new PacketTooLargeException(packet, this.codecKey, packetLength);
2023-11-04 22:36:47 -04:00
+ packet = capPacket(packet, i);
+ if (packet == null) {
+ throw new SkipPacketException(new IllegalArgumentException("Packet too big: " + packet));
}
2023-10-15 18:45:13 -04:00
- // Paper end
- ProtocolSwapHandler.swapProtocolIfNeeded(attribute, packet);
2023-04-08 15:10:07 -04:00
+ friendlyByteBuf.clear();
2023-10-15 18:45:13 -04:00
+ k = this.tryWrite(packet, friendlyByteBuf, channelHandlerContext, i);
2022-07-27 23:57:50 -04:00
}
2023-10-15 18:45:13 -04:00
+ JvmProfiler.INSTANCE.onPacketSent(codecData.protocol(), i, channelHandlerContext.channel().remoteAddress(), k);
+ ProtocolSwapHandler.swapProtocolIfNeeded(attribute, packet);
2023-04-08 15:10:07 -04:00
+ // Scissors end
2022-07-27 23:57:50 -04:00
}
}
}
+ // Scissors start
2023-06-08 20:58:46 -04:00
+ private static Packet capPacket(Packet packet, int i) {
+ if (packet instanceof ClientboundBlockEntityDataPacket blockEntityDataPacket) {
2022-07-27 23:57:50 -04:00
+ packet = new ClientboundBlockEntityDataPacket(blockEntityDataPacket.getPos(), blockEntityDataPacket.getType(), new CompoundTag());
2023-06-08 20:58:46 -04:00
+ } else if (packet instanceof ClientboundLevelChunkPacketData chunkPacket) {
2022-07-27 23:57:50 -04:00
+ chunkPacket.clearNBT();
2023-06-08 20:58:46 -04:00
+ } else if (packet instanceof ClientboundContainerSetContentPacket containerSetContentPacket) {
2023-04-08 15:10:07 -04:00
+ packet = new ClientboundContainerSetContentPacket(containerSetContentPacket.getContainerId(), containerSetContentPacket.getStateId(), NonNullList.create(), ItemStack.EMPTY);
2023-06-08 20:58:46 -04:00
+ } else if (packet instanceof ClientboundSetEquipmentPacket setEquipmentPacket) {
2023-04-08 15:10:07 -04:00
+ packet = new ClientboundSetEquipmentPacket(setEquipmentPacket.getEntity(), Collections.emptyList());
2023-06-08 20:58:46 -04:00
+ } else if (packet instanceof ClientboundContainerSetSlotPacket containerSetSlotPacket) {
2023-04-08 15:10:07 -04:00
+ packet = new ClientboundContainerSetSlotPacket(containerSetSlotPacket.getContainerId(), containerSetSlotPacket.getStateId(), containerSetSlotPacket.getSlot(), ItemStack.EMPTY);
2023-06-08 20:58:46 -04:00
+ } else if (packet instanceof ClientboundMapItemDataPacket mapItemDataPacket) {
2022-07-27 23:57:50 -04:00
+ packet = new ClientboundMapItemDataPacket(mapItemDataPacket.getMapId(), mapItemDataPacket.getScale(), mapItemDataPacket.isLocked(), null, null);
2023-06-08 20:58:46 -04:00
+ } else if (packet instanceof ClientboundPlayerChatPacket playerChatPacket) {
2023-04-08 15:10:07 -04:00
+ final SignedMessageBody.Packed body = playerChatPacket.body();
+ packet = new ClientboundPlayerChatPacket(playerChatPacket.sender(), // Not sending this packet results in a kick when someone says something.
+ playerChatPacket.index(),
+ playerChatPacket.signature(),
+ playerChatPacket.body(),
+ Component.empty().append("** Message too large **").withStyle(ChatFormatting.RED),
+ playerChatPacket.filterMask(),
+ playerChatPacket.chatType()
+ );
2023-06-08 20:58:46 -04:00
+ } else {
2023-04-08 15:10:07 -04:00
+ return null;
2022-07-27 23:57:50 -04:00
+ }
2023-04-08 15:10:07 -04:00
+
+ return packet;
2022-07-27 23:57:50 -04:00
+ }
+ // Scissors end
+
// Paper start
2023-04-28 22:43:33 -04:00
private static int MAX_PACKET_SIZE = 8388608;
2022-07-27 23:57:50 -04:00
diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundBlockEntityDataPacket.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundBlockEntityDataPacket.java
2023-04-08 15:10:07 -04:00
index 3944852921335c78a04a9dc301882ab5b152b1ed..96ee53c7cc862e059328c5cdf5e07f309df6a79e 100644
2022-07-27 23:57:50 -04:00
--- a/src/main/java/net/minecraft/network/protocol/game/ClientboundBlockEntityDataPacket.java
+++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundBlockEntityDataPacket.java
2023-04-08 15:10:07 -04:00
@@ -24,7 +24,7 @@ public class ClientboundBlockEntityDataPacket implements Packet<ClientGamePacket
2022-07-27 23:57:50 -04:00
return create(blockEntity, BlockEntity::getUpdateTag);
}
- private ClientboundBlockEntityDataPacket(BlockPos pos, BlockEntityType<?> blockEntityType, CompoundTag nbt) {
2023-04-08 15:10:07 -04:00
+ public ClientboundBlockEntityDataPacket(BlockPos pos, BlockEntityType<?> blockEntityType, CompoundTag nbt) { // Scissors - private -> public
2022-07-27 23:57:50 -04:00
this.pos = pos;
this.type = blockEntityType;
this.tag = nbt.isEmpty() ? null : nbt;
diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java
2023-10-15 18:45:13 -04:00
index 51e24105facfe71ce9f2757c6c881a21b58dacfd..5692fbae221fb01d32d92edc7bea0f6312e24e1c 100644
2022-07-27 23:57:50 -04:00
--- a/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java
+++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java
2023-06-08 20:58:46 -04:00
@@ -33,6 +33,13 @@ public class ClientboundLevelChunkPacketData {
2022-07-27 23:57:50 -04:00
}
// Paper end
+ // Scissors start
2023-06-08 20:58:46 -04:00
+ public void clearNBT() {
2022-07-27 23:57:50 -04:00
+ this.blockEntitiesData.clear();
+ this.extraPackets.clear();
+ }
+ // Scissors end
+
2023-06-09 02:24:06 -04:00
// Paper start - Anti-Xray - Add chunk packet info
@Deprecated @io.papermc.paper.annotation.DoNotUse public ClientboundLevelChunkPacketData(LevelChunk chunk) { this(chunk, null); }
public ClientboundLevelChunkPacketData(LevelChunk chunk, com.destroystokyo.paper.antixray.ChunkPacketInfo<net.minecraft.world.level.block.state.BlockState> chunkPacketInfo) {
@@ -58,6 +65,7 @@ public class ClientboundLevelChunkPacketData {
int totalTileEntities = 0; // Paper
for(Map.Entry<BlockPos, BlockEntity> entry2 : chunk.getBlockEntities().entrySet()) {
+ if (this.extraPackets.size() > 50) break; // Scissors - Limit extraPackets size
// Paper start
if (++totalTileEntities > TE_LIMIT) {
var packet = entry2.getValue().getUpdatePacket();