9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-21 16:09:19 +00:00
Files
Leaf/patches/server/0096-Hide-specified-item-components-to-clients.patch
Dreeam 7e39ec4d17 Updated Upstream (Paper/Gale)
Upstream has released updates that appear to apply and compile correctly

Paper Changes:
PaperMC/Paper@78feecb Deprecate BlockType#isInteractable (#11427)
PaperMC/Paper@1cb2bf4 Add velocity forwarding secret env override (#10127)
PaperMC/Paper@81d9448 Add ItemStack array serialization methods (#10387)
PaperMC/Paper@2f50b87 Fixup command precprocess cancellation (#11424)
PaperMC/Paper@540deb7 Fix Color Particle API (#10895)
PaperMC/Paper@e8297c4 Expand out datapack API (#10828)
PaperMC/Paper@4514c71 Only call EntityPortalExitEvent if entity is actually in a portal
PaperMC/Paper@acdd6d3 make MenuType implement FeatureDependant
PaperMC/Paper@9b1ee0d Add missing key files and cleanup registry definition order
PaperMC/Paper@1bc02e6 Fix tag lifecycle event handlers not disabling /reload

Gale Changes:
Dreeam-qwq/Gale@7dbafa8 Updated Upstream (Paper)
Dreeam-qwq/Gale@030aa7e Updated Upstream (Paper)
Dreeam-qwq/Gale@6d51182 Updated Upstream (Paper)
Dreeam-qwq/Gale@669798b Updated Upstream (Paper)
2024-09-25 04:05:46 -04:00

219 lines
14 KiB
Diff

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<ClientGamePa
this.items = NonNullList.withSize(contents.size(), ItemStack.EMPTY);
for (int i = 0; i < contents.size(); i++) {
- this.items.set(i, contents.get(i).copy());
+ this.items.set(i, org.dreeam.leaf.util.item.ItemStackObfuscator.stripMeta(contents.get(i), true)); // Leaf - Hide specified item components
}
- this.carriedItem = cursorStack.copy();
+ this.carriedItem = org.dreeam.leaf.util.item.ItemStackObfuscator.stripMeta(cursorStack, true); // Leaf - Hide specified item components
}
private ClientboundContainerSetContentPacket(RegistryFriendlyByteBuf buf) {
this.containerId = buf.readUnsignedByte();
this.stateId = buf.readVarInt();
- this.items = ItemStack.OPTIONAL_LIST_STREAM_CODEC.decode(buf);
- this.carriedItem = ItemStack.OPTIONAL_STREAM_CODEC.decode(buf);
+ this.items = ItemStack.OPTIONAL_LIST_STREAM_CODEC.decode(buf).stream().map(item -> 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<ClientGamePacke
this.containerId = syncId;
this.stateId = revision;
this.slot = slot;
- this.itemStack = stack.copy();
+ this.itemStack = org.dreeam.leaf.util.item.ItemStackObfuscator.stripMeta(stack, true); // Leaf - Hide specified item components
}
private ClientboundContainerSetSlotPacket(RegistryFriendlyByteBuf buf) {
this.containerId = buf.readByte();
this.stateId = buf.readVarInt();
this.slot = buf.readShort();
- this.itemStack = ItemStack.OPTIONAL_STREAM_CODEC.decode(buf);
+ this.itemStack = org.dreeam.leaf.util.item.ItemStackObfuscator.stripMeta(ItemStack.OPTIONAL_STREAM_CODEC.decode(buf), false); // Leaf - Hide specified item components
}
private void write(RegistryFriendlyByteBuf buf) {
diff --git a/src/main/java/net/minecraft/server/level/ServerEntity.java b/src/main/java/net/minecraft/server/level/ServerEntity.java
index 573c380e123473e35c0b72c44b32c8d6ba8e61c6..feacc41ecf7f4028e0a1cce5d2012ced96a26d30 100644
--- a/src/main/java/net/minecraft/server/level/ServerEntity.java
+++ b/src/main/java/net/minecraft/server/level/ServerEntity.java
@@ -374,7 +374,7 @@ public class ServerEntity {
ItemStack itemstack = ((LivingEntity) this.entity).getItemBySlot(enumitemslot);
if (!itemstack.isEmpty()) {
- list.add(Pair.of(enumitemslot, itemstack.copy()));
+ list.add(Pair.of(enumitemslot, org.dreeam.leaf.util.item.ItemStackObfuscator.stripMeta(itemstack, true))); // Leaf - Hide specified item components
}
}
diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index 8c84f58c0d943f0a5abda490bcef8160b97b9b0f..db4a6be9d4494463471cb64034d1cf59ad489fd2 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -2963,7 +2963,7 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
entity.refreshEntityData(ServerGamePacketListenerImpl.this.player);
// SPIGOT-7136 - Allays
if (entity instanceof Allay || entity instanceof net.minecraft.world.entity.animal.horse.AbstractHorse) { // Paper - Fix horse armor desync
- 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()), 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(); // Paper - fix slot desync - always refresh player inventory
diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java
index eb8ddcb671e0c3eacee26e12eb5e96896a85868d..8835b3213b91bcbb625260c84d408e45ccc40537 100644
--- a/src/main/java/net/minecraft/world/entity/LivingEntity.java
+++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java
@@ -3413,7 +3413,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);
@@ -3497,7 +3497,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<ItemStack> 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<DataComponentType<?>> hiddenItemComponentTypes = List.of();
+
+ @Override
+ public void onLoaded() {
+ List<String> 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<DataComponentType<?>> 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<DataComponentType<?>> hiddenTypes = HiddenItemComponents.hiddenItemComponentTypes;
+
+ if (hiddenTypes.isEmpty()) return copy;
+
+ // Remove specified types
+ for (DataComponentType<?> type : hiddenTypes) {
+ // Only remove, no others
+ copy.remove(type);
+ }
+
+ return copy;
+ }
+}