Skip to content

Commit cde2ffd

Browse files
authored
Unify plain and type of for items (#8402)
1 parent be3e363 commit cde2ffd

File tree

5 files changed

+88
-56
lines changed

5 files changed

+88
-56
lines changed

src/main/java/ch/njol/skript/aliases/ItemData.java

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import org.bukkit.inventory.ItemFlag;
2020
import org.bukkit.inventory.ItemStack;
2121
import 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;
2225
import org.jetbrains.annotations.Nullable;
2326

2427
import 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.

src/main/java/ch/njol/skript/aliases/ItemType.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,15 +1616,28 @@ public Material getBlockMaterial() {
16161616
}
16171617

16181618
/**
1619-
* Returns a base item type of this. Essentially, this calls
1620-
* {@link ItemData#aliasCopy()} on all datas and creates a new type
1619+
* Returns a base item type of this. i.e. an item type where all item datas only contain
1620+
* the minimum ItemMeta info to represent the item, in the case of potions/goat horns.
16211621
* containing the results.
16221622
* @return Base item type.
16231623
*/
16241624
public ItemType getBaseType() {
16251625
ItemType copy = new ItemType();
16261626
for (ItemData data : types) {
1627-
copy.add_(data.aliasCopy());
1627+
copy.add_(data.getBaseCopy());
1628+
}
1629+
return copy;
1630+
}
1631+
1632+
/**
1633+
* Returns a plain version of this item type, i.e. an item type where all item datas are plain and only contain
1634+
* the minimum ItemMeta info to represent the item, in the case of potions/goat horns.
1635+
* @return Plain item type.
1636+
*/
1637+
public ItemType getPlainType() {
1638+
ItemType copy = getBaseType();
1639+
for (ItemData data : copy.types) {
1640+
data.setPlain(true);
16281641
}
16291642
return copy;
16301643
}

src/main/java/ch/njol/skript/expressions/ExprPlain.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package ch.njol.skript.expressions;
22

33
import ch.njol.skript.Skript;
4-
import ch.njol.skript.aliases.ItemData;
54
import ch.njol.skript.aliases.ItemType;
65
import ch.njol.skript.doc.Description;
76
import ch.njol.skript.doc.Example;
@@ -12,9 +11,7 @@
1211
import ch.njol.skript.lang.SkriptParser.ParseResult;
1312
import ch.njol.skript.lang.util.SimpleExpression;
1413
import ch.njol.util.Kleenean;
15-
1614
import org.bukkit.event.Event;
17-
import org.bukkit.inventory.ItemStack;
1815
import org.jetbrains.annotations.Nullable;
1916

2017
@Name("Plain Item")
@@ -46,10 +43,7 @@ protected ItemType[] get(Event e) {
4643
ItemType itemType = item.getSingle(e);
4744
if (itemType == null)
4845
return new ItemType[0];
49-
ItemData data = new ItemData(itemType.getMaterial());
50-
data.setPlain(true);
51-
ItemType plain = new ItemType(data);
52-
return new ItemType[]{plain};
46+
return new ItemType[]{itemType.getPlainType()};
5347
}
5448

5549
@Override

src/test/java/org/skriptlang/skript/test/tests/regression/ExprPlainAliasTest.java

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
test "type of has enchants":
2+
set {_dirt} to dirt
3+
set {_dirtSharp1} to dirt of sharpness 1
4+
assert type of {_dirt} is dirt
5+
assert type of {_dirtSharp1} is dirt with "type of enchanted item wasn't the base material"
6+
assert {_dirt} is not {_dirtSharp1}
7+
assert type of {_dirtSharp1} is not {_dirtSharp1} with "type of enchanted item retained enchants"
8+
assert enchantments of type of {_dirtSharp1} is not set with "type of enchanted item retained enchants"

0 commit comments

Comments
 (0)