/
AbstractTrigger.java
152 lines (128 loc) · 5.4 KB
/
AbstractTrigger.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
144
145
146
147
148
149
150
151
152
package com.denizenscript.denizen.scripts.triggers;
import com.denizenscript.denizen.Settings;
import com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.npc.traits.TriggerTrait;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizencore.events.OldEventManager;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.queues.ScriptQueue;
import com.denizenscript.denizencore.scripts.queues.core.InstantQueue;
import com.denizenscript.denizencore.scripts.queues.core.TimedQueue;
import com.denizenscript.denizencore.utilities.debugging.Debug.DebugElement;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.Location;
import java.util.*;
public abstract class AbstractTrigger {
protected String name;
public AbstractTrigger as(String triggerName) {
this.name = triggerName.toUpperCase();
// Register command with Registry
DenizenAPI.getCurrentInstance().getTriggerRegistry().register(triggerName, this);
onEnable();
return this;
}
public String getName() {
// Return the name of the trigger specified upon registration.
return name;
}
/**
* Part of the Plugin disable sequence.
* <p/>
* Can be '@Override'n by a Trigger which requires a method when bukkit sends a
* onDisable() to Denizen. (ie. Server shuts down or restarts)
*/
public void onDisable() {
// Nothing to do here on this level of abstraction.
}
/**
* Part of the Plugin enable sequence.
* <p/>
* Can be '@Override'n by a Trigger which requires a method when bukkit sends a
* onEnable() to Denizen. (ie. Server shuts down or restarts)
*/
public void onEnable() {
// Nothing to do here on this level of abstraction.
}
/**
* Part of the Plugin enable sequence.
* <p/>
* Can be '@Override'n by a Trigger which requires a method when being enabled via
* the Trigger Registry, usually upon startup.
*/
public AbstractTrigger activate() {
// Nothing to do here on this level of abstraction.
return this;
}
public boolean parse(NPCTag npc, PlayerTag player, InteractScriptContainer script) {
return parse(npc, player, script, null, null);
}
public boolean parse(NPCTag npc, PlayerTag player, InteractScriptContainer script, String id) {
return parse(npc, player, script, id, null);
}
public boolean parse(NPCTag npc, PlayerTag player, InteractScriptContainer script, String id, Map<String, ObjectTag> context) {
if (npc == null || player == null || script == null) {
return false;
}
List<ScriptEntry> entries = script.getEntriesFor(this.getClass(), player, npc, id, true);
if (entries.isEmpty()) {
return false;
}
Debug.echoDebug(script, DebugElement.Header, "Parsing " + name + " trigger: n@" + npc.getName() + "/p@" + player.getName());
// Create Queue
long speedTicks;
if (script.contains("SPEED")) {
speedTicks = DurationTag.valueOf(script.getString("SPEED", "0")).getTicks();
}
else {
speedTicks = DurationTag.valueOf(Settings.interactQueueSpeed()).getTicks();
}
ScriptQueue queue;
if (speedTicks > 0) {
queue = new TimedQueue(script.getName()).setSpeed(speedTicks).addEntries(entries);
}
else {
queue = new InstantQueue(script.getName()).addEntries(entries);
}
// Add all entries to set it up
queue.addEntries(entries);
// Add context
if (context != null) {
OldEventManager.OldEventContextSource oecs = new OldEventManager.OldEventContextSource();
oecs.contexts = context;
queue.setContextSource(oecs);
}
// Start it
queue.start();
return true;
}
/**
* This method will find all NPCs within a certain range of a location that
* have a trigger, and the trigger is enabled.
*
* @param location the location to search around
* @param maxRange how far to search
* @return The Set of NPCs that are
*/
// TODO: Delete?
public Set<NPC> getActiveNPCsWithinRangeWithTrigger(Location location, int maxRange) {
Set<NPC> closestNPCs = new HashSet<>();
Iterator<NPC> it = CitizensAPI.getNPCRegistry().iterator();
while (it.hasNext()) {
NPC npc = it.next();
if (npc.isSpawned()
&& npc.getEntity().getLocation().getWorld().equals(location.getWorld())
&& npc.getEntity().getLocation().distance(location) < maxRange
&& npc.hasTrait(TriggerTrait.class)
&& npc.getTrait(TriggerTrait.class).isEnabled(name)) {
closestNPCs.add(npc);
}
}
return closestNPCs;
}
}