-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
ActionHandler.java
77 lines (61 loc) · 3.05 KB
/
ActionHandler.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
package com.denizenscript.denizen.npc.actions;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.scripts.containers.core.AssignmentScriptContainer;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.queues.ContextSource;
import com.denizenscript.denizencore.scripts.queues.ScriptQueue;
import com.denizenscript.denizencore.scripts.queues.core.InstantQueue;
import com.denizenscript.denizencore.utilities.debugging.Debug.DebugElement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ActionHandler {
public ActionHandler() {
}
public String doAction(String actionName, NPCTag npc, PlayerTag player, AssignmentScriptContainer assignment, Map<String, ObjectTag> context) {
if (context == null) {
context = new HashMap<>();
}
String determination = "none";
if (assignment == null) {
// dB.echoDebug("Tried to do 'on " + actionName + ":' but couldn't find a matching script.");
return determination;
}
if (!assignment.contains("actions.on " + actionName)) {
return determination;
}
Debug.report(assignment, "Action",
ArgumentHelper.debugObj("Type", "On " + actionName)
+ ArgumentHelper.debugObj("NPC", npc.toString())
+ assignment.getAsScriptArg().debug()
+ (player != null ? ArgumentHelper.debugObj("Player", player.getName()) : ""));
// Fetch script from Actions
List<ScriptEntry> script = assignment.getEntries(new BukkitScriptEntryData(player, npc), "actions.on " + actionName);
if (script.isEmpty()) {
return determination;
}
Debug.echoDebug(assignment, DebugElement.Header,
"Building action 'On " + actionName.toUpperCase() + "' for " + npc.toString());
// Add entries and context to the queue
ScriptQueue queue = new InstantQueue(assignment.getName());
queue.addEntries(script);
ContextSource.SimpleMap src = new ContextSource.SimpleMap();
src.contexts = context;
src.contexts.put("event_header", new ElementTag(actionName));
queue.setContextSource(src);
// Start the queue!
queue.start();
// Check the determination by asking the DetermineCommand
if (queue.determinations != null && queue.determinations.size() > 0) {
determination = queue.determinations.get(0);
}
// TODO: Multiple determination system
return determination;
}
}