-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
ScriptEngine.java
115 lines (98 loc) · 3.9 KB
/
ScriptEngine.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
package net.aufdemrand.denizencore.scripts.queues;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.CommandExecuter;
import net.aufdemrand.denizencore.scripts.queues.core.Delayable;
import net.aufdemrand.denizencore.utilities.debugging.dB;
public class ScriptEngine {
final private CommandExecuter commandExecuter;
public ScriptEngine() {
// Create Denizen CommandExecuter
commandExecuter = new CommandExecuter();
}
boolean shouldHold(ScriptQueue scriptQueue) {
if (scriptQueue instanceof Delayable && ((Delayable) scriptQueue).isPaused()) {
return true;
}
if (scriptQueue.getLastEntryExecuted() != null
&& scriptQueue.getLastEntryExecuted().shouldWaitFor()) {
if (!(scriptQueue instanceof Delayable)) {
dB.echoDebug(scriptQueue.getLastEntryExecuted(), "Forcing queue " + scriptQueue.id + " into a timed queue...");
scriptQueue.forceToTimed(null);
return true;
}
else {
return true;
}
}
return false;
}
public void revolveOnceForce(ScriptQueue scriptQueue) {
ScriptEntry scriptEntry = scriptQueue.getNext();
if (scriptEntry == null) {
return;
}
scriptEntry.setSendingQueue(scriptQueue);
try {
getScriptExecuter().execute(scriptEntry);
}
catch (Throwable e) {
dB.echoError(scriptEntry.getResidingQueue(), "An exception has been called with this command!");
dB.echoError(scriptEntry.getResidingQueue(), e);
}
scriptQueue.setLastEntryExecuted(scriptEntry);
}
public void revolve(ScriptQueue scriptQueue) {
// Check last ScriptEntry to see if it should be waited for
if (shouldHold(scriptQueue)) {
return;
}
// Okay to run next scriptEntry
ScriptEntry scriptEntry = scriptQueue.getNext();
// Loop through the entries
while (scriptEntry != null) {
// Mark script entry with Queue that is sending it to the executor
scriptEntry.setSendingQueue(scriptQueue);
try {
// Execute the scriptEntry
getScriptExecuter().execute(scriptEntry);
}
// Absolutely NO errors beyond this point!
catch (Throwable e) {
dB.echoError(scriptEntry.getResidingQueue(), "Woah! An exception has been called with this command!");
dB.echoError(scriptEntry.getResidingQueue(), e);
}
// Set as last entry executed
scriptQueue.setLastEntryExecuted(scriptEntry);
// Check if the scriptQueue is delayed (EG, via wait)
if (scriptQueue instanceof Delayable) {
if (((Delayable) scriptQueue).isDelayed()) {
break;
}
if (((Delayable) scriptQueue).isPaused()) {
break;
}
}
// If the entry is instant, and not injected, get the next Entry
if (scriptEntry.isInstant()) {
// If it's holding, even if it's instant, just stop and wait
if (shouldHold(scriptQueue)) {
return;
}
// Remove from execution list
scriptEntry = scriptQueue.getNext();
}
// If entry isn't instant, end the revolutions and wait
else {
break;
}
}
}
/**
* Gets the currently loaded instance of the Command Executer
*
* @return CommandExecuter
*/
public CommandExecuter getScriptExecuter() {
return commandExecuter;
}
}