diff options
author | SHsuperCM <shsupercm@gmail.com> | 2022-02-12 16:30:28 +0200 |
---|---|---|
committer | SHsuperCM <shsupercm@gmail.com> | 2022-02-12 16:30:28 +0200 |
commit | 4d0abf10e4c8e66f06b61b52aa3dafef0d535e79 (patch) | |
tree | d0ff84eda16b6f8c722e8a0c46a65230d23f4ae0 /src/main/java/shcm/shsupercm | |
parent | 3ca2dde4a8e70bd3f8f11b5d7dbad9ddbc9792af (diff) | |
download | CITResewn-4d0abf10e4c8e66f06b61b52aa3dafef0d535e79.tar.gz CITResewn-4d0abf10e4c8e66f06b61b52aa3dafef0d535e79.tar.bz2 CITResewn-4d0abf10e4c8e66f06b61b52aa3dafef0d535e79.zip |
Implemented weight condition parsing
Diffstat (limited to 'src/main/java/shcm/shsupercm')
5 files changed, 59 insertions, 10 deletions
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITConditionContainer.java b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITConditionContainer.java index d307b1a..6803526 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITConditionContainer.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITConditionContainer.java @@ -4,13 +4,13 @@ import shcm.shsupercm.fabric.citresewn.pack.cit.CITCondition; import java.util.function.Supplier; -public abstract class CITConditionContainer<T extends CITCondition> { +public class CITConditionContainer<T extends CITCondition> { public static final String ENTRYPOINT = "citresewn:condition"; public final Class<T> condition; public final Supplier<T> createCondition; public final String[] aliases; - protected CITConditionContainer(Class<T> condition, Supplier<T> createCondition, String... aliases) { + public CITConditionContainer(Class<T> condition, Supplier<T> createCondition, String... aliases) { this.condition = condition; this.createCondition = createCondition; this.aliases = aliases; diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/builtin/WeightCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/builtin/WeightCondition.java new file mode 100644 index 0000000..9882d7b --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/builtin/WeightCondition.java @@ -0,0 +1,27 @@ +package shcm.shsupercm.fabric.citresewn.builtin; + +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.pack.GlobalProperties; +import shcm.shsupercm.fabric.citresewn.pack.cit.CITCondition; +import shcm.shsupercm.fabric.citresewn.pack.cit.CITContext; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; + +public class WeightCondition extends CITCondition { + @Entrypoint(CITConditionContainer.ENTRYPOINT) + public static final CITConditionContainer<WeightCondition> CONTAINER = new CITConditionContainer<>(WeightCondition.class, WeightCondition::new, "weight"); + + public int weight = 0; + + @Override + public void load(PropertyValue value, PropertyGroup properties, GlobalProperties globalProperties) throws CITParsingException { + this.weight = parseInteger(value, properties); + } + + @Override + public boolean test(CITContext context) { + return true; + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CIT.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CIT.java index 0eac632..b4aa9fb 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CIT.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CIT.java @@ -7,12 +7,14 @@ public class CIT { public final String packName; public final CITType type; public final CITCondition[] conditions; + public final int weight; - public CIT(Identifier propertiesIdentifier, String packName, CITType type, CITCondition[] conditions) { + public CIT(Identifier propertiesIdentifier, String packName, CITType type, CITCondition[] conditions, int weight) { this.propertiesIdentifier = propertiesIdentifier; this.packName = packName; this.type = type; this.conditions = conditions; + this.weight = weight; } public boolean test(CITContext context) { diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITCondition.java index f05d4d1..2de5be1 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITCondition.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITCondition.java @@ -1,13 +1,15 @@ package shcm.shsupercm.fabric.citresewn.pack.cit; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; +import shcm.shsupercm.fabric.citresewn.pack.GlobalProperties; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; import java.util.Collections; import java.util.Set; public abstract class CITCondition { - public abstract void load(PropertyValue value) throws CITParsingException; + public abstract void load(PropertyValue value, PropertyGroup properties, GlobalProperties globalProperties) throws CITParsingException; public Set<Class<? extends CITType>> acceptedTypes() { return null; @@ -22,4 +24,12 @@ public abstract class CITCondition { } public abstract boolean test(CITContext context); + + protected int parseInteger(PropertyValue value, PropertyGroup properties) throws CITParsingException { + try { + return Integer.parseInt(value.value()); + } catch (NumberFormatException e) { + throw new CITParsingException("\"" + value.value() + "\" is not a valid integer", properties, value.position()); + } + } } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITRegistry.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITRegistry.java index 2f34de3..fe0bcc7 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITRegistry.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITRegistry.java @@ -4,6 +4,7 @@ import net.fabricmc.loader.api.FabricLoader; import net.minecraft.util.Identifier; import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; +import shcm.shsupercm.fabric.citresewn.builtin.WeightCondition; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.ex.UnknownCITTypeException; import shcm.shsupercm.fabric.citresewn.pack.GlobalProperties; @@ -51,7 +52,7 @@ public class CITRegistry { continue; for (PropertyValue value : entry.getValue()) - conditions.add(parseCondition(properties, entry.getKey(), value)); + conditions.add(parseCondition(entry.getKey(), value, properties, globalProperties)); } for (CITCondition condition : new ArrayList<>(conditions)) @@ -60,15 +61,24 @@ public class CITRegistry { siblingCondition -> siblingConditionType == siblingCondition.getClass() ? condition.modifySibling(siblingConditionType, siblingCondition) : siblingCondition); - - conditions.removeIf(Objects::isNull); + + WeightCondition weight = new WeightCondition(); + + conditions.removeIf(condition -> { + if (condition instanceof WeightCondition weightCondition) { + weight.weight = weightCondition.weight; + return true; + } + + return condition == null; + }); citType.load(conditions, properties, globalProperties); - return new CIT(properties.identifier, properties.packName, citType, conditions.toArray(new CITCondition[0])); + return new CIT(properties.identifier, properties.packName, citType, conditions.toArray(new CITCondition[0]), weight.weight); } - public static CITCondition parseCondition(PropertyGroup properties, PropertyKey key, PropertyValue value) throws CITParsingException { + public static CITCondition parseCondition(PropertyKey key, PropertyValue value, PropertyGroup properties, GlobalProperties globalProperties) throws CITParsingException { CITConditionContainer<? extends CITCondition> conditionContainer = CONDITIONS.get(key); if (conditionContainer == null) { logWarnLoading("Skipping condition: " + CITParsingException.descriptionOf("Unknown condition type", properties, value.position())); @@ -76,7 +86,7 @@ public class CITRegistry { } CITCondition condition = conditionContainer.createCondition.get(); - condition.load(value); + condition.load(value, properties, globalProperties); return condition; } |