Skip to content
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

Re-add support for itemtypes in CondIsOfType #5073

Merged
merged 8 commits into from
Aug 17, 2024
Merged
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));
}

}