Skip to content

Commit

Permalink
Make Data Attachment copy methods public API (neoforged#712)
Browse files Browse the repository at this point in the history
  • Loading branch information
Spinoscythe authored Mar 25, 2024
1 parent 14c4194 commit 9675e0b
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion patches/net/minecraft/world/item/CompassItem.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

2 changes: 1 addition & 1 deletion patches/net/minecraft/world/item/ItemStack.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Heightmap.Types, Heightmap> entry : p_196851_.getHeightmaps()) {
if (ChunkStatus.FULL.heightmapsAfter().contains(entry.getKey())) {
this.setHeightmap(entry.getKey(), entry.getValue().getRawData());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -75,45 +74,14 @@ else if (tag.isEmpty())
return tag;
}

/**
* Copy some attachments to another holder.
*/
private static <H extends AttachmentHolder> void copyAttachments(H from, H to, Predicate<AttachmentType<?>> 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<Object>) 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() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <H extends AttachmentHolder> void copyAttachments(H from, H to, Predicate<AttachmentType<?>> 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<Object>) 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);
}
}

0 comments on commit 9675e0b

Please sign in to comment.