From 39d617b9d1255ca6a419efe394e564b0333a9fa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=9B=9F=E9=BE=99?= <75253383+TheFloodDragon@users.noreply.github.com> Date: Mon, 5 Aug 2024 20:19:35 +0800 Subject: [PATCH] Hide specified item components to clients (#94) * Hide specified item components from clients * Correct and Fix Stupid~ wrong packet edited. www * Fix & Update comments * Optimize & Fix & Add for SetContentPacket * remove changes to dev-improts.txt --- ...specified-item-components-to-clients.patch | 218 ++++++++++++++++++ 1 file changed, 218 insertions(+) create mode 100644 patches/server/0092-Hide-specified-item-components-to-clients.patch diff --git a/patches/server/0092-Hide-specified-item-components-to-clients.patch b/patches/server/0092-Hide-specified-item-components-to-clients.patch new file mode 100644 index 000000000..d2d211563 --- /dev/null +++ b/patches/server/0092-Hide-specified-item-components-to-clients.patch @@ -0,0 +1,218 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: TheFloodDragon <1610105206@qq.com> +Date: Sun, 4 Aug 2024 19:36:11 +0800 +Subject: [PATCH] Hide specified item components to clients + + +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 8cca2ac616a2c80268c96b9f95e33f834a0fc8fd..c2c0e88962ea010ece20f9710dfcd83b7b61bf91 100644 +--- a/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java ++++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java +@@ -23,17 +23,17 @@ public class ClientboundContainerSetContentPacket implements Packet org.dreeam.leaf.util.item.ItemStackObfuscator.stripMeta(item, false)).toList(); // Leaf - Hide specified item components ++ this.carriedItem = org.dreeam.leaf.util.item.ItemStackObfuscator.stripMeta(ItemStack.OPTIONAL_STREAM_CODEC.decode(buf), false); // Leaf - Hide specified item components + } + + // Paper start - Handle large packets disconnecting client +diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetSlotPacket.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetSlotPacket.java +index 63f6a2437da9363786b55af0a7cbc5373232d35b..f4c85b78eafb27331ab7c3e45c8493b271583241 100644 +--- a/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetSlotPacket.java ++++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetSlotPacket.java +@@ -21,14 +21,14 @@ public class ClientboundContainerSetSlotPacket implements Packet Pair.of(slot, ((LivingEntity) entity).getItemBySlot(slot).copy())).collect(Collectors.toList()), true)); // Paper - sanitize // Gale - JettPack - reduce array allocations ++ ServerGamePacketListenerImpl.this.send(new ClientboundSetEquipmentPacket(entity.getId(), Arrays.stream(net.minecraft.world.entity.EquipmentSlot.VALUES).map((slot) -> Pair.of(slot, org.dreeam.leaf.util.item.ItemStackObfuscator.stripMeta(((LivingEntity) entity).getItemBySlot(slot), true))).collect(Collectors.toList()), true)); // Paper - sanitize // Gale - JettPack - reduce array allocations // Leaf - Hide specified item components + ServerGamePacketListenerImpl.this.player.containerMenu.sendAllDataToRemote(); + } + } +diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java +index 7c75298c8ea7a828c2d9cd4e7ffd4d09a0113aed..6036c1a6f61d6c8a220ed97166c8741ea1922ab4 100644 +--- a/src/main/java/net/minecraft/world/entity/LivingEntity.java ++++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java +@@ -3388,7 +3388,7 @@ public abstract class LivingEntity extends Entity implements Attackable { + ItemStack itemstack1 = itemstack; + ItemStack itemstack2 = this.getItemBySlot(enumitemslot); + +- if (this.equipmentHasChanged(itemstack1, itemstack2)) { ++ if (this.equipmentHasChanged(org.dreeam.leaf.util.item.ItemStackObfuscator.stripMeta(itemstack1, true), org.dreeam.leaf.util.item.ItemStackObfuscator.stripMeta(itemstack2, true))) { // Leaf - Hide specified item components + // Paper start - PlayerArmorChangeEvent + if (this instanceof ServerPlayer && enumitemslot.getType() == EquipmentSlot.Type.HUMANOID_ARMOR) { + final org.bukkit.inventory.ItemStack oldItem = CraftItemStack.asBukkitCopy(itemstack1); +@@ -3472,7 +3472,7 @@ public abstract class LivingEntity extends Entity implements Attackable { + equipmentChanges.forEach((enumitemslot, itemstack) -> { + ItemStack itemstack1 = itemstack.copy(); + +- list.add(Pair.of(enumitemslot, itemstack1)); ++ list.add(Pair.of(enumitemslot, org.dreeam.leaf.util.item.ItemStackObfuscator.stripMeta(itemstack1, true))); // Leaf - Hide specified item components + switch (enumitemslot.getType()) { + case HAND: + this.setLastHandItem(enumitemslot, itemstack1); +diff --git a/src/main/java/net/minecraft/world/inventory/AbstractContainerMenu.java b/src/main/java/net/minecraft/world/inventory/AbstractContainerMenu.java +index c491291b522aebf34c7d990d2b485d1a0d19cdcd..267f638bb704002a30b1f5cb4e33b6a89cf0773c 100644 +--- a/src/main/java/net/minecraft/world/inventory/AbstractContainerMenu.java ++++ b/src/main/java/net/minecraft/world/inventory/AbstractContainerMenu.java +@@ -295,7 +295,7 @@ public abstract class AbstractContainerMenu { + private void triggerSlotListeners(int slot, ItemStack stack, Supplier copySupplier) { + ItemStack itemstack1 = (ItemStack) this.lastSlots.get(slot); + +- if (!ItemStack.matches(itemstack1, stack)) { ++ if (!ItemStack.matches(org.dreeam.leaf.util.item.ItemStackObfuscator.stripMeta(itemstack1, true), org.dreeam.leaf.util.item.ItemStackObfuscator.stripMeta(stack, true))) { // Leaf - Hide specified item components + ItemStack itemstack2 = (ItemStack) copySupplier.get(); + + this.lastSlots.set(slot, itemstack2); +@@ -314,7 +314,7 @@ public abstract class AbstractContainerMenu { + if (!this.suppressRemoteUpdates) { + ItemStack itemstack1 = (ItemStack) this.remoteSlots.get(slot); + +- if (!ItemStack.matches(itemstack1, stack)) { ++ if (!ItemStack.matches(org.dreeam.leaf.util.item.ItemStackObfuscator.stripMeta(itemstack1, true), org.dreeam.leaf.util.item.ItemStackObfuscator.stripMeta(stack, true))) { // Leaf - Hide specified item components + ItemStack itemstack2 = (ItemStack) copySupplier.get(); + + this.remoteSlots.set(slot, itemstack2); +@@ -342,7 +342,7 @@ public abstract class AbstractContainerMenu { + + private void synchronizeCarriedToRemote() { + if (!this.suppressRemoteUpdates) { +- if (!ItemStack.matches(this.getCarried(), this.remoteCarried)) { ++ if (!ItemStack.matches(org.dreeam.leaf.util.item.ItemStackObfuscator.stripMeta(this.getCarried(), true), org.dreeam.leaf.util.item.ItemStackObfuscator.stripMeta(this.remoteCarried, true))) { // Leaf - Hide specified item components + this.remoteCarried = this.getCarried().copy(); + if (this.synchronizer != null) { + this.synchronizer.sendCarriedChange(this, this.remoteCarried); +diff --git a/src/main/java/org/dreeam/leaf/config/modules/misc/HiddenItemComponents.java b/src/main/java/org/dreeam/leaf/config/modules/misc/HiddenItemComponents.java +new file mode 100644 +index 0000000000000000000000000000000000000000..d1c1bbd246ff2ec4e78ef3230762f2747a5e315e +--- /dev/null ++++ b/src/main/java/org/dreeam/leaf/config/modules/misc/HiddenItemComponents.java +@@ -0,0 +1,43 @@ ++package org.dreeam.leaf.config.modules.misc; ++ ++import net.minecraft.core.component.DataComponentType; ++import net.minecraft.core.registries.BuiltInRegistries; ++import net.minecraft.resources.ResourceLocation; ++import org.dreeam.leaf.config.ConfigModules; ++import org.dreeam.leaf.config.EnumConfigCategory; ++import org.dreeam.leaf.config.LeafConfig; ++ ++import java.util.ArrayList; ++import java.util.Collections; ++import java.util.List; ++ ++public class HiddenItemComponents extends ConfigModules { ++ ++ public String getBasePath() { ++ return EnumConfigCategory.MISC.getBaseKeyName(); ++ } ++ ++ public static List> hiddenItemComponentTypes = Collections.emptyList(); ++ ++ @Override ++ public void onLoaded() { ++ List list = config.getList(getBasePath() + ".hidden-item-components", Collections.emptyList(), """ ++ Controls whether specified component information is sent to clients. ++ This may break resource packs and mods that rely on this information. ++ It needs a component type list, incorrect things will not work. ++ You can fill it with ["custom_data"] to hide components of CUSTOM_DATA. ++ Also, it can avoid some frequent client animations. ++ NOTICE: You must know what you're filling in and how it works! It will handle all itemStacks! ++ """); ++ List> types = new ArrayList<>(list.size()); ++ for (String id : list) { ++ // Find and check ++ DataComponentType type = BuiltInRegistries.DATA_COMPONENT_TYPE.get(ResourceLocation.parse(id)); ++ if (type != null) { ++ types.add(type); ++ } else LeafConfig.LOGGER.warn("Unknown component type: {}", id); ++ } ++ hiddenItemComponentTypes = types; ++ } ++ ++} +diff --git a/src/main/java/org/dreeam/leaf/util/item/ItemStackObfuscator.java b/src/main/java/org/dreeam/leaf/util/item/ItemStackObfuscator.java +new file mode 100644 +index 0000000000000000000000000000000000000000..4130aaf0df48b6f17206059a3343419f6bfbc0fe +--- /dev/null ++++ b/src/main/java/org/dreeam/leaf/util/item/ItemStackObfuscator.java +@@ -0,0 +1,31 @@ ++package org.dreeam.leaf.util.item; ++ ++import net.minecraft.core.component.DataComponentType; ++import net.minecraft.world.item.ItemStack; ++import org.dreeam.leaf.config.modules.misc.HiddenItemComponents; ++ ++import java.util.List; ++ ++public class ItemStackObfuscator { ++ ++ // Leaf start - Hide specified item components ++ public static ItemStack stripMeta(final ItemStack itemStack, final boolean copyItemStack) { ++ if (itemStack.isEmpty() || itemStack.getComponentsPatch().isEmpty()) return itemStack; ++ ++ final ItemStack copy = copyItemStack ? itemStack.copy() : itemStack; ++ ++ // Get the types which need to hide ++ List> hiddenTypes = HiddenItemComponents.hiddenItemComponentTypes; ++ if (hiddenTypes.isEmpty()) return copy; ++ ++ // Remove specified types ++ for (DataComponentType type : hiddenTypes) { ++ // Only remove, no others ++ copy.remove(type); ++ } ++ ++ return copy; ++ } ++ // Leaf end - Hide specified item components ++ ++}