forked from neoforged/NeoForge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Data Attachment copy methods public API (neoforged#712)
- Loading branch information
1 parent
14c4194
commit 9675e0b
Showing
12 changed files
with
56 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/net/neoforged/neoforge/attachment/AttachmentUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |