-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
ChunkLoadCommand.java
158 lines (131 loc) · 6.24 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
package net.aufdemrand.denizen.scripts.commands.world;
import net.aufdemrand.denizen.Denizen;
import net.aufdemrand.denizen.objects.dChunk;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.depends.Depends;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Duration;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import net.citizensnpcs.api.event.NPCDespawnEvent;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
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 {
@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 (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
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) throws CommandExecutionException {
// Get objects
Element action = scriptEntry.getElement("action");
dLocation chunkloc = (dLocation) scriptEntry.getObject("location");
Duration length = (Duration) scriptEntry.getObject("duration");
dB.report(scriptEntry, getName(),
action.debug()
+ chunkloc.debug()
+ length.debug());
Chunk chunk = chunkloc.getChunk();
String chunkString = chunk.getX() + ", " + chunk.getZ();
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();
break;
case REMOVE:
if (chunkDelays.containsKey(chunkString)) {
chunkDelays.remove(chunkString);
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");
chunkDelays.clear();
break;
}
}
// Map of chunks with delays
Map<String, Long> chunkDelays = new HashMap<String, Long>();
@EventHandler
public void stopUnload(ChunkUnloadEvent e) {
String chunkString = e.getChunk().getX() + ", " + e.getChunk().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);
}
}
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);
}
}
}
}