diff --git a/src/main/java/io/github/null2264/cobblegen/data/CGRange.java b/src/main/java/io/github/null2264/cobblegen/data/CGRange.java new file mode 100644 index 00000000..ac6b89b9 --- /dev/null +++ b/src/main/java/io/github/null2264/cobblegen/data/CGRange.java @@ -0,0 +1,82 @@ +package io.github.null2264.cobblegen.data; + +import blue.endless.jankson.JsonElement; +import blue.endless.jankson.JsonPrimitive; +import blue.endless.jankson.annotation.Deserializer; +import blue.endless.jankson.annotation.Serializer; + +public class CGRange +{ + private final CGRange.Value min; + private final CGRange.Value max; + + public CGRange(CGRange.Value min, CGRange.Value max) { + this.min = min; + this.max = max; + } + + public CGRange.Value getMin() { + return min; + } + + public CGRange.Value getMax() { + return max; + } + + public static class Value { + private final boolean inclusive; + private final int value; + + public Value(int value, boolean isInclusive) { + this.value = value; + this.inclusive = isInclusive; + } + + public int value() { + return value; + } + + public boolean inclusive() { + return inclusive; + } + + public boolean lt(int value) { + if (inclusive) { + return value <= this.value; + } else { + return value < this.value; + } + } + + public boolean gt(int value) { + if (inclusive) { + return value >= this.value; + } else { + return value > this.value; + } + } + } + + public static CGRange fromString(String str) { + return new CGRange( + new Value(Integer.parseInt(str.substring(1, str.indexOf(','))), str.charAt(0) == '['), + new Value(Integer.parseInt(str.substring(str.indexOf(',') + 1, str.length() - 1)), str.charAt(str.length() - 1) == ']') + ); + } + + @Serializer + public JsonElement toJson() { + return JsonPrimitive.of( + (min.inclusive() ? "[" : "(") + + min.value() + + "," + + max.value() + + (min.inclusive() ? "]" : ")") + ); + } + + @Deserializer + public static CGRange fromJson(JsonPrimitive json) { + return fromString(json.asString()); + } +} \ No newline at end of file diff --git a/src/main/java/io/github/null2264/cobblegen/data/CGSemVer.java b/src/main/java/io/github/null2264/cobblegen/data/CGSemVer.java new file mode 100644 index 00000000..cdc04f8e --- /dev/null +++ b/src/main/java/io/github/null2264/cobblegen/data/CGSemVer.java @@ -0,0 +1,96 @@ +package io.github.null2264.cobblegen.data; + +import blue.endless.jankson.JsonElement; +import blue.endless.jankson.JsonPrimitive; +import blue.endless.jankson.annotation.Deserializer; +import blue.endless.jankson.annotation.Serializer; + +import java.util.Objects; + +public class CGSemVer +{ + private final int major; + private final int minor; + private final int patch; + + public CGSemVer(int major, int minor, int patch) { + this.major = major; + this.minor = minor; + this.patch = patch; + } + + public CGSemVer(int major, int minor) { + this(major, minor, 0); + } + + public static CGSemVer fromString(String str) { + String[] str2 = str.split("\\."); + int major = str2.length >= 1 ? Integer.parseInt(str2[0]) : 0; + int minor = str2.length >= 2 ? Integer.parseInt(str2[1]) : 0; + int patch = str2.length >= 3 ? Integer.parseInt(str2[2]) : 0; + return new CGSemVer(major, minor, patch); + } + + @Override + public String toString() { + return major + "." + minor + "." + patch; + } + + public int major() { + return major; + } + + public int minor() { + return minor; + } + + public int patch() { + return patch; + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof CGSemVer)) return false; + CGSemVer other = (CGSemVer) obj; + return this.major() == other.major() && this.minor() == other.minor() && this.patch() == other.patch(); + } + + @Override + public int hashCode() { + return Objects.hash(major, minor, patch); + } + + public boolean lt(CGSemVer other) { + if (this.major() != other.major()) return this.major() < other.major(); + if (this.minor() != other.minor()) return this.minor() < other.minor(); + return this.patch() < other.patch(); + } + + public boolean lte(CGSemVer other) { + if (this.major() != other.major()) return this.major() < other.major(); + if (this.minor() != other.minor()) return this.minor() < other.minor(); + return this.patch() <= other.patch(); + } + + public boolean gt(CGSemVer other) { + if (this.major() != other.major()) return this.major() > other.major(); + if (this.minor() != other.minor()) return this.minor() > other.minor(); + return this.patch() > other.patch(); + } + + public boolean gte(CGSemVer other) { + if (this.major() != other.major()) return this.major() > other.major(); + if (this.minor() != other.minor()) return this.minor() > other.minor(); + return this.patch() >= other.patch(); + } + + @Serializer + public JsonElement toJson() { + return JsonPrimitive.of(major + "." + minor + "." + patch); + } + + @Deserializer + public static CGSemVer fromJson(JsonPrimitive json) { + return CGSemVer.fromString(json.asString()); + } +} \ No newline at end of file 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 cd9eb625..077b4623 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 @@ -4,18 +4,24 @@ import blue.endless.jankson.annotation.Deserializer; import blue.endless.jankson.annotation.Serializer; import io.github.null2264.cobblegen.data.CGIdentifier; +import io.github.null2264.cobblegen.data.CGSemVer; import io.github.null2264.cobblegen.data.JanksonSerializable; import io.github.null2264.cobblegen.data.Pair; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import static io.github.null2264.cobblegen.CobbleGen.META_CONFIG; import static io.github.null2264.cobblegen.compat.CollectionCompat.listOf; @SuppressWarnings("TextBlockMigration") public class ConfigData implements Config, JanksonSerializable { + /** + * Deprecated, moved to {@link io.github.null2264.cobblegen.data.config.ConfigMetaData} + */ @Comment(value = "CobbleGen Format Version, you can leave this alone for now. v2.0 will be released in CobbleGen v6.0") @NotNull + @Deprecated public String formatVersion = "1.0"; @Nullable @@ -127,7 +133,6 @@ public static ConfigData defaultConfig() { @Serializer public JsonObject toJson() { JsonObject json = new JsonObject(); - json.put("formatVersion", JsonPrimitive.of(formatVersion)); if (cobbleGen != null) json.put("cobbleGen", cobbleGen.toJson()); if (stoneGen != null) json.put("stoneGen", stoneGen.toJson()); if (basaltGen != null) json.put("basaltGen", basaltGen.toJson()); @@ -139,13 +144,7 @@ public JsonObject toJson() { @Deserializer public static ConfigData fromJson(JsonObject json) { ConfigData config = new ConfigData(); - JsonElement formatVersion = json.get("formatVersion"); - config.formatVersion = (formatVersion instanceof JsonPrimitive) ? ((JsonPrimitive) formatVersion).asString() : "1.0"; - /* TODO - if (config.formatVersion.equals("1.0")) { - // TODO: Migrate to 2.0 - } - */ + config.formatVersion = META_CONFIG.formatVersion.toString(); config.cobbleGen = ResultList.fromJson(json.get("cobbleGen")); config.stoneGen = ResultList.fromJson(json.get("stoneGen")); config.stoneGen = ResultList.fromJson(json.get("stoneGen")); diff --git a/src/main/java/io/github/null2264/cobblegen/data/config/ConfigMetaData.java b/src/main/java/io/github/null2264/cobblegen/data/config/ConfigMetaData.java index 253f4103..0b87ff4a 100644 --- a/src/main/java/io/github/null2264/cobblegen/data/config/ConfigMetaData.java +++ b/src/main/java/io/github/null2264/cobblegen/data/config/ConfigMetaData.java @@ -1,10 +1,15 @@ package io.github.null2264.cobblegen.data.config; import blue.endless.jankson.Comment; +import io.github.null2264.cobblegen.data.CGSemVer; import org.jetbrains.annotations.NotNull; public class ConfigMetaData implements Config { + @Comment(value = "CobbleGen Format Version, you can leave this alone for now. v2.0 will be released in CobbleGen v6.0") + @NotNull + public CGSemVer formatVersion = CGSemVer.fromString("1.1"); + @Comment(value="Enable Recipe Viewer support (EMI/REI/JEI)") @NotNull public Boolean enableRecipeViewer = true;