Add ItemStack#copyDataFrom (#12224)

This commit is contained in:
TonytheMacaroni
2025-03-23 20:12:57 -04:00
committed by GitHub
parent 5a6ab97be6
commit c467df95a2
3 changed files with 54 additions and 8 deletions

View File

@ -79,7 +79,7 @@ public sealed interface RegistryKey<T> extends Keyed permits RegistryKeyImpl {
RegistryKey<BlockType> BLOCK = create("block");
/**
* @apiNote use preferably only in the context of registry entries.
* @see io.papermc.paper.registry.data
* @see io.papermc.paper.registry.keys.ItemTypeKeys
*/
@ApiStatus.Experimental // Paper - already required for registry builders
RegistryKey<ItemType> ITEM = create("item");

View File

@ -6,6 +6,7 @@ import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Predicate;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.Material;
@ -1306,6 +1307,31 @@ public class ItemStack implements Cloneable, ConfigurationSerializable, Translat
this.craftDelegate.resetData(type);
}
/**
* Copies component values and component removals from the provided ItemStack.
* <p>
* Example:
* <pre>{@code
* Set<DataComponentType> types = Set.of(
* DataComponentTypes.CONSUMABLE,
* DataComponentTypes.ENCHANTMENT_GLINT_OVERRIDE,
* DataComponentTypes.RARITY
* );
*
* ItemStack source = ItemStack.of(Material.ENCHANTED_GOLDEN_APPLE);
* ItemStack target = ItemStack.of(Material.GOLDEN_CARROT);
*
* target.copyDataFrom(source, types::contains);
* }</pre>
*
* @param source the item stack to copy from
* @param filter predicate for which components to copy
*/
@org.jetbrains.annotations.ApiStatus.Experimental
public void copyDataFrom(final @NotNull ItemStack source, final @NotNull Predicate<io.papermc.paper.datacomponent.@NotNull DataComponentType> filter) {
this.craftDelegate.copyDataFrom(source, filter);
}
/**
* Checks if the data component type is overridden from the default for the
* item type.