/
SystemTimeScriptEvent.java
145 lines (125 loc) · 4.12 KB
/
SystemTimeScriptEvent.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.denizenscript.denizencore.events.core;
import com.denizenscript.denizencore.events.ScriptEvent;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.DenizenCore;
import java.util.Calendar;
public class SystemTimeScriptEvent extends ScriptEvent {
// <--[event]
// @Events
// system time [<HH:MM>/hourly/minutely/secondly]
//
// @Switch every:<count> to only run the event every *count* times (like "on system time secondly every:5" for every 5 seconds).
//
// @Regex ^on system time (\d\d\:\d\d|hourly|minutely|secondly)$
//
// @Triggers when the system time changes to the specified value.
// The system time is the real world time set in the server's operating system.
// It is not necessarily in sync with the game server time, which may vary (for example, when the server is lagging).
// For events based on in-game time passage, use <@link event delta time> or <@link command wait>.
//
// @Context
// <context.hour> returns the exact hour of the system time.
// <context.minute> returns the exact minute of the system time.
//
// -->
public static SystemTimeScriptEvent instance;
public SystemTimeScriptEvent() {
instance = this;
}
@Override
public boolean couldMatch(ScriptPath path) {
return path.eventLower.startsWith("system time");
}
public ElementTag hour;
public ScriptEntryData data = null;
public ElementTag minute;
public long seconds;
@Override
public ScriptEntryData getScriptEntryData() {
return data;
}
@Override
public boolean matches(ScriptPath path) {
String time = path.rawEventArgAt(2);
String countString = path.switches.get("every");
int count = countString == null ? 1 : Integer.parseInt(countString);
if (time.equals("secondly")) {
return seconds % count == 0;
}
if (time.equals("minutely")) {
if (!minuteChanged) {
return false;
}
long minutes = seconds / 60;
return minutes % count == 0;
}
if (time.equals("hourly")) {
if (!minuteChanged || lM != 0) {
return false;
}
long hours = seconds / 3600;
return hours % count == 0;
}
return minuteChanged && time.equals(hour.asString() + ":" + minute.asString());
}
@Override
public String getName() {
return "SystemTime";
}
@Override
public ObjectTag getContext(String name) {
if (name.equals("hour")) {
return hour;
}
else if (name.equals("minute")) {
return minute;
}
return super.getContext(name);
}
boolean enab = false;
@Override
public void init() {
enab = true;
}
@Override
public void destroy() {
enab = false;
}
int lH = 0;
int lM = 0;
long lS = 0;
boolean minuteChanged = true;
public void checkTime() {
if (!enab) {
return;
}
seconds = System.currentTimeMillis() / 1000;
if (lS == seconds) {
return;
}
lS = seconds;
Calendar calendar = Calendar.getInstance();
int h = calendar.get(Calendar.HOUR_OF_DAY);
int m = calendar.get(Calendar.MINUTE);
minuteChanged = lH != h || lM != m;
lH = h;
lM = m;
if (h < 10) {
hour = new ElementTag("0" + h);
}
else {
hour = new ElementTag(h);
}
if (m < 10) {
minute = new ElementTag("0" + m);
}
else {
minute = new ElementTag(m);
}
data = DenizenCore.getImplementation().getEmptyScriptEntryData();
fire();
}
}