Skip to content

Commit c463d2e

Browse files
committed
Fix item codecs
1 parent a41046b commit c463d2e

File tree

1 file changed

+44
-30
lines changed

1 file changed

+44
-30
lines changed

papyrus-server/minecraft-patches/sources/net/minecraft/world/item/ItemStack.java.patch

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,12 @@
1616
import org.slf4j.Logger;
1717

1818
public final class ItemStack implements DataComponentHolder {
19-
@@ -131,6 +_,37 @@
19+
@@ -131,6 +_,9 @@
2020
public static final StreamCodec<RegistryFriendlyByteBuf, ItemStack> OPTIONAL_UNTRUSTED_STREAM_CODEC = createOptionalStreamCodec(
2121
DataComponentPatch.DELIMITED_STREAM_CODEC
2222
);
2323
+ // Papyrus start - Item translations. Just a copy of the above but without disabling the translator
24-
+ public static final StreamCodec<RegistryFriendlyByteBuf, ItemStack> OPTIONAL_TRANSLATED_STREAM_CODEC = new StreamCodec<RegistryFriendlyByteBuf, ItemStack>() {
25-
+ private static final StreamCodec<RegistryFriendlyByteBuf, Holder<Item>> ITEM_STREAM_CODEC = ByteBufCodecs.holderRegistry(Registries.ITEM);
26-
+
27-
+ public ItemStack decode(RegistryFriendlyByteBuf registryfriendlybytebuf) {
28-
+ int i = registryfriendlybytebuf.readVarInt();
29-
+
30-
+ if (i <= 0) {
31-
+ return ItemStack.EMPTY;
32-
+ } else {
33-
+ Holder<Item> holder = (Holder) ITEM_STREAM_CODEC.decode(registryfriendlybytebuf);
34-
+ DataComponentPatch datacomponentpatch = (DataComponentPatch) DataComponentPatch.STREAM_CODEC.decode(registryfriendlybytebuf);
35-
+ ItemStack itemstack = new ItemStack(holder, i, datacomponentpatch);
36-
+ if (false && !datacomponentpatch.isEmpty()) {
37-
+ CraftItemStack.setItemMeta(itemstack, CraftItemStack.getItemMeta(itemstack));
38-
+ }
39-
+ return itemstack;
40-
+ }
41-
+ }
42-
+
43-
+ public void encode(RegistryFriendlyByteBuf registryfriendlybytebuf, ItemStack itemstack) {
44-
+ if (itemstack.isEmpty() || itemstack.getItem() == null) { // CraftBukkit - NPE fix itemstack.getItem()
45-
+ registryfriendlybytebuf.writeVarInt(0);
46-
+ } else {
47-
+ registryfriendlybytebuf.writeVarInt(itemstack.getCount());
48-
+ ITEM_STREAM_CODEC.encode(registryfriendlybytebuf, itemstack.getItemHolder()); // CraftBukkit - decompile error
49-
+ DataComponentPatch.STREAM_CODEC.encode(registryfriendlybytebuf, itemstack.components.asClientFilteredPatch()); // Papyrus - Filter out server-only components
50-
+ }
51-
+ }
52-
+ };
24+
+ public static final StreamCodec<RegistryFriendlyByteBuf, ItemStack> OPTIONAL_TRANSLATED_STREAM_CODEC = createOptionalAlwaysTranslatedStreamCodec(DataComponentPatch.STREAM_CODEC);
5325
+ // Papyrus end
5426
public static final StreamCodec<RegistryFriendlyByteBuf, ItemStack> STREAM_CODEC = new StreamCodec<RegistryFriendlyByteBuf, ItemStack>() {
5527
@Override
@@ -64,3 +36,45 @@
6436
private static final Logger LOGGER = LogUtils.getLogger();
6537
public static final ItemStack EMPTY = new ItemStack((Void)null);
6638
private static final Component DISABLED_ITEM_TOOLTIP = Component.translatable("item.disabled").withStyle(ChatFormatting.RED);
39+
@@ -213,6 +_,41 @@
40+
}
41+
};
42+
}
43+
+
44+
+ // Papyrus start - Item translations
45+
+ private static StreamCodec<RegistryFriendlyByteBuf, ItemStack> createOptionalAlwaysTranslatedStreamCodec(
46+
+ final StreamCodec<RegistryFriendlyByteBuf, DataComponentPatch> codec
47+
+ ) {
48+
+ return new StreamCodec<RegistryFriendlyByteBuf, ItemStack>() {
49+
+ @Override
50+
+ public ItemStack decode(RegistryFriendlyByteBuf buffer) {
51+
+ int varInt = buffer.readVarInt();
52+
+ if (varInt <= 0) {
53+
+ return ItemStack.EMPTY;
54+
+ } else {
55+
+ Holder<Item> holder = Item.STREAM_CODEC.decode(buffer);
56+
+ DataComponentPatch dataComponentPatch = codec.decode(buffer);
57+
+ return new ItemStack(holder, varInt, dataComponentPatch);
58+
+ }
59+
+ }
60+
+
61+
+ @Override
62+
+ public void encode(RegistryFriendlyByteBuf buffer, ItemStack value) {
63+
+ if (value.isEmpty() || value.getItem() == null) { // CraftBukkit - NPE fix itemstack.getItem()
64+
+ buffer.writeVarInt(0);
65+
+ } else {
66+
+ buffer.writeVarInt(io.papermc.paper.util.sanitizer.ItemComponentSanitizer.sanitizeCount(io.papermc.paper.util.sanitizer.ItemObfuscationSession.currentSession(), value, value.getCount())); // Paper - potentially sanitize count
67+
+ Item.STREAM_CODEC.encode(buffer, value.getItemHolder());
68+
+ // Paper start - adventure; conditionally render translatable components
69+
+ try (final io.papermc.paper.util.SafeAutoClosable ignored = io.papermc.paper.util.sanitizer.ItemObfuscationSession.withContext(c -> c.itemStack(value))) { // pass the itemstack as context to the obfuscation session
70+
+ codec.encode(buffer, value.components.asPatch());
71+
+ }
72+
+ // Paper end - adventure; conditionally render translatable components
73+
+ }
74+
+ }
75+
+ };
76+
+ }
77+
+ // Papyrus end
78+
79+
public static StreamCodec<RegistryFriendlyByteBuf, ItemStack> validatedStreamCodec(final StreamCodec<RegistryFriendlyByteBuf, ItemStack> codec) {
80+
return new StreamCodec<RegistryFriendlyByteBuf, ItemStack>() {

0 commit comments

Comments
 (0)