-
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathCondExpressionStatement.java
More file actions
106 lines (88 loc) · 3.09 KB
/
CondExpressionStatement.java
File metadata and controls
106 lines (88 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.btk5h.skriptmirror.skript;
import ch.njol.skript.Skript;
import ch.njol.skript.config.SectionNode;
import ch.njol.skript.effects.Delay;
import ch.njol.skript.lang.Condition;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.TriggerItem;
import ch.njol.skript.log.SkriptLogger;
import ch.njol.util.Kleenean;
import com.btk5h.skriptmirror.ObjectWrapper;
import com.btk5h.skriptmirror.SkriptMirror;
import com.btk5h.skriptmirror.skript.reflect.ExprJavaCall;
import com.btk5h.skriptmirror.util.SkriptReflection;
import com.btk5h.skriptmirror.util.SkriptUtil;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.skriptlang.skript.registration.SyntaxInfo;
import org.skriptlang.skript.registration.SyntaxRegistry;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CondExpressionStatement extends Condition {
public static void register(SyntaxRegistry registry) {
registry.register(
SyntaxRegistry.CONDITION,
SyntaxInfo.builder(CondExpressionStatement.class)
.addPattern("[1:await] <.+>")
.supplier(CondExpressionStatement::new)
.priority(SkriptMirror.SHADOW_REALM)
.build());
}
private static final ExecutorService threadPool = Executors.newCachedThreadPool();
private Expression<?> arg;
private boolean isAsynchronous;
private boolean isCondition;
@Override
public boolean check(Event e) {
Object result = ObjectWrapper.unwrapIfNecessary(arg.getSingle(e));
return !isCondition || isTruthy(result);
}
private boolean isTruthy(Object o) {
return o != Boolean.FALSE
&& o != null
&& (!(o instanceof Number) || !(((Number) o).doubleValue() == 0 || Double.isNaN(((Number) o).doubleValue())));
}
@Override
protected TriggerItem walk(Event e) {
if (isAsynchronous) {
Delay.addDelayedEvent(e);
Object localVariables = SkriptReflection.getLocals(e);
CompletableFuture.runAsync(() -> {
SkriptReflection.putLocals(localVariables, e);
check(e);
}, threadPool)
.thenAccept(res -> Bukkit.getScheduler().runTask(SkriptMirror.getInstance(), () -> {
if (getNext() != null)
TriggerItem.walk(getNext(), e);
SkriptReflection.removeLocals(e);
}));
return null;
}
return super.walk(e);
}
@Override
public String toString(Event e, boolean debug) {
return arg.toString(e, debug);
}
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed,
SkriptParser.ParseResult parseResult) {
String rawInput = parseResult.regexes.getFirst().group();
// parse directly as ExprJavaCall to avoid useless parse attempts
arg = ExprJavaCall.parse(rawInput);
if (arg == null) {
return false;
}
isAsynchronous = (parseResult.mark & 1) == 1;
isCondition = SkriptLogger.getNode() instanceof SectionNode;
if (isCondition && isAsynchronous) {
Skript.error("Asynchronous java calls may not be used as conditions.");
return false;
}
if (isAsynchronous)
getParser().setHasDelayBefore(Kleenean.TRUE);
return SkriptUtil.canInitSafely(arg);
}
}