|
16 | 16 | import org.slf4j.Logger; |
17 | 17 |
|
18 | 18 | public final class ItemStack implements DataComponentHolder { |
19 | | -@@ -131,6 +_,37 @@ |
| 19 | +@@ -131,6 +_,9 @@ |
20 | 20 | public static final StreamCodec<RegistryFriendlyByteBuf, ItemStack> OPTIONAL_UNTRUSTED_STREAM_CODEC = createOptionalStreamCodec( |
21 | 21 | DataComponentPatch.DELIMITED_STREAM_CODEC |
22 | 22 | ); |
23 | 23 | + // 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); |
53 | 25 | + // Papyrus end |
54 | 26 | public static final StreamCodec<RegistryFriendlyByteBuf, ItemStack> STREAM_CODEC = new StreamCodec<RegistryFriendlyByteBuf, ItemStack>() { |
55 | 27 | @Override |
|
64 | 36 | private static final Logger LOGGER = LogUtils.getLogger(); |
65 | 37 | public static final ItemStack EMPTY = new ItemStack((Void)null); |
66 | 38 | 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