aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/shcm/shsupercm/fabric/citresewn/pack
diff options
context:
space:
mode:
authorSHsuperCM <shsupercm@gmail.com>2022-02-12 16:30:28 +0200
committerSHsuperCM <shsupercm@gmail.com>2022-02-12 16:30:28 +0200
commit4d0abf10e4c8e66f06b61b52aa3dafef0d535e79 (patch)
treed0ff84eda16b6f8c722e8a0c46a65230d23f4ae0 /src/main/java/shcm/shsupercm/fabric/citresewn/pack
parent3ca2dde4a8e70bd3f8f11b5d7dbad9ddbc9792af (diff)
downloadCITResewn-4d0abf10e4c8e66f06b61b52aa3dafef0d535e79.tar.gz
CITResewn-4d0abf10e4c8e66f06b61b52aa3dafef0d535e79.tar.bz2
CITResewn-4d0abf10e4c8e66f06b61b52aa3dafef0d535e79.zip
Implemented weight condition parsing
Diffstat (limited to 'src/main/java/shcm/shsupercm/fabric/citresewn/pack')
-rw-r--r--src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CIT.java4
-rw-r--r--src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITCondition.java12
-rw-r--r--src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITRegistry.java22
3 files changed, 30 insertions, 8 deletions
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;
}