diff options
Diffstat (limited to 'src/main')
4 files changed, 83 insertions, 9 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 new file mode 100644 index 0000000..d307b1a --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITConditionContainer.java @@ -0,0 +1,18 @@ +package shcm.shsupercm.fabric.citresewn.api; + +import shcm.shsupercm.fabric.citresewn.pack.cit.CITCondition; + +import java.util.function.Supplier; + +public abstract 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) { + this.condition = condition; + this.createCondition = createCondition; + this.aliases = aliases; + } +} 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 e117133..f05d4d1 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,12 +1,13 @@ package shcm.shsupercm.fabric.citresewn.pack.cit; +import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; import java.util.Collections; import java.util.Set; public abstract class CITCondition { - public abstract void load(String keyMetadata, PropertyValue value) throws Exception; + public abstract void load(PropertyValue value) throws CITParsingException; public Set<Class<? extends CITType>> acceptedTypes() { return null; @@ -16,7 +17,7 @@ public abstract class CITCondition { return Collections.emptySet(); } - public <T extends CITCondition> T modifySibling(T sibling) { + public <T extends CITCondition> T modifySibling(Class<? extends CITCondition> siblingType, T sibling) { return sibling; } 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 9dd423b..2f34de3 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 @@ -2,21 +2,35 @@ package shcm.shsupercm.fabric.citresewn.pack.cit; 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.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.ex.UnknownCITTypeException; +import shcm.shsupercm.fabric.citresewn.pack.GlobalProperties; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyKey; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; -import java.util.HashMap; -import java.util.Map; +import java.util.*; import static shcm.shsupercm.fabric.citresewn.CITResewn.info; +import static shcm.shsupercm.fabric.citresewn.CITResewn.logWarnLoading; public class CITRegistry { private static final Map<Identifier, CITTypeContainer<? extends CITType>> TYPES = new HashMap<>(); + private static final Map<PropertyKey, CITConditionContainer<? extends CITCondition>> CONDITIONS = new HashMap<>(); public static void registerAll() { + info("Registering CIT Conditions"); + for (var entrypointContainer : FabricLoader.getInstance().getEntrypointContainers(CITConditionContainer.ENTRYPOINT, CITConditionContainer.class)) { + String namespace = entrypointContainer.getProvider().getMetadata().getId(); + if (namespace.equals("citresewn-defaults")) + namespace = "citresewn"; + + for (String alias : entrypointContainer.getEntrypoint().aliases) + CONDITIONS.put(new PropertyKey(namespace, alias), (CITConditionContainer<? extends CITCondition>) entrypointContainer.getEntrypoint()); + } + info("Registering CIT Types"); for (var entrypointContainer : FabricLoader.getInstance().getEntrypointContainers(CITTypeContainer.ENTRYPOINT, CITTypeContainer.class)) { String namespace = entrypointContainer.getProvider().getMetadata().getId(); @@ -27,7 +41,46 @@ public class CITRegistry { } } - public static CITTypeContainer<? extends CITType> parseType(PropertyGroup properties) throws CITParsingException { + public static CIT load(PropertyGroup properties, GlobalProperties globalProperties) throws CITParsingException { + CITType citType = parseType(properties); + + ArrayList<CITCondition> conditions = new ArrayList<>(); + + for (Map.Entry<PropertyKey, Set<PropertyValue>> entry : properties.properties.entrySet()) { + if (entry.getKey().path().equals("type") && entry.getKey().namespace().equals("citresewn")) + continue; + + for (PropertyValue value : entry.getValue()) + conditions.add(parseCondition(properties, entry.getKey(), value)); + } + + for (CITCondition condition : new ArrayList<>(conditions)) + for (Class<? extends CITCondition> siblingConditionType : condition.siblingConditions()) + conditions.replaceAll( + siblingCondition -> siblingConditionType == siblingCondition.getClass() ? + condition.modifySibling(siblingConditionType, siblingCondition) : + siblingCondition); + + conditions.removeIf(Objects::isNull); + + citType.load(conditions, properties, globalProperties); + + return new CIT(properties.identifier, properties.packName, citType, conditions.toArray(new CITCondition[0])); + } + + public static CITCondition parseCondition(PropertyGroup properties, PropertyKey key, PropertyValue value) throws CITParsingException { + CITConditionContainer<? extends CITCondition> conditionContainer = CONDITIONS.get(key); + if (conditionContainer == null) { + logWarnLoading("Skipping condition: " + CITParsingException.descriptionOf("Unknown condition type", properties, value.position())); + return null; + } + + CITCondition condition = conditionContainer.createCondition.get(); + condition.load(value); + return condition; + } + + public static CITType parseType(PropertyGroup properties) throws CITParsingException { Identifier type = new Identifier("citresewn", "item"); PropertyValue propertiesType = properties.getLast("citresewn", "type"); @@ -43,6 +96,6 @@ public class CITRegistry { // assert (propertiesType != null) because the default citresewn:item should always be registered throw new UnknownCITTypeException(properties, propertiesType == null ? -1 : propertiesType.position()); - return typeContainer; + return typeContainer.createType.get(); } } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITType.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITType.java index 557a34c..bbfa0e9 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITType.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITType.java @@ -1,9 +1,11 @@ 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 java.util.List; public abstract class CITType { - public List<? extends CITCondition> modifyConditions(List<? extends CITCondition> conditions) { - return conditions; - } + public abstract void load(List<? extends CITCondition> conditions, PropertyGroup properties, GlobalProperties globalProperties) throws CITParsingException; } |