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 --- .../shsupercm/fabric/citresewn/pack/cit/CIT.java | 4 +++- .../fabric/citresewn/pack/cit/CITCondition.java | 12 +++++++++++- .../fabric/citresewn/pack/cit/CITRegistry.java | 22 ++++++++++++++++------ 3 files changed, 30 insertions(+), 8 deletions(-) (limited to 'src/main/java/shcm/shsupercm/fabric/citresewn/pack') 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