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 3503423ee0d4ed13f7ebdb8272c8266a827ff4c1..341fad1609308fad6adf5b5f9ad289d57ac4ddd6 100644 --- a/src/main/java/net/minecraft/world/entity/LivingEntity.java +++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java @@ -3404,7 +3404,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); @@ -3488,7 +3488,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 ecfa807e78c16a24099d40becd0c7916f239aed1..f1b4adeeb4dad5178a5e52870f420beaa8e13034 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..c51c12efe6fdc360d8d6e0ea41ecaaaa6969fa7b --- /dev/null +++ b/src/main/java/org/dreeam/leaf/config/modules/misc/HiddenItemComponents.java @@ -0,0 +1,45 @@ +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.List; + +public class HiddenItemComponents extends ConfigModules { + + public String getBasePath() { + return EnumConfigCategory.MISC.getBaseKeyName(); + } + + public static List> hiddenItemComponentTypes = List.of(); + + @Override + public void onLoaded() { + List list = config.getList(getBasePath() + ".hidden-item-components", new ArrayList<>(), """ + 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..55cc252829bff893e6b0f564f3399f06267f7b8e --- /dev/null +++ b/src/main/java/org/dreeam/leaf/util/item/ItemStackObfuscator.java @@ -0,0 +1,29 @@ +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 { + + 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; + } +}