diff --git a/removed-patches-1-20-5/1027-Hide-unnecessary-itemmeta-from-clients.patch b/removed-patches-1-20-5/1037-Hide-unnecessary-itemmeta-from-clients.patch similarity index 51% rename from removed-patches-1-20-5/1027-Hide-unnecessary-itemmeta-from-clients.patch rename to removed-patches-1-20-5/1037-Hide-unnecessary-itemmeta-from-clients.patch index ccb63be0a0..e76adad6ab 100644 --- a/removed-patches-1-20-5/1027-Hide-unnecessary-itemmeta-from-clients.patch +++ b/removed-patches-1-20-5/1037-Hide-unnecessary-itemmeta-from-clients.patch @@ -5,6 +5,123 @@ Subject: [PATCH] Hide unnecessary itemmeta from clients TODO: Needs updating for data components +diff --git a/src/main/java/io/papermc/paper/util/ItemStackObfuscator.java b/src/main/java/io/papermc/paper/util/ItemStackObfuscator.java +new file mode 100644 +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 +--- /dev/null ++++ b/src/main/java/io/papermc/paper/util/ItemStackObfuscator.java +@@ -0,0 +0,0 @@ ++package io.papermc.paper.util; ++ ++import net.minecraft.core.component.DataComponentMap; ++import net.minecraft.core.component.DataComponents; ++import net.minecraft.nbt.CompoundTag; ++import net.minecraft.nbt.ListTag; ++import net.minecraft.world.item.ItemStack; ++import net.minecraft.world.item.Items; ++import net.minecraft.world.item.component.BundleContents; ++import net.minecraft.world.item.component.CustomData; ++import net.minecraft.world.item.enchantment.EnchantmentHelper; ++import net.minecraft.world.item.enchantment.Enchantments; ++import net.minecraft.world.item.enchantment.ItemEnchantments; ++import net.minecraft.world.level.Level; ++import java.util.List; ++ ++public class ItemStackObfuscator { ++ ++ // Paper start - Hide unnecessary item meta ++ public static ItemStack stripMeta(final ItemStack itemStack, final boolean copyItemStack, Level level) { ++ if (itemStack.isEmpty() || (itemStack.getComponentsPatch().isEmpty() && itemStack.getCount() < 2)) { ++ return itemStack; ++ } ++ ++ final ItemStack copy = copyItemStack ? itemStack.copy() : itemStack; ++ if (level.paperConfig().anticheat.obfuscation.items.hideDurability) { ++ // Only show damage values for elytra's, since they show a different texture when broken. ++ if (!copy.is(Items.ELYTRA) || copy.getDamageValue() < copy.getMaxDamage() - 1) { ++ copy.setDamageValue(0); ++ } ++ } ++ ++ final DataComponentMap components = copy.getComponents(); ++ if (level.paperConfig().anticheat.obfuscation.items.hideItemmeta) { ++ // Some resource packs show different textures when there is more than one item. Since this shouldn't provide a big advantage, ++ // we'll tell the client if there's one or (more than) two items. ++ copy.setCount(copy.getCount() > 1 ? 2 : 1); ++ ++ // We can't just strip out display, leather helmets still use the display.color tag. ++ copy.remove(DataComponents.CUSTOM_NAME); ++ copy.remove(DataComponents.ITEM_NAME); ++ copy.remove(DataComponents.LORE); ++ copy.remove(DataComponents.RARITY); // affects name only ++ ++ ++ ItemEnchantments enchantments = copy.getEnchantments(); ++ if (!enchantments.isEmpty()) { ++ ItemEnchantments.Mutable mutableEnchantments = new ItemEnchantments.Mutable(ItemEnchantments.EMPTY); ++ if (EnchantmentHelper.getItemEnchantmentLevel(Enchantments.SOUL_SPEED, itemStack) > 0) { ++ mutableEnchantments.set(Enchantments.SOUL_SPEED, 1); ++ } ++ if (mutableEnchantments.keySet().isEmpty()) { ++ copy.set(DataComponents.ENCHANTMENT_GLINT_OVERRIDE, true); // Add a glint override if there are no enchantments present-- this is so we still get a fake glow ++ } ++ ++ copy.set(DataComponents.ENCHANTMENTS, mutableEnchantments.toImmutable()); ++ } ++ copy.remove(DataComponents.ATTRIBUTE_MODIFIERS); ++ copy.remove(DataComponents.UNBREAKABLE); ++ copy.remove(DataComponents.CUSTOM_DATA); // Persistent data container (remove everything) ++ ++ // Books ++ copy.remove(DataComponents.WRITABLE_BOOK_CONTENT); ++ copy.remove(DataComponents.WRITTEN_BOOK_CONTENT); ++ ++ // Filled maps ++ copy.remove(DataComponents.MAP_ID); ++ copy.remove(DataComponents.MAP_POST_PROCESSING); ++ } ++ ++ if (level.paperConfig().anticheat.obfuscation.items.hideItemmetaWithVisualEffects) { ++ // Lodestone compasses ++ copy.remove(DataComponents.LODESTONE_TRACKER); ++ copy.set(DataComponents.ENCHANTMENT_GLINT_OVERRIDE, true); // override the glint -- simulate ++ } ++ ++ return copy; ++ } ++ // Paper end - Hide unnecessary item meta ++ // Paper start - prevent oversized data ++ public static ItemStack sanitizeItemStack(final ItemStack itemStack, final boolean copyItemStack) { ++ if (itemStack.isEmpty() || itemStack.getComponentsPatch().isEmpty()) { ++ return itemStack; ++ } ++ ++ final ItemStack copy = copyItemStack ? itemStack.copy() : itemStack; ++ final DataComponentMap components = copy.getComponents(); ++ ++ final BundleContents contents = components.get(DataComponents.BUNDLE_CONTENTS); ++ if (copy.is(Items.BUNDLE) && contents != null && !contents.isEmpty()) { ++ // Bundles change their texture based on their fullness. ++ // TODO: Check this ++ int sizeUsed = 0; ++ for (ItemStack item : contents.items()) { ++ int scale = 64 / item.getMaxStackSize(); ++ sizeUsed += scale * item.getCount(); ++ } ++ // Now we add a single fake item that uses the same amount of slots as all other items. ++ copy.set(DataComponents.BUNDLE_CONTENTS, new BundleContents(List.of(new ItemStack(Items.PAPER, sizeUsed)))); ++ } ++ ++ ++ CustomData blockEntityData = components.get(DataComponents.BLOCK_ENTITY_DATA); ++ copy.set(DataComponents.BLOCK_ENTITY_DATA, blockEntityData.update((tag) -> { ++ tag.remove("Items"); ++ })); ++ ++ return copy; ++ } ++ // Paper end - prevent oversized data ++} diff --git a/src/main/java/net/minecraft/server/level/ServerEntity.java b/src/main/java/net/minecraft/server/level/ServerEntity.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/net/minecraft/server/level/ServerEntity.java @@ -15,8 +132,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 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, ((LivingEntity) this.entity).stripMeta(sanitized, false))); // Paper - Hide unnecessary item meta ++ final ItemStack sanitized = io.papermc.paper.util.ItemStackObfuscator.sanitizeItemStack(itemstack.copy(), false); ++ list.add(Pair.of(enumitemslot, io.papermc.paper.util.ItemStackObfuscator.stripMeta(sanitized, false, this.level))); // Paper - Hide unnecessary item meta + // Paper end - prevent oversized data } } @@ -32,7 +149,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 - if (entity instanceof Allay) { - ServerGamePacketListenerImpl.this.send(new ClientboundSetEquipmentPacket(entity.getId(), Arrays.stream(net.minecraft.world.entity.EquipmentSlot.values()).map((slot) -> Pair.of(slot, ((LivingEntity) entity).getItemBySlot(slot).copy())).collect(Collectors.toList()))); + if (entity instanceof Allay allay) { // Paper - Hide unnecessary item meta -+ ServerGamePacketListenerImpl.this.send(new ClientboundSetEquipmentPacket(entity.getId(), Arrays.stream(net.minecraft.world.entity.EquipmentSlot.values()).map((slot) -> Pair.of(slot, allay.stripMeta(allay.getItemBySlot(slot), true))).collect(Collectors.toList()))); // Paper - Hide unnecessary item meta ++ ServerGamePacketListenerImpl.this.send(new ClientboundSetEquipmentPacket(entity.getId(), Arrays.stream(net.minecraft.world.entity.EquipmentSlot.values()).map((slot) -> Pair.of(slot, io.papermc.paper.util.ItemStackObfuscator.stripMeta(allay.getItemBySlot(slot), true, allay.level()))).collect(Collectors.toList()))); // Paper - Hide unnecessary item meta ServerGamePacketListenerImpl.this.player.containerMenu.sendAllDataToRemote(); } } @@ -46,115 +163,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 - list.add(Pair.of(enumitemslot, itemstack1)); + // Paper start - prevent oversized data -+ ItemStack toSend = sanitizeItemStack(itemstack1, true); -+ list.add(Pair.of(enumitemslot, stripMeta(toSend, toSend == itemstack1))); // Paper - Hide unnecessary item meta ++ ItemStack toSend = io.papermc.paper.util.ItemStackObfuscator.sanitizeItemStack(itemstack1, true); ++ list.add(Pair.of(enumitemslot, io.papermc.paper.util.ItemStackObfuscator.stripMeta(toSend, toSend == itemstack1, this.level()))); // Paper - Hide unnecessary item meta + // Paper end - prevent oversized data switch (enumitemslot.getType()) { case HAND: this.setLastHandItem(enumitemslot, itemstack1); -@@ -0,0 +0,0 @@ public abstract class LivingEntity extends Entity implements Attackable { - ((ServerLevel) this.level()).getChunkSource().broadcast(this, new ClientboundSetEquipmentPacket(this.getId(), list)); - } - -+ // Paper start - Hide unnecessary item meta -+ public ItemStack stripMeta(final ItemStack itemStack, final boolean copyItemStack) { -+ if (itemStack.isEmpty() || (!itemStack.hasTag() && itemStack.getCount() < 2)) { -+ return itemStack; -+ } -+ -+ final ItemStack copy = copyItemStack ? itemStack.copy() : itemStack; -+ if (this.level().paperConfig().anticheat.obfuscation.items.hideDurability) { -+ // Only show damage values for elytra's, since they show a different texture when broken. -+ if (!copy.is(Items.ELYTRA) || copy.getDamageValue() < copy.getMaxDamage() - 1) { -+ copy.setDamageValue(0); -+ } -+ } -+ -+ final CompoundTag tag = copy.getTag(); -+ if (this.level().paperConfig().anticheat.obfuscation.items.hideItemmeta) { -+ // Some resource packs show different textures when there is more than one item. Since this shouldn't provide a big advantage, -+ // we'll tell the client if there's one or (more than) two items. -+ copy.setCount(copy.getCount() > 1 ? 2 : 1); -+ // We can't just strip out display, leather helmets still use the display.color tag. -+ if (tag != null) { -+ if (tag.get("display") instanceof CompoundTag displayTag) { -+ displayTag.remove("Lore"); -+ displayTag.remove("Name"); -+ } -+ -+ if (tag.get("Enchantments") instanceof ListTag enchantmentsTag && !enchantmentsTag.isEmpty()) { -+ // The client still renders items with the enchantment glow if the enchantments tag contains at least one (empty) child. -+ ListTag enchantments = new ListTag(); -+ CompoundTag fakeEnchantment = new CompoundTag(); -+ // Soul speed boots generate client side particles. -+ if (EnchantmentHelper.getItemEnchantmentLevel(Enchantments.SOUL_SPEED, itemStack) > 0) { -+ fakeEnchantment.putString("id", org.bukkit.enchantments.Enchantment.SOUL_SPEED.getKey().asString()); -+ fakeEnchantment.putInt("lvl", 1); -+ } -+ enchantments.add(fakeEnchantment); -+ tag.put("Enchantments", enchantments); -+ } -+ tag.remove("AttributeModifiers"); -+ tag.remove("Unbreakable"); -+ tag.remove("PublicBukkitValues"); // Persistent data container1 -+ -+ // Books -+ tag.remove("author"); -+ tag.remove("filtered_title"); -+ tag.remove("pages"); -+ tag.remove("filtered_pages"); -+ tag.remove("title"); -+ tag.remove("generation"); -+ -+ // Filled maps -+ tag.remove("map"); -+ tag.remove("map_scale_direction"); -+ tag.remove("map_to_lock"); -+ } -+ } -+ -+ if (this.level().paperConfig().anticheat.obfuscation.items.hideItemmetaWithVisualEffects && tag != null) { -+ // Lodestone compasses -+ tag.remove("LodestonePos"); -+ if (tag.contains("LodestoneDimension")) { -+ // The client shows the glint if either the position or the dimension is present, so we just wipe -+ // the position and fake the dimension -+ tag.putString("LodestoneDimension", "paper:paper"); -+ } -+ } -+ -+ return copy; -+ } -+ // Paper end - Hide unnecessary item meta -+ -+ // 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 - prevent oversized data -+ - private ItemStack getLastArmorItem(EquipmentSlot slot) { - return (ItemStack) this.lastArmorItemStacks.get(slot.getIndex()); - }