/
Template.java
55 lines (45 loc) · 1.5 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
package net.citizensnpcs.api.npc.templates;
import java.util.List;
import java.util.function.Consumer;
import com.google.common.collect.Lists;
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.persistence.PersistenceLoader;
import net.citizensnpcs.api.util.DataKey;
public class Template {
private final List<Consumer<NPC>> actions = Lists.newArrayList();
private final String name;
private final String namespace;
private Template(String namespace, String name) {
this.namespace = namespace;
this.name = name;
}
private void addAction(Consumer<NPC> action) {
actions.add(action);
}
public void apply(NPC npc) {
boolean respawn = npc.isSpawned();
if (respawn) {
npc.despawn(DespawnReason.PENDING_RESPAWN);
}
for (Consumer<NPC> action : actions) {
action.accept(npc);
}
if (respawn) {
npc.spawn(npc.getStoredLocation());
}
}
public String getName() {
return name;
}
public String getNamespace() {
return namespace;
}
public static Template load(TemplateWorkspace workspace, String namespace, DataKey key) {
Template template = new Template(namespace, key.name());
if (key.keyExists("yaml_replace")) {
template.addAction(PersistenceLoader.load(YamlReplacementAction.class, key.getRelative("yaml_replace")));
}
return template;
}
}