diff --git a/gradle.properties b/gradle.properties index b5350ad..0da238f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,7 +9,7 @@ yarn_mappings=1.21+build.2 loader_version=0.15.11 # Mod Properties -mod_version=0.4.2-beta +mod_version=1.0.0 maven_group=com.mmodding.env.json archives_base_name=env_json diff --git a/src/main/java/com/mmodding/env/json/api/EnvJsonVisitor.java b/src/main/java/com/mmodding/env/json/api/EnvJsonVisitor.java index a2880b6..c4eee27 100644 --- a/src/main/java/com/mmodding/env/json/api/EnvJsonVisitor.java +++ b/src/main/java/com/mmodding/env/json/api/EnvJsonVisitor.java @@ -1,8 +1,5 @@ package com.mmodding.env.json.api; -import com.mmodding.env.json.api.rule.SkyEnvJsonRule; -import com.mmodding.env.json.api.rule.VoidEnvJsonRule; -import com.mmodding.env.json.api.rule.WaterEnvJsonRule; import it.unimi.dsi.fastutil.ints.Int2BooleanFunction; import net.minecraft.registry.RegistryKey; import net.minecraft.registry.tag.TagKey; @@ -27,9 +24,9 @@ public interface EnvJsonVisitor { boolean applySubmerged(boolean submerged); - boolean applySky(SkyEnvJsonRule.Localization localization); + boolean applySky(RelativeLocation localization); - boolean applyWater(WaterEnvJsonRule.Localization localization); + boolean applyWater(RelativeLocation localization); - boolean applyVoid(VoidEnvJsonRule.Localization localization); + boolean applyVoid(RelativeLocation localization); } diff --git a/src/main/java/com/mmodding/env/json/api/RelativeLocation.java b/src/main/java/com/mmodding/env/json/api/RelativeLocation.java new file mode 100644 index 0000000..75c7ea3 --- /dev/null +++ b/src/main/java/com/mmodding/env/json/api/RelativeLocation.java @@ -0,0 +1,7 @@ +package com.mmodding.env.json.api; + +public enum RelativeLocation { + BELOW, + AT, + ABOVE +} diff --git a/src/main/java/com/mmodding/env/json/api/rule/AnyEnvJsonRule.java b/src/main/java/com/mmodding/env/json/api/rule/AnyEnvJsonRule.java deleted file mode 100644 index 76bec82..0000000 --- a/src/main/java/com/mmodding/env/json/api/rule/AnyEnvJsonRule.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.mmodding.env.json.api.rule; - -import java.util.List; - -public interface AnyEnvJsonRule extends EnvJsonRule { - - List rules(); -} diff --git a/src/main/java/com/mmodding/env/json/api/rule/DimensionEnvJsonRule.java b/src/main/java/com/mmodding/env/json/api/rule/DimensionEnvJsonRule.java deleted file mode 100644 index 7a21019..0000000 --- a/src/main/java/com/mmodding/env/json/api/rule/DimensionEnvJsonRule.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.mmodding.env.json.api.rule; - -import net.minecraft.registry.RegistryKey; -import net.minecraft.registry.tag.TagKey; -import net.minecraft.world.World; - -public interface DimensionEnvJsonRule extends EnvJsonRule { - - // If this method returns null, then the other one is nonnull. - RegistryKey dimension(); - - // If this method returns null, then the other one is nonnull. - TagKey tag(); -} diff --git a/src/main/java/com/mmodding/env/json/api/rule/EnvJsonRule.java b/src/main/java/com/mmodding/env/json/api/rule/EnvJsonRule.java index 7ac15e1..c5d49a6 100644 --- a/src/main/java/com/mmodding/env/json/api/rule/EnvJsonRule.java +++ b/src/main/java/com/mmodding/env/json/api/rule/EnvJsonRule.java @@ -9,70 +9,5 @@ static T safeCast(EnvJsonRule rule, Class type) { return EnvJsonRuleImpl.safeCast(rule, type); } - Type type(); - boolean apply(EnvJsonVisitor visitor); - - enum Type { - - /** - * Represents a sequence of combined rules with the AND operator. - */ - SEQUENCE, - - /** - * Represents a sequence of combined rules with the OR operator. - */ - ANY, - - /** - * Reverses the rule. - */ - NOT, - - /** - * Represents a dimension rule where the object should be. - */ - DIMENSION, - - /** - * Represents a biome rule where the object should be. - */ - BIOME, - - /** - * Represents an x coordinate comparator rule which is applied to the object. - */ - X_COORD, - - /** - * Represents a y coordinate comparator rule which is applied to the object. - */ - Y_COORD, - - /** - * Represents a z coordinate comparator rule which is applied to the object. - */ - Z_COORD, - - /** - * Represents a rule checking if the object os surrounded by water. - */ - SUBMERGED, - - /** - * Represents a rule checking if the object is below or above the sky limit. - */ - SKY, - - /** - * Represents a rule checking if the object is below or above the water level. - */ - WATER, - - /** - * Represents a rule checking if the object is below or above the void limit. - */ - VOID; - } } diff --git a/src/main/java/com/mmodding/env/json/api/rule/EnvJsonRules.java b/src/main/java/com/mmodding/env/json/api/rule/EnvJsonRules.java new file mode 100644 index 0000000..05d775b --- /dev/null +++ b/src/main/java/com/mmodding/env/json/api/rule/EnvJsonRules.java @@ -0,0 +1,25 @@ +package com.mmodding.env.json.api.rule; + +import com.google.gson.JsonElement; +import com.mmodding.env.json.api.rule.parsing.EnvJsonRuleParser; +import com.mmodding.env.json.impl.rule.EnvJsonRulesImpl; +import net.minecraft.util.Identifier; +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.NonExtendable +public final class EnvJsonRules { + + public EnvJsonRules() { + throw new IllegalStateException("EnvJsonRules class only contains static definitions"); + } + + /** + * Allows to register a new {@link EnvJsonRuleParser} under an identifier + * @param identifier the identifier + * @param parser the parser + * @param the class of the shape of the json element the parser should parse + */ + public static void register(Identifier identifier, EnvJsonRuleParser parser) { + EnvJsonRulesImpl.REGISTRY.put(identifier, parser); + } +} diff --git a/src/main/java/com/mmodding/env/json/api/rule/NotEnvJsonRule.java b/src/main/java/com/mmodding/env/json/api/rule/NotEnvJsonRule.java deleted file mode 100644 index a8d5f6e..0000000 --- a/src/main/java/com/mmodding/env/json/api/rule/NotEnvJsonRule.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.mmodding.env.json.api.rule; - -public interface NotEnvJsonRule extends EnvJsonRule { - - EnvJsonRule rule(); -} diff --git a/src/main/java/com/mmodding/env/json/api/rule/SequenceEnvJsonRule.java b/src/main/java/com/mmodding/env/json/api/rule/SequenceEnvJsonRule.java deleted file mode 100644 index 43d2544..0000000 --- a/src/main/java/com/mmodding/env/json/api/rule/SequenceEnvJsonRule.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.mmodding.env.json.api.rule; - -import java.util.List; - -public interface SequenceEnvJsonRule extends EnvJsonRule { - - List rules(); -} diff --git a/src/main/java/com/mmodding/env/json/api/rule/SkyEnvJsonRule.java b/src/main/java/com/mmodding/env/json/api/rule/SkyEnvJsonRule.java deleted file mode 100644 index 6063053..0000000 --- a/src/main/java/com/mmodding/env/json/api/rule/SkyEnvJsonRule.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.mmodding.env.json.api.rule; - -public interface SkyEnvJsonRule extends EnvJsonRule { - - Localization localisation(); - - enum Localization { - BELOW, - AT, - ABOVE - } -} diff --git a/src/main/java/com/mmodding/env/json/api/rule/SubmergedEnvJsonRule.java b/src/main/java/com/mmodding/env/json/api/rule/SubmergedEnvJsonRule.java deleted file mode 100644 index 6501f98..0000000 --- a/src/main/java/com/mmodding/env/json/api/rule/SubmergedEnvJsonRule.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.mmodding.env.json.api.rule; - -public interface SubmergedEnvJsonRule extends EnvJsonRule { - - boolean submerged(); -} diff --git a/src/main/java/com/mmodding/env/json/api/rule/VoidEnvJsonRule.java b/src/main/java/com/mmodding/env/json/api/rule/VoidEnvJsonRule.java deleted file mode 100644 index 87e2f87..0000000 --- a/src/main/java/com/mmodding/env/json/api/rule/VoidEnvJsonRule.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.mmodding.env.json.api.rule; - -public interface VoidEnvJsonRule extends EnvJsonRule { - - Localization localisation(); - - enum Localization { - BELOW, - AT, - ABOVE - } -} diff --git a/src/main/java/com/mmodding/env/json/api/rule/WaterEnvJsonRule.java b/src/main/java/com/mmodding/env/json/api/rule/WaterEnvJsonRule.java deleted file mode 100644 index b2b1c72..0000000 --- a/src/main/java/com/mmodding/env/json/api/rule/WaterEnvJsonRule.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.mmodding.env.json.api.rule; - -public interface WaterEnvJsonRule extends EnvJsonRule { - - Localization localisation(); - - enum Localization { - BELOW, - AT, - ABOVE - } -} diff --git a/src/main/java/com/mmodding/env/json/api/rule/builtin/AnyEnvJsonRule.java b/src/main/java/com/mmodding/env/json/api/rule/builtin/AnyEnvJsonRule.java new file mode 100644 index 0000000..45dd920 --- /dev/null +++ b/src/main/java/com/mmodding/env/json/api/rule/builtin/AnyEnvJsonRule.java @@ -0,0 +1,15 @@ +package com.mmodding.env.json.api.rule.builtin; + +import com.mmodding.env.json.api.rule.EnvJsonRule; +import org.jetbrains.annotations.ApiStatus; + +import java.util.List; + +/** + * Represents a sequence of combined rules with the OR operator. + */ +@ApiStatus.NonExtendable +public interface AnyEnvJsonRule extends EnvJsonRule { + + List rules(); +} diff --git a/src/main/java/com/mmodding/env/json/api/rule/BiomeEnvJsonRule.java b/src/main/java/com/mmodding/env/json/api/rule/builtin/BiomeEnvJsonRule.java similarity index 50% rename from src/main/java/com/mmodding/env/json/api/rule/BiomeEnvJsonRule.java rename to src/main/java/com/mmodding/env/json/api/rule/builtin/BiomeEnvJsonRule.java index 896ce4c..66ff362 100644 --- a/src/main/java/com/mmodding/env/json/api/rule/BiomeEnvJsonRule.java +++ b/src/main/java/com/mmodding/env/json/api/rule/builtin/BiomeEnvJsonRule.java @@ -1,9 +1,15 @@ -package com.mmodding.env.json.api.rule; +package com.mmodding.env.json.api.rule.builtin; +import com.mmodding.env.json.api.rule.EnvJsonRule; import net.minecraft.registry.RegistryKey; import net.minecraft.registry.tag.TagKey; import net.minecraft.world.biome.Biome; +import org.jetbrains.annotations.ApiStatus; +/** + * Represents a biome rule where the object should be. + */ +@ApiStatus.NonExtendable public interface BiomeEnvJsonRule extends EnvJsonRule { RegistryKey biome(); diff --git a/src/main/java/com/mmodding/env/json/api/rule/CoordEnvJsonRule.java b/src/main/java/com/mmodding/env/json/api/rule/builtin/CoordEnvJsonRule.java similarity index 77% rename from src/main/java/com/mmodding/env/json/api/rule/CoordEnvJsonRule.java rename to src/main/java/com/mmodding/env/json/api/rule/builtin/CoordEnvJsonRule.java index a2e216d..6439505 100644 --- a/src/main/java/com/mmodding/env/json/api/rule/CoordEnvJsonRule.java +++ b/src/main/java/com/mmodding/env/json/api/rule/builtin/CoordEnvJsonRule.java @@ -1,9 +1,15 @@ -package com.mmodding.env.json.api.rule; +package com.mmodding.env.json.api.rule.builtin; -import com.mmodding.env.json.impl.rule.CoordEnvJsonRuleImpl; +import com.mmodding.env.json.api.rule.EnvJsonRule; +import com.mmodding.env.json.impl.rule.builtin.CoordEnvJsonRuleImpl; +import org.jetbrains.annotations.ApiStatus; import java.util.function.BiFunction; +/** + * Represents a coordinate comparator rule which is applied to the object. + */ +@ApiStatus.NonExtendable public interface CoordEnvJsonRule extends EnvJsonRule { Coord coord(); diff --git a/src/main/java/com/mmodding/env/json/api/rule/builtin/DimensionEnvJsonRule.java b/src/main/java/com/mmodding/env/json/api/rule/builtin/DimensionEnvJsonRule.java new file mode 100644 index 0000000..09eec68 --- /dev/null +++ b/src/main/java/com/mmodding/env/json/api/rule/builtin/DimensionEnvJsonRule.java @@ -0,0 +1,24 @@ +package com.mmodding.env.json.api.rule.builtin; + +import com.mmodding.env.json.api.rule.EnvJsonRule; +import net.minecraft.registry.RegistryKey; +import net.minecraft.registry.tag.TagKey; +import net.minecraft.world.World; +import org.jetbrains.annotations.ApiStatus; + +/** + * Represents a dimension rule where the object should be. + */ +@ApiStatus.NonExtendable +public interface DimensionEnvJsonRule extends EnvJsonRule { + + /** + * If this method returns null, then {@link DimensionEnvJsonRule#tag()} must return a nonnull value. + */ + RegistryKey dimension(); + + /** + * If this method returns null, then {@link DimensionEnvJsonRule#dimension()} must return a nonnull value. + */ + TagKey tag(); +} diff --git a/src/main/java/com/mmodding/env/json/api/rule/builtin/NotEnvJsonRule.java b/src/main/java/com/mmodding/env/json/api/rule/builtin/NotEnvJsonRule.java new file mode 100644 index 0000000..8671dec --- /dev/null +++ b/src/main/java/com/mmodding/env/json/api/rule/builtin/NotEnvJsonRule.java @@ -0,0 +1,13 @@ +package com.mmodding.env.json.api.rule.builtin; + +import com.mmodding.env.json.api.rule.EnvJsonRule; +import org.jetbrains.annotations.ApiStatus; + +/** + * Reverses the rule. + */ +@ApiStatus.NonExtendable +public interface NotEnvJsonRule extends EnvJsonRule { + + EnvJsonRule rule(); +} diff --git a/src/main/java/com/mmodding/env/json/api/rule/builtin/SequenceEnvJsonRule.java b/src/main/java/com/mmodding/env/json/api/rule/builtin/SequenceEnvJsonRule.java new file mode 100644 index 0000000..f28035e --- /dev/null +++ b/src/main/java/com/mmodding/env/json/api/rule/builtin/SequenceEnvJsonRule.java @@ -0,0 +1,15 @@ +package com.mmodding.env.json.api.rule.builtin; + +import com.mmodding.env.json.api.rule.EnvJsonRule; +import org.jetbrains.annotations.ApiStatus; + +import java.util.List; + +/** + * Represents a sequence of combined rules with the AND operator. + */ +@ApiStatus.NonExtendable +public interface SequenceEnvJsonRule extends EnvJsonRule { + + List rules(); +} diff --git a/src/main/java/com/mmodding/env/json/api/rule/builtin/SkyEnvJsonRule.java b/src/main/java/com/mmodding/env/json/api/rule/builtin/SkyEnvJsonRule.java new file mode 100644 index 0000000..71d9080 --- /dev/null +++ b/src/main/java/com/mmodding/env/json/api/rule/builtin/SkyEnvJsonRule.java @@ -0,0 +1,14 @@ +package com.mmodding.env.json.api.rule.builtin; + +import com.mmodding.env.json.api.RelativeLocation; +import com.mmodding.env.json.api.rule.EnvJsonRule; +import org.jetbrains.annotations.ApiStatus; + +/** + * Represents a rule checking if the object is below or above the sky limit. + */ +@ApiStatus.NonExtendable +public interface SkyEnvJsonRule extends EnvJsonRule { + + RelativeLocation location(); +} diff --git a/src/main/java/com/mmodding/env/json/api/rule/builtin/SubmergedEnvJsonRule.java b/src/main/java/com/mmodding/env/json/api/rule/builtin/SubmergedEnvJsonRule.java new file mode 100644 index 0000000..a33ad4e --- /dev/null +++ b/src/main/java/com/mmodding/env/json/api/rule/builtin/SubmergedEnvJsonRule.java @@ -0,0 +1,13 @@ +package com.mmodding.env.json.api.rule.builtin; + +import com.mmodding.env.json.api.rule.EnvJsonRule; +import org.jetbrains.annotations.ApiStatus; + +/** + * Represents a rule checking if the object is surrounded by water. + */ +@ApiStatus.NonExtendable +public interface SubmergedEnvJsonRule extends EnvJsonRule { + + boolean submerged(); +} diff --git a/src/main/java/com/mmodding/env/json/api/rule/builtin/VoidEnvJsonRule.java b/src/main/java/com/mmodding/env/json/api/rule/builtin/VoidEnvJsonRule.java new file mode 100644 index 0000000..255b50a --- /dev/null +++ b/src/main/java/com/mmodding/env/json/api/rule/builtin/VoidEnvJsonRule.java @@ -0,0 +1,14 @@ +package com.mmodding.env.json.api.rule.builtin; + +import com.mmodding.env.json.api.RelativeLocation; +import com.mmodding.env.json.api.rule.EnvJsonRule; +import org.jetbrains.annotations.ApiStatus; + +/** + * Represents a rule checking if the object is below or above the void limit. + */ +@ApiStatus.NonExtendable +public interface VoidEnvJsonRule extends EnvJsonRule { + + RelativeLocation location(); +} diff --git a/src/main/java/com/mmodding/env/json/api/rule/builtin/WaterEnvJsonRule.java b/src/main/java/com/mmodding/env/json/api/rule/builtin/WaterEnvJsonRule.java new file mode 100644 index 0000000..26078ea --- /dev/null +++ b/src/main/java/com/mmodding/env/json/api/rule/builtin/WaterEnvJsonRule.java @@ -0,0 +1,14 @@ +package com.mmodding.env.json.api.rule.builtin; + +import com.mmodding.env.json.api.RelativeLocation; +import com.mmodding.env.json.api.rule.EnvJsonRule; +import org.jetbrains.annotations.ApiStatus; + +/** + * Represents a rule checking if the object is below or above the water level. + */ +@ApiStatus.NonExtendable +public interface WaterEnvJsonRule extends EnvJsonRule { + + RelativeLocation location(); +} diff --git a/src/main/java/com/mmodding/env/json/api/rule/parsing/EnvJsonRuleParser.java b/src/main/java/com/mmodding/env/json/api/rule/parsing/EnvJsonRuleParser.java new file mode 100644 index 0000000..6bff890 --- /dev/null +++ b/src/main/java/com/mmodding/env/json/api/rule/parsing/EnvJsonRuleParser.java @@ -0,0 +1,10 @@ +package com.mmodding.env.json.api.rule.parsing; + +import com.google.gson.JsonElement; +import com.mmodding.env.json.api.rule.EnvJsonRule; + +@FunctionalInterface +public interface EnvJsonRuleParser { + + R parse(E element, ParserCallback callback); +} diff --git a/src/main/java/com/mmodding/env/json/api/rule/parsing/ParserCallback.java b/src/main/java/com/mmodding/env/json/api/rule/parsing/ParserCallback.java new file mode 100644 index 0000000..fa2712c --- /dev/null +++ b/src/main/java/com/mmodding/env/json/api/rule/parsing/ParserCallback.java @@ -0,0 +1,23 @@ +package com.mmodding.env.json.api.rule.parsing; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; +import com.mmodding.env.json.api.rule.EnvJsonRule; +import com.mojang.datafixers.util.Either; +import net.minecraft.registry.Registry; +import net.minecraft.registry.RegistryKey; +import net.minecraft.registry.tag.TagKey; + +import java.util.List; + +public interface ParserCallback { + + > E parseEnum(Class enumeration, JsonPrimitive primitive); + + Either, TagKey> parseReference(RegistryKey> registry, JsonPrimitive primitive); + + EnvJsonRule parseRule(JsonElement element); + + List parseRules(JsonArray array); +} diff --git a/src/main/java/com/mmodding/env/json/impl/EnvJsonInitializer.java b/src/main/java/com/mmodding/env/json/impl/EnvJsonInitializer.java index 95ff77a..d658bff 100644 --- a/src/main/java/com/mmodding/env/json/impl/EnvJsonInitializer.java +++ b/src/main/java/com/mmodding/env/json/impl/EnvJsonInitializer.java @@ -1,7 +1,9 @@ package com.mmodding.env.json.impl; +import com.mmodding.env.json.impl.rule.EnvJsonRulesImpl; import net.fabricmc.api.ModInitializer; +import net.minecraft.util.Identifier; import org.jetbrains.annotations.ApiStatus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -14,5 +16,14 @@ public class EnvJsonInitializer implements ModInitializer { @Override public void onInitialize() { LOGGER.info("hello-world.env.json"); + EnvJsonRulesImpl.registerBuiltinRules(); + } + + public static String id() { + return "env_json"; + } + + public static Identifier createId(String path) { + return Identifier.of(EnvJsonInitializer.id(), path); } } diff --git a/src/main/java/com/mmodding/env/json/impl/EnvJsonParser.java b/src/main/java/com/mmodding/env/json/impl/EnvJsonParser.java index b452cad..6920fd6 100644 --- a/src/main/java/com/mmodding/env/json/impl/EnvJsonParser.java +++ b/src/main/java/com/mmodding/env/json/impl/EnvJsonParser.java @@ -7,9 +7,9 @@ import com.mmodding.env.json.api.EnvJson; import com.mmodding.env.json.api.EnvJsonMember; import com.mmodding.env.json.api.rule.*; -import com.mmodding.env.json.impl.rule.*; -import net.minecraft.registry.RegistryKey; -import net.minecraft.registry.RegistryKeys; +import com.mmodding.env.json.api.rule.parsing.EnvJsonRuleParser; +import com.mmodding.env.json.impl.rule.EnvJsonRulesImpl; +import com.mmodding.env.json.impl.rule.parsing.ParserCallbackImpl; import net.minecraft.util.Identifier; import net.minecraft.util.JsonHelper; import net.minecraft.util.crash.CrashReport; @@ -20,7 +20,6 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.List; -import java.util.function.Supplier; @ApiStatus.Internal public class EnvJsonParser { @@ -43,80 +42,29 @@ public EnvJsonParser(InputStream stream) { this.content = JsonHelper.deserializeArray(new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))); } - private static SequenceEnvJsonRule parseSequenceRule(JsonArray list) { - return new SequenceEnvJsonRuleImpl(EnvJsonParser.parseRules(list)); + public static EnvJsonRule parseRule(JsonObject json) { + String string = json.get("type").getAsString(); + Identifier identifier = !string.contains(":") ? EnvJsonInitializer.createId(string) : Identifier.tryParse(string); + return EnvJsonParser.parseRuleContent(identifier, json.get("rule")); } - private static AnyEnvJsonRule parseAnyRule(JsonArray list) { - return new AnyEnvJsonRuleImpl(EnvJsonParser.parseRules(list)); - } - - private static NotEnvJsonRule parseNotRule(JsonObject rule) { - return new NotEnvJsonRuleImpl(EnvJsonParser.parseRule(EnvJsonRule.Type.valueOf(rule.get("type").getAsString().toUpperCase()), () -> rule.get("rule"))); - } - - private static DimensionEnvJsonRule parseDimensionRule(String string) { - if (string.startsWith("#")) { - return new DimensionEnvJsonRuleImpl(EnvJsonUtils.tryParse(RegistryKeys.WORLD, string)); + @SuppressWarnings("unchecked") + private static R parseRuleContent(Identifier identifier, JsonElement element) { + if (EnvJsonRulesImpl.REGISTRY.containsKey(identifier)) { + EnvJsonRuleParser parser = (EnvJsonRuleParser) EnvJsonRulesImpl.REGISTRY.get(identifier); + R rule = parser.parse((E) element, new ParserCallbackImpl()); + EnvJsonRulesImpl.LOADED_RULES.add(rule.getClass()); + return rule; } else { - return new DimensionEnvJsonRuleImpl(RegistryKey.of(RegistryKeys.WORLD, Identifier.tryParse(string))); + throw new RuntimeException("Could not find " + identifier + " env.json rule!"); } } - private static BiomeEnvJsonRule parseBiomeRule(String string) { - if (string.startsWith("#")) { - return new BiomeEnvJsonRuleImpl(EnvJsonUtils.tryParse(RegistryKeys.BIOME, string)); - } - else { - return new BiomeEnvJsonRuleImpl(RegistryKey.of(RegistryKeys.BIOME, Identifier.tryParse(string))); - } - } - - private static CoordEnvJsonRule parseCoordRule(CoordEnvJsonRule.Coord coord, JsonObject json) { - CoordEnvJsonRule.Comparator comparator = CoordEnvJsonRule.Comparator.fromString(json.get("comparator").getAsString()); - int value = json.get("value").getAsInt(); - return new CoordEnvJsonRuleImpl(coord, comparator, value); - } - - private static SubmergedEnvJsonRule parseSubmergedRule(boolean bool) { - return new SubmergedEnvJsonRuleImpl(bool); - } - - private static SkyEnvJsonRule parseSkyRule(String string) { - return new SkyEnvJsonRuleImpl(SkyEnvJsonRule.Localization.valueOf(string.toUpperCase())); - } - - private static WaterEnvJsonRule parseWaterRule(String string) { - return new WaterEnvJsonRuleImpl(WaterEnvJsonRule.Localization.valueOf(string.toUpperCase())); - } - - private static VoidEnvJsonRule parseVoidRule(String string) { - return new VoidEnvJsonRuleImpl(VoidEnvJsonRule.Localization.valueOf(string.toUpperCase())); - } - - private static EnvJsonRule parseRule(EnvJsonRule.Type type, Supplier supplier) { - return switch (type) { - case SEQUENCE -> EnvJsonParser.parseSequenceRule(supplier.get().getAsJsonArray()); - case ANY -> EnvJsonParser.parseAnyRule(supplier.get().getAsJsonArray()); - case NOT -> EnvJsonParser.parseNotRule(supplier.get().getAsJsonObject()); - case DIMENSION -> EnvJsonParser.parseDimensionRule(supplier.get().getAsString()); - case BIOME -> EnvJsonParser.parseBiomeRule(supplier.get().getAsString()); - case X_COORD -> EnvJsonParser.parseCoordRule(CoordEnvJsonRule.Coord.X, supplier.get().getAsJsonObject()); - case Y_COORD -> EnvJsonParser.parseCoordRule(CoordEnvJsonRule.Coord.Y, supplier.get().getAsJsonObject()); - case Z_COORD -> EnvJsonParser.parseCoordRule(CoordEnvJsonRule.Coord.Z, supplier.get().getAsJsonObject()); - case SUBMERGED -> EnvJsonParser.parseSubmergedRule(supplier.get().getAsBoolean()); - case SKY -> EnvJsonParser.parseSkyRule(supplier.get().getAsString()); - case WATER -> EnvJsonParser.parseWaterRule(supplier.get().getAsString()); - case VOID -> EnvJsonParser.parseVoidRule(supplier.get().getAsString()); - }; - } - private static List parseRules(JsonArray array) { List rules = new ArrayList<>(); for (JsonElement element : array) { - JsonObject rule = element.getAsJsonObject(); - rules.add(EnvJsonParser.parseRule(EnvJsonRule.Type.valueOf(rule.get("type").getAsString().toUpperCase()), () -> rule.get("rule"))); + rules.add(EnvJsonParser.parseRule(element.getAsJsonObject())); } return rules; } diff --git a/src/main/java/com/mmodding/env/json/impl/rule/AnyEnvJsonRuleImpl.java b/src/main/java/com/mmodding/env/json/impl/rule/AnyEnvJsonRuleImpl.java deleted file mode 100644 index e83b587..0000000 --- a/src/main/java/com/mmodding/env/json/impl/rule/AnyEnvJsonRuleImpl.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.mmodding.env.json.impl.rule; - -import com.mmodding.env.json.api.EnvJsonVisitor; -import com.mmodding.env.json.api.rule.AnyEnvJsonRule; -import com.mmodding.env.json.api.rule.EnvJsonRule; -import org.jetbrains.annotations.ApiStatus; - -import java.util.List; - -@ApiStatus.Internal -public class AnyEnvJsonRuleImpl extends EnvJsonRuleImpl implements AnyEnvJsonRule { - - private final List rules; - - public AnyEnvJsonRuleImpl(List rules) { - super(Type.ANY); - this.rules = rules; - } - - @Override - public List rules() { - return this.rules; - } - - @Override - public boolean apply(EnvJsonVisitor visitor) { - for (EnvJsonRule rule : this.rules) { - if (rule.apply(visitor)) { - return true; - } - } - return false; - } -} diff --git a/src/main/java/com/mmodding/env/json/impl/rule/BiomeEnvJsonRuleImpl.java b/src/main/java/com/mmodding/env/json/impl/rule/BiomeEnvJsonRuleImpl.java deleted file mode 100644 index 2990c06..0000000 --- a/src/main/java/com/mmodding/env/json/impl/rule/BiomeEnvJsonRuleImpl.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.mmodding.env.json.impl.rule; - -import com.mmodding.env.json.api.EnvJsonVisitor; -import com.mmodding.env.json.api.rule.BiomeEnvJsonRule; -import net.minecraft.registry.RegistryKey; -import net.minecraft.registry.tag.TagKey; -import net.minecraft.world.biome.Biome; -import org.jetbrains.annotations.ApiStatus; - -@ApiStatus.Internal -public class BiomeEnvJsonRuleImpl extends EnvJsonRuleImpl implements BiomeEnvJsonRule { - - private final RegistryKey biome; - private final TagKey tag; - - public BiomeEnvJsonRuleImpl(RegistryKey biome) { - super(Type.BIOME); - this.biome = biome; - this.tag = null; - } - - public BiomeEnvJsonRuleImpl(TagKey tag) { - super(Type.BIOME); - this.biome = null; - this.tag = tag; - } - - @Override - public RegistryKey biome() { - return this.biome; - } - - @Override - public TagKey tag() { - return this.tag; - } - - @Override - public boolean apply(EnvJsonVisitor visitor) { - if (this.biome != null) { - return visitor.applyBiomeKey(this.biome); - } - else if (this.tag != null) { - return visitor.applyBiomeTag(this.tag); - } - else { - return false; - } - } -} diff --git a/src/main/java/com/mmodding/env/json/impl/rule/CoordEnvJsonRuleImpl.java b/src/main/java/com/mmodding/env/json/impl/rule/CoordEnvJsonRuleImpl.java deleted file mode 100644 index 64b1d19..0000000 --- a/src/main/java/com/mmodding/env/json/impl/rule/CoordEnvJsonRuleImpl.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.mmodding.env.json.impl.rule; - -import com.mmodding.env.json.api.EnvJsonVisitor; -import com.mmodding.env.json.api.rule.CoordEnvJsonRule; -import org.jetbrains.annotations.ApiStatus; - -import java.util.Arrays; - -@ApiStatus.Internal -public class CoordEnvJsonRuleImpl extends EnvJsonRuleImpl implements CoordEnvJsonRule { - - private final Coord coord; - private final Comparator comparator; - private final int value; - - public CoordEnvJsonRuleImpl(Coord coord, Comparator comparator, int value) { - super( - switch (coord) { - case X -> Type.X_COORD; - case Y -> Type.Y_COORD; - case Z -> Type.Z_COORD; - } - ); - this.coord = coord; - this.comparator = comparator; - this.value = value; - } - - public static Comparator comparatorFromString(String string) { - for (Comparator comparator : Comparator.values()) { - if (Arrays.stream(comparator.representations()).toList().contains(string)) { - return comparator; - } - } - return null; - } - - @Override - public Coord coord() { - return this.coord; - } - - @Override - public Comparator comparator() { - return this.comparator; - } - - @Override - public int value() { - return this.value; - } - - @Override - public boolean apply(EnvJsonVisitor visitor) { - return switch (this.coord) { - case X -> visitor.applyXCoord(i -> this.comparator.operation().apply(i, this.value)); - case Y -> visitor.applyYCoord(i -> this.comparator.operation().apply(i, this.value)); - case Z -> visitor.applyZCoord(i -> this.comparator.operation().apply(i, this.value)); - }; - } -} diff --git a/src/main/java/com/mmodding/env/json/impl/rule/DimensionEnvJsonRuleImpl.java b/src/main/java/com/mmodding/env/json/impl/rule/DimensionEnvJsonRuleImpl.java deleted file mode 100644 index da3312e..0000000 --- a/src/main/java/com/mmodding/env/json/impl/rule/DimensionEnvJsonRuleImpl.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.mmodding.env.json.impl.rule; - -import com.mmodding.env.json.api.EnvJsonVisitor; -import com.mmodding.env.json.api.rule.DimensionEnvJsonRule; -import net.minecraft.registry.RegistryKey; -import net.minecraft.registry.tag.TagKey; -import net.minecraft.world.World; -import org.jetbrains.annotations.ApiStatus; - -@ApiStatus.Internal -public class DimensionEnvJsonRuleImpl extends EnvJsonRuleImpl implements DimensionEnvJsonRule { - - private final RegistryKey dimension; - private final TagKey tag; - - public DimensionEnvJsonRuleImpl(RegistryKey dimension) { - super(Type.DIMENSION); - this.dimension = dimension; - this.tag = null; - } - - public DimensionEnvJsonRuleImpl(TagKey tag) { - super(Type.DIMENSION); - this.dimension = null; - this.tag = tag; - } - - @Override - public RegistryKey dimension() { - return this.dimension; - } - - @Override - public TagKey tag() { - return this.tag; - } - - @Override - public boolean apply(EnvJsonVisitor visitor) { - if (this.dimension != null) { - return visitor.applyDimensionKey(this.dimension); - } - else if (this.tag != null) { - return visitor.applyDimensionTag(this.tag); - } - else { - return false; - } - } -} diff --git a/src/main/java/com/mmodding/env/json/impl/rule/EnvJsonRuleImpl.java b/src/main/java/com/mmodding/env/json/impl/rule/EnvJsonRuleImpl.java index 330b303..818495a 100644 --- a/src/main/java/com/mmodding/env/json/impl/rule/EnvJsonRuleImpl.java +++ b/src/main/java/com/mmodding/env/json/impl/rule/EnvJsonRuleImpl.java @@ -4,21 +4,14 @@ import org.jetbrains.annotations.ApiStatus; @ApiStatus.Internal -public abstract class EnvJsonRuleImpl implements EnvJsonRule { +public final class EnvJsonRuleImpl { - private final Type type; - - public EnvJsonRuleImpl(Type type) { - this.type = type; + public EnvJsonRuleImpl() { + throw new IllegalStateException("EnvJsonRuleImpl only contains static definitions"); } @SuppressWarnings("unchecked") public static T safeCast(EnvJsonRule rule, Class ignored) { return (T) rule; } - - @Override - public Type type() { - return this.type; - } } diff --git a/src/main/java/com/mmodding/env/json/impl/rule/EnvJsonRulesImpl.java b/src/main/java/com/mmodding/env/json/impl/rule/EnvJsonRulesImpl.java new file mode 100644 index 0000000..fe8a6f2 --- /dev/null +++ b/src/main/java/com/mmodding/env/json/impl/rule/EnvJsonRulesImpl.java @@ -0,0 +1,78 @@ +package com.mmodding.env.json.impl.rule; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; +import com.mmodding.env.json.api.RelativeLocation; +import com.mmodding.env.json.api.rule.EnvJsonRule; +import com.mmodding.env.json.api.rule.builtin.CoordEnvJsonRule; +import com.mmodding.env.json.api.rule.parsing.EnvJsonRuleParser; +import com.mmodding.env.json.api.rule.EnvJsonRules; +import com.mmodding.env.json.impl.EnvJsonInitializer; +import com.mmodding.env.json.impl.rule.builtin.*; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; +import net.minecraft.registry.RegistryKeys; +import net.minecraft.util.Identifier; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class EnvJsonRulesImpl { + + public static final Map> REGISTRY = new Object2ObjectOpenHashMap<>(); + + public static final Set> LOADED_RULES = new HashSet<>(); + + public static void registerBuiltinRules() { + EnvJsonRules.register( + EnvJsonInitializer.createId("any"), + (array, callback) -> new AnyEnvJsonRuleImpl(callback.parseRules(array)) + ); + EnvJsonRules.register( + EnvJsonInitializer.createId("biome"), + (primitive, callback) -> new BiomeEnvJsonRuleImpl(callback.parseReference(RegistryKeys.BIOME, primitive)) + ); + EnvJsonRules.register(EnvJsonInitializer.createId("x_coord"), EnvJsonRulesImpl.coordinate(CoordEnvJsonRule.Coord.X)); + EnvJsonRules.register(EnvJsonInitializer.createId("y_coord"), EnvJsonRulesImpl.coordinate(CoordEnvJsonRule.Coord.Y)); + EnvJsonRules.register(EnvJsonInitializer.createId("z_coord"), EnvJsonRulesImpl.coordinate(CoordEnvJsonRule.Coord.Z)); + EnvJsonRules.register( + EnvJsonInitializer.createId("dimension"), + (primitive, callback) -> new DimensionEnvJsonRuleImpl(callback.parseReference(RegistryKeys.WORLD, primitive)) + ); + EnvJsonRules.register( + EnvJsonInitializer.createId("not"), + (object, callback) -> new NotEnvJsonRuleImpl(callback.parseRule(object)) + ); + EnvJsonRules.register( + EnvJsonInitializer.createId("sequence"), + (array, callback) -> new SequenceEnvJsonRuleImpl(callback.parseRules(array)) + ); + EnvJsonRules.register( + EnvJsonInitializer.createId("sky"), + (primitive, callback) -> new SkyEnvJsonRuleImpl(callback.parseEnum(RelativeLocation.class, primitive)) + ); + EnvJsonRules.register( + EnvJsonInitializer.createId("submerged"), + (primitive, callback) -> new SubmergedEnvJsonRuleImpl(primitive.getAsBoolean()) + ); + EnvJsonRules.register( + EnvJsonInitializer.createId("void"), + (primitive, callback) -> new VoidEnvJsonRuleImpl(callback.parseEnum(RelativeLocation.class, primitive)) + ); + EnvJsonRules.register( + EnvJsonInitializer.createId("water"), + (primitive, callback) -> new WaterEnvJsonRuleImpl(callback.parseEnum(RelativeLocation.class, primitive)) + ); + } + + private static EnvJsonRuleParser coordinate(CoordEnvJsonRule.Coord coordinate) { + return (object, callback) -> { + CoordEnvJsonRule.Comparator comparator = CoordEnvJsonRule.Comparator.fromString( + object.getAsJsonPrimitive("comparator").getAsString() + ); + int value = object.get("value").getAsInt(); + return new CoordEnvJsonRuleImpl(coordinate, comparator, value); + }; + } +} diff --git a/src/main/java/com/mmodding/env/json/impl/rule/NotEnvJsonRuleImpl.java b/src/main/java/com/mmodding/env/json/impl/rule/NotEnvJsonRuleImpl.java deleted file mode 100644 index c49da5b..0000000 --- a/src/main/java/com/mmodding/env/json/impl/rule/NotEnvJsonRuleImpl.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.mmodding.env.json.impl.rule; - -import com.mmodding.env.json.api.EnvJsonVisitor; -import com.mmodding.env.json.api.rule.EnvJsonRule; -import com.mmodding.env.json.api.rule.NotEnvJsonRule; -import org.jetbrains.annotations.ApiStatus; - -@ApiStatus.Internal -public class NotEnvJsonRuleImpl extends EnvJsonRuleImpl implements NotEnvJsonRule { - - private final EnvJsonRule rule; - - public NotEnvJsonRuleImpl(EnvJsonRule rule) { - super(Type.NOT); - this.rule = rule; - } - - @Override - public EnvJsonRule rule() { - return this.rule; - } - - @Override - public boolean apply(EnvJsonVisitor visitor) { - return !this.rule.apply(visitor); - } -} diff --git a/src/main/java/com/mmodding/env/json/impl/rule/SequenceEnvJsonRuleImpl.java b/src/main/java/com/mmodding/env/json/impl/rule/SequenceEnvJsonRuleImpl.java deleted file mode 100644 index 210f90c..0000000 --- a/src/main/java/com/mmodding/env/json/impl/rule/SequenceEnvJsonRuleImpl.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.mmodding.env.json.impl.rule; - -import com.mmodding.env.json.api.EnvJsonVisitor; -import com.mmodding.env.json.api.rule.EnvJsonRule; -import com.mmodding.env.json.api.rule.SequenceEnvJsonRule; -import org.jetbrains.annotations.ApiStatus; - -import java.util.List; - -@ApiStatus.Internal -public class SequenceEnvJsonRuleImpl extends EnvJsonRuleImpl implements SequenceEnvJsonRule { - - private final List rules; - - public SequenceEnvJsonRuleImpl(List rules) { - super(Type.SEQUENCE); - this.rules = rules; - } - - @Override - public List rules() { - return this.rules; - } - - @Override - public boolean apply(EnvJsonVisitor visitor) { - for (EnvJsonRule rule : this.rules) { - if (!rule.apply(visitor)) { - return false; - } - } - return true; - } -} diff --git a/src/main/java/com/mmodding/env/json/impl/rule/SkyEnvJsonRuleImpl.java b/src/main/java/com/mmodding/env/json/impl/rule/SkyEnvJsonRuleImpl.java deleted file mode 100644 index d588579..0000000 --- a/src/main/java/com/mmodding/env/json/impl/rule/SkyEnvJsonRuleImpl.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.mmodding.env.json.impl.rule; - -import com.mmodding.env.json.api.EnvJsonVisitor; -import com.mmodding.env.json.api.rule.SkyEnvJsonRule; -import org.jetbrains.annotations.ApiStatus; - -@ApiStatus.Internal -public class SkyEnvJsonRuleImpl extends EnvJsonRuleImpl implements SkyEnvJsonRule { - - private final Localization localization; - - public SkyEnvJsonRuleImpl(Localization localization) { - super(Type.SKY); - this.localization = localization; - } - - @Override - public boolean apply(EnvJsonVisitor visitor) { - return visitor.applySky(this.localization); - } - - @Override - public Localization localisation() { - return this.localization; - } -} diff --git a/src/main/java/com/mmodding/env/json/impl/rule/SubmergedEnvJsonRuleImpl.java b/src/main/java/com/mmodding/env/json/impl/rule/SubmergedEnvJsonRuleImpl.java deleted file mode 100644 index 57980e9..0000000 --- a/src/main/java/com/mmodding/env/json/impl/rule/SubmergedEnvJsonRuleImpl.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.mmodding.env.json.impl.rule; - -import com.mmodding.env.json.api.EnvJsonVisitor; -import com.mmodding.env.json.api.rule.SubmergedEnvJsonRule; - -public class SubmergedEnvJsonRuleImpl extends EnvJsonRuleImpl implements SubmergedEnvJsonRule { - - private final boolean submerged; - - public SubmergedEnvJsonRuleImpl(boolean submerged) { - super(Type.SUBMERGED); - this.submerged = submerged; - } - - @Override - public boolean apply(EnvJsonVisitor visitor) { - return visitor.applySubmerged(this.submerged); - } - - @Override - public boolean submerged() { - return submerged; - } -} diff --git a/src/main/java/com/mmodding/env/json/impl/rule/VoidEnvJsonRuleImpl.java b/src/main/java/com/mmodding/env/json/impl/rule/VoidEnvJsonRuleImpl.java deleted file mode 100644 index e5fa0a2..0000000 --- a/src/main/java/com/mmodding/env/json/impl/rule/VoidEnvJsonRuleImpl.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.mmodding.env.json.impl.rule; - -import com.mmodding.env.json.api.EnvJsonVisitor; -import com.mmodding.env.json.api.rule.VoidEnvJsonRule; -import org.jetbrains.annotations.ApiStatus; - -@ApiStatus.Internal -public class VoidEnvJsonRuleImpl extends EnvJsonRuleImpl implements VoidEnvJsonRule { - - private final Localization localization; - - public VoidEnvJsonRuleImpl(Localization localization) { - super(Type.VOID); - this.localization = localization; - } - - @Override - public boolean apply(EnvJsonVisitor visitor) { - return visitor.applyVoid(this.localization); - } - - @Override - public Localization localisation() { - return this.localization; - } -} diff --git a/src/main/java/com/mmodding/env/json/impl/rule/WaterEnvJsonRuleImpl.java b/src/main/java/com/mmodding/env/json/impl/rule/WaterEnvJsonRuleImpl.java deleted file mode 100644 index 2ec4cec..0000000 --- a/src/main/java/com/mmodding/env/json/impl/rule/WaterEnvJsonRuleImpl.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.mmodding.env.json.impl.rule; - -import com.mmodding.env.json.api.EnvJsonVisitor; -import com.mmodding.env.json.api.rule.WaterEnvJsonRule; -import org.jetbrains.annotations.ApiStatus; - -@ApiStatus.Internal -public class WaterEnvJsonRuleImpl extends EnvJsonRuleImpl implements WaterEnvJsonRule { - - private final Localization localization; - - public WaterEnvJsonRuleImpl(Localization localization) { - super(Type.WATER); - this.localization = localization; - } - - @Override - public boolean apply(EnvJsonVisitor visitor) { - return visitor.applyWater(this.localization); - } - - @Override - public Localization localisation() { - return this.localization; - } -} diff --git a/src/main/java/com/mmodding/env/json/impl/rule/builtin/AnyEnvJsonRuleImpl.java b/src/main/java/com/mmodding/env/json/impl/rule/builtin/AnyEnvJsonRuleImpl.java new file mode 100644 index 0000000..4a3e309 --- /dev/null +++ b/src/main/java/com/mmodding/env/json/impl/rule/builtin/AnyEnvJsonRuleImpl.java @@ -0,0 +1,22 @@ +package com.mmodding.env.json.impl.rule.builtin; + +import com.mmodding.env.json.api.EnvJsonVisitor; +import com.mmodding.env.json.api.rule.builtin.AnyEnvJsonRule; +import com.mmodding.env.json.api.rule.EnvJsonRule; +import org.jetbrains.annotations.ApiStatus; + +import java.util.List; + +@ApiStatus.Internal +public record AnyEnvJsonRuleImpl(List rules) implements AnyEnvJsonRule { + + @Override + public boolean apply(EnvJsonVisitor visitor) { + for (EnvJsonRule rule : this.rules) { + if (rule.apply(visitor)) { + return true; + } + } + return false; + } +} diff --git a/src/main/java/com/mmodding/env/json/impl/rule/builtin/BiomeEnvJsonRuleImpl.java b/src/main/java/com/mmodding/env/json/impl/rule/builtin/BiomeEnvJsonRuleImpl.java new file mode 100644 index 0000000..39c5bce --- /dev/null +++ b/src/main/java/com/mmodding/env/json/impl/rule/builtin/BiomeEnvJsonRuleImpl.java @@ -0,0 +1,34 @@ +package com.mmodding.env.json.impl.rule.builtin; + +import com.mmodding.env.json.api.EnvJsonVisitor; +import com.mmodding.env.json.api.rule.builtin.BiomeEnvJsonRule; +import com.mojang.datafixers.util.Either; +import net.minecraft.registry.RegistryKey; +import net.minecraft.registry.tag.TagKey; +import net.minecraft.world.biome.Biome; +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.Internal +public class BiomeEnvJsonRuleImpl implements BiomeEnvJsonRule { + + private final Either, TagKey> either; + + public BiomeEnvJsonRuleImpl(Either, TagKey> either) { + this.either = either; + } + + @Override + public RegistryKey biome() { + return this.either.left().orElseThrow(); + } + + @Override + public TagKey tag() { + return this.either.right().orElseThrow(); + } + + @Override + public boolean apply(EnvJsonVisitor visitor) { + return this.either.map(visitor::applyBiomeKey, visitor::applyBiomeTag); + } +} diff --git a/src/main/java/com/mmodding/env/json/impl/rule/builtin/CoordEnvJsonRuleImpl.java b/src/main/java/com/mmodding/env/json/impl/rule/builtin/CoordEnvJsonRuleImpl.java new file mode 100644 index 0000000..53af7b8 --- /dev/null +++ b/src/main/java/com/mmodding/env/json/impl/rule/builtin/CoordEnvJsonRuleImpl.java @@ -0,0 +1,29 @@ +package com.mmodding.env.json.impl.rule.builtin; + +import com.mmodding.env.json.api.EnvJsonVisitor; +import com.mmodding.env.json.api.rule.builtin.CoordEnvJsonRule; +import org.jetbrains.annotations.ApiStatus; + +import java.util.Arrays; + +@ApiStatus.Internal +public record CoordEnvJsonRuleImpl(Coord coord, Comparator comparator, int value) implements CoordEnvJsonRule { + + public static Comparator comparatorFromString(String string) { + for (Comparator comparator : Comparator.values()) { + if (Arrays.stream(comparator.representations()).toList().contains(string)) { + return comparator; + } + } + return null; + } + + @Override + public boolean apply(EnvJsonVisitor visitor) { + return switch (this.coord) { + case X -> visitor.applyXCoord(i -> this.comparator.operation().apply(i, this.value)); + case Y -> visitor.applyYCoord(i -> this.comparator.operation().apply(i, this.value)); + case Z -> visitor.applyZCoord(i -> this.comparator.operation().apply(i, this.value)); + }; + } +} diff --git a/src/main/java/com/mmodding/env/json/impl/rule/builtin/DimensionEnvJsonRuleImpl.java b/src/main/java/com/mmodding/env/json/impl/rule/builtin/DimensionEnvJsonRuleImpl.java new file mode 100644 index 0000000..d7e15aa --- /dev/null +++ b/src/main/java/com/mmodding/env/json/impl/rule/builtin/DimensionEnvJsonRuleImpl.java @@ -0,0 +1,34 @@ +package com.mmodding.env.json.impl.rule.builtin; + +import com.mmodding.env.json.api.EnvJsonVisitor; +import com.mmodding.env.json.api.rule.builtin.DimensionEnvJsonRule; +import com.mojang.datafixers.util.Either; +import net.minecraft.registry.RegistryKey; +import net.minecraft.registry.tag.TagKey; +import net.minecraft.world.World; +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.Internal +public class DimensionEnvJsonRuleImpl implements DimensionEnvJsonRule { + + private final Either, TagKey> either; + + public DimensionEnvJsonRuleImpl(Either, TagKey> either) { + this.either = either; + } + + @Override + public RegistryKey dimension() { + return this.either.left().orElseThrow(); + } + + @Override + public TagKey tag() { + return this.either.right().orElseThrow(); + } + + @Override + public boolean apply(EnvJsonVisitor visitor) { + return this.either.map(visitor::applyDimensionKey, visitor::applyDimensionTag); + } +} diff --git a/src/main/java/com/mmodding/env/json/impl/rule/builtin/NotEnvJsonRuleImpl.java b/src/main/java/com/mmodding/env/json/impl/rule/builtin/NotEnvJsonRuleImpl.java new file mode 100644 index 0000000..656952c --- /dev/null +++ b/src/main/java/com/mmodding/env/json/impl/rule/builtin/NotEnvJsonRuleImpl.java @@ -0,0 +1,15 @@ +package com.mmodding.env.json.impl.rule.builtin; + +import com.mmodding.env.json.api.EnvJsonVisitor; +import com.mmodding.env.json.api.rule.EnvJsonRule; +import com.mmodding.env.json.api.rule.builtin.NotEnvJsonRule; +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.Internal +public record NotEnvJsonRuleImpl(EnvJsonRule rule) implements NotEnvJsonRule { + + @Override + public boolean apply(EnvJsonVisitor visitor) { + return !this.rule.apply(visitor); + } +} diff --git a/src/main/java/com/mmodding/env/json/impl/rule/builtin/SequenceEnvJsonRuleImpl.java b/src/main/java/com/mmodding/env/json/impl/rule/builtin/SequenceEnvJsonRuleImpl.java new file mode 100644 index 0000000..10ed413 --- /dev/null +++ b/src/main/java/com/mmodding/env/json/impl/rule/builtin/SequenceEnvJsonRuleImpl.java @@ -0,0 +1,22 @@ +package com.mmodding.env.json.impl.rule.builtin; + +import com.mmodding.env.json.api.EnvJsonVisitor; +import com.mmodding.env.json.api.rule.EnvJsonRule; +import com.mmodding.env.json.api.rule.builtin.SequenceEnvJsonRule; +import org.jetbrains.annotations.ApiStatus; + +import java.util.List; + +@ApiStatus.Internal +public record SequenceEnvJsonRuleImpl(List rules) implements SequenceEnvJsonRule { + + @Override + public boolean apply(EnvJsonVisitor visitor) { + for (EnvJsonRule rule : this.rules) { + if (!rule.apply(visitor)) { + return false; + } + } + return true; + } +} diff --git a/src/main/java/com/mmodding/env/json/impl/rule/builtin/SkyEnvJsonRuleImpl.java b/src/main/java/com/mmodding/env/json/impl/rule/builtin/SkyEnvJsonRuleImpl.java new file mode 100644 index 0000000..23f3bee --- /dev/null +++ b/src/main/java/com/mmodding/env/json/impl/rule/builtin/SkyEnvJsonRuleImpl.java @@ -0,0 +1,15 @@ +package com.mmodding.env.json.impl.rule.builtin; + +import com.mmodding.env.json.api.EnvJsonVisitor; +import com.mmodding.env.json.api.RelativeLocation; +import com.mmodding.env.json.api.rule.builtin.SkyEnvJsonRule; +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.Internal +public record SkyEnvJsonRuleImpl(RelativeLocation location) implements SkyEnvJsonRule { + + @Override + public boolean apply(EnvJsonVisitor visitor) { + return visitor.applySky(this.location); + } +} diff --git a/src/main/java/com/mmodding/env/json/impl/rule/builtin/SubmergedEnvJsonRuleImpl.java b/src/main/java/com/mmodding/env/json/impl/rule/builtin/SubmergedEnvJsonRuleImpl.java new file mode 100644 index 0000000..dccba32 --- /dev/null +++ b/src/main/java/com/mmodding/env/json/impl/rule/builtin/SubmergedEnvJsonRuleImpl.java @@ -0,0 +1,12 @@ +package com.mmodding.env.json.impl.rule.builtin; + +import com.mmodding.env.json.api.EnvJsonVisitor; +import com.mmodding.env.json.api.rule.builtin.SubmergedEnvJsonRule; + +public record SubmergedEnvJsonRuleImpl(boolean submerged) implements SubmergedEnvJsonRule { + + @Override + public boolean apply(EnvJsonVisitor visitor) { + return visitor.applySubmerged(this.submerged); + } +} diff --git a/src/main/java/com/mmodding/env/json/impl/rule/builtin/VoidEnvJsonRuleImpl.java b/src/main/java/com/mmodding/env/json/impl/rule/builtin/VoidEnvJsonRuleImpl.java new file mode 100644 index 0000000..90e649b --- /dev/null +++ b/src/main/java/com/mmodding/env/json/impl/rule/builtin/VoidEnvJsonRuleImpl.java @@ -0,0 +1,15 @@ +package com.mmodding.env.json.impl.rule.builtin; + +import com.mmodding.env.json.api.EnvJsonVisitor; +import com.mmodding.env.json.api.RelativeLocation; +import com.mmodding.env.json.api.rule.builtin.VoidEnvJsonRule; +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.Internal +public record VoidEnvJsonRuleImpl(RelativeLocation location) implements VoidEnvJsonRule { + + @Override + public boolean apply(EnvJsonVisitor visitor) { + return visitor.applyVoid(this.location); + } +} diff --git a/src/main/java/com/mmodding/env/json/impl/rule/builtin/WaterEnvJsonRuleImpl.java b/src/main/java/com/mmodding/env/json/impl/rule/builtin/WaterEnvJsonRuleImpl.java new file mode 100644 index 0000000..ea9e48d --- /dev/null +++ b/src/main/java/com/mmodding/env/json/impl/rule/builtin/WaterEnvJsonRuleImpl.java @@ -0,0 +1,15 @@ +package com.mmodding.env.json.impl.rule.builtin; + +import com.mmodding.env.json.api.EnvJsonVisitor; +import com.mmodding.env.json.api.RelativeLocation; +import com.mmodding.env.json.api.rule.builtin.WaterEnvJsonRule; +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.Internal +public record WaterEnvJsonRuleImpl(RelativeLocation location) implements WaterEnvJsonRule { + + @Override + public boolean apply(EnvJsonVisitor visitor) { + return visitor.applyWater(this.location); + } +} diff --git a/src/main/java/com/mmodding/env/json/impl/rule/parsing/ParserCallbackImpl.java b/src/main/java/com/mmodding/env/json/impl/rule/parsing/ParserCallbackImpl.java new file mode 100644 index 0000000..758b9fd --- /dev/null +++ b/src/main/java/com/mmodding/env/json/impl/rule/parsing/ParserCallbackImpl.java @@ -0,0 +1,47 @@ +package com.mmodding.env.json.impl.rule.parsing; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; +import com.mmodding.env.json.api.rule.EnvJsonRule; +import com.mmodding.env.json.api.rule.parsing.ParserCallback; +import com.mmodding.env.json.impl.EnvJsonParser; +import com.mmodding.env.json.impl.EnvJsonUtils; +import com.mojang.datafixers.util.Either; +import net.minecraft.registry.Registry; +import net.minecraft.registry.RegistryKey; +import net.minecraft.registry.tag.TagKey; +import net.minecraft.util.Identifier; +import org.jetbrains.annotations.ApiStatus; + +import java.util.List; + +@ApiStatus.Internal +public class ParserCallbackImpl implements ParserCallback { + + @Override + public > E parseEnum(Class enumeration, JsonPrimitive primitive) { + return E.valueOf(enumeration, primitive.getAsString().toUpperCase()); + } + + @Override + public Either, TagKey> parseReference(RegistryKey> registry, JsonPrimitive primitive) { + String string = primitive.getAsString(); + if (!string.startsWith("#")) { + return Either.left(RegistryKey.of(registry, Identifier.tryParse(string))); + } + else { + return Either.right(EnvJsonUtils.tryParse(registry, string)); + } + } + + @Override + public EnvJsonRule parseRule(JsonElement element) { + return EnvJsonParser.parseRule(element.getAsJsonObject()); + } + + @Override + public List parseRules(JsonArray array) { + return array.asList().stream().map(this::parseRule).toList(); + } +}