-
Notifications
You must be signed in to change notification settings - Fork 7
/
TemplateLoader.java
106 lines (85 loc) · 4.13 KB
/
TemplateLoader.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
package de.melanx.skyblockbuilder.template;
import de.melanx.skyblockbuilder.config.common.TemplatesConfig;
import de.melanx.skyblockbuilder.util.SkyPaths;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.*;
public class TemplateLoader {
private static final List<String> TEMPLATE_NAMES = new ArrayList<>();
private static final Map<String, ConfiguredTemplate> TEMPLATE_MAP = new HashMap<>();
private static ConfiguredTemplate TEMPLATE;
public static void updateTemplates() {
try {
TEMPLATE_NAMES.clear();
TEMPLATE_MAP.clear();
SkyPaths.copyTemplateFile();
Set<String> takenNames = new HashSet<>();
for (TemplateInfo info : TemplatesConfig.templates) {
if (!TemplatesConfig.spawns.containsKey(info.spawns())) {
throw new IllegalArgumentException("Spawn configuration \"" + info.spawns() + "\" is not defined: " + info.name());
}
if (!TemplatesConfig.surroundingBlocks.containsKey(info.surroundingBlocks()) && !info.surroundingBlocks().isEmpty()) {
throw new IllegalArgumentException("Surrounding blocks configuration \"" + info.surroundingBlocks() + "\" is not defined: " + info.name());
}
if (!SkyPaths.TEMPLATES_DIR.resolve(info.file()).toFile().exists()) {
throw new IllegalArgumentException("Template file \"" + info.file() + "\" does not exist: " + info.name());
}
if (takenNames.contains(info.name().toLowerCase(Locale.ROOT))) {
throw new IllegalArgumentException("Template name \"" + info.name() + "\" is defined too many times.");
}
takenNames.add(info.name().toLowerCase(Locale.ROOT));
ConfiguredTemplate template = new ConfiguredTemplate(info);
TEMPLATE_NAMES.add(info.name());
TEMPLATE_MAP.put(info.name().toLowerCase(Locale.ROOT), template);
}
if (TEMPLATE_MAP.isEmpty()) {
throw new IllegalStateException("You need at least one configured template.");
}
if (TEMPLATE == null) {
TEMPLATE = TEMPLATE_MAP.get(TEMPLATE_NAMES.get(0).toLowerCase(Locale.ROOT));
} else {
TEMPLATE = TemplateLoader.getConfiguredTemplate(TEMPLATE.getName());
}
} catch (IOException e) {
throw new RuntimeException("Cannot load templates.", e);
}
}
public static void updateTemplateNames(List<String> names) {
TEMPLATE_NAMES.clear();
TEMPLATE_NAMES.addAll(names);
}
public static List<String> getTemplateNames() {
return TEMPLATE_NAMES;
}
/**
* This provides a list with all the configured templates and its corresponding StructureTemplates.
* <br>
* Should mainly be used on server side. It's possible that there is too much data to sync all templates to the
* client. Use {@link TemplateLoader#getTemplateNames()} on client and send a packet to server to communicate.
*/
public static List<ConfiguredTemplate> getConfiguredTemplates() {
List<ConfiguredTemplate> templates = new ArrayList<>(TEMPLATE_MAP.values());
templates.sort(Comparator.comparing(ConfiguredTemplate::getName));
return templates;
}
public static void setTemplate(ConfiguredTemplate template) {
TEMPLATE = template;
}
public static StructureTemplate getTemplate() {
if (TEMPLATE == null) {
throw new IllegalStateException("Tried to access template before set.");
}
return TEMPLATE.getTemplate();
}
@Nullable
public static ConfiguredTemplate getConfiguredTemplate(String name) {
return TEMPLATE_MAP.get(name.toLowerCase(Locale.ROOT));
}
public static ConfiguredTemplate getConfiguredTemplate() {
return TEMPLATE;
}
public static Set<TemplatesConfig.Spawn> getCurrentSpawns() {
return TEMPLATE.getDefaultSpawns();
}
}