Skip to content

Commit d591070

Browse files
committed
CondObjectOfType - add
1 parent 5963348 commit d591070

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

src/main/java/com/github/skriptdev/skript/plugin/elements/conditions/ConditionHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.github.skriptdev.skript.plugin.elements.conditions.entity.CondEntityIsTameable;
99
import com.github.skriptdev.skript.plugin.elements.conditions.entity.CondEntityIsTamed;
1010
import com.github.skriptdev.skript.plugin.elements.conditions.item.CondInventoryCanHold;
11+
import com.github.skriptdev.skript.plugin.elements.conditions.other.CondObjectOfType;
1112
import com.github.skriptdev.skript.plugin.elements.conditions.player.CondPlayerHasPermission;
1213
import com.github.skriptdev.skript.plugin.elements.conditions.player.CondPlayerHasPlayedBefore;
1314
import com.github.skriptdev.skript.plugin.elements.conditions.player.CondPlayerMovementCrouching;
@@ -43,6 +44,9 @@ public static void register(SkriptRegistration registration) {
4344
// ITEM
4445
CondInventoryCanHold.register(registration);
4546

47+
// OTHER
48+
CondObjectOfType.register(registration);
49+
4650
// PLAYER
4751
CondPlayerHasPermission.register(registration);
4852
CondPlayerHasPlayedBefore.register(registration);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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

Comments
 (0)