Skip to content

Commit

Permalink
added method of adding custom worldjen using jsons
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinsdar committed Sep 13, 2023
1 parent 8bccebe commit 57f86b1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import muramasa.antimatter.worldgen.AntimatterWorldGenerator;
import muramasa.antimatter.worldgen.StoneLayerOre;
import muramasa.antimatter.worldgen.object.WorldGenStoneLayer;
import muramasa.antimatter.worldgen.object.WorldGenStoneLayerBuilder;
import muramasa.antimatter.worldgen.smallore.WorldGenSmallOre;
import muramasa.antimatter.worldgen.vanillaore.WorldGenVanillaOre;
import muramasa.antimatter.worldgen.vein.WorldGenVeinLayer;
Expand Down Expand Up @@ -264,9 +265,13 @@ public static void onResourceReload(boolean serverEvent) {
if (runRegular) {
WorldGenEvent ev = AntimatterPlatformUtils.postWorldEvent(Antimatter.INSTANCE);
veins.addAll(ev.VEINS);
veins.addAll(AntimatterWorldGenerator.readCustomJsonObjects(WorldGenVeinLayer.class, WorldGenVeinLayer::fromJson, "vein_layers"));
smallOres.addAll(ev.SMALL_ORES);
smallOres.addAll(AntimatterWorldGenerator.readCustomJsonObjects(WorldGenSmallOre.class, WorldGenSmallOre::fromJson, "small_ore"));
stoneLayers.addAll(ev.STONE_LAYERS);
stoneLayers.addAll(AntimatterWorldGenerator.readCustomJsonObjects(WorldGenStoneLayer.class, WorldGenStoneLayer::fromJson, "stone_layers"));
vanillaOres.addAll(ev.VANILLA_ORES);
vanillaOres.addAll(AntimatterWorldGenerator.readCustomJsonObjects(WorldGenVanillaOre.class, WorldGenVanillaOre::fromJson, "vanilla_ore"));
ev.COLLISION_MAP.forEach((i, l) -> {
collisionMap.computeIfAbsent(i, i2 -> new ArrayList<>()).addAll(l);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -237,6 +234,29 @@ public static void writeJson(JsonObject json, String id, String path) {
}
}

public static <T extends IAntimatterObject> List<T> readCustomJsonObjects(Class<T> clazz, BiFunction<String, JsonObject, T> function, String path){
File dir = new File(AntimatterPlatformUtils.getConfigDir().toFile(), "antimatter/" + path + "/custom");
if (dir.listFiles() == null) return Collections.emptyList();
List<File> files = Arrays.asList(dir.listFiles());
List<T> objects = new ArrayList<>();
files.forEach(f -> {
if (f.isFile()){
if (f.getAbsolutePath().endsWith(".json")){
try {
Reader reader = Files.newBufferedReader(f.toPath());
JsonObject parsed = JsonParser.parseReader(reader).getAsJsonObject();
T read = function.apply(f.getName().replace(".json", ""), parsed);
reader.close();
objects.add(read);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
return objects;
}

public static <T extends IAntimatterObject> T readJson(Class<T> clazz, T original, BiFunction<String, JsonObject, T> function, String path){
File dir = new File(AntimatterPlatformUtils.getConfigDir().toFile(), "antimatter/" + path + "/overrides");
File target = new File(dir, original.getId() + ".json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private WorldGenVeinLayer buildVeinFromJson(){
this.sporadic,
this.dimensions);
AntimatterWorldGenerator.writeJson(vein.toJson(), this.id, "vein_layers");
return AntimatterWorldGenerator.readJson(WorldGenVeinLayer.class, vein, WorldGenVeinLayer::fromJson, "veins");
return AntimatterWorldGenerator.readJson(WorldGenVeinLayer.class, vein, WorldGenVeinLayer::fromJson, "vein_layers");
}
public final WorldGenVeinLayerBuilder withWeight(int weight) {
this.weight = weight;
Expand Down

0 comments on commit 57f86b1

Please sign in to comment.