From 9675e0bc49ae4d2c59b79f49f2c41a3f0dd04800 Mon Sep 17 00:00:00 2001 From: Spino <84230130+Spinoscythe@users.noreply.github.com> Date: Mon, 25 Mar 2024 16:30:57 +0000 Subject: [PATCH] Make Data Attachment copy methods public API (#712) --- .../cauldron/CauldronInteraction.java.patch | 2 +- .../ServerGamePacketListenerImpl.java.patch | 2 +- .../inventory/EnchantmentMenu.java.patch | 2 +- .../world/item/CompassItem.java.patch | 2 +- .../world/item/FoodOnAStickItem.java.patch | 2 +- .../minecraft/world/item/ItemStack.java.patch | 2 +- .../crafting/BookCloningRecipe.java.patch | 2 +- .../crafting/ShulkerBoxColoring.java.patch | 2 +- .../SmithingTransformRecipe.java.patch | 2 +- .../world/level/chunk/LevelChunk.java.patch | 2 +- .../attachment/AttachmentInternals.java | 36 +-------------- .../neoforge/attachment/AttachmentUtils.java | 44 +++++++++++++++++++ 12 files changed, 56 insertions(+), 44 deletions(-) create mode 100644 src/main/java/net/neoforged/neoforge/attachment/AttachmentUtils.java diff --git a/patches/net/minecraft/core/cauldron/CauldronInteraction.java.patch b/patches/net/minecraft/core/cauldron/CauldronInteraction.java.patch index e42a219a37..861cdd219f 100644 --- a/patches/net/minecraft/core/cauldron/CauldronInteraction.java.patch +++ b/patches/net/minecraft/core/cauldron/CauldronInteraction.java.patch @@ -4,7 +4,7 @@ if (p_175667_.hasTag()) { itemstack.setTag(p_175667_.getTag().copy()); } -+ net.neoforged.neoforge.attachment.AttachmentInternals.copyStackAttachments(p_175667_, itemstack); ++ net.neoforged.neoforge.attachment.AttachmentUtils.copyStackAttachments(p_175667_, itemstack); p_175665_.setItemInHand(p_175666_, itemstack); p_175665_.awardStat(Stats.CLEAN_SHULKER_BOX); diff --git a/patches/net/minecraft/server/network/ServerGamePacketListenerImpl.java.patch b/patches/net/minecraft/server/network/ServerGamePacketListenerImpl.java.patch index 4f5e3ba0d4..976dee8343 100644 --- a/patches/net/minecraft/server/network/ServerGamePacketListenerImpl.java.patch +++ b/patches/net/minecraft/server/network/ServerGamePacketListenerImpl.java.patch @@ -69,7 +69,7 @@ if (compoundtag != null) { itemstack1.setTag(compoundtag.copy()); } -+ net.neoforged.neoforge.attachment.AttachmentInternals.copyStackAttachments(itemstack, itemstack1); ++ net.neoforged.neoforge.attachment.AttachmentUtils.copyStackAttachments(itemstack, itemstack1); itemstack1.addTagElement("author", StringTag.valueOf(this.player.getName().getString())); if (this.player.isTextFilteringEnabled()) { diff --git a/patches/net/minecraft/world/inventory/EnchantmentMenu.java.patch b/patches/net/minecraft/world/inventory/EnchantmentMenu.java.patch index f9cd08e648..f307613524 100644 --- a/patches/net/minecraft/world/inventory/EnchantmentMenu.java.patch +++ b/patches/net/minecraft/world/inventory/EnchantmentMenu.java.patch @@ -41,7 +41,7 @@ if (compoundtag != null) { itemstack2.setTag(compoundtag.copy()); } -+ net.neoforged.neoforge.attachment.AttachmentInternals.copyStackAttachments(itemstack, itemstack2); ++ net.neoforged.neoforge.attachment.AttachmentUtils.copyStackAttachments(itemstack, itemstack2); this.enchantSlots.setItem(0, itemstack2); } diff --git a/patches/net/minecraft/world/item/CompassItem.java.patch b/patches/net/minecraft/world/item/CompassItem.java.patch index cd678fece3..c942fef140 100644 --- a/patches/net/minecraft/world/item/CompassItem.java.patch +++ b/patches/net/minecraft/world/item/CompassItem.java.patch @@ -4,7 +4,7 @@ ItemStack itemstack1 = new ItemStack(Items.COMPASS, 1); CompoundTag compoundtag = itemstack.hasTag() ? itemstack.getTag().copy() : new CompoundTag(); itemstack1.setTag(compoundtag); -+ net.neoforged.neoforge.attachment.AttachmentInternals.copyStackAttachments(itemstack, itemstack1); ++ net.neoforged.neoforge.attachment.AttachmentUtils.copyStackAttachments(itemstack, itemstack1); if (!player.getAbilities().instabuild) { itemstack.shrink(1); } diff --git a/patches/net/minecraft/world/item/FoodOnAStickItem.java.patch b/patches/net/minecraft/world/item/FoodOnAStickItem.java.patch index 18a2f880eb..2402e3e0b1 100644 --- a/patches/net/minecraft/world/item/FoodOnAStickItem.java.patch +++ b/patches/net/minecraft/world/item/FoodOnAStickItem.java.patch @@ -4,7 +4,7 @@ if (itemstack.isEmpty()) { ItemStack itemstack1 = new ItemStack(Items.FISHING_ROD); itemstack1.setTag(itemstack.getTag()); -+ net.neoforged.neoforge.attachment.AttachmentInternals.copyStackAttachments(itemstack, itemstack1); ++ net.neoforged.neoforge.attachment.AttachmentUtils.copyStackAttachments(itemstack, itemstack1); return InteractionResultHolder.success(itemstack1); } diff --git a/patches/net/minecraft/world/item/ItemStack.java.patch b/patches/net/minecraft/world/item/ItemStack.java.patch index cac51359c0..17a83bcd12 100644 --- a/patches/net/minecraft/world/item/ItemStack.java.patch +++ b/patches/net/minecraft/world/item/ItemStack.java.patch @@ -203,7 +203,7 @@ return EMPTY; } else { ItemStack itemstack = new ItemStack(this.getItem(), this.count); -+ net.neoforged.neoforge.attachment.AttachmentInternals.copyStackAttachments(this, itemstack); ++ net.neoforged.neoforge.attachment.AttachmentUtils.copyStackAttachments(this, itemstack); itemstack.setPopTime(this.getPopTime()); if (this.tag != null) { itemstack.tag = this.tag.copy(); diff --git a/patches/net/minecraft/world/item/crafting/BookCloningRecipe.java.patch b/patches/net/minecraft/world/item/crafting/BookCloningRecipe.java.patch index 3e71bb92f9..cecf874366 100644 --- a/patches/net/minecraft/world/item/crafting/BookCloningRecipe.java.patch +++ b/patches/net/minecraft/world/item/crafting/BookCloningRecipe.java.patch @@ -4,7 +4,7 @@ CompoundTag compoundtag = itemstack.getTag().copy(); compoundtag.putInt("generation", WrittenBookItem.getGeneration(itemstack) + 1); itemstack2.setTag(compoundtag); -+ net.neoforged.neoforge.attachment.AttachmentInternals.copyStackAttachments(itemstack, itemstack2); ++ net.neoforged.neoforge.attachment.AttachmentUtils.copyStackAttachments(itemstack, itemstack2); return itemstack2; } else { return ItemStack.EMPTY; diff --git a/patches/net/minecraft/world/item/crafting/ShulkerBoxColoring.java.patch b/patches/net/minecraft/world/item/crafting/ShulkerBoxColoring.java.patch index b6571c284b..a4f1004828 100644 --- a/patches/net/minecraft/world/item/crafting/ShulkerBoxColoring.java.patch +++ b/patches/net/minecraft/world/item/crafting/ShulkerBoxColoring.java.patch @@ -33,7 +33,7 @@ - ItemStack itemstack2 = ShulkerBoxBlock.getColoredItemStack(dyeitem.getDyeColor()); + ItemStack itemstack2 = ShulkerBoxBlock.getColoredItemStack(dyecolor); -+ net.neoforged.neoforge.attachment.AttachmentInternals.copyStackAttachments(itemstack, itemstack2); ++ net.neoforged.neoforge.attachment.AttachmentUtils.copyStackAttachments(itemstack, itemstack2); if (itemstack.hasTag()) { itemstack2.setTag(itemstack.getTag().copy()); } diff --git a/patches/net/minecraft/world/item/crafting/SmithingTransformRecipe.java.patch b/patches/net/minecraft/world/item/crafting/SmithingTransformRecipe.java.patch index fff45e940f..b95e850477 100644 --- a/patches/net/minecraft/world/item/crafting/SmithingTransformRecipe.java.patch +++ b/patches/net/minecraft/world/item/crafting/SmithingTransformRecipe.java.patch @@ -4,7 +4,7 @@ if (compoundtag != null) { itemstack.setTag(compoundtag.copy()); } -+ net.neoforged.neoforge.attachment.AttachmentInternals.copyStackAttachments(p_267036_.getItem(1), itemstack); ++ net.neoforged.neoforge.attachment.AttachmentUtils.copyStackAttachments(p_267036_.getItem(1), itemstack); return itemstack; } diff --git a/patches/net/minecraft/world/level/chunk/LevelChunk.java.patch b/patches/net/minecraft/world/level/chunk/LevelChunk.java.patch index 24f66f32f2..9abba04219 100644 --- a/patches/net/minecraft/world/level/chunk/LevelChunk.java.patch +++ b/patches/net/minecraft/world/level/chunk/LevelChunk.java.patch @@ -13,7 +13,7 @@ this.setAllStarts(p_196851_.getAllStarts()); this.setAllReferences(p_196851_.getAllReferences()); -+ net.neoforged.neoforge.attachment.AttachmentInternals.copyChunkAttachmentsOnPromotion(p_196851_.getAttachmentHolder(), this.getAttachmentHolder()); ++ net.neoforged.neoforge.attachment.AttachmentUtils.copyChunkAttachmentsOnPromotion(p_196851_.getAttachmentHolder(), this.getAttachmentHolder()); for(Entry entry : p_196851_.getHeightmaps()) { if (ChunkStatus.FULL.heightmapsAfter().contains(entry.getKey())) { this.setHeightmap(entry.getKey(), entry.getValue().getRawData()); diff --git a/src/main/java/net/neoforged/neoforge/attachment/AttachmentInternals.java b/src/main/java/net/neoforged/neoforge/attachment/AttachmentInternals.java index 99467637c0..8043e58f81 100644 --- a/src/main/java/net/neoforged/neoforge/attachment/AttachmentInternals.java +++ b/src/main/java/net/neoforged/neoforge/attachment/AttachmentInternals.java @@ -5,7 +5,6 @@ package net.neoforged.neoforge.attachment; -import java.util.function.Predicate; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.Tag; import net.minecraft.world.item.Item; @@ -75,45 +74,14 @@ else if (tag.isEmpty()) return tag; } - /** - * Copy some attachments to another holder. - */ - private static void copyAttachments(H from, H to, Predicate> filter) { - if (from.attachments == null) { - return; - } - for (var entry : from.attachments.entrySet()) { - AttachmentType type = entry.getKey(); - if (type.serializer == null) { - continue; - } - @SuppressWarnings("unchecked") - var copyHandler = (IAttachmentCopyHandler) type.copyHandler; - if (filter.test(type)) { - Object copy = copyHandler.copy(to.getExposedHolder(), entry.getValue()); - if (copy != null) { - to.getAttachmentMap().put(type, copy); - } - } - } - } - - public static void copyStackAttachments(ItemStack from, ItemStack to) { - copyAttachments(from, to, type -> true); - } - - public static void copyChunkAttachmentsOnPromotion(AttachmentHolder.AsField from, AttachmentHolder.AsField to) { - copyAttachments(from, to, type -> true); - } - @SubscribeEvent public static void onPlayerClone(PlayerEvent.Clone event) { - copyAttachments(event.getOriginal(), event.getEntity(), event.isWasDeath() ? type -> type.copyOnDeath : type -> true); + AttachmentUtils.copyAttachments(event.getOriginal(), event.getEntity(), event.isWasDeath() ? type -> type.copyOnDeath : type -> true); } @SubscribeEvent public static void onLivingConvert(LivingConversionEvent.Post event) { - copyAttachments(event.getEntity(), event.getOutcome(), type -> type.copyOnDeath); + AttachmentUtils.copyAttachments(event.getEntity(), event.getOutcome(), type -> type.copyOnDeath); } private AttachmentInternals() {} diff --git a/src/main/java/net/neoforged/neoforge/attachment/AttachmentUtils.java b/src/main/java/net/neoforged/neoforge/attachment/AttachmentUtils.java new file mode 100644 index 0000000000..3f21d26267 --- /dev/null +++ b/src/main/java/net/neoforged/neoforge/attachment/AttachmentUtils.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) NeoForged and contributors + * SPDX-License-Identifier: LGPL-2.1-only + */ + +package net.neoforged.neoforge.attachment; + +import java.util.function.Predicate; +import net.minecraft.world.item.ItemStack; + +public final class AttachmentUtils { + private AttachmentUtils() {} + + /** + * Copy some attachments to another holder. + */ + public static void copyAttachments(H from, H to, Predicate> filter) { + if (from.attachments == null) { + return; + } + for (var entry : from.attachments.entrySet()) { + AttachmentType type = entry.getKey(); + if (type.serializer == null) { + continue; + } + @SuppressWarnings("unchecked") + var copyHandler = (IAttachmentCopyHandler) type.copyHandler; + if (filter.test(type)) { + Object copy = copyHandler.copy(to.getExposedHolder(), entry.getValue()); + if (copy != null) { + to.getAttachmentMap().put(type, copy); + } + } + } + } + + public static void copyStackAttachments(ItemStack from, ItemStack to) { + copyAttachments(from, to, type -> true); + } + + public static void copyChunkAttachmentsOnPromotion(AttachmentHolder.AsField from, AttachmentHolder.AsField to) { + copyAttachments(from, to, type -> true); + } +}