diff options
author | SHsuperCM <shsupercm@gmail.com> | 2022-02-12 09:21:09 +0200 |
---|---|---|
committer | SHsuperCM <shsupercm@gmail.com> | 2022-02-12 09:21:09 +0200 |
commit | d51ec9fd6cde3ad393d4d0c91e30cc5d03945eaf (patch) | |
tree | 0f31635f43220e6a3ffcc9bc9390de7016242959 /src/main/java/shcm/shsupercm/fabric/citresewn | |
parent | ba570a6b2e37f6f45b149515cd41d10037cbda7f (diff) | |
download | CITResewn-d51ec9fd6cde3ad393d4d0c91e30cc5d03945eaf.tar.gz CITResewn-d51ec9fd6cde3ad393d4d0c91e30cc5d03945eaf.tar.bz2 CITResewn-d51ec9fd6cde3ad393d4d0c91e30cc5d03945eaf.zip |
More work on type registry/api
Diffstat (limited to 'src/main/java/shcm/shsupercm/fabric/citresewn')
12 files changed, 121 insertions, 13 deletions
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/Disposable.java b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITDisposable.java index 85b8849..240f197 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/api/Disposable.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITDisposable.java @@ -1,7 +1,7 @@ package shcm.shsupercm.fabric.citresewn.api; @FunctionalInterface -public interface Disposable { +public interface CITDisposable { 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/CITGlobalProperties.java index ac8228a..ada5147 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/api/GlobalPropertiesHandler.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITGlobalProperties.java @@ -3,7 +3,7 @@ package shcm.shsupercm.fabric.citresewn.api; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; @FunctionalInterface -public interface GlobalPropertiesHandler { +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<T extends CITType> implements CITDisposable { + public static final String ENTRYPOINT = "citresewn:type"; + public final Class<T> type; + public final Supplier<T> createType; + public final String id; + + public CITTypeContainer(Class<T> type, Supplier<T> createType, String id) { + this.type = type; + this.createType = createType; + this.id = id; + } + + public abstract void load(Collection<CIT> parsedCITs); +} 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<GlobalPropertiesHandler> container : FabricLoader.getInstance().getEntrypointContainers("citresewn:cit_global_properties", GlobalPropertiesHandler.class)) { + for (EntrypointContainer<CITGlobalProperties> 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<Identifier, CITTypeContainer<? extends CITType>> 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<? extends CITType>) entrypointContainer.getEntrypoint()); + } + } + + public static CITTypeContainer<? extends CITType> 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<? extends CITType> 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<PropertyValue> 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)) |