Reset large tags (#11)

This commit is contained in:
Allink 2022-04-19 00:20:08 +01:00 committed by GitHub
parent d120903a36
commit c2618f2522
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,249 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Business Goose <arclicious@vivaldi.net>
Date: Mon, 18 Apr 2022 03:56:09 +0100
Subject: [PATCH] Reset large tags
diff --git a/src/main/java/com/github/atlasmediagroup/scissors/NbtUtility.java b/src/main/java/com/github/atlasmediagroup/scissors/NbtUtility.java
new file mode 100644
index 0000000000000000000000000000000000000000..058203440fd071ce5edbe18391ea60f0a5fbee3a
--- /dev/null
+++ b/src/main/java/com/github/atlasmediagroup/scissors/NbtUtility.java
@@ -0,0 +1,28 @@
+package com.github.atlasmediagroup.scissors;
+
+import net.minecraft.nbt.CompoundTag;
+
+import javax.annotation.Nullable;
+import java.nio.charset.StandardCharsets;
+
+public class NbtUtility
+{
+
+ public static boolean isTooLarge(@Nullable CompoundTag tag)
+ {
+ if (tag == null) return false;
+ return tag.toString().getBytes(StandardCharsets.UTF_8).length > (256 * 1024);
+ }
+
+ public static class Item
+ {
+
+ public static CompoundTag removeItemData(CompoundTag tag)
+ {
+ CompoundTag cleaned = new CompoundTag();
+ cleaned.putString("id", tag.getString("id"));
+ cleaned.putByte("Count", tag.getByte("Count"));
+ return cleaned;
+ }
+ }
+}
diff --git a/src/main/java/net/minecraft/world/item/ItemStack.java b/src/main/java/net/minecraft/world/item/ItemStack.java
index d02339a07e1da56fd5dc171a5e32b00d63fa555c..fe6cb52573ee50a2c02d7add38b5e8411d7177f4 100644
--- a/src/main/java/net/minecraft/world/item/ItemStack.java
+++ b/src/main/java/net/minecraft/world/item/ItemStack.java
@@ -1,5 +1,6 @@
package net.minecraft.world.item;
+import com.github.atlasmediagroup.scissors.NbtUtility;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
@@ -7,23 +8,28 @@ import com.google.gson.JsonParseException;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.serialization.Codec;
+import com.mojang.serialization.Dynamic;
import com.mojang.serialization.codecs.RecordCodecBuilder;
-import java.text.DecimalFormat;
-import java.text.DecimalFormatSymbols;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.Locale;
-import java.util.Map.Entry;
-import java.util.Objects;
-import java.util.Optional;
-import java.util.Random;
-import java.util.function.Consumer;
-import java.util.function.Predicate;
-import java.util.stream.Collectors;
-import javax.annotation.Nullable;
+import net.minecraft.ChatFormatting;
+import net.minecraft.Util;
+import net.minecraft.advancements.CriteriaTriggers;
+import net.minecraft.commands.arguments.blocks.BlockPredicateArgument;
+import net.minecraft.commands.arguments.blocks.BlockStateParser;
+import net.minecraft.core.BlockPos;
+import net.minecraft.core.Direction;
+import net.minecraft.core.Registry;
+import net.minecraft.nbt.CompoundTag;
+import net.minecraft.nbt.ListTag;
+import net.minecraft.nbt.NbtOps;
+import net.minecraft.network.chat.*;
+import net.minecraft.network.protocol.game.ClientboundBlockUpdatePacket;
+import net.minecraft.resources.ResourceLocation;
+import net.minecraft.server.MinecraftServer;
+import net.minecraft.server.level.ServerLevel;
+import net.minecraft.server.level.ServerPlayer;
+import net.minecraft.sounds.SoundEvent;
+import net.minecraft.sounds.SoundSource;
+import net.minecraft.stats.Stats;
import net.minecraft.tags.BlockTags;
import net.minecraft.tags.Tag;
import net.minecraft.tags.TagContainer;
@@ -31,11 +37,7 @@ import net.minecraft.util.datafix.fixes.References;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
-import net.minecraft.world.entity.Entity;
-import net.minecraft.world.entity.EquipmentSlot;
-import net.minecraft.world.entity.LivingEntity;
-import net.minecraft.world.entity.MobType;
-import net.minecraft.world.entity.SlotAccess;
+import net.minecraft.world.entity.*;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
@@ -51,50 +53,13 @@ import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.Level;
-import net.minecraft.world.level.block.BaseEntityBlock;
-import net.minecraft.world.level.block.Block;
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
-
-// CraftBukkit start
-import com.mojang.serialization.Dynamic;
-import java.util.List;
-import java.util.Map;
-import net.minecraft.ChatFormatting;
-import net.minecraft.Util;
-import net.minecraft.advancements.CriteriaTriggers;
-import net.minecraft.commands.arguments.blocks.BlockPredicateArgument;
-import net.minecraft.commands.arguments.blocks.BlockStateParser;
-import net.minecraft.core.BlockPos;
-import net.minecraft.core.Direction;
-import net.minecraft.core.Registry;
-import net.minecraft.nbt.CompoundTag;
-import net.minecraft.nbt.ListTag;
-import net.minecraft.nbt.NbtOps;
-import net.minecraft.network.chat.Component;
-import net.minecraft.network.chat.ComponentUtils;
-import net.minecraft.network.chat.HoverEvent;
-import net.minecraft.network.chat.MutableComponent;
-import net.minecraft.network.chat.Style;
-import net.minecraft.network.chat.TextComponent;
-import net.minecraft.network.chat.TranslatableComponent;
-import net.minecraft.network.protocol.game.ClientboundBlockUpdatePacket;
-import net.minecraft.resources.ResourceLocation;
-import net.minecraft.server.MinecraftServer;
-import net.minecraft.server.level.ServerLevel;
-import net.minecraft.server.level.ServerPlayer;
-import net.minecraft.sounds.SoundEvent;
-import net.minecraft.sounds.SoundSource;
-import net.minecraft.stats.Stats;
-import net.minecraft.world.level.block.Blocks;
-import net.minecraft.world.level.block.JukeboxBlock;
-import net.minecraft.world.level.block.SaplingBlock;
-import net.minecraft.world.level.block.SoundType;
-import net.minecraft.world.level.block.WitherSkullBlock;
+import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.SignBlockEntity;
import net.minecraft.world.level.block.entity.SkullBlockEntity;
import net.minecraft.world.level.block.state.pattern.BlockInWorld;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
import org.bukkit.Location;
import org.bukkit.TreeType;
import org.bukkit.block.BlockState;
@@ -106,6 +71,15 @@ import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockFertilizeEvent;
import org.bukkit.event.player.PlayerItemDamageEvent;
import org.bukkit.event.world.StructureGrowEvent;
+
+import javax.annotation.Nullable;
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.util.*;
+import java.util.Map.Entry;
+import java.util.function.Consumer;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
// CraftBukkit end
public final class ItemStack {
@@ -245,6 +219,13 @@ public final class ItemStack {
// CraftBukkit - break into own method
private void load(CompoundTag nbttagcompound) {
+ // Scissors start - Reset large tags
+ if (NbtUtility.isTooLarge(nbttagcompound)) {
+ // Reset tag without destroying item
+ nbttagcompound = NbtUtility.Item.removeItemData(nbttagcompound);
+ }
+ // Scissors end
+
this.item = (Item) Registry.ITEM.get(new ResourceLocation(nbttagcompound.getString("id")));
this.count = nbttagcompound.getByte("Count");
if (nbttagcompound.contains("tag", 10)) {
@@ -485,7 +466,11 @@ public final class ItemStack {
nbt.putString("id", minecraftkey == null ? "minecraft:air" : minecraftkey.toString());
nbt.putByte("Count", (byte) this.count);
if (this.tag != null) {
- nbt.put("tag", this.tag.copy());
+ // Scissors start - Don't save large tags
+ if (!NbtUtility.isTooLarge(this.tag)) {
+ nbt.put("tag", this.tag.copy());
+ }
+ // Scissors end
}
return nbt;
@@ -815,6 +800,9 @@ public final class ItemStack {
}
// Paper end
public void setTag(@Nullable CompoundTag nbt) {
+ // Scissors start - Ignore large tags
+ if (NbtUtility.isTooLarge(nbt)) return;
+ // Scissors end
this.tag = nbt;
processEnchantOrder(this.tag); // Paper
if (this.getItem().canBeDepleted()) {
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java
index 14da2997b5fff4434b1fe8d5a1b3109dde143740..a4385cea2d472611cfc7ef43662367bb1d780333 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java
@@ -1,16 +1,12 @@
package org.bukkit.craftbukkit.inventory;
-import static org.bukkit.craftbukkit.inventory.CraftMetaItem.*;
import com.google.common.collect.ImmutableMap;
-import java.util.Map;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.world.item.Item;
import org.apache.commons.lang.Validate;
import org.bukkit.Material;
-import org.bukkit.NamespacedKey;
import org.bukkit.configuration.serialization.DelegateDeserialization;
-import org.bukkit.craftbukkit.enchantments.CraftEnchantment;
import org.bukkit.craftbukkit.util.CraftLegacy;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
@@ -19,6 +15,11 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.material.MaterialData;
+import java.util.Map;
+
+import static org.bukkit.craftbukkit.inventory.CraftMetaItem.ENCHANTMENTS_ID;
+import static org.bukkit.craftbukkit.inventory.CraftMetaItem.ENCHANTMENTS_LVL;
+
@DelegateDeserialization(ItemStack.class)
public final class CraftItemStack extends ItemStack {