Skip to content

Commit

Permalink
refactor: Replace the rest of config data with its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
null2264 committed Dec 10, 2023
1 parent ab9e619 commit 3a0dcc1
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 144 deletions.
4 changes: 4 additions & 0 deletions src/main/java/io/github/null2264/cobblegen/data/Pair.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ public Pair(A a, B b) {
this.second = b;
}

public static <A, B> Pair<A, B> of(A a, B b) {
return new Pair<>(a, b);
}

public final A getFirst() {
return this.first;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@
import io.github.null2264.cobblegen.data.JanksonSerializable;
import io.github.null2264.cobblegen.data.Pair;
import lombok.val;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static io.github.null2264.cobblegen.data.config.ConfigHelper.listFromJson;
import static io.github.null2264.cobblegen.util.Constants.JANKSON;

public class ConfigData implements Config, JanksonSerializable
{
@Comment(value = "CobbleGen Format Version, you can leave this alone for now. v2.0 will be released in CobbleGen v6.0")
@NotNull
public String formatVersion = "1.0";

@Nullable
Expand All @@ -38,13 +35,13 @@ public class ConfigData implements Config, JanksonSerializable
"minY": 0,
"maxY": 69
}""")
public List<WeightedBlock> cobbleGen;
public ResultList cobbleGen;

@Nullable
public List<WeightedBlock> stoneGen;
public ResultList stoneGen;

@Nullable
public List<WeightedBlock> basaltGen;
public ResultList basaltGen;

@Nullable
@Comment(value = """
Expand All @@ -71,11 +68,11 @@ public class ConfigData implements Config, JanksonSerializable
public CustomGen customGen;

@Nullable
public Map<String, Map<String, AdvancedGen>> advanced;
public FluidInteractionMap advanced;

public static ConfigData defaultConfig() {
ConfigData config = new ConfigData();
config.cobbleGen = List.of(new WeightedBlock(
config.cobbleGen = ResultList.of(new WeightedBlock(
"minecraft:cobblestone",
100.0,
null,
Expand All @@ -84,14 +81,14 @@ public static ConfigData defaultConfig() {
0,
null
), new WeightedBlock("minecraft:cobbled_deepslate", 100.0, null, null, 0, null, null));
config.stoneGen = List.of(new WeightedBlock("minecraft:stone", 100.0));
config.basaltGen = List.of(new WeightedBlock("minecraft:basalt", 100.0));
config.stoneGen = ResultList.of(new WeightedBlock("minecraft:stone", 100.0));
config.basaltGen = ResultList.of(new WeightedBlock("minecraft:basalt", 100.0));
config.customGen = new CustomGen(
// Cobble Gen
GeneratorMap.of(
new Pair<>(
Pair.of(
CGIdentifier.of("minecraft:bedrock"),
List.of(
ResultList.of(
new WeightedBlock("minecraft:emerald_ore", 2.0),
new WeightedBlock("minecraft:diamond_ore", 5.0),
new WeightedBlock("minecraft:lapis_ore", 8.0),
Expand All @@ -104,9 +101,9 @@ public static ConfigData defaultConfig() {
),
// Stone Gen
GeneratorMap.of(
new Pair<>(
Pair.of(
CGIdentifier.of("minecraft:bedrock"),
List.of(
ResultList.of(
new WeightedBlock("minecraft:stone", 40.0),
new WeightedBlock("minecraft:diorite", 20.0),
new WeightedBlock("minecraft:andesite", 20.0),
Expand All @@ -116,9 +113,9 @@ public static ConfigData defaultConfig() {
),
// Basalt Gen
GeneratorMap.of(
new Pair<>(
Pair.of(
CGIdentifier.of("minecraft:bedrock"),
List.of(
ResultList.of(
new WeightedBlock("minecraft:end_stone", 100.0, List.of("minecraft:the_end")),
new WeightedBlock("minecraft:blackstone", 100.0, null, List.of("minecraft:overworld"))
)
Expand All @@ -133,19 +130,11 @@ public static ConfigData defaultConfig() {
public JsonObject toJson() {
JsonObject json = new JsonObject();
json.put("formatVersion", JsonPrimitive.of(formatVersion));
json.put("cobbleGen", JANKSON.toJson(cobbleGen));
json.put("stoneGen", JANKSON.toJson(stoneGen));
json.put("basaltGen", JANKSON.toJson(basaltGen));
if (cobbleGen != null) json.put("cobbleGen", cobbleGen.toJson());
if (stoneGen != null) json.put("stoneGen", stoneGen.toJson());
if (basaltGen != null) json.put("basaltGen", basaltGen.toJson());
if (customGen != null) json.put("customGen", customGen.toJson());
if (advanced != null) {
JsonObject advancedRoot = new JsonObject();
advanced.forEach((fluid1, fluidMap) -> {
JsonObject advancedSubroot = new JsonObject();
fluidMap.forEach((fluid2, advancedGen) -> advancedSubroot.put(fluid2, advancedGen.toJson()));
advancedRoot.put(fluid1, advancedSubroot);
});
json.put("advanced", advancedRoot);
}
if (advanced != null) json.put("advanced", advanced.toJson());
return json;
}

Expand All @@ -154,21 +143,11 @@ public static ConfigData fromJson(JsonObject json) {
ConfigData config = new ConfigData();
val formatVersion = json.get("formatVersion");
config.formatVersion = (formatVersion instanceof JsonPrimitive) ? ((JsonPrimitive) formatVersion).asString() : "1.0";
config.cobbleGen = listFromJson((JsonArray) json.get("cobbleGen"), WeightedBlock::fromJson);
config.stoneGen = listFromJson((JsonArray) json.get("stoneGen"), WeightedBlock::fromJson);
config.stoneGen = listFromJson((JsonArray) json.get("stoneGen"), WeightedBlock::fromJson);
config.cobbleGen = ResultList.fromJson(json.get("cobbleGen"));
config.stoneGen = ResultList.fromJson(json.get("stoneGen"));
config.stoneGen = ResultList.fromJson(json.get("stoneGen"));
config.customGen = CustomGen.fromJson(json.getObject("customGen"));
JsonObject e = json.getObject("advanced");
if (e == null) return config;

config.advanced = new HashMap<>();
e.forEach((fluid1, fluidMap) -> {
if (!(fluidMap instanceof JsonObject advancedSubroot)) return;

Map<String, AdvancedGen> advanced = new HashMap<>();
advancedSubroot.forEach((fluid2, jsonElement) -> advanced.put(fluid2, AdvancedGen.fromJson((JsonObject) jsonElement)));
config.advanced.put(fluid1, advanced);
});
config.advanced = FluidInteractionMap.fromJson(json.getObject("advanced"));
return config;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.github.null2264.cobblegen.data.config;

import blue.endless.jankson.JsonObject;
import blue.endless.jankson.annotation.Deserializer;
import blue.endless.jankson.annotation.Serializer;
import io.github.null2264.cobblegen.data.JanksonSerializable;

import java.util.HashMap;

public class FluidInteractionMap extends HashMap<String, HashMap<String, AdvancedGen>> implements JanksonSerializable {
@Override
@Serializer
public JsonObject toJson() {
JsonObject root = new JsonObject();
this.forEach((fluid1, fluidMap) -> {
JsonObject subroot = new JsonObject();
fluidMap.forEach((fluid2, advancedGen) -> subroot.put(fluid2, advancedGen.toJson()));
root.put(fluid1, subroot);
});
return root;
}

@Deserializer
public static FluidInteractionMap fromJson(JsonObject json) {
if (json == null) return null;

FluidInteractionMap result = new FluidInteractionMap();
json.forEach((fluid1, fluidMap) -> {
if (!(fluidMap instanceof JsonObject subroot)) return;

HashMap<String, AdvancedGen> root = new HashMap<>();
subroot.forEach((fluid2, jsonElement) -> root.put(fluid2, AdvancedGen.fromJson((JsonObject) jsonElement)));
result.put(fluid1, root);
});
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
package io.github.null2264.cobblegen.data.config;

import blue.endless.jankson.JsonArray;
import blue.endless.jankson.JsonElement;
import blue.endless.jankson.JsonObject;
import blue.endless.jankson.annotation.Deserializer;
import blue.endless.jankson.annotation.Serializer;
import com.google.common.primitives.Ints;
import io.github.null2264.cobblegen.data.CGIdentifier;
import io.github.null2264.cobblegen.data.JanksonSerializable;
import io.github.null2264.cobblegen.data.Pair;
import io.github.null2264.cobblegen.data.model.PacketSerializable;
import lombok.val;
import net.minecraft.network.FriendlyByteBuf;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import static io.github.null2264.cobblegen.data.config.ConfigHelper.listFromJson;
import static io.github.null2264.cobblegen.util.Constants.JANKSON;
public class GeneratorMap extends HashMap<CGIdentifier, ResultList> implements JanksonSerializable, PacketSerializable<GeneratorMap> {
public GeneratorMap() {}

public GeneratorMap(int capacity) {
super(capacity);
}

public class GeneratorMap extends HashMap<CGIdentifier, List<WeightedBlock>> implements JanksonSerializable {
@SafeVarargs
public static GeneratorMap of(Pair<CGIdentifier, List<WeightedBlock>>... gens) {
public static GeneratorMap of(Pair<CGIdentifier, ResultList>... gens) {
val map = new GeneratorMap();
Arrays.stream(gens).forEach(pair -> map.put(pair.getFirst(), pair.getSecond()));
return map;
}

@Override
@Serializer
public JsonObject toJson() {
return (JsonObject) JANKSON.toJson(this);
public JsonElement toJson() {
JsonObject result = new JsonObject();
this.forEach((k, v) -> result.put(k.toString(), v.toJson()));
return result;
}

@Deserializer
Expand All @@ -37,10 +45,49 @@ public static GeneratorMap fromJson(JsonObject json) {
GeneratorMap result = new GeneratorMap();
json.forEach((k, v) -> {
if (!(v instanceof JsonArray)) return;
val id = CGIdentifier.of(k);
List<WeightedBlock> list = listFromJson((JsonArray) v, WeightedBlock::fromJson);
result.put(id, list);
result.put(CGIdentifier.of(k), ResultList.fromJson(v));
});
return result;
}

static int capacity(int expectedSize) {
if (expectedSize < 3) {
if (expectedSize < 0) {
throw new IllegalArgumentException("expectedSize cannot be negative but was: " + expectedSize);
}
return expectedSize + 1;
}
if (expectedSize < Ints.MAX_POWER_OF_TWO) {
// This is the calculation used in JDK8 to resize when a putAll
// happens; it seems to be the most conservative calculation we
// can make. 0.75 is the default load factor.
return (int) ((float) expectedSize / 0.75F + 1.0F);
}
return Integer.MAX_VALUE; // any large value
}

@Override
public void toPacket(FriendlyByteBuf buf) {
buf.writeMap(
this,
(o, key) -> key.writeToBuf(o), (o, blocks) -> o.writeCollection(blocks, (p, block) -> block.toPacket(p))
);
}

public static GeneratorMap withExpectedSize(int expectedSize) {
return new GeneratorMap(capacity(expectedSize));
}

public static GeneratorMap fromPacket(FriendlyByteBuf buf) {
int i = buf.readVarInt();
val map = GeneratorMap.withExpectedSize(i);

for(int j = 0; j < i; ++j) {
val id = CGIdentifier.readFromBuf(buf);
val list = buf.readList(WeightedBlock::fromPacket);
map.put(id, new ResultList(list));
}

return map;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.github.null2264.cobblegen.data.config;

import blue.endless.jankson.JsonArray;
import blue.endless.jankson.JsonElement;
import blue.endless.jankson.JsonObject;
import blue.endless.jankson.annotation.Deserializer;
import blue.endless.jankson.annotation.Serializer;
import io.github.null2264.cobblegen.data.JanksonSerializable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ResultList extends ArrayList<WeightedBlock> implements JanksonSerializable {
public ResultList() {}

public ResultList(List<WeightedBlock> weightedBlocks) {
this.addAll(weightedBlocks);
}

public static ResultList of(WeightedBlock... weightedBlocks) {
return new ResultList(Arrays.asList(weightedBlocks));
}

@Override
@Serializer
public JsonElement toJson() {
JsonArray array = new JsonArray();
this.forEach((block) -> array.add(block.toJson()));
return array;
}

@Deserializer
public static ResultList fromJson(JsonElement json) {
if (json == null) return null;

ResultList result = new ResultList();
((JsonArray) json).stream()
.filter((e) -> e instanceof JsonObject)
.forEach((e) -> {
WeightedBlock block = WeightedBlock.fromJson((JsonObject) e);
if (block != null) result.add(block);
});
return result;
}
}
Loading

0 comments on commit 3a0dcc1

Please sign in to comment.