1919import org .bukkit .inventory .ItemFlag ;
2020import org .bukkit .inventory .ItemStack ;
2121import org .bukkit .inventory .meta .ItemMeta ;
22+ import org .bukkit .inventory .meta .MusicInstrumentMeta ;
23+ import org .bukkit .inventory .meta .PotionMeta ;
24+ import org .bukkit .potion .PotionEffect ;
2225import org .jetbrains .annotations .Nullable ;
2326
2427import java .io .IOException ;
@@ -38,7 +41,7 @@ public class ItemData implements Cloneable, YggdrasilExtendedSerializable {
3841 Variables .yggdrasil .registerSingleClass (ItemData .class , "NewItemData" );
3942 Variables .yggdrasil .registerSingleClass (OldItemData .class , "ItemData" );
4043 }
41-
44+
4245 /**
4346 * Represents old ItemData (before aliases rework and MC 1.13).
4447 */
@@ -604,7 +607,9 @@ public void deserialize(Fields fields) throws StreamCorruptedException, NotSeria
604607 * <li>Name: custom names made with anvil do not change item type
605608 * </ul>
606609 * @return A modified copy of this item data.
610+ * @deprecated Use {@link #getBaseCopy()} instead if you want a plain copy.
607611 */
612+ @ Deprecated (since = "INSERT_VERSION" , forRemoval = true )
608613 public ItemData aliasCopy () {
609614 ItemData data = new ItemData ();
610615 if (stack != null ) {
@@ -624,7 +629,63 @@ public ItemData aliasCopy() {
624629 data .itemForm = itemForm ;
625630 return data ;
626631 }
627-
632+
633+ /**
634+ * Returns a base version of this item data, i.e. only contains
635+ * the minimum ItemMeta info to represent the item, in the case of potions/goat horns.
636+ * @return Plain item type.
637+ */
638+ public @ Nullable ItemData getBaseCopy () {
639+ ItemData data = new ItemData ();
640+ data .type = type ;
641+ data .blockValues = blockValues ;
642+ data .itemForm = itemForm ;
643+ if (stack != null ) {
644+ data .stack = new ItemStack (type , 1 );
645+ if (stack .hasItemMeta ()) {
646+ ItemMeta meta = stack .getItemMeta ();
647+ if (meta instanceof PotionMeta potionMeta ) {
648+ copyPotionInfo (potionMeta , data );
649+ } else if (meta instanceof MusicInstrumentMeta musicMeta ) {
650+ copyMusicInfo (musicMeta , data );
651+ }
652+ }
653+ }
654+ return data ;
655+ }
656+
657+ // Can remove after 1.21.3 is the minimum supported version
658+ private static final boolean HAS_CUSTOM_NAME = Skript .methodExists (PotionMeta .class , "hasCustomPotionName" );
659+
660+ private static void copyPotionInfo (PotionMeta potionMeta , ItemData data ) {
661+ PotionMeta newMeta = (PotionMeta ) itemFactory .getItemMeta (data .type );
662+ if (newMeta .equals (potionMeta ))
663+ return ;
664+ // copy potion meta info
665+ if (potionMeta .hasBasePotionType ())
666+ newMeta .setBasePotionType (potionMeta .getBasePotionType ());
667+ if (potionMeta .hasCustomEffects ()) {
668+ for (PotionEffect effect : potionMeta .getCustomEffects ()) {
669+ newMeta .addCustomEffect (effect , false );
670+ }
671+ }
672+ if (potionMeta .hasColor ())
673+ newMeta .setColor (potionMeta .getColor ());
674+ if (HAS_CUSTOM_NAME && potionMeta .hasCustomPotionName ())
675+ newMeta .setCustomPotionName (potionMeta .getCustomPotionName ());
676+ data .itemFlags = ItemFlags .CHANGED_TAGS ;
677+ data .setItemMeta (newMeta );
678+ }
679+
680+ private static void copyMusicInfo (MusicInstrumentMeta musicMeta , ItemData data ) {
681+ MusicInstrumentMeta newMeta = (MusicInstrumentMeta ) itemFactory .getItemMeta (data .type );
682+ if (newMeta .equals (musicMeta ))
683+ return ;
684+ newMeta .setInstrument (musicMeta .getInstrument ());
685+ data .itemFlags = ItemFlags .CHANGED_TAGS ;
686+ data .setItemMeta (newMeta );
687+ }
688+
628689 /**
629690 * Applies tags to this item.
630691 * @param tags Tags in Mojang's JSON format.
0 commit comments