From d51ec9fd6cde3ad393d4d0c91e30cc5d03945eaf Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Sat, 12 Feb 2022 09:21:09 +0200 Subject: More work on type registry/api --- .../shcm/shsupercm/fabric/citresewn/CITResewn.java | 3 +- .../fabric/citresewn/api/CITDisposable.java | 8 ++++ .../fabric/citresewn/api/CITGlobalProperties.java | 10 +++++ .../fabric/citresewn/api/CITTypeContainer.java | 22 ++++++++++ .../shsupercm/fabric/citresewn/api/Disposable.java | 8 ---- .../citresewn/api/GlobalPropertiesHandler.java | 10 ----- .../fabric/citresewn/ex/CITParsingException.java | 13 ++++++ .../citresewn/ex/UnknownCITTypeException.java | 9 ++++ .../fabric/citresewn/pack/GlobalProperties.java | 4 +- .../fabric/citresewn/pack/PackParser.java | 2 +- .../fabric/citresewn/pack/cit/ActiveCITs.java | 6 +-- .../shsupercm/fabric/citresewn/pack/cit/CIT.java | 16 ++++++-- .../fabric/citresewn/pack/cit/CITRegistry.java | 48 ++++++++++++++++++++++ .../citresewn/pack/format/PropertyGroup.java | 7 ++++ 14 files changed, 137 insertions(+), 29 deletions(-) create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/api/CITDisposable.java create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/api/CITGlobalProperties.java create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/api/CITTypeContainer.java delete mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/api/Disposable.java delete mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/api/GlobalPropertiesHandler.java create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/ex/CITParsingException.java create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/ex/UnknownCITTypeException.java create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITRegistry.java (limited to 'src/main') diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewn.java b/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewn.java index 807aa97..ce908ed 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewn.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewn.java @@ -5,6 +5,7 @@ import net.fabricmc.api.ClientModInitializer; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig; +import shcm.shsupercm.fabric.citresewn.pack.cit.CITRegistry; public class CITResewn implements ClientModInitializer { public static final Logger LOG = LogManager.getLogger("CITResewn"); @@ -13,7 +14,7 @@ public class CITResewn implements ClientModInitializer { @Override public void onInitializeClient() { - info("init"); + CITRegistry.registerAll(); } public static void info(String message) { diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITDisposable.java b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITDisposable.java new file mode 100644 index 0000000..240f197 --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITDisposable.java @@ -0,0 +1,8 @@ +package shcm.shsupercm.fabric.citresewn.api; + +@FunctionalInterface +public interface CITDisposable { + String ENTRYPOINT = "citresewn:dispose"; + + void dispose(); +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITGlobalProperties.java b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITGlobalProperties.java new file mode 100644 index 0000000..ada5147 --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITGlobalProperties.java @@ -0,0 +1,10 @@ +package shcm.shsupercm.fabric.citresewn.api; + +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; + +@FunctionalInterface +public interface CITGlobalProperties { + String ENTRYPOINT = "citresewn:global_property"; + + void globalProperty(String key, PropertyValue value) throws Exception; +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITTypeContainer.java b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITTypeContainer.java new file mode 100644 index 0000000..d691ab2 --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITTypeContainer.java @@ -0,0 +1,22 @@ +package shcm.shsupercm.fabric.citresewn.api; + +import shcm.shsupercm.fabric.citresewn.pack.cit.CIT; +import shcm.shsupercm.fabric.citresewn.pack.cit.CITType; + +import java.util.Collection; +import java.util.function.Supplier; + +public abstract class CITTypeContainer implements CITDisposable { + public static final String ENTRYPOINT = "citresewn:type"; + public final Class type; + public final Supplier createType; + public final String id; + + public CITTypeContainer(Class type, Supplier createType, String id) { + this.type = type; + this.createType = createType; + this.id = id; + } + + public abstract void load(Collection parsedCITs); +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/api/Disposable.java b/src/main/java/shcm/shsupercm/fabric/citresewn/api/Disposable.java deleted file mode 100644 index 85b8849..0000000 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/api/Disposable.java +++ /dev/null @@ -1,8 +0,0 @@ -package shcm.shsupercm.fabric.citresewn.api; - -@FunctionalInterface -public interface Disposable { - String ENTRYPOINT = "citresewn:dispose"; - - void dispose(); -} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/api/GlobalPropertiesHandler.java b/src/main/java/shcm/shsupercm/fabric/citresewn/api/GlobalPropertiesHandler.java deleted file mode 100644 index ac8228a..0000000 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/api/GlobalPropertiesHandler.java +++ /dev/null @@ -1,10 +0,0 @@ -package shcm.shsupercm.fabric.citresewn.api; - -import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; - -@FunctionalInterface -public interface GlobalPropertiesHandler { - String ENTRYPOINT = "citresewn:global_property"; - - void globalProperty(String key, PropertyValue value) throws Exception; -} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/ex/CITParsingException.java b/src/main/java/shcm/shsupercm/fabric/citresewn/ex/CITParsingException.java new file mode 100644 index 0000000..964b58d --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/ex/CITParsingException.java @@ -0,0 +1,13 @@ +package shcm.shsupercm.fabric.citresewn.ex; + +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; + +public class CITParsingException extends Exception { + public CITParsingException(String message, PropertyGroup propertyGroup, int position) { + super("Errored while parsing CIT: " + descriptionOf(message, propertyGroup, position)); + } + + public static String descriptionOf(String message, PropertyGroup propertyGroup, int position) { + return message + " at " + position + " in " + propertyGroup.identifier.toString() + " from " + propertyGroup.packName; + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/ex/UnknownCITTypeException.java b/src/main/java/shcm/shsupercm/fabric/citresewn/ex/UnknownCITTypeException.java new file mode 100644 index 0000000..eacbc69 --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/ex/UnknownCITTypeException.java @@ -0,0 +1,9 @@ +package shcm.shsupercm.fabric.citresewn.ex; + +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; + +public class UnknownCITTypeException extends CITParsingException { + public UnknownCITTypeException(PropertyGroup propertyGroup, int position) { + super("Unknown type", propertyGroup, position); + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/GlobalProperties.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/GlobalProperties.java index d4f89f0..c9e2cf8 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/GlobalProperties.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/GlobalProperties.java @@ -8,7 +8,7 @@ import shcm.shsupercm.fabric.citresewn.CITResewn; 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 shcm.shsupercm.fabric.citresewn.api.GlobalPropertiesHandler; +import shcm.shsupercm.fabric.citresewn.api.CITGlobalProperties; import java.io.IOException; import java.io.InputStream; @@ -37,7 +37,7 @@ public class GlobalProperties extends PropertyGroup { } public void callHandlers() { - for (EntrypointContainer container : FabricLoader.getInstance().getEntrypointContainers("citresewn:cit_global_properties", GlobalPropertiesHandler.class)) { + for (EntrypointContainer container : FabricLoader.getInstance().getEntrypointContainers("citresewn:cit_global_properties", CITGlobalProperties.class)) { String containerNamespace = container.getProvider().getMetadata().getId(); if (containerNamespace.equals("citresewn-defaults")) containerNamespace = "citresewn"; diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java index 463d34a..4945120 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java @@ -41,7 +41,7 @@ public class PackParser { globalProperties.load(pack.getName(), identifier, pack.open(ResourceType.CLIENT_RESOURCES, identifier)); } catch (ResourceNotFoundException ignored) { } catch (IOException e) { - CITResewn.logErrorLoading("Errored while loading global properties: " + identifier + " in " + pack.getName()); + CITResewn.logErrorLoading("Errored while loading global properties: " + identifier + " from " + pack.getName()); e.printStackTrace(); } } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/ActiveCITs.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/ActiveCITs.java index f8df1ee..62f011e 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/ActiveCITs.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/ActiveCITs.java @@ -3,11 +3,11 @@ package shcm.shsupercm.fabric.citresewn.pack.cit; import net.fabricmc.loader.api.FabricLoader; import net.minecraft.resource.ResourceManager; import net.minecraft.util.profiler.Profiler; -import shcm.shsupercm.fabric.citresewn.api.Disposable; +import shcm.shsupercm.fabric.citresewn.api.CITDisposable; import shcm.shsupercm.fabric.citresewn.pack.GlobalProperties; import shcm.shsupercm.fabric.citresewn.pack.PackParser; -public class ActiveCITs implements Disposable { private ActiveCITs() {} +public class ActiveCITs implements CITDisposable { private ActiveCITs() {} private static ActiveCITs active = null; public static ActiveCITs getActive() { @@ -39,7 +39,7 @@ public class ActiveCITs implements Disposable { private ActiveCITs() {} @Override public void dispose() { - for (Disposable disposable : FabricLoader.getInstance().getEntrypoints(Disposable.ENTRYPOINT, Disposable.class)) + for (CITDisposable disposable : FabricLoader.getInstance().getEntrypoints(CITDisposable.ENTRYPOINT, CITDisposable.class)) disposable.dispose(); 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 ee293ce..0eac632 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 @@ -3,10 +3,10 @@ package shcm.shsupercm.fabric.citresewn.pack.cit; import net.minecraft.util.Identifier; public class CIT { - private final Identifier propertiesIdentifier; - private final String packName; - private final CITType type; - private final CITCondition[] conditions; + public final Identifier propertiesIdentifier; + public final String packName; + public final CITType type; + public final CITCondition[] conditions; public CIT(Identifier propertiesIdentifier, String packName, CITType type, CITCondition[] conditions) { this.propertiesIdentifier = propertiesIdentifier; @@ -14,4 +14,12 @@ public class CIT { this.type = type; this.conditions = conditions; } + + public boolean test(CITContext context) { + for (CITCondition condition : conditions) + if (!condition.test(context)) + return false; + + return true; + } } 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 new file mode 100644 index 0000000..9dd423b --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITRegistry.java @@ -0,0 +1,48 @@ +package shcm.shsupercm.fabric.citresewn.pack.cit; + +import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.util.Identifier; +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.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; + +import java.util.HashMap; +import java.util.Map; + +import static shcm.shsupercm.fabric.citresewn.CITResewn.info; + +public class CITRegistry { + private static final Map> TYPES = new HashMap<>(); + + public static void registerAll() { + info("Registering CIT Types"); + for (var entrypointContainer : FabricLoader.getInstance().getEntrypointContainers(CITTypeContainer.ENTRYPOINT, CITTypeContainer.class)) { + String namespace = entrypointContainer.getProvider().getMetadata().getId(); + if (namespace.equals("citresewn-defaults")) + namespace = "citresewn"; + + TYPES.put(new Identifier(namespace, entrypointContainer.getEntrypoint().id), (CITTypeContainer) entrypointContainer.getEntrypoint()); + } + } + + public static CITTypeContainer parseType(PropertyGroup properties) throws CITParsingException { + Identifier type = new Identifier("citresewn", "item"); + + PropertyValue propertiesType = properties.getLast("citresewn", "type"); + if (propertiesType != null) { + String value = propertiesType.value(); + if (!value.contains(":")) + value = "citresewn:" + value; + type = new Identifier(value); + } + + CITTypeContainer typeContainer = TYPES.get(type); + if (typeContainer == null) + // assert (propertiesType != null) because the default citresewn:item should always be registered + throw new UnknownCITTypeException(properties, propertiesType == null ? -1 : propertiesType.position()); + + return typeContainer; + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyGroup.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyGroup.java index 3864569..6955ac1 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyGroup.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyGroup.java @@ -40,6 +40,13 @@ public abstract class PropertyGroup { return values; } + public PropertyValue getLast(String namespace, String... pathAliases) { + PropertyValue value = null; + for (Iterator iterator = get(namespace, pathAliases).iterator(); iterator.hasNext(); value = iterator.next()); + + return value; + } + public static PropertyGroup tryParseGroup(String packName, Identifier identifier, InputStream is) throws IOException { PropertyGroup group = null; if (identifier.getPath().endsWith(PropertiesGroupAdapter.EXTENSION)) -- cgit