/
SyncCommand.java
88 lines (77 loc) · 3.04 KB
/
SyncCommand.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
package net.aufdemrand.denizencore.scripts.commands.queue;
import net.aufdemrand.denizencore.DenizenCore;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.BracedCommand;
import net.aufdemrand.denizencore.scripts.commands.Holdable;
import net.aufdemrand.denizencore.scripts.queues.ScriptQueue;
import net.aufdemrand.denizencore.scripts.queues.core.InstantQueue;
import net.aufdemrand.denizencore.utilities.scheduling.OneTimeSchedulable;
import java.util.HashMap;
import java.util.List;
public class SyncCommand extends BracedCommand implements Holdable {
// <--[command]
// @Name Sync
// @Syntax sync [<commands>]
// @Required 0
// @Short Runs commands synchronously. Inverse of <@link command async>.
// @Group queue
//
// @Description
// Runs commands synchronously. This means that anything executed within will run on the
// main server thread, without the possibility of corrupting anything that an asynchronous
// queue could theoretically do. This is only needed for use alongside <@link command async>.
//
// @Tags
// None
//
// @Usage
// Use to perform possibly not thread-safe commands.
// - sync:
// - edit the world, etc
//
// -->
@Override
public void onEnable() {
setBraced();
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
scriptEntry.addObject("braces", getBracedCommands(scriptEntry));
}
@Override
public void execute(ScriptEntry scriptEntry) {
ScriptQueue residingQueue = scriptEntry.getResidingQueue();
final InstantQueue queue = new InstantQueue("SYNC_COMMAND");
queue.addEntries(((List<BracedData>) scriptEntry.getObject("braces")).get(0).value);
queue.getAllDefinitions().putAll(residingQueue.getAllDefinitions());
if (residingQueue.cachedContext != null) {
queue.cachedContext = new HashMap<>();
queue.cachedContext.putAll(residingQueue.cachedContext);
}
// Setup a callback if the queue is being waited on
if (scriptEntry.shouldWaitFor()) {
// Record the ScriptEntry
final ScriptEntry se = scriptEntry;
queue.callBack(new Runnable() {
@Override
public void run() {
se.setFinished(true);
}
});
}
// If the current queue is asynchronous, delay this queue until the next tick
if (residingQueue.run_async) {
DenizenCore.schedule(new OneTimeSchedulable(new Runnable() {
@Override
public void run() {
queue.start();
}
}, 0));
}
else {
// TODO: warn user about running SyncCommand when already sync?
queue.start();
}
}
}