/
dNPCRegistry.java
171 lines (149 loc) · 5.13 KB
/
dNPCRegistry.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package net.aufdemrand.denizen.npc;
import net.aufdemrand.denizen.Denizen;
import net.aufdemrand.denizen.objects.dNPC;
import net.aufdemrand.denizen.npc.actions.ActionHandler;
import net.aufdemrand.denizen.scripts.containers.core.WorldScriptHelper;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.event.NPCDespawnEvent;
import net.citizensnpcs.api.event.NPCRemoveEvent;
import net.citizensnpcs.api.event.NPCSpawnEvent;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/**
* Used for keeping track and retrieving dNPC objects which offer some Denizen-specific
* methods when dealing with NPCs.
*
* @author Jeremy Schroeder
*
*/
public class dNPCRegistry implements Listener {
public static Map<Integer, dNPC> denizenNPCs = new ConcurrentHashMap<Integer, dNPC>(8, 0.9f, 1);
public static dNPCRegistry getCurrentInstance() {
return DenizenAPI.getCurrentInstance().getNPCRegistry();
}
private Denizen plugin;
private ActionHandler actionHandler;
public dNPCRegistry(Denizen denizen) {
plugin = denizen;
plugin.getServer().getPluginManager().registerEvents(this, plugin);
actionHandler = new ActionHandler(plugin);
}
/**
* Gets the currently loaded instance of the ActionHandler
*
* @return ActionHandler
*
*/
public ActionHandler getActionHandler() {
return actionHandler;
}
private static boolean _isRegistered (NPC npc) {
return denizenNPCs.containsKey(npc.getId());
}
private static void _registerNPC(NPC npc) {
if (npc == null) return;
if (!denizenNPCs.containsKey(npc.getId())) {
denizenNPCs.put(npc.getId(), new dNPC(npc));
}
// dB.log("Constructing NPC " + getDenizen(npc).toString());
}
/**
* Returns a dNPC object when given a valid NPC. DenizenNPCs have some methods
* specific to Denizen functionality as well as easy access to the attached NPC and LivingEntity.
*
* @param npc the Citizens NPC
*
* @return a dNPC
*
*/
public static dNPC getDenizen(NPC npc) {
if (npc == null) return null;
if (!denizenNPCs.containsKey(npc.getId()))
_registerNPC(npc);
return denizenNPCs.get(npc.getId());
}
/**
* Similar to getting NPCs from Citizens' NPCRegistry, but this will filter out
* unspawned NPCs
*
* @return map of NPC, dNPC of all spawned NPCs
*
*/
public static Set<dNPC> getSpawnedNPCs() {
Iterator<Map.Entry<Integer, dNPC>> it = denizenNPCs.entrySet().iterator();
Set<dNPC> npcs = new HashSet<dNPC>();
while (it.hasNext()) {
Map.Entry<Integer, dNPC> npc = it.next();
if (npc.getValue().getCitizen() == null) {
dB.log(ChatColor.RED + "Removed NPC from dRegistry.");
try { denizenNPCs.remove(npc.getKey()); } catch (Exception e) { dB.echoDebug("Report this error to aufdemrand! Err: posconcurrency1"); }
}
else if (npc.getValue().isSpawned())
npcs.add(npc.getValue());
}
return npcs;
}
/**
* Fires the 'On Spawn:' action in an NPCs Assignment, if set.
*
* @param event NPCSpawnEvent
*
*/
@EventHandler
public void onSpawn(NPCSpawnEvent event) {
_registerNPC(event.getNPC());
// Do world script event 'On NPC Completes Navigation'
WorldScriptHelper.doEvents(Arrays.asList
("npc spawns"),
dNPC.mirrorCitizensNPC(event.getNPC()), null, null);
// On Spawn action
getDenizen(event.getNPC()).action("spawn", null);
}
// <--[action]
// @Actions
// despawn
// @Triggers when the NPC is despawned.
// This can be because a command was issues, or a chunk has been unloaded.
//
// @Context
// None
// -->
/**
* Fires a world script event and then NPC action when the NPC despawns.
*
* @param event NPCDespawnEvent
*/
@EventHandler
public void despawn(NPCDespawnEvent event) {
// Do world script event 'On NPC Completes Navigation'
WorldScriptHelper.doEvents(Arrays.asList
("npc despawns"),
dNPC.mirrorCitizensNPC(event.getNPC()), null, null);
getDenizen(event.getNPC()).action("despawn", null);
}
// <--[action]
// @Actions
// remove
// @Triggers when the NPC is removed.
//
// @Context
// None
// -->
/**
* Removes an NPC from the Registry when removed from Citizens.
*
* @param event NPCRemoveEvent
*/
@EventHandler
public void onRemove(NPCRemoveEvent event) {
getDenizen(event.getNPC()).action("remove", null);
if (_isRegistered(event.getNPC()))
denizenNPCs.remove(event.getNPC().getId());
dB.log(ChatColor.RED + "Deconstructing Denizen NPC " + event.getNPC().getName() + "/" + event.getNPC().getId());
}
}