|
| 1 | +package com.github.skriptdev.skript.plugin.elements.conditions.other; |
| 2 | + |
| 3 | +import com.github.skriptdev.skript.api.skript.registration.SkriptRegistration; |
| 4 | +import io.github.syst3ms.skriptparser.lang.Expression; |
| 5 | +import io.github.syst3ms.skriptparser.lang.Literal; |
| 6 | +import io.github.syst3ms.skriptparser.lang.TriggerContext; |
| 7 | +import io.github.syst3ms.skriptparser.lang.base.ConditionalExpression; |
| 8 | +import io.github.syst3ms.skriptparser.parsing.ParseContext; |
| 9 | +import io.github.syst3ms.skriptparser.types.Type; |
| 10 | +import org.jetbrains.annotations.NotNull; |
| 11 | + |
| 12 | +import java.util.Optional; |
| 13 | + |
| 14 | +public class CondObjectOfType extends ConditionalExpression { |
| 15 | + |
| 16 | + public static void register(SkriptRegistration reg) { |
| 17 | + reg.newExpression(CondObjectOfType.class, Boolean.class, true, |
| 18 | + "%objects% (is a[n]|are) %*types%") |
| 19 | + .name("Object is of Type") |
| 20 | + .description("Checks if the objects are of the specified types.") |
| 21 | + .examples("if {var} is a Player:", |
| 22 | + "if {var} is an Entity:") |
| 23 | + .since("INSERT VERSION") |
| 24 | + .register(); |
| 25 | + } |
| 26 | + |
| 27 | + private Expression<?> objects; |
| 28 | + private Type<?> type; |
| 29 | + |
| 30 | + @SuppressWarnings("unchecked") |
| 31 | + @Override |
| 32 | + public boolean init(Expression<?> @NotNull [] expressions, int matchedPattern, @NotNull ParseContext parseContext) { |
| 33 | + this.objects = expressions[0]; |
| 34 | + Literal<Type<?>> type = (Literal<Type<?>>) expressions[1]; |
| 35 | + Optional<? extends Type<?>> single = type.getSingle(); |
| 36 | + if (single.isEmpty()) { |
| 37 | + return false; |
| 38 | + } |
| 39 | + this.type = single.get(); |
| 40 | + return true; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public boolean check(@NotNull TriggerContext ctx) { |
| 45 | + return this.objects.check(ctx, o -> |
| 46 | + this.type.getTypeClass().isAssignableFrom(o.getClass())); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public String toString(@NotNull TriggerContext ctx, boolean debug) { |
| 51 | + String plural = this.objects.isSingle() ? " is a " : " are "; |
| 52 | + return this.objects + plural + this.type; |
| 53 | + } |
| 54 | + |
| 55 | +} |
0 commit comments