/
ScriptEngine.java
94 lines (78 loc) · 3.27 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
package net.aufdemrand.denizen.scripts;
import net.aufdemrand.denizen.Denizen;
import net.aufdemrand.denizen.scripts.commands.CommandExecuter;
import net.aufdemrand.denizen.scripts.queues.ScriptQueue;
import net.aufdemrand.denizen.scripts.requirements.RequirementChecker;
import net.aufdemrand.denizen.utilities.debugging.dB;
public class ScriptEngine {
final private Denizen denizen;
final private RequirementChecker requirementChecker;
final private CommandExecuter commandExecuter;
public ScriptEngine(Denizen denizenPlugin) {
denizen = denizenPlugin;
// Create Denizen Executer and RequirementChecker
commandExecuter = new CommandExecuter(denizen);
requirementChecker = new RequirementChecker(denizen);
}
public void revolve(ScriptQueue scriptQueue) {
// Check last ScriptEntry to see if it should be waited for
if (scriptQueue.getLastEntryExecuted() != null
&& scriptQueue.getLastEntryExecuted().shouldWaitFor())
if (!scriptQueue.getLastEntryExecuted().isDone()) return;
ScriptEntry scriptEntry = scriptQueue.getNext();
while (scriptEntry != null) {
// Find next ScriptEntry
scriptEntry.setSendingQueue(scriptQueue);
// Check if last entry is still holding up the queue
if (scriptQueue.getLastEntryExecuted() != null
&& scriptQueue.getLastEntryExecuted().getHoldTime() > System.currentTimeMillis()) {
break;
}
// Check allowed run-time of next ScriptEntry
if (scriptEntry.getRunTime() < System.currentTimeMillis()) {
// Mark script entry with Queue that is sending it to the executer
scriptEntry.setSendingQueue(scriptQueue);
// Execute the scriptEntry
try {
getScriptExecuter().execute(scriptEntry);
} catch (Throwable e) {
dB.echoError("Woah! An exception has been called with this command!");
if (!dB.showStackTraces)
dB.echoError("Enable '/denizen stacktrace' for the nitty-gritty.");
else e.printStackTrace();
}
// Set as last entry executed
scriptQueue.setLastEntryExecuted(scriptEntry);
if (scriptEntry.isInstant() && !scriptQueue.hasInjectedItems) {
// Remove from execution list
scriptEntry = scriptQueue.getNext();
}
else if (scriptQueue.hasInjectedItems) {
scriptQueue.hasInjectedItems = false;
break;
}
// If entry isn't instant, end the revolution and wait for another
else
break;
}
}
}
/**
* Gets the currently loaded instance of the RequirementChecker
*
* @return ScriptHelper
*
*/
public RequirementChecker getRequirementChecker() {
return requirementChecker;
}
/**
* Gets the currently loaded instance of the Command Executer
*
* @return CommandExecuter
*
*/
public CommandExecuter getScriptExecuter() {
return commandExecuter;
}
}