Skip to content

Expand Custom Model Data syntaxes to support new CMD component #7807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -1,32 +1,64 @@
package ch.njol.skript.conditions;

import org.bukkit.inventory.meta.ItemMeta;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.conditions.base.PropertyCondition;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.RequiredPlugins;
import ch.njol.skript.doc.Since;
import ch.njol.skript.doc.*;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.components.CustomModelDataComponent;

import java.util.Locale;

@Name("Has Custom Model Data")
@Description("Check if an item has a custom model data tag")
@Examples("player's tool has custom model data")
@RequiredPlugins("1.14+")
@Since("2.5")
@Since("2.5, INSERT VERSION (expanded data types)")
@RequiredPlugins("1.21.4+ (floats/flags/strings/colours)")
public class CondHasCustomModelData extends PropertyCondition<ItemType> {

static {
if (Skript.methodExists(ItemMeta.class, "hasCustomModelData")) {
if (Skript.methodExists(ItemMeta.class, "getCustomModelDataComponent")) {
// new style
register(CondHasCustomModelData.class, PropertyType.HAVE, "[custom] model data [1:floats|2:flags|3:strings|4:colo[u]rs]", "itemtypes");
} else {
// old style
register(CondHasCustomModelData.class, PropertyType.HAVE, "[custom] model data", "itemtypes");
}
}


private enum CMDType {
ANY,
FLOATS,
FLAGS,
STRINGS,
COLORS
}

private CMDType dataType;

@Override
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
dataType = CMDType.values()[parseResult.mark];
return super.init(expressions, matchedPattern, isDelayed, parseResult);
}

@Override
@SuppressWarnings("UnstableApiUsage")
public boolean check(ItemType item) {
return item.getItemMeta().hasCustomModelData();
ItemMeta meta = item.getItemMeta();
if (dataType == CMDType.ANY)
return meta.hasCustomModelData();
CustomModelDataComponent component = meta.getCustomModelDataComponent();
return switch (dataType) {
case FLOATS -> !component.getFloats().isEmpty();
case FLAGS -> !component.getFlags().isEmpty();
case STRINGS -> !component.getStrings().isEmpty();
case COLORS -> !component.getColors().isEmpty();
case ANY -> throw new IllegalStateException("Wrong path for CMDType.ANY.");
};
}

@Override
Expand All @@ -36,7 +68,7 @@ protected PropertyType getPropertyType() {

@Override
protected String getPropertyName() {
return "custom model data";
return "custom model data" + (dataType != CMDType.ANY ? " " + dataType.name().toLowerCase(Locale.ENGLISH) : "");
}

}
Expand Down
Loading