diff options
Diffstat (limited to 'src/main/java/shcm/shsupercm/fabric/citresewn')
3 files changed, 107 insertions, 0 deletions
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<T extends Enum<? extends EnumCondition.Aliased>> extends CITCondition { + protected final Supplier<T[]> values; + protected final boolean ignoreCase; + + protected T value; + + protected abstract T getValue(CITContext context); + + protected EnumCondition(Supplier<T[]> values, boolean ignoreCase) { + this.values = values; + this.ignoreCase = ignoreCase; + } + + protected EnumCondition(Supplier<T[]> 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)); + } +} |