From 0484498863626dc57d55d381b7b394b5cc9234c2 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Fri, 25 Mar 2022 11:51:59 +0300 Subject: Cleanup with built in conditions --- .../fabric/citresewn/cit/CITRegistry.java | 2 +- .../cit/builtin/conditions/ConstantCondition.java | 40 ++++++++++++++++------ .../cit/builtin/conditions/WeightCondition.java | 32 ----------------- .../builtin/conditions/core/WeightCondition.java | 28 +++++++++++++++ .../fabric/citresewn/pack/PackParser.java | 2 +- 5 files changed, 60 insertions(+), 44 deletions(-) delete mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/WeightCondition.java create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/core/WeightCondition.java (limited to 'src/main/java/shcm/shsupercm/fabric/citresewn') diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java index bf7c2ca..a33f331 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/CITRegistry.java @@ -92,7 +92,7 @@ public final class CITRegistry { private CITRegistry(){} CITConditionContainer conditionContainer = CONDITIONS.get(key); if (conditionContainer == null) { logWarnLoading(properties.messageWithDescriptorOf("Unknown condition type \"" + key.toString() + "\"", value.position())); - return new ConstantCondition(false); + return ConstantCondition.FALSE; } CITCondition condition = conditionContainer.createCondition.get(); diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/ConstantCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/ConstantCondition.java index 5bc9354..ed967c1 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/ConstantCondition.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/ConstantCondition.java @@ -9,23 +9,43 @@ import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; /** * Common condition type with no parsing for constant true/false testing output. */ -public class ConstantCondition extends CITCondition { +public abstract class ConstantCondition extends CITCondition { /** - * What testing contexts will always result in. + * Constant condition that always tests positive. */ - public final boolean value; + public static final ConstantCondition TRUE = new ConstantCondition(true) { + @Override + public boolean test(CITContext context) { + return true; + } + }; - public ConstantCondition(boolean value) { - this.value = value; + /** + * Constant condition that always tests negative. + */ + public static final ConstantCondition FALSE = new ConstantCondition(false) { + @Override + public boolean test(CITContext context) { + return false; + } + }; + + /** + * @return constant condition for the given boolean value + */ + public static ConstantCondition of(boolean value) { + return value ? TRUE : FALSE; } - @Override - public void load(PropertyValue value, PropertyGroup properties) throws CITParsingException { + /** + * What testing contexts will always result in. + */ + public final boolean value; + private ConstantCondition(boolean value) { + this.value = value; } @Override - public boolean test(CITContext context) { - return value; - } + public void load(PropertyValue value, PropertyGroup properties) throws CITParsingException { } } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/WeightCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/WeightCondition.java deleted file mode 100644 index a53e019..0000000 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/WeightCondition.java +++ /dev/null @@ -1,32 +0,0 @@ -package shcm.shsupercm.fabric.citresewn.cit.builtin.conditions; - -import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; -import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; -import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; -import shcm.shsupercm.fabric.citresewn.cit.CITCondition; -import shcm.shsupercm.fabric.citresewn.cit.CITContext; -import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; -import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; - -/** - * Internal condition used to determine the priority CITs get tested in.
- * Weights default to 0 and higher weights get chosen over lower weights.
- * When two conflicting CITs have the same weight, their path in the resourcepack is used as a tie breaker. - */ -public class WeightCondition extends IntegerCondition { - @Entrypoint(CITConditionContainer.ENTRYPOINT) - public static final CITConditionContainer CONTAINER = new CITConditionContainer<>(WeightCondition.class, WeightCondition::new, - "weight"); - - public WeightCondition() { - super(false, true, false); - } - - public int getWeight() { - return this.min; - } - - public void setWeight(int weight) { - this.min = weight; - } -} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/core/WeightCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/core/WeightCondition.java new file mode 100644 index 0000000..cb728c9 --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/core/WeightCondition.java @@ -0,0 +1,28 @@ +package shcm.shsupercm.fabric.citresewn.cit.builtin.conditions.core; + +import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; +import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; +import shcm.shsupercm.fabric.citresewn.cit.builtin.conditions.IntegerCondition; + +/** + * Internal condition used to determine the priority CITs get tested in.
+ * Weights default to 0 and higher weights get chosen over lower weights.
+ * When two conflicting CITs have the same weight, their path in the resourcepack is used as a tie breaker. + */ +public class WeightCondition extends IntegerCondition { + @Entrypoint(CITConditionContainer.ENTRYPOINT) + public static final CITConditionContainer CONTAINER = new CITConditionContainer<>(WeightCondition.class, WeightCondition::new, + "weight"); + + public WeightCondition() { + super(false, true, false); + } + + public int getWeight() { + return this.min; + } + + public void setWeight(int weight) { + this.min = weight; + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java index cd85af1..ad1b9ba 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java @@ -6,7 +6,7 @@ import net.minecraft.resource.ResourcePack; import net.minecraft.resource.ResourceType; import net.minecraft.util.Identifier; import shcm.shsupercm.fabric.citresewn.CITResewn; -import shcm.shsupercm.fabric.citresewn.cit.builtin.conditions.WeightCondition; +import shcm.shsupercm.fabric.citresewn.cit.builtin.conditions.core.WeightCondition; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.cit.CIT; import shcm.shsupercm.fabric.citresewn.cit.CITCondition; -- cgit