Skip to content

Commit

Permalink
Optimize & Fix & Add for SetContentPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFloodDragon committed Aug 4, 2024
1 parent a831a69 commit c5a459b
Showing 1 changed file with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: TheFloodDragon <1610105206@qq.com>
Date: Sun, 4 Aug 2024 19:09:53 +0800
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
Expand Down Expand Up @@ -106,7 +132,7 @@ index c491291b522aebf34c7d990d2b485d1a0d19cdcd..267f638bb704002a30b1f5cb4e33b6a8
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..1d4bb2807d1f026065a2eb832ebbee94b356b7cf
index 0000000000000000000000000000000000000000..d1c1bbd246ff2ec4e78ef3230762f2747a5e315e
--- /dev/null
+++ b/src/main/java/org/dreeam/leaf/config/modules/misc/HiddenItemComponents.java
@@ -0,0 +1,43 @@
Expand Down Expand Up @@ -139,7 +165,7 @@ index 0000000000000000000000000000000000000000..1d4bb2807d1f026065a2eb832ebbee94
+ 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!
+ 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) {
Expand All @@ -155,16 +181,18 @@ index 0000000000000000000000000000000000000000..1d4bb2807d1f026065a2eb832ebbee94
+}
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..688a4f84424759c894735b6dc84d91aabf7f9d1d
index 0000000000000000000000000000000000000000..4130aaf0df48b6f17206059a3343419f6bfbc0fe
--- /dev/null
+++ b/src/main/java/org/dreeam/leaf/util/item/ItemStackObfuscator.java
@@ -0,0 +1,25 @@
@@ -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
Expand All @@ -173,10 +201,14 @@ index 0000000000000000000000000000000000000000..688a4f84424759c894735b6dc84d91aa
+
+ 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<?> hiddenType : HiddenItemComponents.hiddenItemComponentTypes) {
+ for (DataComponentType<?> type : hiddenTypes) {
+ // Only remove, no others
+ copy.remove(hiddenType);
+ copy.remove(type);
+ }
+
+ return copy;
Expand Down

0 comments on commit c5a459b

Please sign in to comment.