aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0719-Prevent-sending-oversized-item-data-in-equipment-and.patch
blob: 9658237f9b31a22a74624e1b5e6f7e4279a3760a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Nassim Jahnke <nassim@njahnke.dev>
Date: Wed, 1 Dec 2021 12:36:25 +0100
Subject: [PATCH] Prevent sending oversized item data in equipment and metadata


diff --git a/src/main/java/net/minecraft/network/syncher/EntityDataSerializers.java b/src/main/java/net/minecraft/network/syncher/EntityDataSerializers.java
index 97da8896865ff0bdd4fe8f2155b0830b42051bb1..9ca897d92c5bdd2764d114c74d64c776674d6beb 100644
--- a/src/main/java/net/minecraft/network/syncher/EntityDataSerializers.java
+++ b/src/main/java/net/minecraft/network/syncher/EntityDataSerializers.java
@@ -42,7 +42,7 @@ public class EntityDataSerializers {
     public static final EntityDataSerializer<ItemStack> ITEM_STACK = new EntityDataSerializer<ItemStack>() {
         @Override
         public void write(FriendlyByteBuf buf, ItemStack value) {
-            buf.writeItem(value);
+            buf.writeItem(net.minecraft.world.entity.LivingEntity.sanitizeItemStack(value, true)); // Paper - prevent oversized data
         }
 
         @Override
diff --git a/src/main/java/net/minecraft/server/level/ServerEntity.java b/src/main/java/net/minecraft/server/level/ServerEntity.java
index e0802f1cb73a80b08482832c2b269ac8485d5c1a..8d2870c780c4c253f6570c7ef73f6e7c2ccc46ad 100644
--- a/src/main/java/net/minecraft/server/level/ServerEntity.java
+++ b/src/main/java/net/minecraft/server/level/ServerEntity.java
@@ -335,7 +335,10 @@ public class ServerEntity {
                 ItemStack itemstack = ((LivingEntity) this.entity).getItemBySlot(enumitemslot);
 
                 if (!itemstack.isEmpty()) {
-                    list.add(Pair.of(enumitemslot, itemstack.copy()));
+                    // Paper start - prevent oversized data
+                    final ItemStack sanitized = LivingEntity.sanitizeItemStack(itemstack.copy(), false);
+                    list.add(Pair.of(enumitemslot, sanitized));
+                    // Paper end
                 }
             }
 
diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java
index ac66d2ee0608b079ac0f1809d9116242daf557c1..f6b8518df044cd57eeafa84890920bba5c36c012 100644
--- a/src/main/java/net/minecraft/world/entity/LivingEntity.java
+++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java
@@ -3191,7 +3191,10 @@ public abstract class LivingEntity extends Entity implements Attackable {
         equipmentChanges.forEach((enumitemslot, itemstack) -> {
             ItemStack itemstack1 = itemstack.copy();
 
-            list.add(Pair.of(enumitemslot, itemstack1));
+            // Paper start - prevent oversized data
+            ItemStack toSend = sanitizeItemStack(itemstack1, true);
+            list.add(Pair.of(enumitemslot, toSend));
+            // Paper end
             switch (enumitemslot.getType()) {
                 case HAND:
                     this.setLastHandItem(enumitemslot, itemstack1);
@@ -3204,6 +3207,34 @@ public abstract class LivingEntity extends Entity implements Attackable {
         ((ServerLevel) this.level()).getChunkSource().broadcast(this, new ClientboundSetEquipmentPacket(this.getId(), list));
     }
 
+    // Paper start - prevent oversized data
+    public static ItemStack sanitizeItemStack(final ItemStack itemStack, final boolean copyItemStack) {
+        if (itemStack.isEmpty() || !itemStack.hasTag()) {
+            return itemStack;
+        }
+
+        final ItemStack copy = copyItemStack ? itemStack.copy() : itemStack;
+        final CompoundTag tag = copy.getTag();
+        if (copy.is(Items.BUNDLE) && tag.get("Items") instanceof ListTag oldItems && !oldItems.isEmpty()) {
+            // Bundles change their texture based on their fullness.
+            org.bukkit.inventory.meta.BundleMeta bundleMeta = (org.bukkit.inventory.meta.BundleMeta) copy.asBukkitMirror().getItemMeta();
+            int sizeUsed = 0;
+            for (org.bukkit.inventory.ItemStack item : bundleMeta.getItems()) {
+                int scale = 64 / item.getMaxStackSize();
+                sizeUsed += scale * item.getAmount();
+            }
+            // Now we add a single fake item that uses the same amount of slots as all other items.
+            ListTag items = new ListTag();
+            items.add(new ItemStack(Items.PAPER, sizeUsed).save(new CompoundTag()));
+            tag.put("Items", items);
+        }
+        if (tag.get("BlockEntityTag") instanceof CompoundTag blockEntityTag) {
+            blockEntityTag.remove("Items");
+        }
+        return copy;
+    }
+    // Paper end
+
     private ItemStack getLastArmorItem(EquipmentSlot slot) {
         return (ItemStack) this.lastArmorItemStacks.get(slot.getIndex());
     }