-
-
Notifications
You must be signed in to change notification settings - Fork 307
/
CitizensNPCRegistry.java
250 lines (209 loc) · 6.87 KB
/
CitizensNPCRegistry.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
241
242
243
244
245
246
247
248
249
250
package net.citizensnpcs.npc;
import gnu.trove.map.hash.TIntObjectHashMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.event.NPCCreateEvent;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCDataStore;
import net.citizensnpcs.api.npc.NPCRegistry;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.util.NMS;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
public class CitizensNPCRegistry implements NPCRegistry {
private final NPCCollection npcs = TROVE_EXISTS ? new TroveNPCCollection() : new MapNPCCollection();
private final NPCDataStore saves;
public CitizensNPCRegistry(NPCDataStore store) {
saves = store;
}
@Override
public NPC createNPC(EntityType type, String name) {
return createNPC(type, UUID.randomUUID(), generateUniqueId(), name);
}
@Override
public NPC createNPC(EntityType type, UUID uuid, int id, String name) {
Preconditions.checkNotNull(name, "name cannot be null");
Preconditions.checkNotNull(type, "type cannot be null");
CitizensNPC npc = getByType(type, uuid, id, name);
if (npc == null)
throw new IllegalStateException("Could not create NPC.");
npcs.put(npc.getId(), npc);
Bukkit.getPluginManager().callEvent(new NPCCreateEvent(npc));
return npc;
}
@Override
public void deregister(NPC npc) {
npcs.remove(npc);
if (saves != null) {
saves.clearData(npc);
}
npc.despawn(DespawnReason.REMOVAL);
}
@Override
public void deregisterAll() {
Iterator<NPC> itr = iterator();
while (itr.hasNext()) {
NPC npc = itr.next();
itr.remove();
npc.despawn(DespawnReason.REMOVAL);
for (Trait t : npc.getTraits()) {
t.onRemove();
}
if (saves != null) {
saves.clearData(npc);
}
}
}
private int generateUniqueId() {
return saves.createUniqueNPCId(this);
}
@Override
public NPC getById(int id) {
if (id < 0)
throw new IllegalArgumentException("invalid id");
return npcs.get(id);
}
private CitizensNPC getByType(EntityType type, UUID uuid, int id, String name) {
return new CitizensNPC(uuid, id, name, EntityControllers.createForType(type), this);
}
@Override
public NPC getByUniqueId(UUID uuid) {
return npcs.get(uuid);
}
@Override
public NPC getByUniqueIdGlobal(UUID uuid) {
NPC npc = getByUniqueId(uuid);
if (npc != null)
return npc;
for (NPCRegistry registry : CitizensAPI.getNPCRegistries()) {
if (registry != this) {
NPC other = registry.getByUniqueId(uuid);
if (other != null) {
return other;
}
}
}
return null;
}
@Override
public NPC getNPC(Entity entity) {
if (entity == null)
return null;
if (entity instanceof NPCHolder)
return ((NPCHolder) entity).getNPC();
if (!(entity instanceof LivingEntity))
return null;
Object handle = NMS.getHandle((LivingEntity) entity);
return handle instanceof NPCHolder ? ((NPCHolder) handle).getNPC() : null;
}
@Override
public boolean isNPC(Entity entity) {
return getNPC(entity) != null;
}
@Override
public Iterator<NPC> iterator() {
return npcs.iterator();
}
@Override
public Iterable<NPC> sorted() {
return npcs.sorted();
}
public static class MapNPCCollection implements NPCCollection {
private final Map<Integer, NPC> npcs = Maps.newHashMap();
private final Map<UUID, NPC> uniqueNPCs = Maps.newHashMap();
@Override
public NPC get(int id) {
return npcs.get(id);
}
@Override
public NPC get(UUID uuid) {
return uniqueNPCs.get(uuid);
}
@Override
public Iterator<NPC> iterator() {
return npcs.values().iterator();
}
@Override
public void put(int id, NPC npc) {
npcs.put(id, npc);
uniqueNPCs.put(npc.getUniqueId(), npc);
}
@Override
public void remove(NPC npc) {
npcs.remove(npc.getId());
uniqueNPCs.remove(npc.getUniqueId());
}
@Override
public Iterable<NPC> sorted() {
List<NPC> vals = new ArrayList<NPC>(npcs.values());
Collections.sort(vals, NPC_COMPARATOR);
return vals;
}
}
public static interface NPCCollection extends Iterable<NPC> {
public NPC get(int id);
public NPC get(UUID uuid);
public void put(int id, NPC npc);
public void remove(NPC npc);
public Iterable<NPC> sorted();
}
public static class TroveNPCCollection implements NPCCollection {
private final TIntObjectHashMap<NPC> npcs = new TIntObjectHashMap<NPC>();
private final Map<UUID, NPC> uniqueNPCs = Maps.newHashMap();
@Override
public NPC get(int id) {
return npcs.get(id);
}
@Override
public NPC get(UUID uuid) {
return uniqueNPCs.get(uuid);
}
@Override
public Iterator<NPC> iterator() {
return npcs.valueCollection().iterator();
}
@Override
public void put(int id, NPC npc) {
npcs.put(id, npc);
uniqueNPCs.put(npc.getUniqueId(), npc);
}
@Override
public void remove(NPC npc) {
npcs.remove(npc.getId());
uniqueNPCs.remove(npc.getUniqueId());
}
@Override
public Iterable<NPC> sorted() {
List<NPC> vals = new ArrayList<NPC>(npcs.valueCollection());
Collections.sort(vals, NPC_COMPARATOR);
return vals;
}
}
private static final Comparator<NPC> NPC_COMPARATOR = new Comparator<NPC>() {
@Override
public int compare(NPC o1, NPC o2) {
return o1.getId() - o2.getId();
}
};
private static boolean TROVE_EXISTS = false;
static {
// allow trove dependency to be optional for debugging purposes
try {
Class.forName("gnu.trove.map.hash.TIntObjectHashMap").newInstance();
TROVE_EXISTS = true;
} catch (Exception e) {
}
}
}