-
-
Notifications
You must be signed in to change notification settings - Fork 315
/
Template.java
138 lines (117 loc) · 4.32 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
package net.citizensnpcs.npc;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.MemoryDataKey;
import net.citizensnpcs.api.util.YamlStorage;
import net.citizensnpcs.api.util.YamlStorage.YamlKey;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
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();
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);
}
public void delete() {
templates.load();
templates.getKey("").removeKey(name);
templates.save();
}
public String getName() {
return 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 class TemplateBuilder {
private final String name;
private boolean override;
private final Map<String, Object> replacements = Maps.newHashMap();
private TemplateBuilder(String name) {
this.name = name;
}
public Template buildAndSave() {
save();
return new Template(name, replacements, override);
}
public TemplateBuilder from(NPC npc) {
replacements.clear();
MemoryDataKey key = new MemoryDataKey();
((CitizensNPC) npc).save(key);
replacements.putAll(key.getValuesDeep());
return this;
}
public TemplateBuilder override(boolean override) {
this.override = override;
return this;
}
public void save() {
templates.load();
DataKey root = templates.getKey(name);
root.setBoolean("override", override);
root.setRaw("replacements", replacements);
templates.save();
}
public static TemplateBuilder create(String name) {
return new TemplateBuilder(name);
}
}
public static Iterable<Template> allTemplates() {
templates.load();
return Iterables.transform(templates.getKey("").getSubKeys(), new Function<DataKey, Template>() {
@Override
public Template apply(DataKey arg0) {
return Template.byName(arg0.name());
}
});
}
public static Template byName(String name) {
templates.load();
if (!templates.getKey("").keyExists(name))
return null;
YamlKey key = templates.getKey(name);
boolean override = key.getBoolean("override", false);
Map<String, Object> replacements = key.getRelative("replacements").getValuesDeep();
return new Template(name, replacements, override);
}
private static YamlStorage templates = new YamlStorage(new File(CitizensAPI.getDataFolder(), "templates.yml"));
static {
templates.load();
}
}