-
-
Notifications
You must be signed in to change notification settings - Fork 307
/
CitizensNPCRegistry.java
252 lines (222 loc) · 7.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
251
252
package net.citizensnpcs.npc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.ItemStack;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import gnu.trove.map.hash.TIntObjectHashMap;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.event.NPCCreateEvent;
import net.citizensnpcs.api.event.SpawnReason;
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.api.util.RemoveReason;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.trait.ArmorStandTrait;
import net.citizensnpcs.trait.LookClose;
import net.citizensnpcs.trait.MountTrait;
import net.citizensnpcs.util.NMS;
public class CitizensNPCRegistry implements NPCRegistry {
private final String name;
private final TIntObjectHashMap<NPC> npcs = new TIntObjectHashMap<NPC>();
private final NPCDataStore saves;
private final Map<UUID, NPC> uniqueNPCs = Maps.newHashMap();
public CitizensNPCRegistry(NPCDataStore store) {
this(store, "");
}
public CitizensNPCRegistry(NPCDataStore store, String registryName) {
saves = store;
name = registryName;
}
private CitizensNPC create(EntityType type, UUID uuid, int id, String name) {
return new CitizensNPC(uuid, id, name, EntityControllers.createForType(type), this);
}
@Override
public NPC createNPC(EntityType type, String name) {
return createNPC(type, UUID.randomUUID(), generateIntegerId(), name);
}
@Override
public NPC createNPC(EntityType type, String name, Location loc) {
NPC npc = createNPC(type, name);
npc.spawn(loc, SpawnReason.PLUGIN);
return npc;
}
@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 = create(type, uuid, id, name);
if (npc == null)
throw new IllegalStateException("Could not create NPC.");
npcs.put(id, npc);
uniqueNPCs.put(npc.getUniqueId(), npc);
Bukkit.getPluginManager().callEvent(new NPCCreateEvent(npc));
if (type == EntityType.ARMOR_STAND && !npc.hasTrait(ArmorStandTrait.class)) {
npc.addTrait(ArmorStandTrait.class);
}
if (Setting.DEFAULT_LOOK_CLOSE.asBoolean()) {
npc.addTrait(LookClose.class);
}
npc.addTrait(MountTrait.class);
return npc;
}
@Override
public NPC createNPCUsingItem(EntityType type, String name, ItemStack item) {
NPC npc = createNPC(type, name);
if (type == EntityType.DROPPED_ITEM || type == EntityType.FALLING_BLOCK || type == EntityType.ITEM_FRAME
|| type.name().equals("GLOW_ITEM_FRAME") || type.name().equals("ITEM_DISPLAY")) {
npc.data().set(NPC.Metadata.ITEM_AMOUNT, item.getAmount());
npc.data().set(NPC.Metadata.ITEM_ID, item.getType().name());
npc.data().set(NPC.Metadata.ITEM_DATA, item.getData().getData());
npc.setItemProvider(() -> item);
} else {
throw new UnsupportedOperationException("Not an item entity type");
}
return npc;
}
@Override
public void deregister(NPC npc) {
npc.despawn(DespawnReason.REMOVAL);
npcs.remove(npc.getId());
uniqueNPCs.remove(npc.getUniqueId());
if (saves != null) {
saves.clearData(npc);
}
}
@Override
public void deregisterAll() {
Iterator<NPC> itr = iterator();
while (itr.hasNext()) {
NPC npc = itr.next();
npc.despawn(DespawnReason.REMOVAL);
for (Trait t : npc.getTraits()) {
HandlerList.unregisterAll(t);
t.onRemove(RemoveReason.REMOVAL);
}
itr.remove();
if (saves != null) {
saves.clearData(npc);
}
}
}
@Override
public void despawnNPCs(DespawnReason reason) {
Iterator<NPC> itr = iterator();
while (itr.hasNext()) {
NPC npc = itr.next();
try {
npc.despawn(reason);
} catch (Throwable e) {
e.printStackTrace(); // ensure that all entities are despawned
}
itr.remove();
}
}
private int generateIntegerId() {
return saves.createUniqueNPCId(this);
}
@Override
public NPC getById(int id) {
if (id < 0)
throw new IllegalArgumentException("invalid id");
return npcs.get(id);
}
@Override
public NPC getByUniqueId(UUID uuid) {
if (uuid.version() == 2) {
long msb = uuid.getMostSignificantBits();
msb &= ~0x0000000000002000L;
msb |= 0x0000000000004000L;
uuid = new UUID(msb, uuid.getLeastSignificantBits());
}
return uniqueNPCs.get(uuid);
}
@Override
public NPC getByUniqueIdGlobal(UUID uuid) {
if (uuid.version() == 2) {
long msb = uuid.getMostSignificantBits();
msb &= ~0x0000000000002000L;
msb |= 0x0000000000004000L;
uuid = new UUID(msb, uuid.getLeastSignificantBits());
}
NPC npc = getByUniqueId(uuid);
if (npc != null)
return npc;
for (NPCRegistry registry : CitizensAPI.getNPCRegistries()) {
if (registry == this)
continue;
NPC other = registry.getByUniqueId(uuid);
if (other != null) {
return other;
}
}
return null;
}
@Override
public String getName() {
return name;
}
@Override
public NPC getNPC(Entity entity) {
if (entity == null)
return null;
if (entity instanceof NPCHolder)
return ((NPCHolder) entity).getNPC();
return NMS.getNPC(entity);
}
@Override
public boolean isNPC(Entity entity) {
return getNPC(entity) != null;
}
@Override
public Iterator<NPC> iterator() {
return new Iterator<NPC>() {
Iterator<NPC> itr = npcs.valueCollection().iterator();
UUID lastUUID;
@Override
public boolean hasNext() {
return itr.hasNext();
}
@Override
public NPC next() {
NPC npc = itr.next();
if (npc != null && npc.getUniqueId() != null) {
lastUUID = npc.getUniqueId();
}
return npc;
}
@Override
public void remove() {
itr.remove();
if (lastUUID != null) {
uniqueNPCs.remove(lastUUID);
lastUUID = null;
}
}
};
}
@Override
public void saveToStore() {
saves.storeAll(this);
saves.saveToDiskImmediate();
}
@Override
public Iterable<NPC> sorted() {
List<NPC> vals = new ArrayList<NPC>(npcs.valueCollection());
Collections.sort(vals, (a, b) -> Integer.compare(a.getId(), b.getId()));
return vals;
}
}