-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathDelay.java
More file actions
132 lines (110 loc) · 4.54 KB
/
Delay.java
File metadata and controls
132 lines (110 loc) · 4.54 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package ch.njol.skript.effects;
import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Example;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.Trigger;
import ch.njol.skript.lang.TriggerItem;
import ch.njol.skript.timings.SkriptTimings;
import ch.njol.skript.util.Timespan;
import ch.njol.skript.variables.Variables;
import ch.njol.skript.variables.VariablesMap;
import ch.njol.util.Kleenean;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.Set;
import java.util.WeakHashMap;
@Name("Delay")
@Description("Delays the script's execution by a given timespan. Please note that delays are not persistent, e.g. trying to create a tempban script with <code>ban player → wait 7 days → unban player</code> will not work if you restart your server anytime within these 7 days. You also have to be careful even when using small delays!")
@Example("wait 2 minutes")
@Example("halt for 5 minecraft hours")
@Example("wait a tick")
@Since("1.4")
public class Delay extends Effect {
static {
Skript.registerEffect(Delay.class, "(wait|halt) [for] %timespan%");
}
@SuppressWarnings("NotNullFieldNotInitialized")
protected Expression<Timespan> duration;
@SuppressWarnings({"unchecked", "null"})
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
getParser().setHasDelayBefore(Kleenean.TRUE);
duration = (Expression<Timespan>) exprs[0];
if (duration instanceof Literal) { // If we can, do sanity check for delays
Timespan timespan = ((Literal<Timespan>) duration).getSingle();
if (timespan.isInfinite()) {
Skript.error("Delaying for an eternity is not allowed. Use the 'stop' effect instead.");
return false;
}
long millis = timespan.getAs(Timespan.TimePeriod.MILLISECOND);
if (millis < 50) {
Skript.warning("Delays less than one tick are not possible, defaulting to one tick.");
}
}
return true;
}
@Override
@Nullable
protected TriggerItem walk(Event event) {
debug(event, true);
long start = Skript.debug() ? System.nanoTime() : 0;
TriggerItem next = getNext();
if (next != null && Skript.getInstance().isEnabled()) { // See https://github.com/SkriptLang/Skript/issues/3702
Timespan duration = this.duration.getSingle(event);
if (duration == null)
return null;
// Back up local variables
VariablesMap localVars = Variables.removeLocals(event);
Bukkit.getScheduler().scheduleSyncDelayedTask(Skript.getInstance(), () -> {
addDelayedEvent(event);
Skript.debug(getIndentation() + "... continuing after " + (System.nanoTime() - start) / 1_000_000_000. + "s");
// Re-set local variables
if (localVars != null)
Variables.setLocalVariables(event, localVars);
Object timing = null; // Timings reference must be kept so that it can be stopped after TriggerItem execution
if (SkriptTimings.enabled()) { // getTrigger call is not free, do it only if we must
Trigger trigger = getTrigger();
if (trigger != null)
timing = SkriptTimings.start(trigger.getDebugLabel());
}
TriggerItem.walk(next, event);
Variables.removeLocals(event); // Clean up local vars, we may be exiting now
SkriptTimings.stop(timing); // Stop timing if it was even started
}, Math.max(duration.getAs(Timespan.TimePeriod.TICK), 1)); // Minimum delay is one tick, less than it is useless!
}
return null;
}
@Override
protected void execute(Event event) {
throw new UnsupportedOperationException();
}
@Override
public String toString(@Nullable Event event, boolean debug) {
return "wait for " + duration.toString(event, debug) + (event == null ? "" : "...");
}
private static final Set<Event> DELAYED =
Collections.synchronizedSet(Collections.newSetFromMap(new WeakHashMap<>()));
/**
* The main method for checking if the execution of {@link TriggerItem}s has been delayed.
* @param event The event to check for a delay.
* @return Whether {@link TriggerItem} execution has been delayed.
*/
public static boolean isDelayed(Event event) {
return DELAYED.contains(event);
}
/**
* The main method for marking the execution of {@link TriggerItem}s as delayed.
* @param event The event to mark as delayed.
*/
public static void addDelayedEvent(Event event) {
DELAYED.add(event);
}
}