mirror of
https://github.com/AtlasMediaGroup/Scissors.git
synced 2024-11-14 19:34:54 -05:00
Pushing here
This commit is contained in:
parent
c32bbf0728
commit
042c4d854f
12 changed files with 609 additions and 6 deletions
365
patches/server/0020-block-server-side-chunkbans.patch
Normal file
365
patches/server/0020-block-server-side-chunkbans.patch
Normal file
|
@ -0,0 +1,365 @@
|
|||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
||||
From: Telesphoreo <me@telesphoreo.me>
|
||||
Date: Fri, 22 Apr 2022 01:30:41 -0500
|
||||
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 5fce1177e7198d791d4ab1c64b394c5b1c145782..273af3a2e336c4dd23ef0028f51bcb5d3e9b26e4 100644
|
||||
--- a/src/main/java/net/minecraft/network/PacketEncoder.java
|
||||
+++ b/src/main/java/net/minecraft/network/PacketEncoder.java
|
||||
@@ -5,9 +5,17 @@ import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
import io.papermc.paper.adventure.PaperAdventure; // Paper
|
||||
+
|
||||
import java.io.IOException;
|
||||
+
|
||||
+import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.PacketFlow;
|
||||
+import net.minecraft.network.protocol.game.*;
|
||||
+import net.minecraft.network.syncher.SynchedEntityData;
|
||||
+import net.minecraft.server.MinecraftServer;
|
||||
+import net.minecraft.world.item.ItemStack;
|
||||
+import net.minecraft.world.level.chunk.LevelChunk;
|
||||
import net.minecraft.util.profiling.jfr.JvmProfiler;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
@@ -41,30 +49,93 @@ public class PacketEncoder extends MessageToByteEncoder<Packet<?>> {
|
||||
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);
|
||||
- } else {
|
||||
- int k = channelHandlerContext.channel().attr(Connection.ATTRIBUTE_PROTOCOL).get().getId();
|
||||
- JvmProfiler.INSTANCE.onPacketSent(k, integer, channelHandlerContext.channel().remoteAddress(), j);
|
||||
+ // Scissors start
|
||||
+ noKicking(friendlyByteBuf, packet, integer, channelHandlerContext);
|
||||
+ //throw new IllegalArgumentException("Packet too big (is " + j + ", should be less than 8388608): " + packet);
|
||||
+ // Scissors end
|
||||
}
|
||||
} catch (Throwable var10) {
|
||||
+ // Scissors start
|
||||
+ noKicking(friendlyByteBuf, packet, integer, channelHandlerContext);
|
||||
+ /*
|
||||
LOGGER.error("Packet encoding of packet ID {} threw (skippable? {})", integer, packet.isSkippable(), var10); // Paper - Give proper error message
|
||||
if (packet.isSkippable()) {
|
||||
throw new SkipPacketException(var10);
|
||||
} else {
|
||||
throw var10;
|
||||
}
|
||||
+ */
|
||||
+ // Scissors end
|
||||
}
|
||||
|
||||
// Paper start
|
||||
int packetLength = friendlyByteBuf.readableBytes();
|
||||
if (packetLength > MAX_PACKET_SIZE) {
|
||||
- throw new PacketTooLargeException(packet, packetLength);
|
||||
+ // Scissors start
|
||||
+ friendlyByteBuf.clear();
|
||||
+ noKicking(friendlyByteBuf, packet, integer, channelHandlerContext);
|
||||
+ packetLength = friendlyByteBuf.readableBytes();
|
||||
+ if (packetLength > MAX_PACKET_SIZE) {
|
||||
+ friendlyByteBuf.clear();
|
||||
+ //throw new PacketTooLargeException(packet, packetLength);
|
||||
+ }
|
||||
+ //throw new PacketTooLargeException(packet, packetLength);
|
||||
+ // Scissors end
|
||||
}
|
||||
// Paper end
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+ // Scissors start
|
||||
+ private static void noKicking(FriendlyByteBuf friendlyByteBuf, Packet packet, Integer integer, ChannelHandlerContext channelHandlerContext) {
|
||||
+ // no kicking!!
|
||||
+ friendlyByteBuf.clear();
|
||||
+ friendlyByteBuf.writeVarInt(integer);
|
||||
+ friendlyByteBuf.adventure$locale = channelHandlerContext.channel().attr(PaperAdventure.LOCALE_ATTRIBUTE).get(); // Paper
|
||||
+ boolean didIt = true;
|
||||
+ 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 entityDataPacket) {
|
||||
+ friendlyByteBuf.writeVarInt(entityDataPacket.getId());
|
||||
+ friendlyByteBuf.writeByte(255);
|
||||
+ didIt = false;//prevent default packet writing
|
||||
+ } else if (packet instanceof ClientboundContainerSetContentPacket containerSetContentPacket) {
|
||||
+ containerSetContentPacket.clearNbt();
|
||||
+ } else if (packet instanceof ClientboundSetEquipmentPacket setEquipmentPacket) {
|
||||
+ friendlyByteBuf.writeVarInt(setEquipmentPacket.getEntity());
|
||||
+ didIt = false;//prevent default
|
||||
+ } else if (packet instanceof ClientboundContainerSetSlotPacket containerSetSlotPacket) {
|
||||
+ //i really would rather cancel this packet entirely buuut idk how sOOOOoooo
|
||||
+ friendlyByteBuf.writeByte(containerSetSlotPacket.getContainerId());
|
||||
+ friendlyByteBuf.writeVarInt(containerSetSlotPacket.getStateId());
|
||||
+ friendlyByteBuf.writeShort(containerSetSlotPacket.getSlot());
|
||||
+ friendlyByteBuf.writeItem(ItemStack.EMPTY);
|
||||
+ didIt = false;//prevent default
|
||||
+ } else if (packet instanceof ClientboundMapItemDataPacket mapItemDataPacket) {
|
||||
+ packet = new ClientboundMapItemDataPacket(mapItemDataPacket.getMapId(),mapItemDataPacket.getScale(),mapItemDataPacket.isLocked(),null,null);
|
||||
+ } else {
|
||||
+ didIt = false;
|
||||
+ LOGGER.info(packet.getClass().getName() + " overflowed/errored and was not caught!!");
|
||||
+ }
|
||||
+ if (didIt) {
|
||||
+ try {
|
||||
+ int i = friendlyByteBuf.writerIndex();
|
||||
+ packet.write(friendlyByteBuf);
|
||||
+ int j = friendlyByteBuf.writerIndex() - i;
|
||||
+ if (j > 8388608) {
|
||||
+ friendlyByteBuf.clear();
|
||||
+ //throw new IllegalArgumentException("Packet too big (is " + j + ", should be less than 8388608): " + packet);
|
||||
+ }
|
||||
+ } catch (Throwable var69) {
|
||||
+ friendlyByteBuf.clear();
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+ // Scissors end
|
||||
+
|
||||
// Paper start
|
||||
private static int MAX_PACKET_SIZE = 2097152;
|
||||
|
||||
diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java
|
||||
index dbd8b9b09b82c1b75e8be9dc7416d9f0863c8c87..f71f68d1482f7e0481a95533e42e8ee5089f15ff 100644
|
||||
--- a/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java
|
||||
+++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java
|
||||
@@ -10,7 +10,14 @@ public class ClientboundContainerSetContentPacket implements Packet<ClientGamePa
|
||||
private final int containerId;
|
||||
private final int stateId;
|
||||
private final List<ItemStack> items;
|
||||
- private final ItemStack carriedItem;
|
||||
+ private ItemStack carriedItem; // Scissors - removed "final"
|
||||
+
|
||||
+ // Scissors start
|
||||
+ public void clearNbt(){
|
||||
+ this.items.clear();
|
||||
+ this.carriedItem = ItemStack.EMPTY;
|
||||
+ }
|
||||
+ // Scissors end
|
||||
|
||||
public ClientboundContainerSetContentPacket(int syncId, int revision, NonNullList<ItemStack> contents, ItemStack cursorStack) {
|
||||
this.containerId = syncId;
|
||||
diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacket.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacket.java
|
||||
new file mode 100644
|
||||
index 0000000000000000000000000000000000000000..89c385c5ec88c8b51f9e118b65f3b9c2a58c7d9b
|
||||
--- /dev/null
|
||||
+++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacket.java
|
||||
@@ -0,0 +1,212 @@
|
||||
+package net.minecraft.network.protocol.game;
|
||||
+
|
||||
+import com.google.common.collect.Lists;
|
||||
+import io.netty.buffer.ByteBuf;
|
||||
+import io.netty.buffer.Unpooled;
|
||||
+import java.util.BitSet;
|
||||
+import java.util.List;
|
||||
+import java.util.Map.Entry;
|
||||
+import net.minecraft.core.BlockPos;
|
||||
+import net.minecraft.nbt.CompoundTag;
|
||||
+import net.minecraft.nbt.LongArrayTag;
|
||||
+import net.minecraft.network.FriendlyByteBuf;
|
||||
+import net.minecraft.network.protocol.Packet;
|
||||
+import net.minecraft.world.level.ChunkPos;
|
||||
+import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
+import net.minecraft.world.level.chunk.ChunkBiomeContainer;
|
||||
+import net.minecraft.world.level.chunk.LevelChunk;
|
||||
+import net.minecraft.world.level.chunk.LevelChunkSection;
|
||||
+import net.minecraft.world.level.levelgen.Heightmap;
|
||||
+
|
||||
+public class ClientboundLevelChunkPacket implements Packet<ClientGamePacketListener> {
|
||||
+ public static final int TWO_MEGABYTES = 2097152;
|
||||
+ private final int x;
|
||||
+ private final int z;
|
||||
+ private final BitSet availableSections;
|
||||
+ private final CompoundTag heightmaps;
|
||||
+ private final int[] biomes;
|
||||
+ private final byte[] buffer;
|
||||
+ private final List<CompoundTag> blockEntitiesTags;
|
||||
+ // Paper start
|
||||
+ private final java.util.List<Packet> extraPackets = new java.util.ArrayList<>();
|
||||
+ private static final int TE_LIMIT = Integer.getInteger("Paper.excessiveTELimit", 750);
|
||||
+
|
||||
+ @Override
|
||||
+ public java.util.List<Packet> getExtraPackets() {
|
||||
+ return extraPackets;
|
||||
+ }
|
||||
+ // Paper end
|
||||
+
|
||||
+ // Paper start - Async-Anti-Xray - Ready flag for the connection
|
||||
+ private volatile boolean ready;
|
||||
+
|
||||
+ @Override
|
||||
+ public boolean isReady() {
|
||||
+ return this.ready;
|
||||
+ }
|
||||
+
|
||||
+ public void setReady(boolean ready) {
|
||||
+ this.ready = ready;
|
||||
+ }
|
||||
+ // Paper end
|
||||
+
|
||||
+ // Scissors start
|
||||
+ public void clearNbt() {
|
||||
+ this.blockEntitiesTags.clear();
|
||||
+ this.extraPackets.clear();
|
||||
+ }
|
||||
+ // Scissors end
|
||||
+
|
||||
+ // Paper start - Anti-Xray - Add chunk packet info
|
||||
+ @Deprecated public ClientboundLevelChunkPacket(LevelChunk chunk) { this(chunk, true); } // Notice for updates: Please make sure this constructor isn't used anywhere
|
||||
+ public ClientboundLevelChunkPacket(LevelChunk chunk, boolean modifyBlocks) {
|
||||
+ com.destroystokyo.paper.antixray.ChunkPacketInfo<net.minecraft.world.level.block.state.BlockState> chunkPacketInfo = modifyBlocks ? chunk.level.chunkPacketBlockController.getChunkPacketInfo(this, chunk) : null;
|
||||
+ // Paper end
|
||||
+ ChunkPos chunkPos = chunk.getPos();
|
||||
+ this.x = chunkPos.x;
|
||||
+ this.z = chunkPos.z;
|
||||
+ this.heightmaps = new CompoundTag();
|
||||
+
|
||||
+ for(Entry<Heightmap.Types, Heightmap> entry : chunk.getHeightmaps()) {
|
||||
+ if (entry.getKey().sendToClient()) {
|
||||
+ this.heightmaps.put(entry.getKey().getSerializationKey(), new LongArrayTag(entry.getValue().getRawData()));
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ this.biomes = chunk.getBiomes().writeBiomes();
|
||||
+ this.buffer = new byte[this.calculateChunkSize(chunk)];
|
||||
+
|
||||
+ // Paper start - Anti-Xray - Add chunk packet info
|
||||
+ if (chunkPacketInfo != null) {
|
||||
+ chunkPacketInfo.setBuffer(this.buffer);
|
||||
+ }
|
||||
+
|
||||
+ this.availableSections = this.extractChunkData(new FriendlyByteBuf(this.getWriteBuffer()), chunk, chunkPacketInfo);
|
||||
+ // Paper end
|
||||
+ this.blockEntitiesTags = Lists.newArrayList();
|
||||
+ int totalTileEntities = 0; // Paper
|
||||
+
|
||||
+ for(Entry<BlockPos, BlockEntity> entry2 : chunk.getBlockEntities().entrySet()) {
|
||||
+ BlockEntity blockEntity = entry2.getValue();
|
||||
+ // Paper start - improve oversized chunk data packet handling
|
||||
+ if (++totalTileEntities > TE_LIMIT) {
|
||||
+ ClientboundBlockEntityDataPacket updatePacket = blockEntity.getUpdatePacket();
|
||||
+ if (updatePacket != null) {
|
||||
+ this.extraPackets.add(updatePacket);
|
||||
+ continue;
|
||||
+ }
|
||||
+ }
|
||||
+ // Paper end
|
||||
+ CompoundTag compoundTag = blockEntity.getUpdateTag();
|
||||
+ if (blockEntity instanceof net.minecraft.world.level.block.entity.SkullBlockEntity) { net.minecraft.world.level.block.entity.SkullBlockEntity.sanitizeTileEntityUUID(compoundTag); } // Paper
|
||||
+ this.blockEntitiesTags.add(compoundTag);
|
||||
+ }
|
||||
+
|
||||
+ chunk.level.chunkPacketBlockController.modifyBlocks(this, chunkPacketInfo); // Paper - Anti-Xray - Modify blocks
|
||||
+ }
|
||||
+
|
||||
+ public ClientboundLevelChunkPacket(FriendlyByteBuf buf) {
|
||||
+ this.x = buf.readInt();
|
||||
+ this.z = buf.readInt();
|
||||
+ this.availableSections = buf.readBitSet();
|
||||
+ this.heightmaps = buf.readNbt();
|
||||
+ if (this.heightmaps == null) {
|
||||
+ throw new RuntimeException("Can't read heightmap in packet for [" + this.x + ", " + this.z + "]");
|
||||
+ } else {
|
||||
+ this.biomes = buf.readVarIntArray(ChunkBiomeContainer.MAX_SIZE);
|
||||
+ int i = buf.readVarInt();
|
||||
+ if (i > 2097152) { // Paper - diff on change - if this changes, update PacketEncoder
|
||||
+ throw new RuntimeException("Chunk Packet trying to allocate too much memory on read.");
|
||||
+ } else {
|
||||
+ this.buffer = new byte[i];
|
||||
+ buf.readBytes(this.buffer);
|
||||
+ this.blockEntitiesTags = buf.readList(FriendlyByteBuf::readNbt);
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public void write(FriendlyByteBuf buf) {
|
||||
+ buf.writeInt(this.x);
|
||||
+ buf.writeInt(this.z);
|
||||
+ buf.writeBitSet(this.availableSections);
|
||||
+ buf.writeNbt(this.heightmaps);
|
||||
+ buf.writeVarIntArray(this.biomes);
|
||||
+ buf.writeVarInt(this.buffer.length);
|
||||
+ buf.writeBytes(this.buffer);
|
||||
+ buf.writeCollection(this.blockEntitiesTags, FriendlyByteBuf::writeNbt);
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public void handle(ClientGamePacketListener listener) {
|
||||
+ listener.handleLevelChunk(this);
|
||||
+ }
|
||||
+
|
||||
+ public FriendlyByteBuf getReadBuffer() {
|
||||
+ return new FriendlyByteBuf(Unpooled.wrappedBuffer(this.buffer));
|
||||
+ }
|
||||
+
|
||||
+ private ByteBuf getWriteBuffer() {
|
||||
+ ByteBuf byteBuf = Unpooled.wrappedBuffer(this.buffer);
|
||||
+ byteBuf.writerIndex(0);
|
||||
+ return byteBuf;
|
||||
+ }
|
||||
+
|
||||
+ // Paper start - Anti-Xray - Add chunk packet info
|
||||
+ @Deprecated public BitSet extractChunkData(FriendlyByteBuf buf, LevelChunk chunk) { return extractChunkData(buf, chunk, null); } // Notice for updates: Please make sure this method isn't used anywhere
|
||||
+ public BitSet extractChunkData(FriendlyByteBuf buf, LevelChunk chunk, com.destroystokyo.paper.antixray.ChunkPacketInfo<net.minecraft.world.level.block.state.BlockState> chunkPacketInfo) {
|
||||
+ // Paper end
|
||||
+ BitSet bitSet = new BitSet();
|
||||
+ LevelChunkSection[] levelChunkSections = chunk.getSections();
|
||||
+ int i = 0;
|
||||
+
|
||||
+ for(int j = levelChunkSections.length; i < j; ++i) {
|
||||
+ LevelChunkSection levelChunkSection = levelChunkSections[i];
|
||||
+ if (levelChunkSection != LevelChunk.EMPTY_SECTION && !levelChunkSection.isEmpty()) {
|
||||
+ bitSet.set(i);
|
||||
+ levelChunkSection.write(buf, chunkPacketInfo); // Paper - Anti-Xray - Add chunk packet info
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ return bitSet;
|
||||
+ }
|
||||
+
|
||||
+ protected int calculateChunkSize(LevelChunk chunk) {
|
||||
+ int i = 0;
|
||||
+ LevelChunkSection[] levelChunkSections = chunk.getSections();
|
||||
+ int j = 0;
|
||||
+
|
||||
+ for(int k = levelChunkSections.length; j < k; ++j) {
|
||||
+ LevelChunkSection levelChunkSection = levelChunkSections[j];
|
||||
+ if (levelChunkSection != LevelChunk.EMPTY_SECTION && !levelChunkSection.isEmpty()) {
|
||||
+ i += levelChunkSection.getSerializedSize();
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ return i;
|
||||
+ }
|
||||
+
|
||||
+ public int getX() {
|
||||
+ return this.x;
|
||||
+ }
|
||||
+
|
||||
+ public int getZ() {
|
||||
+ return this.z;
|
||||
+ }
|
||||
+
|
||||
+ public BitSet getAvailableSections() {
|
||||
+ return this.availableSections;
|
||||
+ }
|
||||
+
|
||||
+ public CompoundTag getHeightmaps() {
|
||||
+ return this.heightmaps;
|
||||
+ }
|
||||
+
|
||||
+ public List<CompoundTag> getBlockEntitiesTags() {
|
||||
+ return this.blockEntitiesTags;
|
||||
+ }
|
||||
+
|
||||
+ public int[] getBiomes() {
|
||||
+ return this.biomes;
|
||||
+ }
|
||||
+}
|
|
@ -0,0 +1,41 @@
|
|||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
||||
From: Telesphoreo <me@telesphoreo.me>
|
||||
Date: Fri, 22 Apr 2022 01:24:05 -0500
|
||||
Subject: [PATCH] Validate coordinates before attempting to get block entities
|
||||
when handling Creative Inventory packets
|
||||
|
||||
|
||||
diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
|
||||
index 9df5b678ce4343d0bb54133393f6bbe40fe5366b..4186c5c880e94d858fa7e661a1c795269170cf31 100644
|
||||
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
|
||||
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
|
||||
@@ -2990,20 +2990,25 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Ser
|
||||
if (this.player.gameMode.isCreative()) {
|
||||
boolean flag = packet.getSlotNum() < 0;
|
||||
ItemStack itemstack = packet.getItem();
|
||||
- CompoundTag nbttagcompound = BlockItem.getBlockEntityData(itemstack);
|
||||
|
||||
+ CompoundTag nbttagcompound = BlockItem.getBlockEntityData(itemstack);
|
||||
if (!itemstack.isEmpty() && nbttagcompound != null && nbttagcompound.contains("x") && nbttagcompound.contains("y") && nbttagcompound.contains("z") && this.player.getBukkitEntity().hasPermission("minecraft.nbt.copy")) { // Spigot
|
||||
BlockPos blockposition = BlockEntity.getPosFromTag(nbttagcompound);
|
||||
// Paper start
|
||||
- BlockEntity tileentity = null;
|
||||
+ // Scissors start - Validate coordinates and whether or not the player can reach them
|
||||
+ if (Level.isInSpawnableBounds(blockposition) && !isOutsideOfReach(blockposition.getX(), blockposition.getY(), blockposition.getZ())) {
|
||||
+ BlockEntity tileentity = null;
|
||||
if (this.player.distanceToSqr(blockposition.getX(), blockposition.getY(), blockposition.getZ()) < 32 * 32 && this.player.getLevel().isLoadedAndInBounds(blockposition)) {
|
||||
tileentity = this.player.level.getBlockEntity(blockposition);
|
||||
}
|
||||
// Paper end
|
||||
|
||||
- if (tileentity != null) {
|
||||
- tileentity.saveToItem(itemstack);
|
||||
+ if (tileentity != null) {
|
||||
+ tileentity.saveToItem(
|
||||
+ itemstack);
|
||||
+ }
|
||||
}
|
||||
+ // Scissors end
|
||||
}
|
||||
|
||||
boolean flag1 = packet.getSlotNum() >= 1 && packet.getSlotNum() <= 45;
|
|
@ -0,0 +1,199 @@
|
|||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
||||
From: Telesphoreo <me@telesphoreo.me>
|
||||
Date: Fri, 22 Apr 2022 01:26:55 -0500
|
||||
Subject: [PATCH] Better handling of invalid JSON components
|
||||
|
||||
|
||||
diff --git a/src/main/java/net/minecraft/network/chat/Component.java b/src/main/java/net/minecraft/network/chat/Component.java
|
||||
index 984105c226f16746b43bb2d2932e0b87f3a6a70c..faa41d102c06b52bc758ebe3484820376c482093 100644
|
||||
--- a/src/main/java/net/minecraft/network/chat/Component.java
|
||||
+++ b/src/main/java/net/minecraft/network/chat/Component.java
|
||||
@@ -24,6 +24,8 @@ import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Optional;
|
||||
import javax.annotation.Nullable;
|
||||
+
|
||||
+import net.minecraft.ChatFormatting;
|
||||
import net.minecraft.Util;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.util.FormattedCharSequence;
|
||||
@@ -427,6 +429,26 @@ public interface Component extends Message, FormattedText, Iterable<Component> {
|
||||
return Component.Serializer.GSON.toJsonTree(text);
|
||||
}
|
||||
|
||||
+ // Scissors start
|
||||
+ @Nullable
|
||||
+ public static MutableComponent fromJsonSafe(String json) {
|
||||
+ try {
|
||||
+ return fromJson(json);
|
||||
+ } catch (Exception ex) {
|
||||
+ return new TextComponent("** Invalid JSON Component **").withStyle(ChatFormatting.RED);
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ @Nullable
|
||||
+ public static MutableComponent fromJsonSafe(JsonElement json) {
|
||||
+ try {
|
||||
+ return fromJson(json);
|
||||
+ } catch (Exception ex) {
|
||||
+ return new TextComponent("** Invalid JSON Component **").withStyle(ChatFormatting.RED);
|
||||
+ }
|
||||
+ }
|
||||
+ // Scissors end
|
||||
+
|
||||
@Nullable
|
||||
public static MutableComponent fromJson(String json) {
|
||||
return (MutableComponent) GsonHelper.fromJson(Component.Serializer.GSON, json, MutableComponent.class, false);
|
||||
diff --git a/src/main/java/net/minecraft/network/chat/HoverEvent.java b/src/main/java/net/minecraft/network/chat/HoverEvent.java
|
||||
index 63dbbc0f027ab66d183f81d6a8e0f9e0b85ea562..21ee39172b3efb6414d1cf8fbf36cd463194abab 100644
|
||||
--- a/src/main/java/net/minecraft/network/chat/HoverEvent.java
|
||||
+++ b/src/main/java/net/minecraft/network/chat/HoverEvent.java
|
||||
@@ -79,7 +79,7 @@ public class HoverEvent {
|
||||
if (jsonElement != null) {
|
||||
return action.deserialize(jsonElement);
|
||||
} else {
|
||||
- Component component = Component.Serializer.fromJson(json.get("value"));
|
||||
+ Component component = Component.Serializer.fromJsonSafe(json.get("value")); // Scissors - Use safer method for getting Components from JSON
|
||||
return component != null ? action.deserializeFromLegacy(component) : null;
|
||||
}
|
||||
}
|
||||
@@ -94,7 +94,7 @@ public class HoverEvent {
|
||||
}
|
||||
|
||||
public static class Action<T> {
|
||||
- public static final HoverEvent.Action<Component> SHOW_TEXT = new HoverEvent.Action<>("show_text", true, Component.Serializer::fromJson, Component.Serializer::toJsonTree, Function.identity());
|
||||
+ public static final HoverEvent.Action<Component> SHOW_TEXT = new HoverEvent.Action<>("show_text", true, Component.Serializer::fromJsonSafe, Component.Serializer::toJsonTree, Function.identity()); // Scissors - Use safer method for getting Components from JSON
|
||||
public static final HoverEvent.Action<HoverEvent.ItemStackInfo> SHOW_ITEM = new HoverEvent.Action<>("show_item", true, HoverEvent.ItemStackInfo::create, HoverEvent.ItemStackInfo::serialize, HoverEvent.ItemStackInfo::create);
|
||||
public static final HoverEvent.Action<HoverEvent.EntityTooltipInfo> SHOW_ENTITY = new HoverEvent.Action<>("show_entity", true, HoverEvent.EntityTooltipInfo::create, HoverEvent.EntityTooltipInfo::serialize, HoverEvent.EntityTooltipInfo::create);
|
||||
private static final Map<String, HoverEvent.Action<?>> LOOKUP = Stream.of(SHOW_TEXT, SHOW_ITEM, SHOW_ENTITY).collect(ImmutableMap.toImmutableMap(HoverEvent.Action::getName, (action) -> {
|
||||
@@ -182,7 +182,7 @@ public class HoverEvent {
|
||||
return null;
|
||||
}
|
||||
// Scissors end
|
||||
- Component component = Component.Serializer.fromJson(jsonObject.get("name"));
|
||||
+ Component component = Component.Serializer.fromJsonSafe(jsonObject.get("name")); // Scissors - Use safer method for getting Components from JSON
|
||||
return new HoverEvent.EntityTooltipInfo(entityType, uUID, component);
|
||||
}
|
||||
}
|
||||
@@ -191,7 +191,7 @@ public class HoverEvent {
|
||||
public static HoverEvent.EntityTooltipInfo create(Component text) {
|
||||
try {
|
||||
CompoundTag compoundTag = TagParser.parseTag(text.getString());
|
||||
- Component component = Component.Serializer.fromJson(compoundTag.getString("name"));
|
||||
+ Component component = Component.Serializer.fromJsonSafe(compoundTag.getString("name")); // Scissors - Use safer method for getting Components from JSON
|
||||
EntityType<?> entityType = Registry.ENTITY_TYPE.get(new ResourceLocation(compoundTag.getString("type")));
|
||||
// Scissors start
|
||||
UUID uUID;
|
||||
diff --git a/src/main/java/net/minecraft/network/chat/NbtComponent.java b/src/main/java/net/minecraft/network/chat/NbtComponent.java
|
||||
index e3dc56de3f91e03b1543257f72448a734d914ed7..f518e47818a47be0dfd7cccc4845ba9736c6cd8b 100644
|
||||
--- a/src/main/java/net/minecraft/network/chat/NbtComponent.java
|
||||
+++ b/src/main/java/net/minecraft/network/chat/NbtComponent.java
|
||||
@@ -78,13 +78,14 @@ public abstract class NbtComponent extends BaseComponent implements ContextAware
|
||||
if (this.interpreting) {
|
||||
Component component = DataFixUtils.orElse(ComponentUtils.updateForEntity(source, this.separator, sender, depth), ComponentUtils.DEFAULT_NO_STYLE_SEPARATOR);
|
||||
return stream.flatMap((text) -> {
|
||||
+ // Scissors start - Use safer method for getting Components from JSON
|
||||
try {
|
||||
- MutableComponent mutableComponent = Component.Serializer.fromJson(text);
|
||||
+ MutableComponent mutableComponent = Component.Serializer.fromJsonSafe(text);
|
||||
return Stream.of(ComponentUtils.updateForEntity(source, mutableComponent, sender, depth));
|
||||
} catch (Exception var5) {
|
||||
- LOGGER.warn("Failed to parse component: {}", text, var5);
|
||||
return Stream.of();
|
||||
}
|
||||
+ // Scissors end
|
||||
}).reduce((accumulator, current) -> {
|
||||
return accumulator.append(component).append(current);
|
||||
}).orElseGet(() -> {
|
||||
@@ -95,7 +96,7 @@ public abstract class NbtComponent extends BaseComponent implements ContextAware
|
||||
return stream.map((string) -> {
|
||||
return new TextComponent(string);
|
||||
}).reduce((accumulator, current) -> {
|
||||
- return accumulator.append(text).append(current);
|
||||
+ return (TextComponent) accumulator.append(text).append(current);
|
||||
}).orElseGet(() -> {
|
||||
return new TextComponent("");
|
||||
});
|
||||
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
|
||||
index 49cf3601df7b145d49b1fe9a71ba0bc60c5394b3..9ed0f73eb9b5b3b06dd1639dac6152e3c6b5b4d7 100644
|
||||
--- a/src/main/java/net/minecraft/world/entity/Entity.java
|
||||
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
|
||||
@@ -2214,12 +2214,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
|
||||
this.setRot(this.getYRot(), this.getXRot());
|
||||
if (nbt.contains("CustomName", 8)) {
|
||||
String s = nbt.getString("CustomName");
|
||||
-
|
||||
- try {
|
||||
- this.setCustomName(Component.Serializer.fromJson(s));
|
||||
- } catch (Exception exception) {
|
||||
- Entity.LOGGER.warn("Failed to parse entity custom name {}", s, exception);
|
||||
- }
|
||||
+ this.setCustomName(Component.Serializer.fromJsonSafe(s)); // Scissors - Use safer method for getting Components from JSON
|
||||
}
|
||||
|
||||
this.setCustomNameVisible(nbt.getBoolean("CustomNameVisible"));
|
||||
diff --git a/src/main/java/net/minecraft/world/level/block/entity/BeaconBlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/BeaconBlockEntity.java
|
||||
index 8d5c9813010a0256bd2712a1eabbc91f0f473a41..6ec48f834f5fef8678c690fd5d1ea530ebd2720f 100644
|
||||
--- a/src/main/java/net/minecraft/world/level/block/entity/BeaconBlockEntity.java
|
||||
+++ b/src/main/java/net/minecraft/world/level/block/entity/BeaconBlockEntity.java
|
||||
@@ -402,7 +402,7 @@ public class BeaconBlockEntity extends BlockEntity implements MenuProvider {
|
||||
this.levels = nbt.getInt("Levels"); // SPIGOT-5053, use where available
|
||||
// CraftBukkit end
|
||||
if (nbt.contains("CustomName", 8)) {
|
||||
- this.name = net.minecraft.server.MCUtil.getBaseComponentFromNbt("CustomName", nbt); // Paper - Catch ParseException
|
||||
+ this.name = Component.Serializer.fromJsonSafe(nbt.getString("CustomName")); // Scissors - Use safer method for getting Components from JSON
|
||||
}
|
||||
|
||||
this.lockKey = LockCode.fromTag(nbt);
|
||||
diff --git a/src/main/java/net/minecraft/world/level/block/entity/EnchantmentTableBlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/EnchantmentTableBlockEntity.java
|
||||
index 1eccf9424bd8a4bcbeed4ebb1795fd113fe5af18..fcc3d2fbc679fdb70da27d20d19380fa238ba7ea 100644
|
||||
--- a/src/main/java/net/minecraft/world/level/block/entity/EnchantmentTableBlockEntity.java
|
||||
+++ b/src/main/java/net/minecraft/world/level/block/entity/EnchantmentTableBlockEntity.java
|
||||
@@ -43,7 +43,7 @@ public class EnchantmentTableBlockEntity extends BlockEntity implements Nameable
|
||||
public void load(CompoundTag nbt) {
|
||||
super.load(nbt);
|
||||
if (nbt.contains("CustomName", 8)) {
|
||||
- this.name = net.minecraft.server.MCUtil.getBaseComponentFromNbt("CustomName", nbt); // Paper - Catch ParseException
|
||||
+ this.name = Component.Serializer.fromJsonSafe(nbt.getString("CustomName")); // Scissors - Use safer method for getting Components from JSON
|
||||
}
|
||||
|
||||
}
|
||||
diff --git a/src/main/java/net/minecraft/world/scores/ScoreboardSaveData.java b/src/main/java/net/minecraft/world/scores/ScoreboardSaveData.java
|
||||
index 0efb63d1a4cefede25712daf9cea0f5c3256980f..07fe4043184eda6fa11e0680930100eb914f64c1 100644
|
||||
--- a/src/main/java/net/minecraft/world/scores/ScoreboardSaveData.java
|
||||
+++ b/src/main/java/net/minecraft/world/scores/ScoreboardSaveData.java
|
||||
@@ -35,7 +35,7 @@ public class ScoreboardSaveData extends SavedData {
|
||||
CompoundTag compoundTag = nbt.getCompound(i);
|
||||
String string = compoundTag.getString("Name");
|
||||
PlayerTeam playerTeam = this.scoreboard.addPlayerTeam(string);
|
||||
- Component component = Component.Serializer.fromJson(compoundTag.getString("DisplayName"));
|
||||
+ Component component = Component.Serializer.fromJsonSafe(compoundTag.getString("DisplayName")); // Scissors - Use safer method for getting Components from JSON
|
||||
if (component != null) {
|
||||
playerTeam.setDisplayName(component);
|
||||
}
|
||||
@@ -53,14 +53,14 @@ public class ScoreboardSaveData extends SavedData {
|
||||
}
|
||||
|
||||
if (compoundTag.contains("MemberNamePrefix", 8)) {
|
||||
- Component component2 = Component.Serializer.fromJson(compoundTag.getString("MemberNamePrefix"));
|
||||
+ Component component2 = Component.Serializer.fromJsonSafe(compoundTag.getString("MemberNamePrefix")); // Scissors - Use safer method for getting Components from JSON
|
||||
if (component2 != null) {
|
||||
playerTeam.setPlayerPrefix(component2);
|
||||
}
|
||||
}
|
||||
|
||||
if (compoundTag.contains("MemberNameSuffix", 8)) {
|
||||
- Component component3 = Component.Serializer.fromJson(compoundTag.getString("MemberNameSuffix"));
|
||||
+ Component component3 = Component.Serializer.fromJsonSafe(compoundTag.getString("MemberNameSuffix")); // Scissors - Use safer method for getting Components from JSON
|
||||
if (component3 != null) {
|
||||
playerTeam.setPlayerSuffix(component3);
|
||||
}
|
||||
@@ -115,7 +115,7 @@ public class ScoreboardSaveData extends SavedData {
|
||||
CompoundTag compoundTag = nbt.getCompound(i);
|
||||
ObjectiveCriteria.byName(compoundTag.getString("CriteriaName")).ifPresent((criterion) -> {
|
||||
String string = compoundTag.getString("Name");
|
||||
- Component component = Component.Serializer.fromJson(compoundTag.getString("DisplayName"));
|
||||
+ Component component = Component.Serializer.fromJsonSafe(compoundTag.getString("DisplayName")); // Scissors - Use safer method for getting Components from JSON
|
||||
ObjectiveCriteria.RenderType renderType = ObjectiveCriteria.RenderType.byId(compoundTag.getString("RenderType"));
|
||||
this.scoreboard.addObjective(string, criterion, component, renderType);
|
||||
});
|
|
@ -1,6 +1,6 @@
|
|||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
||||
From: Telesphoreo <me@telesphoreo.me>
|
||||
Date: Tue, 19 Apr 2022 19:03:51 -0500
|
||||
Date: Fri, 22 Apr 2022 01:27:54 -0500
|
||||
Subject: [PATCH] Update for 1.18.2
|
||||
|
||||
|
||||
|
@ -43,16 +43,14 @@ index f92610edcfb724374c69e66443eab60919ee955a..1531d21c6e73ae69ec728d107b917967
|
|||
// Scissors end
|
||||
|
||||
diff --git a/src/main/java/net/minecraft/network/chat/NbtComponent.java b/src/main/java/net/minecraft/network/chat/NbtComponent.java
|
||||
index e3dc56de3f91e03b1543257f72448a734d914ed7..8bb0544ef08eb0c221f06ddf96b63be5d02dc5ab 100644
|
||||
index f518e47818a47be0dfd7cccc4845ba9736c6cd8b..218525ba1742d68cde10f6caf09ee30eb2d01d50 100644
|
||||
--- a/src/main/java/net/minecraft/network/chat/NbtComponent.java
|
||||
+++ b/src/main/java/net/minecraft/network/chat/NbtComponent.java
|
||||
@@ -95,7 +95,8 @@ public abstract class NbtComponent extends BaseComponent implements ContextAware
|
||||
@@ -96,6 +96,7 @@ public abstract class NbtComponent extends BaseComponent implements ContextAware
|
||||
return stream.map((string) -> {
|
||||
return new TextComponent(string);
|
||||
}).reduce((accumulator, current) -> {
|
||||
- return accumulator.append(text).append(current);
|
||||
+ // Scissors - untested, likely needs fixing
|
||||
+ return (TextComponent)accumulator.append(text).append(current);
|
||||
return (TextComponent) accumulator.append(text).append(current);
|
||||
}).orElseGet(() -> {
|
||||
return new TextComponent("");
|
||||
});
|
Loading…
Reference in a new issue