Skip to content

Commit

Permalink
Re-add support for itemtypes in CondIsOfType (SkriptLang#5073)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Darth authored Aug 17, 2024
1 parent e44ed6c commit d78bf61
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
30 changes: 14 additions & 16 deletions src/main/java/ch/njol/skript/conditions/CondIsOfType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,42 +39,39 @@
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}"})
@Since("1.4")
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);
return true;
}

@Override
public boolean check(final Event e) {
return what.check(e,
(Checker<Object>) o1 -> types.check(e,
public boolean check(Event event) {
return what.check(event,
(Checker<Object>) o1 -> types.check(event,
(Checker<Object>) 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) {
Expand All @@ -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));
}

}
39 changes: 39 additions & 0 deletions src/test/skript/tests/syntaxes/conditions/CondIsOfType.sk
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d78bf61

Please sign in to comment.