From d78bf61ca493691632f9026ff870fd54de41ea6b Mon Sep 17 00:00:00 2001 From: "Mr. Darth" <66969562+Mr-Darth@users.noreply.github.com> Date: Sun, 18 Aug 2024 00:03:32 +0300 Subject: [PATCH] Re-add support for itemtypes in CondIsOfType (#5073) --- .../njol/skript/conditions/CondIsOfType.java | 30 +++++++------- .../tests/syntaxes/conditions/CondIsOfType.sk | 39 +++++++++++++++++++ 2 files changed, 53 insertions(+), 16 deletions(-) create mode 100644 src/test/skript/tests/syntaxes/conditions/CondIsOfType.sk diff --git a/src/main/java/ch/njol/skript/conditions/CondIsOfType.java b/src/main/java/ch/njol/skript/conditions/CondIsOfType.java index 5ca721081bc..1a02f2a4ab4 100644 --- a/src/main/java/ch/njol/skript/conditions/CondIsOfType.java +++ b/src/main/java/ch/njol/skript/conditions/CondIsOfType.java @@ -20,6 +20,7 @@ import org.bukkit.entity.Entity; import org.bukkit.event.Event; +import org.bukkit.inventory.ItemStack; import org.eclipse.jdt.annotation.Nullable; import ch.njol.skript.aliases.ItemType; @@ -38,11 +39,8 @@ import ch.njol.util.Checker; import ch.njol.util.Kleenean; -/** - * @author Peter Güttinger - */ @Name("Is of Type") -@Description("Checks whether an item of an entity is of the given type. This is mostly useful for variables," + +@Description("Checks whether an item or an entity is of the given type. This is mostly useful for variables," + " as you can use the general 'is' condition otherwise (e.g. 'victim is a creeper').") @Examples({"tool is of type {selected type}", "victim is of type {villager type}"}) @@ -50,17 +48,17 @@ public class CondIsOfType extends Condition { static { - PropertyCondition.register(CondIsOfType.class, "of type[s] %entitytypes/entitydatas%", "itemstacks/entities"); + PropertyCondition.register(CondIsOfType.class, "of type[s] %itemtypes/entitydatas%", "itemstacks/entities"); } - @SuppressWarnings("null") + @SuppressWarnings("NotNullFieldNotInitialized") private Expression what; - @SuppressWarnings("null") + @SuppressWarnings("NotNullFieldNotInitialized") private Expression types; @SuppressWarnings("null") @Override - public boolean init(final Expression[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) { + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { what = exprs[0]; types = exprs[1]; setNegated(matchedPattern == 1); @@ -68,12 +66,12 @@ public boolean init(final Expression[] exprs, final int matchedPattern, final } @Override - public boolean check(final Event e) { - return what.check(e, - (Checker) o1 -> types.check(e, + public boolean check(Event event) { + return what.check(event, + (Checker) o1 -> types.check(event, (Checker) o2 -> { - if (o2 instanceof ItemType && o1 instanceof ItemType) { - return ((ItemType) o2).isSupertypeOf((ItemType) o1); + if (o2 instanceof ItemType && o1 instanceof ItemStack) { + return ((ItemType) o2).isSupertypeOf(new ItemType((ItemStack) o1)); } else if (o2 instanceof EntityData && o1 instanceof Entity) { return ((EntityData) o2).isInstance((Entity) o1); } else if (o2 instanceof ItemType && o1 instanceof Entity) { @@ -86,9 +84,9 @@ public boolean check(final Event e) { } @Override - public String toString(final @Nullable Event e, final boolean debug) { - return PropertyCondition.toString(this, PropertyType.BE, e, debug, what, - "of " + (types.isSingle() ? "type " : "types") + types.toString(e, debug)); + public String toString(@Nullable Event event, boolean debug) { + return PropertyCondition.toString(this, PropertyType.BE, event, debug, what, + "of " + (types.isSingle() ? "type " : "types ") + types.toString(event, debug)); } } diff --git a/src/test/skript/tests/syntaxes/conditions/CondIsOfType.sk b/src/test/skript/tests/syntaxes/conditions/CondIsOfType.sk new file mode 100644 index 00000000000..c4a7ce6e5e1 --- /dev/null +++ b/src/test/skript/tests/syntaxes/conditions/CondIsOfType.sk @@ -0,0 +1,39 @@ +test "item is of type condition": + assert stone is of type stone with "stone isn't of type stone" + assert 12 stone is of type stone with "12 stone isn't of type stone" + assert stone named "test" is of type stone with "named stone isn't of type stone" + assert stone is of type plain stone with "stone isn't of type plain stone" + + assert diamond sword of sharpness is of type diamond sword with "enchanted diamond sword isn't of type diamond sword" + assert stone sword is of type sword with "stone sword isn't of type sword" + + assert gold sword of smite named "test" is not of type iron sword with "enchanted named gold sword is of type iron sword" + + assert armor stand is of type armor stand with "armor stand isn't of type armor stand" + assert 12 armor stand is of type 1 armor stand with "12 armor stand isn't of type 1 armor stand" + assert armor stand named "test" is of type armor stand with "named armor stand isn't of type armor stand" + + assert ender pearl is of type ender pearl with "ender pearl isn't of type ender pearl" + assert ender pearl is of type plain ender pearl with "ender pearl isn't of type plain ender pearl" + +test "entity is of type condition": + spawn zombie at spawn of "world" + assert last spawned zombie is of type zombie with "zombie isn't of type zombie" + assert last spawned zombie is not of type ghast with "zombie isn't of type ghast" + set {_zombie} to last spawned zombie + assert {_zombie} is of type zombie with "zombie in a variable isn't of type zombie" + delete last spawned zombie + + spawn armor stand at spawn of "world" + assert last spawned armor stand is of type armor stand with "armor stand isn't of type armor stand" + assert last spawned armor stand is not of type ghast with "armor stand isn't of type ghast" + set {_armor-stand} to last spawned armor stand + assert {_armor-stand} is of type armor stand with "armor stand in a variable isn't of type armor stand" + delete last spawned armor stand + + spawn enderpearl at spawn of "world" + assert last spawned enderpearl is of type ender pearl with "enderpearl isn't of type enderpearl" + assert last spawned enderpearl is not of type ghast with "enderpearl isn't of type ghast" + set {_enderpearl} to last spawned enderpearl + assert {_enderpearl} is of type ender pearl with "enderpearl in a variable isn't of type enderpearl" + delete last spawned enderpearl