-
Notifications
You must be signed in to change notification settings - Fork 7
/
SkyPaths.java
185 lines (156 loc) · 8.04 KB
/
SkyPaths.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
package de.melanx.skyblockbuilder.util;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import de.melanx.skyblockbuilder.SkyblockBuilder;
import de.melanx.skyblockbuilder.config.StartingInventory;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.levelgen.carver.ConfiguredWorldCarver;
import net.minecraft.world.level.levelgen.feature.ConfiguredFeature;
import net.minecraft.world.level.levelgen.structure.Structure;
import net.minecraftforge.fml.loading.FMLPaths;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.Stream;
public class SkyPaths {
// paths
public static final Path MOD_CONFIG = FMLPaths.CONFIGDIR.get().resolve("skyblockbuilder");
public static final Path SKYBLOCK_UTILS = FMLPaths.GAMEDIR.get().resolve("skyblockbuilder");
public static final Path MOD_EXPORTS = SKYBLOCK_UTILS.resolve("exports");
public static final Path CONVERT_INPUT = SKYBLOCK_UTILS.resolve("convert_input");
public static final Path CONVERT_OUTPUT = SKYBLOCK_UTILS.resolve("convert_output");
public static final Path TEMPLATES_DIR = MOD_CONFIG.resolve("templates");
public static final Path ICONS_DIR = TEMPLATES_DIR.resolve("icons");
public static final Path DATA_DIR = MOD_CONFIG.resolve("data");
// files
public static final Path ITEMS_FILE = MOD_CONFIG.resolve("starter_inventory.json5");
public static final Path SCHEMATIC_FILE = TEMPLATES_DIR.resolve("default.nbt");
private static final Path FEATURES_FILE = DATA_DIR.resolve("features.txt");
private static final Path STRUCTURES_FILE = DATA_DIR.resolve("structures.txt");
private static final Path BIOMES_FILE = DATA_DIR.resolve("biomes.txt");
private static final Path CARVERS_FILE = DATA_DIR.resolve("carvers.txt");
private static final Path DIMENSIONS_FILE = DATA_DIR.resolve("dimensions.txt");
public static void createDirectories() {
try {
Files.createDirectories(MOD_CONFIG);
Files.createDirectories(SKYBLOCK_UTILS);
Files.createDirectories(MOD_EXPORTS);
Files.createDirectories(CONVERT_INPUT);
Files.createDirectories(CONVERT_OUTPUT);
Files.createDirectories(TEMPLATES_DIR);
Files.createDirectories(ICONS_DIR);
Files.createDirectories(DATA_DIR);
} catch (IOException e) {
throw new RuntimeException("Unable to create default directories.", e);
}
}
public static void generateDefaultFiles(@Nullable MinecraftServer server) {
try {
createDirectories();
copyTemplateFile();
generateStarterItemsFile();
if (server != null) {
generateFeatureInformation(server);
generateStructureInformation(server);
generateBiomeInformation(server);
generateCarversInformation(server);
generateDimensionInformation(server);
}
StartingInventory.loadStarterItems();
} catch (IOException e) {
SkyblockBuilder.getLogger().error("Unable to generate default files", e);
}
}
public static void copyTemplateFile() throws IOException {
//noinspection ConstantConditions
if (Arrays.stream(TEMPLATES_DIR.toFile().listFiles())
.anyMatch(file -> file.isFile() && (file.getName().endsWith(".nbt") || file.getName().endsWith(".snbt")))) {
return;
}
//noinspection ConstantConditions
Files.copy(SkyblockBuilder.class.getResourceAsStream("/skyblockbuilder-template.nbt"), SCHEMATIC_FILE);
}
private static void generateStarterItemsFile() throws IOException {
if (Files.isRegularFile(ITEMS_FILE)) {
return;
}
JsonObject object = new JsonObject();
JsonArray items = new JsonArray();
object.add("items", items);
BufferedWriter w = Files.newBufferedWriter(ITEMS_FILE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
w.write("// See here for more information:\n");
w.write("// https://melanx.github.io/SkyblockBuilder/1.20.x/config/inventory/#starting-inventory\n");
w.write(SkyblockBuilder.PRETTY_GSON.toJson(object));
w.close();
}
public static void generateFeatureInformation(MinecraftServer server) throws IOException {
BufferedWriter w = Files.newBufferedWriter(FEATURES_FILE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
Stream<Holder.Reference<ConfiguredFeature<?, ?>>> stream = server.registryAccess().registryOrThrow(Registries.CONFIGURED_FEATURE).holders();
//noinspection DuplicatedCode
stream.sorted(Comparator.comparing(Holder.Reference::key)).forEach(holder -> {
try {
w.write(holder.key().location() + "\n");
} catch (IOException e) {
SkyblockBuilder.getLogger().error("Failed to write '" + holder.key().location() + "' to file", e);
}
});
w.close();
}
public static void generateStructureInformation(MinecraftServer server) throws IOException {
BufferedWriter w = Files.newBufferedWriter(STRUCTURES_FILE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
Stream<Holder.Reference<Structure>> stream = server.registryAccess().registryOrThrow(Registries.STRUCTURE).holders();
//noinspection DuplicatedCode
stream.sorted(Comparator.comparing(Holder.Reference::key)).forEach(holder -> {
try {
w.write(holder.key().location() + "\n");
} catch (IOException e) {
SkyblockBuilder.getLogger().error("Failed to write '" + holder.key().location() + "' to file", e);
}
});
w.close();
}
public static void generateBiomeInformation(MinecraftServer server) throws IOException {
BufferedWriter w = Files.newBufferedWriter(BIOMES_FILE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
Stream<Holder.Reference<Biome>> stream = server.registryAccess().registryOrThrow(Registries.BIOME).holders();
//noinspection DuplicatedCode
stream.sorted(Comparator.comparing(Holder.Reference::key)).forEach(holder -> {
try {
w.write(holder.key().location() + "\n");
} catch (IOException e) {
SkyblockBuilder.getLogger().error("Failed to write '" + holder.key().location() + "' to file", e);
}
});
w.close();
}
public static void generateCarversInformation(MinecraftServer server) throws IOException {
BufferedWriter w = Files.newBufferedWriter(CARVERS_FILE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
Stream<Holder.Reference<ConfiguredWorldCarver<?>>> stream = server.registryAccess().registryOrThrow(Registries.CONFIGURED_CARVER).holders();
//noinspection DuplicatedCode
stream.sorted(Comparator.comparing(Holder.Reference::key)).forEach(holder -> {
try {
w.write(holder.key().location() + "\n");
} catch (IOException e) {
SkyblockBuilder.getLogger().error("Failed to write '" + holder.key().location() + "' to file", e);
}
});
w.close();
}
public static void generateDimensionInformation(@Nonnull MinecraftServer server) throws IOException {
BufferedWriter w = Files.newBufferedWriter(DIMENSIONS_FILE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
for (ResourceKey<Level> levelKey : server.levelKeys()) {
w.write(levelKey.location() + "\n");
}
w.close();
}
}