Inspired by 2020's April Fools' 20w14infinite Snapshot, this mod brings endless randomly generated dimensions into Minecraft.
1package net.lerariemann.infinity.util.loading;
2
3import com.google.gson.JsonElement;
4import com.google.gson.JsonParser;
5import com.mojang.serialization.Codec;
6import com.mojang.serialization.DataResult;
7import com.mojang.serialization.JsonOps;
8import net.lerariemann.infinity.util.core.CommonIO;
9import net.lerariemann.infinity.util.InfinityMethods;
10import net.minecraft.core.WritableRegistry;
11import net.minecraft.nbt.CompoundTag;
12import net.minecraft.resources.RegistryOps;
13import net.minecraft.resources.ResourceKey;
14import net.minecraft.resources.ResourceLocation;
15import org.apache.commons.io.FileUtils;
16import org.apache.logging.log4j.LogManager;
17
18import java.io.File;
19import java.io.IOException;
20import java.nio.charset.StandardCharsets;
21import java.nio.file.Path;
22import java.util.stream.Stream;
23
24import static java.nio.file.Files.walk;
25
26//? if >1.21 {
27import net.minecraft.core.RegistrationInfo;
28 //?} else {
29/*import com.mojang.serialization.Lifecycle;
30*///?}
31
32public class JsonGrabber<E> {
33 Codec<E> decoder;
34 WritableRegistry<E> registry;
35 RegistryOps.RegistryInfoLookup registryInfoGetter;
36
37 //? if >1.21 {
38 static RegistrationInfo ChemicalX = RegistrationInfo.BUILT_IN;
39 //?} else {
40 /*static Lifecycle ChemicalX = Lifecycle.stable();
41 *///?}
42
43 JsonGrabber(RegistryOps.RegistryInfoLookup get, Codec<E> dec, WritableRegistry<E> reg) {
44 decoder = dec;
45 registry = reg;
46 registryInfoGetter = get;
47 }
48
49 public void grabAll(Path rootdir) {
50 grabAll(rootdir, false);
51 }
52
53 void grabAll(Path rootdir, boolean bl) {
54 if(!rootdir.toFile().exists()) return;
55 try (Stream<Path> files = walk(rootdir)) {
56 files.forEach(a -> grab(a, bl));
57 } catch (IOException e) {
58 throw new RuntimeException(e);
59 }
60 }
61
62 void grab(Path path, boolean bl) {
63 String path1 = path.toString();
64 if (path1.endsWith(".json")) {
65 String fullname = path1.substring(path1.lastIndexOf("/") + 1, path1.length() - 5);
66 int i = fullname.lastIndexOf("\\");
67 if (i>=0) fullname = fullname.substring(i + 1);
68 ResourceKey<E> key = ResourceKey.create(registry.key(), InfinityMethods.getId(fullname));
69 grab(path1, key, bl);
70 }
71 }
72
73 void grab(ResourceLocation id, CompoundTag compound, boolean bl) {
74 grab(ResourceKey.create(registry.key(), id), JsonParser.parseString(CommonIO.compoundToString(compound)), bl);
75 }
76
77 void grab(ResourceKey<E> key, JsonElement jsonElement, boolean bl) {
78 RegistryOps<JsonElement> registryOps = RegistryOps.create(JsonOps.INSTANCE, registryInfoGetter);
79 try {
80 DataResult<E> dataResult = decoder.parse(registryOps, jsonElement);
81 if(dataResult.result().isPresent()) {
82 E object = dataResult.result().get();
83 if (bl || !registry.containsKey(key)) registry.register(key, object, ChemicalX);
84 }
85 else {
86 LogManager.getLogger().info(jsonElement);
87 }
88 }
89 catch (Exception e) {
90 LogManager.getLogger().info(jsonElement);
91 throw new RuntimeException(e.getMessage() + " Element affected: " + jsonElement.toString());
92 }
93 }
94
95 void grab(String path, ResourceKey<E> registryKey, boolean bl) {
96 File file = new File(path);
97 String content;
98 try {
99 content = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
100 } catch (IOException e) {
101 throw new RuntimeException(e);
102 }
103 JsonElement jsonElement = JsonParser.parseString(content);
104 grab(registryKey, jsonElement, bl);
105 }
106
107 E grabWithReturn(String rootdir, String i, boolean register) {
108 String path = rootdir + "/" + i + ".json";
109 ResourceKey<E> key = ResourceKey.create(registry.key(), InfinityMethods.getId(i));
110 File file = new File(path);
111 String content;
112 try {
113 content = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
114 } catch (IOException e) {
115 throw new RuntimeException(e);
116 }
117 JsonElement jsonElement = JsonParser.parseString(content);
118 RegistryOps<JsonElement> registryOps = RegistryOps.create(JsonOps.INSTANCE, registryInfoGetter);
119 DataResult<E> dataResult = decoder.parse(registryOps, jsonElement);
120 E object = dataResult.getOrThrow(
121 //? if >1.21 {
122 (error) -> null
123 //?} else {
124 /*false, (error) -> {}
125 *///?}
126 );
127 if (register) registry.register(key, object, ChemicalX);
128 return object;
129 }
130}