Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 63 additions & 2 deletions src/main/java/ch/njol/skript/aliases/ItemData.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.MusicInstrumentMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionEffect;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
Expand All @@ -38,7 +41,7 @@ public class ItemData implements Cloneable, YggdrasilExtendedSerializable {
Variables.yggdrasil.registerSingleClass(ItemData.class, "NewItemData");
Variables.yggdrasil.registerSingleClass(OldItemData.class, "ItemData");
}

/**
* Represents old ItemData (before aliases rework and MC 1.13).
*/
Expand Down Expand Up @@ -604,7 +607,9 @@ public void deserialize(Fields fields) throws StreamCorruptedException, NotSeria
* <li>Name: custom names made with anvil do not change item type
* </ul>
* @return A modified copy of this item data.
* @deprecated Use {@link #getBaseCopy()} instead if you want a plain copy.
*/
@Deprecated(since = "INSERT_VERSION", forRemoval = true)
public ItemData aliasCopy() {
ItemData data = new ItemData();
if (stack != null) {
Expand All @@ -624,7 +629,63 @@ public ItemData aliasCopy() {
data.itemForm = itemForm;
return data;
}


/**
* Returns a base version of this item data, i.e. only contains
* the minimum ItemMeta info to represent the item, in the case of potions/goat horns.
* @return Plain item type.
*/
public @Nullable ItemData getBaseCopy() {
ItemData data = new ItemData();
data.type = type;
data.blockValues = blockValues;
data.itemForm = itemForm;
if (stack != null) {
data.stack = new ItemStack(type, 1);
if (stack.hasItemMeta()) {
ItemMeta meta = stack.getItemMeta();
if (meta instanceof PotionMeta potionMeta) {
copyPotionInfo(potionMeta, data);
} else if (meta instanceof MusicInstrumentMeta musicMeta) {
copyMusicInfo(musicMeta, data);
}
}
}
return data;
}

// Can remove after 1.21.3 is the minimum supported version
private static final boolean HAS_CUSTOM_NAME = Skript.methodExists(PotionMeta.class, "hasCustomPotionName");

private static void copyPotionInfo(PotionMeta potionMeta, ItemData data) {
PotionMeta newMeta = (PotionMeta) itemFactory.getItemMeta(data.type);
if (newMeta.equals(potionMeta))
return;
// copy potion meta info
if (potionMeta.hasBasePotionType())
newMeta.setBasePotionType(potionMeta.getBasePotionType());
if (potionMeta.hasCustomEffects()) {
for (PotionEffect effect : potionMeta.getCustomEffects()) {
newMeta.addCustomEffect(effect, false);
}
}
if (potionMeta.hasColor())
newMeta.setColor(potionMeta.getColor());
if (HAS_CUSTOM_NAME && potionMeta.hasCustomPotionName())
newMeta.setCustomPotionName(potionMeta.getCustomPotionName());
data.itemFlags = ItemFlags.CHANGED_TAGS;
data.setItemMeta(newMeta);
}

private static void copyMusicInfo(MusicInstrumentMeta musicMeta, ItemData data) {
MusicInstrumentMeta newMeta = (MusicInstrumentMeta) itemFactory.getItemMeta(data.type);
if (newMeta.equals(musicMeta))
return;
newMeta.setInstrument(musicMeta.getInstrument());
data.itemFlags = ItemFlags.CHANGED_TAGS;
data.setItemMeta(newMeta);
}

/**
* Applies tags to this item.
* @param tags Tags in Mojang's JSON format.
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/ch/njol/skript/aliases/ItemType.java
Original file line number Diff line number Diff line change
Expand Up @@ -1616,15 +1616,28 @@ public Material getBlockMaterial() {
}

/**
* Returns a base item type of this. Essentially, this calls
* {@link ItemData#aliasCopy()} on all datas and creates a new type
* Returns a base item type of this. i.e. an item type where all item datas only contain
* the minimum ItemMeta info to represent the item, in the case of potions/goat horns.
* containing the results.
* @return Base item type.
*/
public ItemType getBaseType() {
ItemType copy = new ItemType();
for (ItemData data : types) {
copy.add_(data.aliasCopy());
copy.add_(data.getBaseCopy());
}
return copy;
}

/**
* Returns a plain version of this item type, i.e. an item type where all item datas are plain and only contain
* the minimum ItemMeta info to represent the item, in the case of potions/goat horns.
* @return Plain item type.
*/
public ItemType getPlainType() {
ItemType copy = getBaseType();
for (ItemData data : copy.types) {
data.setPlain(true);
}
return copy;
}
Expand Down
8 changes: 1 addition & 7 deletions src/main/java/ch/njol/skript/expressions/ExprPlain.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ch.njol.skript.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemData;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Example;
Expand All @@ -12,9 +11,7 @@
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;

import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;

@Name("Plain Item")
Expand Down Expand Up @@ -46,10 +43,7 @@ protected ItemType[] get(Event e) {
ItemType itemType = item.getSingle(e);
if (itemType == null)
return new ItemType[0];
ItemData data = new ItemData(itemType.getMaterial());
data.setPlain(true);
ItemType plain = new ItemType(data);
return new ItemType[]{plain};
return new ItemType[]{itemType.getPlainType()};
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
test "type of has enchants":
set {_dirt} to dirt
set {_dirtSharp1} to dirt of sharpness 1
assert type of {_dirt} is dirt
assert type of {_dirtSharp1} is dirt with "type of enchanted item wasn't the base material"
assert {_dirt} is not {_dirtSharp1}
assert type of {_dirtSharp1} is not {_dirtSharp1} with "type of enchanted item retained enchants"
assert enchantments of type of {_dirtSharp1} is not set with "type of enchanted item retained enchants"