-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
SimpleNPCDataStore.java
117 lines (101 loc) · 3.6 KB
/
SimpleNPCDataStore.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
package net.citizensnpcs.api.npc;
import java.util.UUID;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.api.util.Storage;
import org.bukkit.entity.EntityType;
public class SimpleNPCDataStore implements NPCDataStore {
private final Storage root;
protected SimpleNPCDataStore(Storage saves) {
root = saves;
}
@Override
public void clearData(NPC npc) {
root.getKey("npc").removeKey(Integer.toString(npc.getId()));
}
@Override
public int createUniqueNPCId(NPCRegistry registry) {
DataKey key = root.getKey("");
int newId = key.getInt("last-created-npc-id", -1);
if (newId == -1 || registry.getById(newId + 1) != null) {
int maxId = Integer.MIN_VALUE;
for (NPC npc : registry) {
if (npc.getId() > maxId) {
maxId = npc.getId();
}
}
newId = maxId == Integer.MIN_VALUE ? 0 : maxId + 1;
} else {
newId++;
}
key.setInt("last-created-npc-id", newId);
return newId;
}
@Override
public void loadInto(NPCRegistry registry) {
for (DataKey key : root.getKey("npc").getIntegerSubKeys()) {
int id = Integer.parseInt(key.name());
if (!key.keyExists("name")) {
Messaging.logTr(LOAD_NAME_NOT_FOUND, id);
continue;
}
String unparsedEntityType = key.getString("traits.type", "PLAYER");
EntityType type = matchEntityType(unparsedEntityType);
if (type == null) {
Messaging.logTr(LOAD_UNKNOWN_NPC_TYPE, unparsedEntityType);
continue;
}
NPC npc = registry.createNPC(type,
!key.getString("uuid", "").isEmpty() ? UUID.fromString(key.getString("uuid")) : UUID.randomUUID(),
id, key.getString("name"));
npc.load(key);
}
}
@Override
public void saveToDisk() {
new Thread() {
@Override
public void run() {
root.save();
}
}.start();
}
@Override
public void saveToDiskImmediate() {
root.save();
}
@Override
public void store(NPC npc) {
npc.save(root.getKey("npc." + npc.getId()));
}
@Override
public void storeAll(NPCRegistry registry) {
for (NPC npc : registry)
store(npc);
}
public static NPCDataStore create(Storage storage) {
return new SimpleNPCDataStore(storage);
}
private static EntityType matchEntityType(String toMatch) {
EntityType type = EntityType.fromName(toMatch);
if (type != null)
return type;
return matchEnum(EntityType.values(), toMatch);
}
private static <T extends Enum<?>> T matchEnum(T[] values, String toMatch) {
T type = null;
for (T check : values) {
String name = check.name();
if (name.matches(toMatch) || name.equalsIgnoreCase(toMatch)
|| name.replace("_", "").equalsIgnoreCase(toMatch)
|| name.replace('_', '-').equalsIgnoreCase(toMatch)
|| name.replace('_', ' ').equalsIgnoreCase(toMatch) || name.startsWith(toMatch)) {
type = check;
break;
}
}
return type;
}
private static final String LOAD_NAME_NOT_FOUND = "citizens.notifications.npc-name-not-found";
private static final String LOAD_UNKNOWN_NPC_TYPE = "citizens.notifications.unknown-npc-type";
}