/
ChunkLoadCommand.java
143 lines (115 loc) · 5.23 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
package net.aufdemrand.denizen.scripts.commands.world;
import java.util.HashMap;
import java.util.Map;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.Element;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.objects.Duration;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;
import net.citizensnpcs.api.event.NPCDespawnEvent;
import org.bukkit.Chunk;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.world.ChunkUnloadEvent;
public class ChunkLoadCommand extends AbstractCommand implements Listener {
@Override
public void onEnable() {
denizen.getServer().getPluginManager().registerEvents(this, denizen);
}
/*
* Keeps a chunk loaded
*/
private enum Action { ADD, REMOVE, REMOVEALL }
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
Chunk chunk = null;
Duration length = new Duration(0);
Action action = Action.ADD;
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (arg.matchesEnum(Action.values())
&& !scriptEntry.hasObject("action"))
scriptEntry.addObject("action", new Element(arg.getValue().toUpperCase()));
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 throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg.raw_value);
}
if (!scriptEntry.hasObject("location"))
throw new InvalidArgumentsException(Messages.DEBUG_SET_LOCATION);
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(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("...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("...allowing unloading of chunk "+chunk.getX() + ", "+ chunk.getZ());
}
else
dB.echoError("Chunk was not on the load list!");
break;
case REMOVEALL:
dB.echoDebug("...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);
}
}
@EventHandler
public void stopDespawn(NPCDespawnEvent e) {
Chunk chnk = e.getNPC().getBukkitEntity().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);
}
}
}