From 3a0dcc1146aaae9501f8ff2521e5bdac684464d4 Mon Sep 17 00:00:00 2001 From: ziro Date: Sun, 10 Dec 2023 10:24:13 +0700 Subject: [PATCH] refactor: Replace the rest of config data with its own class --- .../github/null2264/cobblegen/data/Pair.java | 4 ++ .../cobblegen/data/config/ConfigData.java | 67 +++++++------------ .../data/config/FluidInteractionMap.java | 37 ++++++++++ .../cobblegen/data/config/GeneratorMap.java | 67 ++++++++++++++++--- .../cobblegen/data/config/ResultList.java | 46 +++++++++++++ .../data/generator/BasaltGenerator.java | 29 ++++---- .../data/generator/CobbleGenerator.java | 50 ++++++-------- .../data/generator/StoneGenerator.java | 32 +++++---- .../data/model/BuiltInGenerator.java | 14 ++-- .../cobblegen/data/model/Generator.java | 7 +- .../cobblegen/integration/BuiltInPlugin.java | 27 ++++---- .../cobblegen/integration/CreatePlugin.java | 7 +- 12 files changed, 243 insertions(+), 144 deletions(-) create mode 100644 src/main/java/io/github/null2264/cobblegen/data/config/FluidInteractionMap.java create mode 100644 src/main/java/io/github/null2264/cobblegen/data/config/ResultList.java diff --git a/src/main/java/io/github/null2264/cobblegen/data/Pair.java b/src/main/java/io/github/null2264/cobblegen/data/Pair.java index c4be386..685df3c 100644 --- a/src/main/java/io/github/null2264/cobblegen/data/Pair.java +++ b/src/main/java/io/github/null2264/cobblegen/data/Pair.java @@ -9,6 +9,10 @@ public Pair(A a, B b) { this.second = b; } + public static Pair of(A a, B b) { + return new Pair<>(a, b); + } + public final A getFirst() { return this.first; } diff --git a/src/main/java/io/github/null2264/cobblegen/data/config/ConfigData.java b/src/main/java/io/github/null2264/cobblegen/data/config/ConfigData.java index 557362a..5dd3d0a 100644 --- a/src/main/java/io/github/null2264/cobblegen/data/config/ConfigData.java +++ b/src/main/java/io/github/null2264/cobblegen/data/config/ConfigData.java @@ -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 @@ -38,13 +35,13 @@ public class ConfigData implements Config, JanksonSerializable "minY": 0, "maxY": 69 }""") - public List cobbleGen; + public ResultList cobbleGen; @Nullable - public List stoneGen; + public ResultList stoneGen; @Nullable - public List basaltGen; + public ResultList basaltGen; @Nullable @Comment(value = """ @@ -71,11 +68,11 @@ public class ConfigData implements Config, JanksonSerializable public CustomGen customGen; @Nullable - public Map> 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, @@ -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), @@ -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), @@ -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")) ) @@ -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; } @@ -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 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; } } \ No newline at end of file diff --git a/src/main/java/io/github/null2264/cobblegen/data/config/FluidInteractionMap.java b/src/main/java/io/github/null2264/cobblegen/data/config/FluidInteractionMap.java new file mode 100644 index 0000000..d47df19 --- /dev/null +++ b/src/main/java/io/github/null2264/cobblegen/data/config/FluidInteractionMap.java @@ -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> 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 root = new HashMap<>(); + subroot.forEach((fluid2, jsonElement) -> root.put(fluid2, AdvancedGen.fromJson((JsonObject) jsonElement))); + result.put(fluid1, root); + }); + return result; + } +} \ No newline at end of file diff --git a/src/main/java/io/github/null2264/cobblegen/data/config/GeneratorMap.java b/src/main/java/io/github/null2264/cobblegen/data/config/GeneratorMap.java index ab8c699..1bad51e 100644 --- a/src/main/java/io/github/null2264/cobblegen/data/config/GeneratorMap.java +++ b/src/main/java/io/github/null2264/cobblegen/data/config/GeneratorMap.java @@ -1,24 +1,30 @@ 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 implements JanksonSerializable, PacketSerializable { + public GeneratorMap() {} + + public GeneratorMap(int capacity) { + super(capacity); + } -public class GeneratorMap extends HashMap> implements JanksonSerializable { @SafeVarargs - public static GeneratorMap of(Pair>... gens) { + public static GeneratorMap of(Pair... gens) { val map = new GeneratorMap(); Arrays.stream(gens).forEach(pair -> map.put(pair.getFirst(), pair.getSecond())); return map; @@ -26,8 +32,10 @@ public static GeneratorMap of(Pair>... gens) { @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 @@ -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 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; + } } \ No newline at end of file diff --git a/src/main/java/io/github/null2264/cobblegen/data/config/ResultList.java b/src/main/java/io/github/null2264/cobblegen/data/config/ResultList.java new file mode 100644 index 0000000..d9b9e6e --- /dev/null +++ b/src/main/java/io/github/null2264/cobblegen/data/config/ResultList.java @@ -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 implements JanksonSerializable { + public ResultList() {} + + public ResultList(List 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; + } +} \ No newline at end of file diff --git a/src/main/java/io/github/null2264/cobblegen/data/generator/BasaltGenerator.java b/src/main/java/io/github/null2264/cobblegen/data/generator/BasaltGenerator.java index 2a8f018..a4f9175 100644 --- a/src/main/java/io/github/null2264/cobblegen/data/generator/BasaltGenerator.java +++ b/src/main/java/io/github/null2264/cobblegen/data/generator/BasaltGenerator.java @@ -1,5 +1,8 @@ package io.github.null2264.cobblegen.data.generator; +import io.github.null2264.cobblegen.data.Pair; +import io.github.null2264.cobblegen.data.config.GeneratorMap; +import io.github.null2264.cobblegen.data.config.ResultList; import io.github.null2264.cobblegen.data.config.WeightedBlock; import io.github.null2264.cobblegen.data.CGIdentifier; import io.github.null2264.cobblegen.data.model.BlockGenerator; @@ -18,36 +21,35 @@ import org.jetbrains.annotations.NotNull; import java.util.*; -import java.util.stream.Collectors; public class BasaltGenerator extends BlockGenerator { - private final Map> possibleBlocks; + private final GeneratorMap possibleBlocks; private final Block block; private final boolean silent; - public BasaltGenerator(List possibleBlocks, Block block, boolean silent) { - this(Map.of(CGIdentifier.fromMC(Util.getBlockId(Blocks.SOUL_SOIL)), possibleBlocks), block, silent); + public BasaltGenerator(ResultList possibleBlocks, Block block, boolean silent) { + this(GeneratorMap.of(Pair.of(CGIdentifier.fromMC(Util.getBlockId(Blocks.SOUL_SOIL)), possibleBlocks)), block, silent); } - public BasaltGenerator(Map> possibleBlocks, Block block, boolean silent) { + public BasaltGenerator(GeneratorMap possibleBlocks, Block block, boolean silent) { this.possibleBlocks = possibleBlocks; this.block = block; this.silent = silent; } public static BasaltGenerator fromString(Map> possibleBlocks, Block block, boolean silent) { + val map = new GeneratorMap(); + possibleBlocks.forEach((k, v) -> map.put(CGIdentifier.of(k), new ResultList(v))); return new BasaltGenerator( - possibleBlocks.entrySet().stream() - .map(e -> Map.entry(CGIdentifier.of(e.getKey()), e.getValue())) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)), + map, block, silent ); } @Override - public @NotNull Map> getOutput() { + public @NotNull GeneratorMap getOutput() { return possibleBlocks; } @@ -87,11 +89,7 @@ public void toPacket(FriendlyByteBuf buf) { buf.writeUtf(Util.getBlockId(block).toString()); buf.writeBoolean(silent); - val outMap = getOutput(); - buf.writeMap( - outMap, - (o, key) -> key.writeToBuf(o), (o, blocks) -> o.writeCollection(blocks, (p, block) -> block.toPacket(p)) - ); + getOutput().toPacket(buf); } @SuppressWarnings("unused") @@ -99,8 +97,7 @@ public static Generator fromPacket(FriendlyByteBuf buf) { val block = Util.getBlock(buf.readResourceLocation()); val silent = buf.readBoolean(); - Map> outMap = - buf.readMap(CGIdentifier::readFromBuf, (o) -> o.readList(WeightedBlock::fromPacket)); + GeneratorMap outMap = GeneratorMap.fromPacket(buf); return new BasaltGenerator(outMap, block, silent); } diff --git a/src/main/java/io/github/null2264/cobblegen/data/generator/CobbleGenerator.java b/src/main/java/io/github/null2264/cobblegen/data/generator/CobbleGenerator.java index 259838e..3ea60e8 100644 --- a/src/main/java/io/github/null2264/cobblegen/data/generator/CobbleGenerator.java +++ b/src/main/java/io/github/null2264/cobblegen/data/generator/CobbleGenerator.java @@ -1,7 +1,10 @@ package io.github.null2264.cobblegen.data.generator; import io.github.null2264.cobblegen.CobbleGen; +import io.github.null2264.cobblegen.data.Pair; import io.github.null2264.cobblegen.data.config.ConfigMetaData; +import io.github.null2264.cobblegen.data.config.GeneratorMap; +import io.github.null2264.cobblegen.data.config.ResultList; import io.github.null2264.cobblegen.data.config.WeightedBlock; import io.github.null2264.cobblegen.data.CGIdentifier; import io.github.null2264.cobblegen.data.model.BlockGenerator; @@ -23,24 +26,23 @@ import org.jetbrains.annotations.Nullable; import java.util.*; -import java.util.stream.Collectors; public class CobbleGenerator extends BlockGenerator { - private final Map> possibleBlocks; - private final Map> obsidianReplacements; + private final GeneratorMap possibleBlocks; + private final GeneratorMap obsidianReplacements; private Fluid fluid; private final boolean silent; - public CobbleGenerator(List possibleBlocks, Fluid fluid, boolean silent) { - this(possibleBlocks, fluid, silent, Map.of()); + public CobbleGenerator(ResultList possibleBlocks, Fluid fluid, boolean silent) { + this(possibleBlocks, fluid, silent, GeneratorMap.of()); } - public CobbleGenerator(List possibleBlocks, Fluid fluid, boolean silent, Map> obsidianReplacements) { - this(Map.of(CGIdentifier.wildcard(), possibleBlocks), fluid, silent, obsidianReplacements); + public CobbleGenerator(ResultList possibleBlocks, Fluid fluid, boolean silent, GeneratorMap obsidianReplacements) { + this(GeneratorMap.of(Pair.of(CGIdentifier.wildcard(), possibleBlocks)), fluid, silent, obsidianReplacements); } - public CobbleGenerator(Map> possibleBlocks, Fluid fluid, boolean silent, Map> obsidianReplacements) { + public CobbleGenerator(GeneratorMap possibleBlocks, Fluid fluid, boolean silent, GeneratorMap obsidianReplacements) { this.possibleBlocks = possibleBlocks; this.obsidianReplacements = obsidianReplacements; this.fluid = fluid; @@ -48,25 +50,25 @@ public CobbleGenerator(Map> possibleBlocks, Fl } public static CobbleGenerator fromString(Map> possibleBlocks, Fluid fluid, boolean silent, Map> obsidianReplacements) { + val map1 = new GeneratorMap(); + possibleBlocks.forEach((k, v) -> map1.put(CGIdentifier.of(k), new ResultList(v))); + val map2 = new GeneratorMap(); + obsidianReplacements.forEach((k, v) -> map2.put(CGIdentifier.of(k), new ResultList(v))); return new CobbleGenerator( - possibleBlocks.entrySet().stream() - .map(e -> Map.entry(CGIdentifier.of(e.getKey()), e.getValue())) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)), + map1, fluid, silent, - obsidianReplacements.entrySet().stream() - .map(e -> Map.entry(CGIdentifier.of(e.getKey()), e.getValue())) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)) + map2 ); } @Override - public @NotNull Map> getOutput() { + public @NotNull GeneratorMap getOutput() { return possibleBlocks; } @Override - public Map> getObsidianOutput() { + public GeneratorMap getObsidianOutput() { return obsidianReplacements; } @@ -122,14 +124,8 @@ public void toPacket(FriendlyByteBuf buf) { buf.writeResourceLocation(Util.getFluidId(fluid)); buf.writeBoolean(silent); - buf.writeMap( - getOutput(), - (o, key) -> key.writeToBuf(o), (o, blocks) -> o.writeCollection(blocks, (p, block) -> block.toPacket(p)) - ); - buf.writeMap( - getObsidianOutput(), - (o, key) -> key.writeToBuf(o), (o, blocks) -> o.writeCollection(blocks, (p, block) -> block.toPacket(p)) - ); + getOutput().toPacket(buf); + getObsidianOutput().toPacket(buf); } @SuppressWarnings("unused") @@ -137,10 +133,8 @@ public static Generator fromPacket(FriendlyByteBuf buf) { val fluid = Util.getFluid(buf.readResourceLocation()); val silent = buf.readBoolean(); - Map> outMap = - buf.readMap(CGIdentifier::readFromBuf, (o) -> o.readList(WeightedBlock::fromPacket)); - Map> obiMap = - buf.readMap(CGIdentifier::readFromBuf, (o) -> o.readList(WeightedBlock::fromPacket)); + GeneratorMap outMap = GeneratorMap.fromPacket(buf); + GeneratorMap obiMap = GeneratorMap.fromPacket(buf); return new CobbleGenerator(outMap, fluid, silent, obiMap); } diff --git a/src/main/java/io/github/null2264/cobblegen/data/generator/StoneGenerator.java b/src/main/java/io/github/null2264/cobblegen/data/generator/StoneGenerator.java index d8657db..1d1740f 100644 --- a/src/main/java/io/github/null2264/cobblegen/data/generator/StoneGenerator.java +++ b/src/main/java/io/github/null2264/cobblegen/data/generator/StoneGenerator.java @@ -1,5 +1,8 @@ package io.github.null2264.cobblegen.data.generator; +import io.github.null2264.cobblegen.data.Pair; +import io.github.null2264.cobblegen.data.config.GeneratorMap; +import io.github.null2264.cobblegen.data.config.ResultList; import io.github.null2264.cobblegen.data.config.WeightedBlock; import io.github.null2264.cobblegen.data.CGIdentifier; import io.github.null2264.cobblegen.data.model.BuiltInGenerator; @@ -23,38 +26,38 @@ public class StoneGenerator implements BuiltInGenerator { - private final Map> possibleBlocks; + private final GeneratorMap possibleBlocks; private final Fluid fluid; private final boolean silent; - public StoneGenerator(List possibleBlocks, Fluid fluid, boolean silent) { - this(Map.of(CGIdentifier.wildcard(), possibleBlocks), fluid, silent); + public StoneGenerator(ResultList possibleBlocks, Fluid fluid, boolean silent) { + this(GeneratorMap.of(Pair.of(CGIdentifier.wildcard(), possibleBlocks)), fluid, silent); } - public StoneGenerator(Map> possibleBlocks, Fluid fluid, boolean silent) { + public StoneGenerator(GeneratorMap possibleBlocks, Fluid fluid, boolean silent) { this.possibleBlocks = possibleBlocks; this.fluid = fluid; this.silent = silent; } public static StoneGenerator fromString(Map> possibleBlocks, Fluid fluid, boolean silent) { + val map = new GeneratorMap(); + possibleBlocks.forEach((k, v) -> map.put(CGIdentifier.of(k), new ResultList(v))); return new StoneGenerator( - possibleBlocks.entrySet().stream() - .map(e -> Map.entry(CGIdentifier.of(e.getKey()), e.getValue())) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)), + map, fluid, silent ); } @Override - public @NotNull Map> getOutput() { + public @NotNull GeneratorMap getOutput() { return possibleBlocks; } @Override - public Map> getObsidianOutput() { - return Map.of(CGIdentifier.wildcard(), List.of(WeightedBlock.fromBlock(Blocks.STONE, 100D))); + public GeneratorMap getObsidianOutput() { + return GeneratorMap.of(Pair.of(CGIdentifier.wildcard(), ResultList.of(WeightedBlock.fromBlock(Blocks.STONE, 100D)))); } @Override @@ -104,11 +107,7 @@ public void toPacket(FriendlyByteBuf buf) { buf.writeResourceLocation(Util.getFluidId(fluid)); buf.writeBoolean(silent); - val outMap = getOutput(); - buf.writeMap( - outMap, - (o, key) -> key.writeToBuf(o), (o, blocks) -> o.writeCollection(blocks, (p, block) -> block.toPacket(p)) - ); + getOutput().toPacket(buf); } @SuppressWarnings("unused") @@ -116,8 +115,7 @@ public static Generator fromPacket(FriendlyByteBuf buf) { val fluid = Util.getFluid(buf.readResourceLocation()); val silent = buf.readBoolean(); - Map> outMap = - buf.readMap(CGIdentifier::readFromBuf, (o) -> o.readList(WeightedBlock::fromPacket)); + GeneratorMap outMap = GeneratorMap.fromPacket(buf); return new StoneGenerator(outMap, fluid, silent); } diff --git a/src/main/java/io/github/null2264/cobblegen/data/model/BuiltInGenerator.java b/src/main/java/io/github/null2264/cobblegen/data/model/BuiltInGenerator.java index 110d0fc..d14308f 100644 --- a/src/main/java/io/github/null2264/cobblegen/data/model/BuiltInGenerator.java +++ b/src/main/java/io/github/null2264/cobblegen/data/model/BuiltInGenerator.java @@ -1,5 +1,7 @@ package io.github.null2264.cobblegen.data.model; +import io.github.null2264.cobblegen.data.config.GeneratorMap; +import io.github.null2264.cobblegen.data.config.ResultList; import io.github.null2264.cobblegen.data.config.WeightedBlock; import io.github.null2264.cobblegen.data.CGIdentifier; import io.github.null2264.cobblegen.util.Util; @@ -11,9 +13,7 @@ import net.minecraft.world.level.block.state.BlockState; import org.jetbrains.annotations.ApiStatus; -import java.util.ArrayList; import java.util.List; -import java.util.Map; import java.util.Optional; import java.util.concurrent.atomic.AtomicReference; @@ -21,13 +21,13 @@ public interface BuiltInGenerator extends Generator { // https://stackoverflow.com/a/6737362 - private String randomizeBlockId(Block key, String dim, Integer yLevel, Map> candidates) { + private String randomizeBlockId(Block key, String dim, Integer yLevel, GeneratorMap candidates) { val blockIds = candidates.getOrDefault( CGIdentifier.fromMC(Util.getBlockId(key)), - candidates.getOrDefault(CGIdentifier.wildcard(), List.of()) + candidates.getOrDefault(CGIdentifier.wildcard(), new ResultList()) ); - ArrayList filteredBlockIds = new ArrayList<>(); + ResultList filteredBlockIds = new ResultList(); AtomicReference totalWeight = new AtomicReference<>(0.0); for (WeightedBlock block : blockIds) { @@ -64,11 +64,11 @@ private String randomizeBlockId(Block key, String dim, Integer yLevel, Map getBlockCandidate(LevelAccessor level, BlockPos pos, Map> candidates) { + default Optional getBlockCandidate(LevelAccessor level, BlockPos pos, GeneratorMap candidates) { return getBlockCandidate(level, pos, candidates, null); } - default Optional getBlockCandidate(LevelAccessor level, BlockPos pos, Map> candidates, Block defaultBlock) { + default Optional getBlockCandidate(LevelAccessor level, BlockPos pos, GeneratorMap candidates, Block defaultBlock) { val replacementId = randomizeBlockId( level.getBlockState(pos.below()).getBlock(), Util.getDimension(level), diff --git a/src/main/java/io/github/null2264/cobblegen/data/model/Generator.java b/src/main/java/io/github/null2264/cobblegen/data/model/Generator.java index 76563a0..9fa8e8e 100644 --- a/src/main/java/io/github/null2264/cobblegen/data/model/Generator.java +++ b/src/main/java/io/github/null2264/cobblegen/data/model/Generator.java @@ -1,5 +1,6 @@ package io.github.null2264.cobblegen.data.model; +import io.github.null2264.cobblegen.data.config.GeneratorMap; import io.github.null2264.cobblegen.data.config.WeightedBlock; import io.github.null2264.cobblegen.data.CGIdentifier; import io.github.null2264.cobblegen.util.CGLog; @@ -53,13 +54,13 @@ default Optional tryGenerate(LevelAccessor level, BlockPos pos, Flui } @NotNull - Map> getOutput(); + GeneratorMap getOutput(); /** * @return The output block when a source fluid met another fluid (e.g. Water -> Stone / Lava -> Obsidian) */ - default Map> getObsidianOutput() { - return Map.of(); + default GeneratorMap getObsidianOutput() { + return GeneratorMap.of(); } @NotNull diff --git a/src/main/java/io/github/null2264/cobblegen/integration/BuiltInPlugin.java b/src/main/java/io/github/null2264/cobblegen/integration/BuiltInPlugin.java index 47e259b..8b1a646 100644 --- a/src/main/java/io/github/null2264/cobblegen/integration/BuiltInPlugin.java +++ b/src/main/java/io/github/null2264/cobblegen/integration/BuiltInPlugin.java @@ -4,7 +4,8 @@ import io.github.null2264.cobblegen.CobbleGenPlugin; import io.github.null2264.cobblegen.compat.LoaderCompat; import io.github.null2264.cobblegen.data.config.ConfigData; -import io.github.null2264.cobblegen.data.config.WeightedBlock; +import io.github.null2264.cobblegen.data.config.GeneratorMap; +import io.github.null2264.cobblegen.data.config.ResultList; import io.github.null2264.cobblegen.data.CGIdentifier; import io.github.null2264.cobblegen.data.generator.BasaltGenerator; import io.github.null2264.cobblegen.data.generator.CobbleGenerator; @@ -23,10 +24,6 @@ import java.io.File; import java.nio.file.Path; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; import static io.github.null2264.cobblegen.CobbleGen.MOD_ID; @@ -59,19 +56,19 @@ public void registerInteraction(CGRegistry registry) { AtomicInteger count = new AtomicInteger(); - Map> stoneGen = new HashMap<>(); + GeneratorMap stoneGen = new GeneratorMap(); if (config.customGen != null && config.customGen.stoneGen != null) - stoneGen = new HashMap<>(config.customGen.stoneGen); - Map> cobbleGen = new HashMap<>(); + stoneGen = config.customGen.stoneGen; + GeneratorMap cobbleGen = new GeneratorMap(); if (config.customGen != null && config.customGen.cobbleGen != null) - cobbleGen = new HashMap<>(config.customGen.cobbleGen); - Map> basaltGen = new HashMap<>(); + cobbleGen = config.customGen.cobbleGen; + GeneratorMap basaltGen = new GeneratorMap(); if (config.customGen != null && config.customGen.basaltGen != null) - basaltGen = new HashMap<>(config.customGen.basaltGen); + basaltGen = config.customGen.basaltGen; - stoneGen.put(CGIdentifier.wildcard(), notNullOr(config.stoneGen, new ArrayList<>())); - cobbleGen.put(CGIdentifier.wildcard(), notNullOr(config.cobbleGen, new ArrayList<>())); - basaltGen.put(CGIdentifier.fromBlock(Blocks.SOUL_SOIL), notNullOr(config.basaltGen, new ArrayList<>())); + stoneGen.put(CGIdentifier.wildcard(), notNullOr(config.stoneGen, new ResultList())); + cobbleGen.put(CGIdentifier.wildcard(), notNullOr(config.cobbleGen, new ResultList())); + basaltGen.put(CGIdentifier.fromBlock(Blocks.SOUL_SOIL), notNullOr(config.basaltGen, new ResultList())); if (config.advanced != null) config.advanced.forEach((fluid, value) -> { @@ -109,7 +106,7 @@ public void registerInteraction(CGRegistry registry) { }); registry.addGenerator(Fluids.LAVA, new StoneGenerator(stoneGen, Fluids.WATER, false)); - registry.addGenerator(Fluids.LAVA, new CobbleGenerator(cobbleGen, Fluids.WATER, false, Map.of())); + registry.addGenerator(Fluids.LAVA, new CobbleGenerator(cobbleGen, Fluids.WATER, false, GeneratorMap.of())); registry.addGenerator(Fluids.LAVA, new BasaltGenerator(basaltGen, Blocks.BLUE_ICE, false)); count.addAndGet(3); diff --git a/src/main/java/io/github/null2264/cobblegen/integration/CreatePlugin.java b/src/main/java/io/github/null2264/cobblegen/integration/CreatePlugin.java index 6e8f631..067d442 100644 --- a/src/main/java/io/github/null2264/cobblegen/integration/CreatePlugin.java +++ b/src/main/java/io/github/null2264/cobblegen/integration/CreatePlugin.java @@ -4,6 +4,7 @@ import io.github.null2264.cobblegen.CobbleGen; import io.github.null2264.cobblegen.CobbleGenPlugin; import io.github.null2264.cobblegen.compat.LoaderCompat; +import io.github.null2264.cobblegen.data.config.ResultList; import io.github.null2264.cobblegen.data.config.WeightedBlock; import io.github.null2264.cobblegen.data.generator.CobbleGenerator; import io.github.null2264.cobblegen.data.model.CGRegistry; @@ -12,8 +13,6 @@ import net.minecraft.resources.ResourceLocation; import net.minecraft.world.level.material.Fluids; -import java.util.List; - @CGPlugin public class CreatePlugin implements CobbleGenPlugin { @@ -25,7 +24,7 @@ public void registerInteraction(CGRegistry registry) { registry.addGenerator( Fluids.LAVA, new CobbleGenerator( - List.of(WeightedBlock.fromBlock(Util.getBlock(new ResourceLocation("create", "limestone")), 1.0)), + ResultList.of(WeightedBlock.fromBlock(Util.getBlock(new ResourceLocation("create", "limestone")), 1.0)), Util.getFluid(new ResourceLocation("create", "honey")), false ) @@ -33,7 +32,7 @@ public void registerInteraction(CGRegistry registry) { registry.addGenerator( Fluids.LAVA, new CobbleGenerator( - List.of(WeightedBlock.fromBlock(Util.getBlock(new ResourceLocation("create", "scoria")), 1.0)), + ResultList.of(WeightedBlock.fromBlock(Util.getBlock(new ResourceLocation("create", "scoria")), 1.0)), Util.getFluid(new ResourceLocation("create", "chocolate")), false )