From a50fd46a00b56a15161ae3288cf2ad55d9badc17 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Thu, 17 Feb 2022 15:04:18 +0200 Subject: Added more common condition types --- .../cit/builtin/conditions/BooleanCondition.java | 28 ++++++++++++ .../cit/builtin/conditions/EnumCondition.java | 50 ++++++++++++++++++++++ .../builtin/conditions/IdentifierCondition.java | 29 +++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/BooleanCondition.java create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/EnumCondition.java create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/IdentifierCondition.java (limited to 'src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin') diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/BooleanCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/BooleanCondition.java new file mode 100644 index 0000000..fcf976c --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/BooleanCondition.java @@ -0,0 +1,28 @@ +package shcm.shsupercm.fabric.citresewn.cit.builtin.conditions; + +import shcm.shsupercm.fabric.citresewn.cit.CITCondition; +import shcm.shsupercm.fabric.citresewn.cit.CITContext; +import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; + +public abstract class BooleanCondition extends CITCondition { + protected boolean value; + + protected abstract boolean getValue(CITContext context); + + @Override + public void load(PropertyValue value, PropertyGroup properties) throws CITParsingException { + if (value.value().equalsIgnoreCase("true")) + this.value = true; + else if (value.value().equalsIgnoreCase("false")) + this.value = false; + else + throw new CITParsingException("Not a boolean", properties, value.position()); + } + + @Override + public boolean test(CITContext context) { + return getValue(context) == this.value; + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/EnumCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/EnumCondition.java new file mode 100644 index 0000000..cdfd65d --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/EnumCondition.java @@ -0,0 +1,50 @@ +package shcm.shsupercm.fabric.citresewn.cit.builtin.conditions; + +import shcm.shsupercm.fabric.citresewn.cit.CITCondition; +import shcm.shsupercm.fabric.citresewn.cit.CITContext; +import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; + +import java.util.function.Supplier; + +public abstract class EnumCondition> extends CITCondition { + protected final Supplier values; + protected final boolean ignoreCase; + + protected T value; + + protected abstract T getValue(CITContext context); + + protected EnumCondition(Supplier values, boolean ignoreCase) { + this.values = values; + this.ignoreCase = ignoreCase; + } + + protected EnumCondition(Supplier values) { + this(values, true); + } + + @Override + public void load(PropertyValue value, PropertyGroup properties) throws CITParsingException { + for (T enumConstant : values.get()) + for (String alias : ((Aliased) enumConstant).getAliases()) + if (ignoreCase ? alias.equalsIgnoreCase(value.value()) : alias.equals(value.value())) { + this.value = enumConstant; + return; + } + + throw new CITParsingException("Unrecognized value", properties, value.position()); + } + + @Override + public boolean test(CITContext context) { + return getValue(context) == this.value; + } + + public interface Aliased { + default String[] getAliases() { + return new String[] { ((Enum) this).name() }; + } + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/IdentifierCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/IdentifierCondition.java new file mode 100644 index 0000000..2f45f2f --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/cit/builtin/conditions/IdentifierCondition.java @@ -0,0 +1,29 @@ +package shcm.shsupercm.fabric.citresewn.cit.builtin.conditions; + +import net.minecraft.util.Identifier; +import net.minecraft.util.InvalidIdentifierException; +import shcm.shsupercm.fabric.citresewn.cit.CITCondition; +import shcm.shsupercm.fabric.citresewn.cit.CITContext; +import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; + +public abstract class IdentifierCondition extends CITCondition { + protected Identifier value; + + protected abstract Identifier getValue(CITContext context); + + @Override + public void load(PropertyValue value, PropertyGroup properties) throws CITParsingException { + try { + this.value = new Identifier(value.value()); + } catch (InvalidIdentifierException e) { + throw new CITParsingException(e.getMessage(), properties, value.position()); + } + } + + @Override + public boolean test(CITContext context) { + return this.value.equals(getValue(context)); + } +} -- cgit