/
TickScriptEvent.java
85 lines (69 loc) · 2.11 KB
/
TickScriptEvent.java
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
package com.denizenscript.denizencore.events.core;
import com.denizenscript.denizencore.events.ScriptEvent;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.DenizenCore;
public class TickScriptEvent extends ScriptEvent {
// <--[event]
// @Events
// tick
//
// @Switch every:<count> to only run the event every *count* times (like "every:5" for every 5 ticks).
//
// @Regex ^on tick$
//
// @Warning This event fires very rapidly and is usually not the most ideal way to handle things.
//
// @Triggers every single tick.
//
// @Context
// <context.tick> how many ticks have passed since the server started.
//
// -->
public static TickScriptEvent instance;
public long ticks = 0;
public ScriptEntryData data = null;
@Override
public void reset() {
data = DenizenCore.getImplementation().getEmptyScriptEntryData();
super.reset();
}
@Override
public ScriptEntryData getScriptEntryData() {
return data;
}
@Override
public ObjectTag getContext(String name) {
if (name.equals("tick")) {
return new ElementTag(ticks);
}
return super.getContext(name);
}
public TickScriptEvent() {
instance = this;
}
@Override
public boolean couldMatch(ScriptPath path) {
return path.eventLower.startsWith("tick");
}
@Override
public boolean matches(ScriptPath path) {
String countString = path.switches.get("every");
int count = countString == null ? 1 : Integer.parseInt(countString);
return ticks % count == 0;
}
@Override
public String getName() {
return "Tick";
}
public boolean enabled = false;
@Override
public void init() {
enabled = true;
}
@Override
public void destroy() {
enabled = false;
}
}