/
ChunkLoadCommand.java
236 lines (215 loc) · 9.31 KB
/
ChunkLoadCommand.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package com.denizenscript.denizen.scripts.commands.world;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.utilities.debugging.dB;
import com.denizenscript.denizen.utilities.depends.Depends;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.objects.dChunk;
import com.denizenscript.denizen.objects.dLocation;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.Duration;
import com.denizenscript.denizencore.objects.Element;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import net.citizensnpcs.api.event.NPCDespawnEvent;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.event.Cancellable;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.world.ChunkUnloadEvent;
import java.util.HashMap;
import java.util.Map;
public class ChunkLoadCommand extends AbstractCommand implements Listener {
// <--[command]
// @Name ChunkLoad
// @Syntax chunkload ({add}/remove/removeall) [<chunk>] (duration:<value>)
// @Required 1
// @Short Keeps a chunk actively loaded and allowing activity.
// @Group world
//
// @Description
// Forces a chunk to load and stay loaded in the world for the duration specified or until removed.
// This will not over server restarts.
// If no duration is specified it defaults to 0 (forever).
// While a chunk is loaded all normal activity such as crop growth and npc activity continues,
// other than activity that requires a nearby player.
//
// @Tags
// <w@world.loaded_chunks>
// <ch@chunk.is_loaded>
//
// @Usage
// Use to load a chunk.
// - chunkload ch@0,0,world
//
// @Usage
// Use to temporarily load a chunk.
// - chunkload ch@0,0,world duration:5m
//
// @Usage
// Use to stop loading a chunk.
// - chunkload remove ch@0,0,world
//
// @Usage
// Use to stop loading all chunks.
// - chunkload removeall
// -->
@Override
public void onEnable() {
Denizen denizen = DenizenAPI.getCurrentInstance();
denizen.getServer().getPluginManager().registerEvents(this, denizen);
if (Depends.citizens != null) {
denizen.getServer().getPluginManager().registerEvents(new ChunkLoadCommandNPCEvents(), denizen);
}
}
/*
* Keeps a chunk loaded
*/
private enum Action {ADD, REMOVE, REMOVEALL}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : ArgumentHelper.interpretArguments(scriptEntry.aHArgs)) {
if (arg.matchesEnum(Action.values())
&& !scriptEntry.hasObject("action")) {
scriptEntry.addObject("action", new Element(arg.getValue().toUpperCase()));
if (arg.getValue().equalsIgnoreCase("removeall")) {
scriptEntry.addObject("location", new dLocation(Bukkit.getWorlds().get(0), 0, 0, 0));
}
}
else if (arg.matchesArgumentType(dChunk.class)
&& !scriptEntry.hasObject("location")) {
scriptEntry.addObject("location", arg.asType(dChunk.class).getCenter());
}
else if (arg.matchesArgumentType(dLocation.class)
&& !scriptEntry.hasObject("location")) {
scriptEntry.addObject("location", arg.asType(dLocation.class));
}
else if (arg.matchesArgumentType(Duration.class)
&& !scriptEntry.hasObject("duration")) {
scriptEntry.addObject("duration", arg.asType(Duration.class));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("location")) {
throw new InvalidArgumentsException("Missing location argument!");
}
if (!scriptEntry.hasObject("action")) {
scriptEntry.addObject("action", new Element("ADD"));
}
if (!scriptEntry.hasObject("duration")) {
scriptEntry.addObject("duration", new Duration(0));
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
// Get objects
Element action = scriptEntry.getElement("action");
dLocation chunkloc = (dLocation) scriptEntry.getObject("location");
Duration length = (Duration) scriptEntry.getObject("duration");
if (scriptEntry.dbCallShouldDebug()) {
dB.report(scriptEntry, getName(),
action.debug()
+ chunkloc.debug()
+ length.debug());
}
Chunk chunk = chunkloc.getChunk();
String chunkString = chunk.getX() + ", " + chunk.getZ() + "," + chunkloc.getWorldName();
switch (Action.valueOf(action.asString())) {
case ADD:
if (length.getSeconds() != 0) {
chunkDelays.put(chunkString, System.currentTimeMillis() + length.getMillis());
}
else {
chunkDelays.put(chunkString, (long) 0);
}
dB.echoDebug(scriptEntry, "...added chunk " + chunk.getX() + ", " + chunk.getZ() + " with a delay of " + length.getSeconds() + " seconds.");
if (!chunk.isLoaded()) {
chunk.load();
}
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_14_R1)) {
chunk.setForceLoaded(true);
if (length.getSeconds() > 0) {
Bukkit.getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
if (chunkDelays.containsKey(chunkString) && chunkDelays.get(chunkString) <= System.currentTimeMillis()) {
chunk.setForceLoaded(false);
chunkDelays.remove(chunkString);
}
}
}, length.getTicks() + 20);
}
}
break;
case REMOVE:
if (chunkDelays.containsKey(chunkString)) {
chunkDelays.remove(chunkString);
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_14_R1)) {
chunk.setForceLoaded(false);
}
dB.echoDebug(scriptEntry, "...allowing unloading of chunk " + chunk.getX() + ", " + chunk.getZ());
}
else {
dB.echoError("Chunk was not on the load list!");
}
break;
case REMOVEALL:
dB.echoDebug(scriptEntry, "...allowing unloading of all stored chunks");
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_14_R1)) {
for (String chunkStr : chunkDelays.keySet()) {
dChunk loopChunk = dChunk.valueOf(chunkStr);
loopChunk.getChunk().setForceLoaded(false);
}
}
chunkDelays.clear();
break;
}
}
// Map of chunks with delays
Map<String, Long> chunkDelays = new HashMap<>();
@EventHandler
public void stopUnload(ChunkUnloadEvent e) {
if (!(e instanceof Cancellable)) { // Not cancellable in 1.14
return;
}
String chunkString = e.getChunk().getX() + ", " + e.getChunk().getZ();
if (chunkDelays.containsKey(chunkString)) {
if (chunkDelays.get(chunkString) == 0) {
((Cancellable) e).setCancelled(true);
}
else if (System.currentTimeMillis() < chunkDelays.get(chunkString)) {
((Cancellable) e).setCancelled(true);
}
else {
chunkDelays.remove(chunkString);
}
}
}
public class ChunkLoadCommandNPCEvents implements Listener {
@EventHandler
public void stopDespawn(NPCDespawnEvent e) {
if (e.getNPC() == null || !e.getNPC().isSpawned()) {
return;
}
Chunk chnk = e.getNPC().getEntity().getLocation().getChunk();
String chunkString = chnk.getX() + ", " + chnk.getZ();
if (chunkDelays.containsKey(chunkString)) {
if (chunkDelays.get(chunkString) == 0) {
e.setCancelled(true);
}
else if (System.currentTimeMillis() < chunkDelays.get(chunkString)) {
e.setCancelled(true);
}
else {
chunkDelays.remove(chunkString);
}
}
}
}
}