-
-
Notifications
You must be signed in to change notification settings - Fork 307
/
Template.java
191 lines (168 loc) · 6.68 KB
/
Template.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
package net.citizensnpcs.npc;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.event.SpawnReason;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.MemoryDataKey;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.api.util.YamlStorage;
import net.citizensnpcs.api.util.YamlStorage.YamlKey;
public class Template {
private final String name;
private final boolean override;
private final Map<String, Object> replacements;
private Template(String name, Map<String, Object> replacements, boolean override) {
this.replacements = replacements;
this.override = override;
this.name = name;
}
@SuppressWarnings("unchecked")
public void apply(NPC npc) {
MemoryDataKey memoryKey = new MemoryDataKey();
boolean wasSpawned = npc.isSpawned();
if (wasSpawned) {
npc.despawn(DespawnReason.PENDING_RESPAWN);
}
npc.save(memoryKey);
List<Node> queue = Lists.newArrayList(new Node("", replacements));
for (int i = 0; i < queue.size(); i++) {
Node node = queue.get(i);
for (Entry<String, Object> entry : node.map.entrySet()) {
String fullKey = node.headKey.isEmpty() ? entry.getKey() : node.headKey + '.' + entry.getKey();
if (entry.getValue() instanceof Map<?, ?>) {
queue.add(new Node(fullKey, (Map<String, Object>) entry.getValue()));
continue;
}
boolean overwrite = memoryKey.keyExists(fullKey) || override;
if (!overwrite || fullKey.equals("uuid")) {
continue;
}
memoryKey.setRaw(fullKey, entry.getValue());
}
}
npc.load(memoryKey);
if (wasSpawned && npc.getStoredLocation() != null) {
npc.spawn(npc.getStoredLocation(), SpawnReason.RESPAWN);
}
}
public void delete() {
new File(getDirectory(), name + ".yml").delete();
}
public String getName() {
return name;
}
public void save() throws IOException {
File file = new File(getDirectory(), name + ".yml");
if (!file.getParentFile().equals(getDirectory()))
throw new IOException();
try {
file.createNewFile();
} catch (IOException e) {
throw e;
}
YamlStorage storage = new YamlStorage(file);
DataKey root = storage.getKey("");
root.setBoolean("override", override);
root.setRaw("replacements", replacements);
storage.save();
}
public static class Builder {
private final String name;
private boolean override = true;
private final Map<String, Object> replacements = Maps.newHashMap();
private Builder(String name) {
this.name = name;
}
public Template buildAndSave() throws IOException {
Template tpl = new Template(name, replacements, override);
tpl.save();
return tpl;
}
public Builder from(NPC npc) {
replacements.clear();
MemoryDataKey key = new MemoryDataKey();
((CitizensNPC) npc).save(key);
replacements.putAll(key.getValuesDeep());
return this;
}
public Builder override(boolean override) {
this.override = override;
return this;
}
public static Builder create(String name) {
return new Builder(name);
}
}
private static class Node {
String headKey;
Map<String, Object> map;
private Node(String headKey, Map<String, Object> map) {
this.headKey = headKey;
this.map = map;
}
}
public static Template byName(String name) {
if (TEMPLATES.containsKey(name))
return TEMPLATES.get(name);
File file = new File(getDirectory(), name + ".yml");
if (!file.exists() || !file.getParentFile().equals(getDirectory()))
return null;
YamlStorage storage = new YamlStorage(file);
storage.load();
YamlKey key = storage.getKey("");
boolean override = key.getBoolean("override", true);
Map<String, Object> replacements = key.getRelative("replacements").getValuesDeep();
Template res = new Template(name, replacements, override);
TEMPLATES.put(name, res);
return res;
}
private static File getDirectory() {
return new File(CitizensAPI.getDataFolder(), "templates");
}
public static Iterable<Template> getTemplates() {
return Arrays.asList(getDirectory().list()).stream().map(f -> new File(getDirectory(), f))
.filter(f -> !f.isDirectory() && Files.getFileExtension(f.getName()).equals(".yml"))
.map(f -> byName(Files.getNameWithoutExtension(f.getName()))).collect(Collectors.toList());
}
public static void migrate() {
File folder = getDirectory();
if (!folder.exists()) {
folder.mkdir();
}
File from = new File(CitizensAPI.getDataFolder(), "templates.yml");
if (from.exists()) {
YamlStorage storage = new YamlStorage(from);
storage.load();
for (DataKey key : storage.getKey("").getSubKeys()) {
String name = key.name();
Map<String, Object> replacements = key.getRelative("replacements").getValuesDeep();
boolean override = key.getBoolean("override", true);
try {
new Template(name, replacements, override).save();
} catch (IOException e) {
Messaging.severe("Unable to migrate template", name, "due to invalid filename");
}
}
try {
Files.move(from, new File(CitizensAPI.getDataFolder(), "templates_migrated.yml"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void shutdown() {
TEMPLATES.clear();
}
private static Map<String, Template> TEMPLATES = Maps.newHashMap();
}