From 4d0abf10e4c8e66f06b61b52aa3dafef0d535e79 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Sat, 12 Feb 2022 16:30:28 +0200 Subject: Implemented weight condition parsing --- .../citresewn/api/CITConditionContainer.java | 4 ++-- .../fabric/citresewn/builtin/WeightCondition.java | 27 ++++++++++++++++++++++ .../shsupercm/fabric/citresewn/pack/cit/CIT.java | 4 +++- .../fabric/citresewn/pack/cit/CITCondition.java | 12 +++++++++- .../fabric/citresewn/pack/cit/CITRegistry.java | 22 +++++++++++++----- 5 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/builtin/WeightCondition.java (limited to 'src/main/java/shcm/shsupercm') 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 { +public class CITConditionContainer { public static final String ENTRYPOINT = "citresewn:condition"; public final Class condition; public final Supplier createCondition; public final String[] aliases; - protected CITConditionContainer(Class condition, Supplier createCondition, String... aliases) { + public CITConditionContainer(Class condition, Supplier 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 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> 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 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; } -- cgit