/
dNPCRegistry.java
240 lines (213 loc) · 7.59 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package net.aufdemrand.denizen.npc;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.Denizen;
import net.aufdemrand.denizen.flags.FlagManager;
import net.aufdemrand.denizen.npc.actions.ActionHandler;
import net.aufdemrand.denizen.objects.dNPC;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.depends.Depends;
import net.aufdemrand.denizencore.events.OldEventManager;
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 net.citizensnpcs.api.trait.trait.Equipment;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
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 {
private static Map<Integer, dNPC> denizenNPCs = new ConcurrentHashMap<Integer, dNPC>(8, 0.9f, 1);
private static Map<Integer, Inventory> npcInventories = new ConcurrentHashMap<Integer, Inventory>(8, 0.9f, 1);
public static dNPCRegistry getCurrentInstance() {
return DenizenAPI.getCurrentInstance().getNPCRegistry();
}
private Denizen plugin;
private ActionHandler actionHandler;
public dNPCRegistry(Denizen denizen) {
plugin = denizen;
if (Depends.citizens != null)
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;
}
public static boolean _isRegistered(NPC npc) {
return denizenNPCs.containsKey(npc.getId());
}
public static boolean _isRegistered(int id) {
return denizenNPCs.containsKey(id);
}
public static void _registerNPC(dNPC denizenNPC) {
if (denizenNPC == null || !denizenNPC.isValid()) return;
int id = denizenNPC.getId();
if (!denizenNPCs.containsKey(id)) {
denizenNPCs.put(id, denizenNPC);
Inventory npcInventory = Bukkit.getServer().createInventory(denizenNPC, InventoryType.PLAYER);
npcInventory.setContents(Arrays.copyOf(denizenNPC.getInventoryTrait().getContents(), npcInventory.getSize()));
npcInventories.put(id, npcInventory);
}
}
private static void _registerNPC(NPC npc) {
if (npc == null) return;
if (!denizenNPCs.containsKey(npc.getId())) {
_registerNPC(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());
}
public static dNPC getDenizen(int id) {
if (denizenNPCs.containsKey(id))
return denizenNPCs.get(id);
else
return null;
}
/**
* Returns a dInventory object from the Inventory trait of a valid NPC.
*
* @param npc the Citizens NPC
* @return the NPC's dInventory
*/
public static Inventory getInventory(NPC npc) {
if (npc == null) return null;
if (!npcInventories.containsKey(npc.getId()))
_registerNPC(npc);
return npcInventories.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.echoError("Report this error to aufdemrand! Err: posconcurrency1");
}
}
else if (npc.getValue().isSpawned())
npcs.add(npc.getValue());
}
return npcs;
}
// <--[action]
// @Actions
// spawn
// @Triggers when the NPC is spawned.
// This will fire whenever an NPC's chunk is loaded, or a spawn command is issued.
//
// @Context
// None
// -->
/**
* 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 spawns'
OldEventManager.doEvents(Arrays.asList
("npc spawns"),
new BukkitScriptEntryData(null, dNPC.mirrorCitizensNPC(event.getNPC())), 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 issued, 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) {
dNPC npc = getDenizen(event.getNPC().getId());
// Do world script event 'On NPC Despawns'
if (npc != null)
OldEventManager.doEvents(Arrays.asList("npc despawns"), new BukkitScriptEntryData(null, npc), null);
if (npc != null)
npc.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) {
NPC npc = event.getNPC();
getDenizen(npc).action("remove", null);
if (_isRegistered(npc)) {
denizenNPCs.remove(npc.getId());
npcInventories.remove(npc.getId());
}
FlagManager.clearNPCFlags(npc.getId());
}
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
Inventory inventory = event.getInventory();
if (inventory.getHolder() instanceof dNPC) {
dNPC npc = (dNPC) inventory.getHolder();
npc.getInventory().setContents(inventory.getContents());
Equipment equipment = npc.getEquipmentTrait();
for (int i = 0; i < 5; i++)
equipment.set(i, inventory.getItem(i));
}
}
}