mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-08 00:36:15 +00:00
make Registry immutable, remove other registry implementations
This commit is contained in:
@@ -17,7 +17,6 @@ import com.dfsek.terra.addons.manifest.api.monad.Init;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPostLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
import com.dfsek.terra.api.util.function.monad.Monad;
|
||||
import com.dfsek.terra.api.util.generic.Construct;
|
||||
@@ -49,14 +48,14 @@ public class BiomeExtrusionAddon implements MonadAddonInitializer {
|
||||
});
|
||||
return functionalEventHandler.register(base, ConfigPackPreLoadEvent.class)
|
||||
.then(event -> {
|
||||
CheckedRegistry<Supplier<ObjectTemplate<BiomeProvider>>> providerRegistry =
|
||||
Registry<Supplier<ObjectTemplate<BiomeProvider>>> providerRegistry =
|
||||
event.getPack()
|
||||
.getOrCreateRegistry(PROVIDER_REGISTRY_KEY);
|
||||
.createRegistry(PROVIDER_REGISTRY_KEY);
|
||||
providerRegistry.register(base.key("EXTRUSION"), BiomeExtrusionTemplate::new);
|
||||
})
|
||||
.then(event -> {
|
||||
CheckedRegistry<Supplier<ObjectTemplate<Extrusion>>> extrusionRegistry =
|
||||
event.getPack().getOrCreateRegistry(
|
||||
Registry<Supplier<ObjectTemplate<Extrusion>>> extrusionRegistry =
|
||||
event.getPack().createRegistry(
|
||||
EXTRUSION_REGISTRY_KEY);
|
||||
extrusionRegistry.register(base.key("SET"), SetExtrusionTemplate::new);
|
||||
extrusionRegistry.register(base.key("REPLACE"), ReplaceExtrusionTemplate::new);
|
||||
|
||||
@@ -15,12 +15,9 @@ import com.dfsek.terra.addons.manifest.api.MonadAddonInitializer;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Do;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Get;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Init;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.addon.BaseAddon;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
|
||||
import com.dfsek.terra.api.inject.annotations.Inject;
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
import com.dfsek.terra.api.util.function.monad.Monad;
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
import com.dfsek.terra.api.world.biome.Biome;
|
||||
@@ -40,7 +37,7 @@ public class ImageBiomeProviderAddon implements MonadAddonInitializer {
|
||||
((functionalEventHandler, base, platform) -> Init.ofPure(
|
||||
functionalEventHandler.register(base, ConfigPackPreLoadEvent.class)
|
||||
.then(event -> {
|
||||
CheckedRegistry<Supplier<ObjectTemplate<BiomeProvider>>> providerRegistry = event.getPack().getOrCreateRegistry(
|
||||
Registry<Supplier<ObjectTemplate<BiomeProvider>>> providerRegistry = event.getPack().createRegistry(
|
||||
PROVIDER_REGISTRY_KEY);
|
||||
providerRegistry.register(base.key("IMAGE"),
|
||||
() -> new ImageProviderTemplate(event.getPack().getRegistry(Biome.class)));
|
||||
|
||||
@@ -27,13 +27,9 @@ import com.dfsek.terra.addons.manifest.api.MonadAddonInitializer;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Do;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Get;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Init;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.addon.BaseAddon;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPostLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
|
||||
import com.dfsek.terra.api.inject.annotations.Inject;
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
import com.dfsek.terra.api.util.function.monad.Monad;
|
||||
import com.dfsek.terra.api.util.generic.Construct;
|
||||
@@ -60,20 +56,20 @@ public class BiomePipelineAddon implements MonadAddonInitializer {
|
||||
((functionalEventHandler, base) -> Init.ofPure(Construct.construct(() -> {
|
||||
functionalEventHandler.register(base, ConfigPackPreLoadEvent.class)
|
||||
.then(event -> {
|
||||
CheckedRegistry<Supplier<ObjectTemplate<BiomeProvider>>> providerRegistry =
|
||||
event.getPack().getOrCreateRegistry(
|
||||
Registry<Supplier<ObjectTemplate<BiomeProvider>>> providerRegistry =
|
||||
event.getPack().createRegistry(
|
||||
PROVIDER_REGISTRY_KEY);
|
||||
providerRegistry.register(base.key("PIPELINE"), BiomePipelineTemplate::new);
|
||||
})
|
||||
.then(event -> {
|
||||
CheckedRegistry<Supplier<ObjectTemplate<BiomeSource>>> sourceRegistry =
|
||||
event.getPack().getOrCreateRegistry(
|
||||
Registry<Supplier<ObjectTemplate<BiomeSource>>> sourceRegistry =
|
||||
event.getPack().createRegistry(
|
||||
SOURCE_REGISTRY_KEY);
|
||||
sourceRegistry.register(base.key("SAMPLER"), SamplerSourceTemplate::new);
|
||||
})
|
||||
.then(event -> {
|
||||
CheckedRegistry<Supplier<ObjectTemplate<Stage>>> stageRegistry =
|
||||
event.getPack().getOrCreateRegistry(
|
||||
Registry<Supplier<ObjectTemplate<Stage>>> stageRegistry =
|
||||
event.getPack().createRegistry(
|
||||
STAGE_REGISTRY_KEY);
|
||||
stageRegistry.register(base.key("FRACTAL_EXPAND"), ExpanderStageTemplate::new);
|
||||
stageRegistry.register(base.key("SMOOTH"), SmoothMutatorTemplate::new);
|
||||
|
||||
@@ -17,7 +17,7 @@ import com.dfsek.terra.addons.manifest.api.monad.Get;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Init;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
import com.dfsek.terra.api.util.function.monad.Monad;
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
|
||||
@@ -36,7 +36,7 @@ public class SingleBiomeProviderAddon implements MonadAddonInitializer {
|
||||
((handler, base, platform) -> Init.ofPure(
|
||||
handler.register(base, ConfigPackPreLoadEvent.class)
|
||||
.then(event -> {
|
||||
CheckedRegistry<Supplier<ObjectTemplate<BiomeProvider>>> providerRegistry = event.getPack().getOrCreateRegistry(
|
||||
Registry<Supplier<ObjectTemplate<BiomeProvider>>> providerRegistry = event.getPack().createRegistry(
|
||||
PROVIDER_REGISTRY_KEY);
|
||||
providerRegistry.register(base.key("SINGLE"), SingleBiomeProviderTemplate::new);
|
||||
})
|
||||
|
||||
@@ -45,7 +45,7 @@ public class NoiseChunkGenerator3DAddon implements MonadAddonInitializer {
|
||||
NoiseChunkGeneratorPackConfigTemplate config = event.loadTemplate(new NoiseChunkGeneratorPackConfigTemplate());
|
||||
|
||||
event.getPack()
|
||||
.getOrCreateRegistry(ChunkGeneratorProvider.class)
|
||||
.createRegistry(ChunkGeneratorProvider.class)
|
||||
.register(base.key("NOISE_3D"),
|
||||
pack -> new NoiseChunkGenerator3D(pack, platform, config.getElevationBlend(),
|
||||
config.getHorizontalRes(),
|
||||
|
||||
@@ -7,13 +7,11 @@ import com.dfsek.terra.addons.manifest.api.MonadAddonInitializer;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Do;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Get;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Init;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.addon.BaseAddon;
|
||||
import com.dfsek.terra.api.command.CommandSender;
|
||||
import com.dfsek.terra.api.command.arguments.RegistryArgument;
|
||||
import com.dfsek.terra.api.event.events.platform.CommandRegistrationEvent;
|
||||
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
|
||||
import com.dfsek.terra.api.inject.annotations.Inject;
|
||||
import com.dfsek.terra.api.util.function.monad.Monad;
|
||||
|
||||
|
||||
@@ -37,7 +35,7 @@ public class AddonsCommandAddon implements MonadAddonInitializer {
|
||||
.handler(context -> {
|
||||
StringBuilder addons = new StringBuilder(
|
||||
"Installed addons:\n");
|
||||
platform.getAddons()
|
||||
platform.addons()
|
||||
.forEach(addon -> addons
|
||||
.append(" - ")
|
||||
.append(addon.getID())
|
||||
@@ -50,7 +48,7 @@ public class AddonsCommandAddon implements MonadAddonInitializer {
|
||||
.command(
|
||||
manager.commandBuilder("addons")
|
||||
.argument(
|
||||
RegistryArgument.of("addon", platform.getAddons()))
|
||||
RegistryArgument.of("addon", platform.addons()))
|
||||
.permission("terra.addons.info")
|
||||
.handler(context -> {
|
||||
BaseAddon addon = context.get("addon");
|
||||
|
||||
@@ -2,31 +2,21 @@ package com.dfsek.terra.addons.commands.packs;
|
||||
|
||||
import cloud.commandframework.ArgumentDescription;
|
||||
import cloud.commandframework.CommandManager;
|
||||
import com.dfsek.tectonic.api.config.template.object.ObjectTemplate;
|
||||
|
||||
import com.dfsek.terra.addons.manifest.api.MonadAddonInitializer;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Do;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Get;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Init;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.util.function.monad.Monad;
|
||||
import com.dfsek.terra.api.world.biome.Biome;
|
||||
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.addon.BaseAddon;
|
||||
import com.dfsek.terra.api.command.CommandSender;
|
||||
import com.dfsek.terra.api.command.arguments.RegistryArgument;
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.event.events.platform.CommandRegistrationEvent;
|
||||
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
|
||||
import com.dfsek.terra.api.inject.annotations.Inject;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
||||
public class PacksCommandAddon implements MonadAddonInitializer {
|
||||
|
||||
@@ -25,12 +25,9 @@ import com.dfsek.terra.addons.manifest.api.MonadAddonInitializer;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Do;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Get;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Init;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.addon.BaseAddon;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
|
||||
import com.dfsek.terra.api.inject.annotations.Inject;
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
import com.dfsek.terra.api.structure.feature.Distributor;
|
||||
import com.dfsek.terra.api.util.function.monad.Monad;
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
@@ -48,9 +45,9 @@ public class DistributorAddon implements MonadAddonInitializer {
|
||||
((functionalEventHandler, base) -> Init.ofPure(
|
||||
functionalEventHandler.register(base, ConfigPackPreLoadEvent.class)
|
||||
.then(event -> {
|
||||
CheckedRegistry<Supplier<ObjectTemplate<Distributor>>> distributorRegistry = event
|
||||
Registry<Supplier<ObjectTemplate<Distributor>>> distributorRegistry = event
|
||||
.getPack()
|
||||
.getOrCreateRegistry(DISTRIBUTOR_TOKEN);
|
||||
.createRegistry(DISTRIBUTOR_TOKEN);
|
||||
|
||||
distributorRegistry.register(base.key("SAMPLER"), SamplerDistributorTemplate::new);
|
||||
distributorRegistry.register(base.key("POINTS"), PointSetDistributorTemplate::new);
|
||||
|
||||
@@ -35,12 +35,9 @@ import com.dfsek.terra.addons.manifest.api.MonadAddonInitializer;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Do;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Get;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Init;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.addon.BaseAddon;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
|
||||
import com.dfsek.terra.api.inject.annotations.Inject;
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
import com.dfsek.terra.api.structure.feature.Locator;
|
||||
import com.dfsek.terra.api.util.function.monad.Monad;
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
@@ -62,7 +59,7 @@ public class LocatorAddon implements MonadAddonInitializer {
|
||||
((functionalEventHandler, base, platform) -> Init.ofPure(
|
||||
functionalEventHandler.register(base, ConfigPackPreLoadEvent.class)
|
||||
.then(event -> {
|
||||
CheckedRegistry<Supplier<ObjectTemplate<Locator>>> locatorRegistry = event.getPack().getOrCreateRegistry(LOCATOR_TOKEN);
|
||||
Registry<Supplier<ObjectTemplate<Locator>>> locatorRegistry = event.getPack().createRegistry(LOCATOR_TOKEN);
|
||||
locatorRegistry.register(base.key("SURFACE"), SurfaceLocatorTemplate::new);
|
||||
locatorRegistry.register(base.key("TOP"), TopLocatorTemplate::new);
|
||||
|
||||
@@ -80,7 +77,7 @@ public class LocatorAddon implements MonadAddonInitializer {
|
||||
locatorRegistry.register(base.key("XOR"), XorLocatorTemplate::new);
|
||||
})
|
||||
.then(event -> {
|
||||
CheckedRegistry<Supplier<ObjectTemplate<Pattern>>> patternRegistry = event.getPack().getOrCreateRegistry(PATTERN_TOKEN);
|
||||
Registry<Supplier<ObjectTemplate<Pattern>>> patternRegistry = event.getPack().createRegistry(PATTERN_TOKEN);
|
||||
patternRegistry.register(base.key("MATCH_AIR"), AirMatchPatternTemplate::new);
|
||||
patternRegistry.register(base.key("MATCH_SOLID"), SolidMatchPatternTemplate::new);
|
||||
patternRegistry.register(base.key("MATCH"), SingleBlockMatchPatternTemplate::new);
|
||||
|
||||
@@ -54,17 +54,12 @@ import com.dfsek.terra.addons.noise.samplers.noise.simplex.PerlinSampler;
|
||||
import com.dfsek.terra.addons.noise.samplers.noise.simplex.SimplexSampler;
|
||||
import com.dfsek.terra.addons.noise.samplers.noise.value.ValueCubicSampler;
|
||||
import com.dfsek.terra.addons.noise.samplers.noise.value.ValueSampler;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.addon.BaseAddon;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
|
||||
import com.dfsek.terra.api.inject.annotations.Inject;
|
||||
import com.dfsek.terra.api.noise.NoiseSampler;
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
import com.dfsek.terra.api.util.function.monad.Monad;
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
import com.dfsek.terra.api.world.biome.Biome;
|
||||
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
|
||||
|
||||
|
||||
public class NoiseAddon implements MonadAddonInitializer {
|
||||
@@ -80,7 +75,7 @@ public class NoiseAddon implements MonadAddonInitializer {
|
||||
((functionalEventHandler, base, platform) -> Init.ofPure(
|
||||
functionalEventHandler.register(base, ConfigPackPreLoadEvent.class)
|
||||
.then(event -> {
|
||||
CheckedRegistry<Supplier<ObjectTemplate<NoiseSampler>>> noiseRegistry = event.getPack().getOrCreateRegistry(
|
||||
Registry<Supplier<ObjectTemplate<NoiseSampler>>> noiseRegistry = event.getPack().createRegistry(
|
||||
NOISE_SAMPLER_TOKEN);
|
||||
event.getPack()
|
||||
.applyLoader(CellularSampler.DistanceFunction.class,
|
||||
|
||||
@@ -7,25 +7,15 @@
|
||||
|
||||
package com.dfsek.terra.addons.palette;
|
||||
|
||||
import com.dfsek.tectonic.api.config.template.object.ObjectTemplate;
|
||||
|
||||
import com.dfsek.terra.addons.manifest.api.MonadAddonInitializer;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Do;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Get;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Init;
|
||||
import com.dfsek.terra.addons.palette.palette.PaletteLayerHolder;
|
||||
import com.dfsek.terra.addons.palette.palette.PaletteLayerLoader;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.addon.BaseAddon;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
|
||||
import com.dfsek.terra.api.inject.annotations.Inject;
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.util.function.monad.Monad;
|
||||
import com.dfsek.terra.api.world.biome.Biome;
|
||||
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
||||
public class PaletteAddon implements MonadAddonInitializer {
|
||||
|
||||
@@ -24,13 +24,10 @@ import com.dfsek.terra.addons.manifest.api.MonadAddonInitializer;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Do;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Get;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Init;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.addon.BaseAddon;
|
||||
import com.dfsek.terra.api.config.meta.Meta;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigurationLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
|
||||
import com.dfsek.terra.api.inject.annotations.Inject;
|
||||
import com.dfsek.terra.api.properties.Context;
|
||||
import com.dfsek.terra.api.properties.PropertyKey;
|
||||
import com.dfsek.terra.api.structure.feature.Feature;
|
||||
@@ -39,7 +36,6 @@ import com.dfsek.terra.api.util.generic.Construct;
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
import com.dfsek.terra.api.world.biome.Biome;
|
||||
import com.dfsek.terra.api.world.chunk.generation.stage.GenerationStage;
|
||||
import com.dfsek.terra.api.world.chunk.generation.util.provider.ChunkGeneratorProvider;
|
||||
|
||||
|
||||
public class FeatureGenerationAddon implements MonadAddonInitializer {
|
||||
@@ -61,7 +57,7 @@ public class FeatureGenerationAddon implements MonadAddonInitializer {
|
||||
((handler, base, platform) -> Init.ofPure(Construct.construct(() -> {
|
||||
handler.register(base, ConfigPackPreLoadEvent.class)
|
||||
.then(event -> event.getPack()
|
||||
.getOrCreateRegistry(STAGE_TYPE_KEY)
|
||||
.createRegistry(STAGE_TYPE_KEY)
|
||||
.register(base.key("FEATURE"), () -> new FeatureStageTemplate(platform, biomeFeaturesKey)))
|
||||
.failThrough();
|
||||
return handler.register(base, ConfigurationLoadEvent.class)
|
||||
|
||||
@@ -24,7 +24,7 @@ public class StructureGenerationAddon implements MonadAddonInitializer {
|
||||
((handler, base, platform) -> Init.ofPure(Construct.construct(() -> {
|
||||
handler.register(base, ConfigPackPreLoadEvent.class)
|
||||
.then(event -> event.getPack()
|
||||
.getOrCreateRegistry(GenerationStageProvider.class)
|
||||
.createRegistry(GenerationStageProvider.class)
|
||||
.register(base.key("STRUCTURE"), pack -> new StructureGenerationStage(platform)))
|
||||
.failThrough();
|
||||
return handler.register(base, ConfigurationLoadEvent.class)
|
||||
|
||||
@@ -4,15 +4,25 @@ import com.dfsek.terra.addons.manifest.impl.InitInfo;
|
||||
import com.dfsek.terra.api.util.function.Functions;
|
||||
import com.dfsek.terra.api.util.function.monad.Monad;
|
||||
|
||||
import io.vavr.Function0;
|
||||
import io.vavr.Function1;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
|
||||
public class Init<T> implements Monad<T, Init<?>> {
|
||||
private final Function<InitInfo, T> get;
|
||||
|
||||
public static <T> Init<T> of(Function<InitInfo, T> get) {
|
||||
protected static <T> Init<T> of(Function<InitInfo, T> get) {
|
||||
return new Init<>(get);
|
||||
}
|
||||
protected static Init<Void> unit(Consumer<InitInfo> get) {
|
||||
return new Init<>(info -> {
|
||||
get.accept(info);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
private Init(Function<InitInfo, T> get) {
|
||||
this.get = get;
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.dfsek.terra.addons.manifest.api.monad;
|
||||
|
||||
import com.dfsek.terra.api.registry.key.Keyed;
|
||||
import com.dfsek.terra.api.util.function.monad.Monad;
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
|
||||
|
||||
public class Register {
|
||||
private static <T> Monad<Void, Init<?>> register(TypeKey<T> type, String key, T object) {
|
||||
return Init.unit(info -> {
|
||||
throw new UnsupportedOperationException("TODO");
|
||||
});
|
||||
}
|
||||
|
||||
public static <T> Monad<Void, Init<?>> register(Class<T> clazz, String key, T object) {
|
||||
return register(TypeKey.of(clazz), key, object);
|
||||
}
|
||||
|
||||
public static <T extends Keyed<T>> Monad<Void, Init<?>> register(Class<T> clazz, Keyed<T> keyed) {
|
||||
return register(TypeKey.of(clazz), keyed.getID(), keyed.coerce());
|
||||
}
|
||||
}
|
||||
@@ -32,8 +32,7 @@ import com.dfsek.terra.api.addon.BaseAddon;
|
||||
import com.dfsek.terra.api.block.state.BlockState;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
|
||||
import com.dfsek.terra.api.inject.annotations.Inject;
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
import com.dfsek.terra.api.structure.Structure;
|
||||
import com.dfsek.terra.api.util.StringUtil;
|
||||
|
||||
@@ -59,7 +58,7 @@ public class SpongeSchematicAddon implements MonadAddonInitializer {
|
||||
((handler, base, platform) -> Init.ofPure(
|
||||
handler.register(base, ConfigPackPreLoadEvent.class)
|
||||
.then(event -> {
|
||||
CheckedRegistry<Structure> structureRegistry = event.getPack().getOrCreateRegistry(Structure.class);
|
||||
Registry<Structure> structureRegistry = event.getPack().createRegistry(Structure.class);
|
||||
event.getPack()
|
||||
.getLoader()
|
||||
.open("", ".schem")
|
||||
|
||||
@@ -14,12 +14,9 @@ import com.dfsek.terra.addons.manifest.api.monad.Init;
|
||||
import com.dfsek.terra.addons.terrascript.parser.exceptions.ParseException;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.FunctionBuilder;
|
||||
import com.dfsek.terra.addons.terrascript.script.StructureScript;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.addon.BaseAddon;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
|
||||
import com.dfsek.terra.api.inject.annotations.Inject;
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
import com.dfsek.terra.api.structure.LootTable;
|
||||
import com.dfsek.terra.api.structure.Structure;
|
||||
import com.dfsek.terra.api.util.StringUtil;
|
||||
@@ -36,8 +33,8 @@ public class TerraScriptAddon implements MonadAddonInitializer {
|
||||
((handler, base, platform) -> Init.ofPure(
|
||||
handler.register(base, ConfigPackPreLoadEvent.class)
|
||||
.then(event -> {
|
||||
CheckedRegistry<Structure> structureRegistry = event.getPack().getOrCreateRegistry(Structure.class);
|
||||
CheckedRegistry<LootTable> lootRegistry = event.getPack().getOrCreateRegistry(LootTable.class);
|
||||
Registry<Structure> structureRegistry = event.getPack().createRegistry(Structure.class);
|
||||
Registry<LootTable> lootRegistry = event.getPack().createRegistry(LootTable.class);
|
||||
event.getPack().getLoader().open("", ".tesf").thenEntries(
|
||||
entries ->
|
||||
entries.stream()
|
||||
@@ -50,7 +47,7 @@ public class TerraScriptAddon implements MonadAddonInitializer {
|
||||
platform,
|
||||
structureRegistry,
|
||||
lootRegistry,
|
||||
event.getPack().getOrCreateRegistry(FunctionBuilder.class));
|
||||
event.getPack().createRegistry(FunctionBuilder.class));
|
||||
} catch(ParseException e) {
|
||||
throw new RuntimeException("Failed to load script \"" + entry.getKey() + "\"", e);
|
||||
}
|
||||
|
||||
@@ -1,33 +1,17 @@
|
||||
package com.dfsek.terra.addon.terrascript.check;
|
||||
|
||||
import com.dfsek.terra.addons.manifest.api.MonadAddonInitializer;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Do;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Get;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Init;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.FunctionBuilder;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.addon.BaseAddon;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
|
||||
import com.dfsek.terra.api.inject.annotations.Inject;
|
||||
import com.dfsek.terra.api.util.function.monad.Monad;
|
||||
|
||||
import static com.dfsek.terra.addons.manifest.api.monad.Get.platform;
|
||||
import static com.dfsek.terra.addons.manifest.api.monad.Register.register;
|
||||
|
||||
|
||||
public class TerraScriptCheckFunctionAddon implements MonadAddonInitializer {
|
||||
@Override
|
||||
public Monad<?, Init<?>> initialize() {
|
||||
return Do.with(
|
||||
Get.eventManager().map(eventManager -> eventManager.getHandler(FunctionalEventHandler.class)),
|
||||
Get.addon(),
|
||||
Get.platform(),
|
||||
((handler, base, platform) -> Init.ofPure(
|
||||
handler.register(base, ConfigPackPreLoadEvent.class)
|
||||
.priority(1)
|
||||
.then(event -> event
|
||||
.getPack()
|
||||
.getOrCreateRegistry(FunctionBuilder.class)
|
||||
.register(base.key("check"), new CheckFunctionBuilder(platform)))
|
||||
.failThrough()))
|
||||
);
|
||||
return platform().bind(platform -> register(FunctionBuilder.class, "check", new CheckFunctionBuilder(platform)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,11 +6,8 @@ import com.dfsek.terra.addons.manifest.api.monad.Get;
|
||||
import com.dfsek.terra.addons.manifest.api.monad.Init;
|
||||
import com.dfsek.terra.addons.noise.NoiseConfigPackTemplate;
|
||||
import com.dfsek.terra.addons.terrascript.parser.lang.functions.FunctionBuilder;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.addon.BaseAddon;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
|
||||
import com.dfsek.terra.api.inject.annotations.Inject;
|
||||
import com.dfsek.terra.api.util.function.monad.Monad;
|
||||
|
||||
|
||||
@@ -25,7 +22,7 @@ public class TerraScriptSamplerFunctionAddon implements MonadAddonInitializer {
|
||||
.priority(51)
|
||||
.then(event -> event
|
||||
.getPack()
|
||||
.getOrCreateRegistry(FunctionBuilder.class)
|
||||
.createRegistry(FunctionBuilder.class)
|
||||
.register(base.key("sampler"), new SamplerFunctionBuilder(event.getPack().getContext().get(
|
||||
NoiseConfigPackTemplate.class).getSamplers())))
|
||||
.failThrough()))
|
||||
|
||||
@@ -7,6 +7,9 @@
|
||||
|
||||
package com.dfsek.terra.api;
|
||||
|
||||
import com.dfsek.terra.api.registry.meta.RegistryHolder;
|
||||
import com.dfsek.terra.api.world.chunk.generation.ChunkGenerator;
|
||||
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -19,7 +22,6 @@ import com.dfsek.terra.api.event.EventManager;
|
||||
import com.dfsek.terra.api.handle.ItemHandle;
|
||||
import com.dfsek.terra.api.handle.WorldHandle;
|
||||
import com.dfsek.terra.api.profiler.Profiler;
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
import com.dfsek.terra.api.tectonic.LoaderRegistrar;
|
||||
|
||||
@@ -27,24 +29,13 @@ import com.dfsek.terra.api.tectonic.LoaderRegistrar;
|
||||
/**
|
||||
* Represents a Terra mod/plugin instance.
|
||||
*/
|
||||
public interface Platform extends LoaderRegistrar {
|
||||
public interface Platform extends LoaderRegistrar, RegistryHolder {
|
||||
boolean reload();
|
||||
|
||||
@NotNull
|
||||
@Contract(pure = true)
|
||||
String platformName();
|
||||
|
||||
/**
|
||||
* Runs a task that may or may not be thread safe, depending on platform.
|
||||
* <p>
|
||||
* Allows platforms to define what code is safe to be run asynchronously.
|
||||
*
|
||||
* @param task Task to be run.
|
||||
*/
|
||||
default void runPossiblyUnsafeTask(@NotNull Runnable task) {
|
||||
task.run();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Contract(pure = true)
|
||||
WorldHandle getWorldHandle();
|
||||
@@ -59,11 +50,7 @@ public interface Platform extends LoaderRegistrar {
|
||||
|
||||
@NotNull
|
||||
@Contract(pure = true)
|
||||
CheckedRegistry<ConfigPack> getConfigRegistry();
|
||||
|
||||
@NotNull
|
||||
@Contract(pure = true)
|
||||
Registry<BaseAddon> getAddons();
|
||||
Registry<BaseAddon> addons();
|
||||
|
||||
@NotNull
|
||||
@Contract(pure = true)
|
||||
|
||||
@@ -17,7 +17,7 @@ import com.dfsek.terra.api.addon.BaseAddon;
|
||||
import com.dfsek.terra.api.properties.PropertyHolder;
|
||||
import com.dfsek.terra.api.registry.key.Keyed;
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
import com.dfsek.terra.api.registry.meta.CheckedRegistryHolder;
|
||||
import com.dfsek.terra.api.registry.meta.RegistryHolder;
|
||||
import com.dfsek.terra.api.registry.meta.RegistryProvider;
|
||||
import com.dfsek.terra.api.tectonic.ConfigLoadingDelegate;
|
||||
import com.dfsek.terra.api.tectonic.LoaderRegistrar;
|
||||
@@ -30,7 +30,7 @@ import com.dfsek.terra.api.world.chunk.generation.util.provider.ChunkGeneratorPr
|
||||
|
||||
public interface ConfigPack extends LoaderRegistrar,
|
||||
ConfigLoadingDelegate,
|
||||
CheckedRegistryHolder,
|
||||
RegistryHolder,
|
||||
RegistryProvider,
|
||||
Keyed<ConfigPack>,
|
||||
PropertyHolder {
|
||||
|
||||
@@ -11,17 +11,17 @@ import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.config.ConfigType;
|
||||
import com.dfsek.terra.api.event.events.FailThroughEvent;
|
||||
import com.dfsek.terra.api.event.events.PackEvent;
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
import com.dfsek.terra.api.util.reflection.ReflectionUtil;
|
||||
|
||||
|
||||
public abstract class ConfigTypeLoadEvent implements PackEvent, FailThroughEvent {
|
||||
private final ConfigType<?, ?> type;
|
||||
private final CheckedRegistry<?> registry;
|
||||
private final Registry<?> registry;
|
||||
|
||||
private final ConfigPack pack;
|
||||
|
||||
public ConfigTypeLoadEvent(ConfigType<?, ?> type, CheckedRegistry<?> registry, ConfigPack pack) {
|
||||
public ConfigTypeLoadEvent(ConfigType<?, ?> type, Registry<?> registry, ConfigPack pack) {
|
||||
this.type = type;
|
||||
this.registry = registry;
|
||||
this.pack = pack;
|
||||
@@ -37,11 +37,11 @@ public abstract class ConfigTypeLoadEvent implements PackEvent, FailThroughEvent
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> CheckedRegistry<T> getRegistry(Class<T> clazz) {
|
||||
public <T> Registry<T> getRegistry(Class<T> clazz) {
|
||||
if(!clazz.isAssignableFrom(type.getTypeKey().getRawType()))
|
||||
throw new ClassCastException(
|
||||
"Cannot assign object from loader of type " + ReflectionUtil.typeToString(type.getTypeKey().getType()) + " to class " +
|
||||
clazz.getCanonicalName());
|
||||
return (CheckedRegistry<T>) registry;
|
||||
return (Registry<T>) registry;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,11 +9,11 @@ package com.dfsek.terra.api.event.events.config.type;
|
||||
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.config.ConfigType;
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
|
||||
|
||||
public class ConfigTypePostLoadEvent extends ConfigTypeLoadEvent {
|
||||
public ConfigTypePostLoadEvent(ConfigType<?, ?> type, CheckedRegistry<?> registry, ConfigPack pack) {
|
||||
public ConfigTypePostLoadEvent(ConfigType<?, ?> type, Registry<?> registry, ConfigPack pack) {
|
||||
super(type, registry, pack);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,11 +9,11 @@ package com.dfsek.terra.api.event.events.config.type;
|
||||
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.config.ConfigType;
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
|
||||
|
||||
public class ConfigTypePreLoadEvent extends ConfigTypeLoadEvent {
|
||||
public ConfigTypePreLoadEvent(ConfigType<?, ?> type, CheckedRegistry<?> registry, ConfigPack pack) {
|
||||
public ConfigTypePreLoadEvent(ConfigType<?, ?> type, Registry<?> registry, ConfigPack pack) {
|
||||
super(type, registry, pack);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2020-2021 Polyhedral Development
|
||||
*
|
||||
* The Terra API is licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in the common/api directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.api.registry;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
|
||||
import com.dfsek.terra.api.registry.key.Keyed;
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
|
||||
|
||||
public interface CheckedRegistry<T> extends Registry<T> {
|
||||
/**
|
||||
* Add a value to this registry, checking whether it is present first.
|
||||
*
|
||||
* @param identifier Identifier to assign value.
|
||||
* @param value Value to register.
|
||||
*
|
||||
* @throws DuplicateEntryException If an entry with the same identifier is already present.
|
||||
*/
|
||||
void register(@NotNull RegistryKey identifier, @NotNull T value) throws DuplicateEntryException;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
default void register(@NotNull Keyed<? extends T> value) throws DuplicateEntryException {
|
||||
register(value.getRegistryKey(), (T) value);
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2020-2021 Polyhedral Development
|
||||
*
|
||||
* The Terra API is licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in the common/api directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.api.registry;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
|
||||
import com.dfsek.terra.api.registry.key.Keyed;
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
|
||||
|
||||
public interface OpenRegistry<T> extends Registry<T> {
|
||||
/**
|
||||
* Add a value to this registry.
|
||||
*
|
||||
* @param identifier Identifier to assign value.
|
||||
* @param value Value to register.
|
||||
*/
|
||||
boolean register(@NotNull RegistryKey identifier, @NotNull T value);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
default boolean register(@NotNull Keyed<? extends T> value) {
|
||||
return register(value.getRegistryKey(), (T) value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a value to this registry, checking whether it is present first.
|
||||
*
|
||||
* @param identifier Identifier to assign value.
|
||||
* @param value Value to register.
|
||||
*
|
||||
* @throws DuplicateEntryException If an entry with the same identifier is already present.
|
||||
*/
|
||||
void registerChecked(@NotNull RegistryKey identifier, @NotNull T value) throws DuplicateEntryException;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
default void registerChecked(@NotNull Keyed<? extends T> value) {
|
||||
registerChecked(value.getRegistryKey(), (T) value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears all entries from the registry.
|
||||
*/
|
||||
void clear();
|
||||
}
|
||||
@@ -8,16 +8,17 @@
|
||||
package com.dfsek.terra.api.registry;
|
||||
|
||||
import com.dfsek.tectonic.api.loader.type.TypeLoader;
|
||||
|
||||
import com.dfsek.terra.api.registry.key.Keyed;
|
||||
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
@@ -65,7 +66,22 @@ public interface Registry<T> extends TypeLoader<T> {
|
||||
*/
|
||||
@NotNull
|
||||
@Contract(pure = true)
|
||||
Collection<T> entries();
|
||||
Set<T> entries();
|
||||
|
||||
/**
|
||||
* Add a value to this registry.
|
||||
*
|
||||
* @param identifier Identifier to assign value.
|
||||
* @param value Value to register.
|
||||
*/
|
||||
@Contract(pure = true, value = "_,_->new")
|
||||
Registry<T> register(@NotNull RegistryKey identifier, @NotNull T value);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Contract(pure = true, value = "_->new")
|
||||
default Registry<T> register(@NotNull Keyed<? extends T> value) {
|
||||
return register(value.getRegistryKey(), (T) value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the keys in this registry.
|
||||
@@ -82,31 +98,7 @@ public interface Registry<T> extends TypeLoader<T> {
|
||||
return getType().getRawType();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
default Optional<T> getByID(String id) {
|
||||
return getByID(id, map -> {
|
||||
if(map.isEmpty()) return Optional.empty();
|
||||
if(map.size() == 1) {
|
||||
return map.values().stream().findFirst(); // only one value.
|
||||
}
|
||||
throw new IllegalArgumentException("ID \"" + id + "\" is ambiguous; matches: " + map
|
||||
.keySet()
|
||||
.stream()
|
||||
.map(RegistryKey::toString)
|
||||
.reduce("", (a, b) -> a + "\n - " + b));
|
||||
});
|
||||
}
|
||||
Map<RegistryKey, T> getID(String id);
|
||||
|
||||
default Collection<T> getAllWithID(String id) {
|
||||
return getMatches(id).values();
|
||||
}
|
||||
|
||||
Map<RegistryKey, T> getMatches(String id);
|
||||
|
||||
default Optional<T> getByID(String attempt, Function<Map<RegistryKey, T>, Optional<T>> reduction) {
|
||||
if(attempt.contains(":")) {
|
||||
return get(RegistryKey.parse(attempt));
|
||||
}
|
||||
return reduction.apply(getMatches(attempt));
|
||||
}
|
||||
Map<RegistryKey, T> getNamespace(String namespace);
|
||||
}
|
||||
|
||||
@@ -13,4 +13,9 @@ public interface Keyed<T extends Keyed<T>> extends Namespaced, StringIdentifiabl
|
||||
default String getID() {
|
||||
return getRegistryKey().getID();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
default T coerce() {
|
||||
return (T) this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.dfsek.terra.api.registry.meta;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
|
||||
|
||||
public interface CheckedRegistryHolder extends RegistryHolder {
|
||||
default <T> CheckedRegistry<T> getCheckedRegistry(Class<T> clazz) throws IllegalStateException {
|
||||
return getCheckedRegistry((Type) clazz);
|
||||
}
|
||||
|
||||
default <T> CheckedRegistry<T> getCheckedRegistry(TypeKey<T> type) throws IllegalStateException {
|
||||
return getCheckedRegistry(type.getType());
|
||||
}
|
||||
|
||||
<T> CheckedRegistry<T> getCheckedRegistry(Type type);
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
package com.dfsek.terra.api.registry.meta;
|
||||
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
|
||||
|
||||
public interface RegistryProvider {
|
||||
default <T> CheckedRegistry<T> getOrCreateRegistry(Class<T> clazz) {
|
||||
return getOrCreateRegistry(TypeKey.of(clazz));
|
||||
default <T> Registry<T> createRegistry(Class<T> clazz) {
|
||||
return createRegistry(TypeKey.of(clazz));
|
||||
}
|
||||
|
||||
<T> CheckedRegistry<T> getOrCreateRegistry(TypeKey<T> type);
|
||||
<T> Registry<T> createRegistry(TypeKey<T> type);
|
||||
}
|
||||
|
||||
@@ -58,19 +58,15 @@ import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
|
||||
import com.dfsek.terra.api.inject.Injector;
|
||||
import com.dfsek.terra.api.inject.impl.InjectorImpl;
|
||||
import com.dfsek.terra.api.profiler.Profiler;
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
import com.dfsek.terra.api.registry.key.StringIdentifiable;
|
||||
import com.dfsek.terra.api.util.generic.pair.Pair;
|
||||
import com.dfsek.terra.api.util.mutable.MutableBoolean;
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
import com.dfsek.terra.config.GenericLoaders;
|
||||
import com.dfsek.terra.config.PluginConfigImpl;
|
||||
import com.dfsek.terra.event.EventManagerImpl;
|
||||
import com.dfsek.terra.profiler.ProfilerImpl;
|
||||
import com.dfsek.terra.registry.CheckedRegistryImpl;
|
||||
import com.dfsek.terra.registry.LockedRegistryImpl;
|
||||
import com.dfsek.terra.registry.OpenRegistryImpl;
|
||||
import com.dfsek.terra.registry.RegistryImpl;
|
||||
import com.dfsek.terra.registry.master.ConfigRegistry;
|
||||
|
||||
|
||||
@@ -84,9 +80,7 @@ public abstract class AbstractPlatform implements Platform {
|
||||
|
||||
private static final MutableBoolean LOADED = new MutableBoolean(false);
|
||||
private final EventManager eventManager = new EventManagerImpl();
|
||||
private final ConfigRegistry configRegistry = new ConfigRegistry();
|
||||
|
||||
private final CheckedRegistry<ConfigPack> checkedConfigRegistry = new CheckedRegistryImpl<>(configRegistry);
|
||||
private final Registry<ConfigPack> configRegistry = new ConfigRegistry();
|
||||
|
||||
private final Profiler profiler = new ProfilerImpl();
|
||||
|
||||
@@ -94,9 +88,7 @@ public abstract class AbstractPlatform implements Platform {
|
||||
|
||||
private final PluginConfigImpl config = new PluginConfigImpl();
|
||||
|
||||
private final CheckedRegistry<BaseAddon> addonRegistry = new CheckedRegistryImpl<>(new OpenRegistryImpl<>(TypeKey.of(BaseAddon.class)));
|
||||
|
||||
private final Registry<BaseAddon> lockedAddonRegistry = new LockedRegistryImpl<>(addonRegistry);
|
||||
private final Registry<BaseAddon> addonRegistry = ;
|
||||
|
||||
public ConfigRegistry getRawConfigRegistry() {
|
||||
return configRegistry;
|
||||
@@ -321,13 +313,13 @@ public abstract class AbstractPlatform implements Platform {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull CheckedRegistry<ConfigPack> getConfigRegistry() {
|
||||
return checkedConfigRegistry;
|
||||
public @NotNull Registry<ConfigPack> getConfigRegistry() {
|
||||
return configRegistry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Registry<BaseAddon> getAddons() {
|
||||
return lockedAddonRegistry;
|
||||
public @NotNull Registry<BaseAddon> addons() {
|
||||
return addonRegistry;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -56,7 +56,7 @@ public class GenericLoaders implements LoaderRegistrar {
|
||||
.registerLoader(LinkedHashMap.class, new LinkedHashMapLoader());
|
||||
|
||||
if(platform != null) {
|
||||
registry.registerLoader(BaseAddon.class, platform.getAddons())
|
||||
registry.registerLoader(BaseAddon.class, platform.addons())
|
||||
.registerLoader(BlockType.class, (type, object, configLoader, depthTracker) -> platform
|
||||
.getWorldHandle().createBlockState((String) object).getBlockType())
|
||||
.registerLoader(BlockState.class, (type, object, configLoader, depthTracker) -> platform
|
||||
|
||||
@@ -67,8 +67,6 @@ import com.dfsek.terra.api.event.events.config.pack.ConfigPackPostLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.type.ConfigTypePostLoadEvent;
|
||||
import com.dfsek.terra.api.properties.Context;
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.registry.OpenRegistry;
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
import com.dfsek.terra.api.tectonic.ShortcutLoader;
|
||||
@@ -89,8 +87,7 @@ import com.dfsek.terra.config.preprocessor.MetaNumberPreprocessor;
|
||||
import com.dfsek.terra.config.preprocessor.MetaStringPreprocessor;
|
||||
import com.dfsek.terra.config.preprocessor.MetaValuePreprocessor;
|
||||
import com.dfsek.terra.config.prototype.ProtoConfig;
|
||||
import com.dfsek.terra.registry.CheckedRegistryImpl;
|
||||
import com.dfsek.terra.registry.OpenRegistryImpl;
|
||||
import com.dfsek.terra.registry.RegistryImpl;
|
||||
import com.dfsek.terra.registry.ShortcutHolder;
|
||||
|
||||
|
||||
@@ -113,10 +110,10 @@ public class ConfigPackImpl implements ConfigPack {
|
||||
|
||||
private final BiomeProvider seededBiomeProvider;
|
||||
|
||||
private final Map<Type, CheckedRegistryImpl<?>> registryMap = new HashMap<>();
|
||||
private final Map<Type, RegistryImpl<?>> registryMap = new HashMap<>();
|
||||
private final Map<Type, ShortcutHolder<?>> shortcuts = new HashMap<>();
|
||||
|
||||
private final OpenRegistry<ConfigType<?, ?>> configTypeRegistry;
|
||||
private final Registry<ConfigType<?, ?>> configTypeRegistry;
|
||||
private final TreeMap<Integer, List<Pair<RegistryKey, ConfigType<?, ?>>>> configTypes = new TreeMap<>();
|
||||
|
||||
private final RegistryKey key;
|
||||
@@ -202,7 +199,7 @@ public class ConfigPackImpl implements ConfigPack {
|
||||
}, ListMultimap::putAll);
|
||||
|
||||
configTypeRegistry.forEach(configType -> {
|
||||
CheckedRegistry registry = getCheckedRegistry(configType.getTypeKey());
|
||||
Registry registry = getRegistry(configType.getTypeKey());
|
||||
abstractConfigLoader
|
||||
.loadConfigs(multimap.get(configType))
|
||||
.stream()
|
||||
@@ -311,35 +308,33 @@ public class ConfigPackImpl implements ConfigPack {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> CheckedRegistry<T> getOrCreateRegistry(TypeKey<T> typeKey) {
|
||||
return (CheckedRegistry<T>) registryMap.computeIfAbsent(typeKey.getType(), c -> {
|
||||
OpenRegistry<T> registry = new OpenRegistryImpl<>(typeKey);
|
||||
selfLoader.registerLoader(c, registry);
|
||||
abstractConfigLoader.registerLoader(c, registry);
|
||||
logger.debug("Registered loader for registry of class {}", ReflectionUtil.typeToString(c));
|
||||
|
||||
if(typeKey.getType() instanceof ParameterizedType param) {
|
||||
Type base = param.getRawType();
|
||||
if(base instanceof Class // should always be true but we'll check anyways
|
||||
&& Supplier.class.isAssignableFrom((Class<?>) base)) { // If it's a supplier
|
||||
Type supplied = param.getActualTypeArguments()[0]; // Grab the supplied type
|
||||
if(supplied instanceof ParameterizedType suppliedParam) {
|
||||
Type suppliedBase = suppliedParam.getRawType();
|
||||
if(suppliedBase instanceof Class // should always be true but we'll check anyways
|
||||
&& ObjectTemplate.class.isAssignableFrom((Class<?>) suppliedBase)) {
|
||||
Type templateType = suppliedParam.getActualTypeArguments()[0];
|
||||
GenericTemplateSupplierLoader<?> loader = new GenericTemplateSupplierLoader<>(
|
||||
(Registry<Supplier<ObjectTemplate<Supplier<ObjectTemplate<?>>>>>) registry);
|
||||
selfLoader.registerLoader(templateType, loader);
|
||||
abstractConfigLoader.registerLoader(templateType, loader);
|
||||
logger.debug("Registered template loader for registry of class {}", ReflectionUtil.typeToString(templateType));
|
||||
}
|
||||
public <T> Registry<T> createRegistry(TypeKey<T> typeKey) {
|
||||
Registry<T> registry = RegistryImpl.empty(typeKey);
|
||||
selfLoader.registerLoader(c, registry);
|
||||
abstractConfigLoader.registerLoader(c, registry);
|
||||
logger.debug("Registered loader for registry of class {}", ReflectionUtil.typeToString(c));
|
||||
|
||||
if(typeKey.getType() instanceof ParameterizedType param) {
|
||||
Type base = param.getRawType();
|
||||
if(base instanceof Class // should always be true but we'll check anyway
|
||||
&& Supplier.class.isAssignableFrom((Class<?>) base)) { // If it's a supplier
|
||||
Type supplied = param.getActualTypeArguments()[0]; // Grab the supplied type
|
||||
if(supplied instanceof ParameterizedType suppliedParam) {
|
||||
Type suppliedBase = suppliedParam.getRawType();
|
||||
if(suppliedBase instanceof Class // should always be true but we'll check anyway
|
||||
&& ObjectTemplate.class.isAssignableFrom((Class<?>) suppliedBase)) {
|
||||
Type templateType = suppliedParam.getActualTypeArguments()[0];
|
||||
GenericTemplateSupplierLoader<?> loader = new GenericTemplateSupplierLoader<>(
|
||||
(Registry<Supplier<ObjectTemplate<Supplier<ObjectTemplate<?>>>>>) registry);
|
||||
selfLoader.registerLoader(templateType, loader);
|
||||
abstractConfigLoader.registerLoader(templateType, loader);
|
||||
logger.debug("Registered template loader for registry of class {}", ReflectionUtil.typeToString(templateType));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new CheckedRegistryImpl<>(registry);
|
||||
});
|
||||
}
|
||||
return registry;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -366,7 +361,7 @@ public class ConfigPackImpl implements ConfigPack {
|
||||
@Override
|
||||
public <T> ConfigPack registerShortcut(TypeKey<T> clazz, String shortcut, ShortcutLoader<T> loader) {
|
||||
ShortcutHolder<?> holder = shortcuts
|
||||
.computeIfAbsent(clazz.getType(), c -> new ShortcutHolder<>(getOrCreateRegistry(clazz)))
|
||||
.computeIfAbsent(clazz.getType(), c -> new ShortcutHolder<>(createRegistry(clazz)))
|
||||
.register(shortcut, (ShortcutLoader) loader);
|
||||
selfLoader.registerLoader(clazz.getType(), holder);
|
||||
abstractConfigLoader.registerLoader(clazz.getType(), holder);
|
||||
@@ -378,17 +373,17 @@ public class ConfigPackImpl implements ConfigPack {
|
||||
return template.getGeneratorProvider();
|
||||
}
|
||||
|
||||
private OpenRegistry<ConfigType<?, ?>> createConfigRegistry() {
|
||||
return new OpenRegistryImpl<>(new LinkedHashMap<>(), CONFIG_TYPE_TYPE_KEY) {
|
||||
private Registry<ConfigType<?, ?>> createConfigRegistry() {
|
||||
return new RegistryImpl<>(new LinkedHashMap<>(), CONFIG_TYPE_TYPE_KEY) {
|
||||
@Override
|
||||
public boolean register(@NotNull RegistryKey key, @NotNull ConfigType<?, ?> value) {
|
||||
if(!registryMap
|
||||
.containsKey(value.getTypeKey()
|
||||
.getType())) {
|
||||
OpenRegistry<?> openRegistry = new OpenRegistryImpl<>(value.getTypeKey());
|
||||
selfLoader.registerLoader(value.getTypeKey().getType(), openRegistry);
|
||||
abstractConfigLoader.registerLoader(value.getTypeKey().getType(), openRegistry);
|
||||
registryMap.put(value.getTypeKey().getType(), new CheckedRegistryImpl<>(openRegistry));
|
||||
Registry<?> Registry = new RegistryImpl<>(value.getTypeKey());
|
||||
selfLoader.registerLoader(value.getTypeKey().getType(), Registry);
|
||||
abstractConfigLoader.registerLoader(value.getTypeKey().getType(), Registry);
|
||||
registryMap.put(value.getTypeKey().getType(), new RegistryImpl<>(Registry));
|
||||
}
|
||||
return super.register(key, value);
|
||||
}
|
||||
@@ -396,9 +391,10 @@ public class ConfigPackImpl implements ConfigPack {
|
||||
}
|
||||
|
||||
private void checkDeadEntries() {
|
||||
registryMap.forEach((clazz, pair) -> ((OpenRegistryImpl<?>) pair.getRegistry())
|
||||
.getDeadEntries()
|
||||
.forEach((id, value) -> logger.debug("Dead entry in '{}' registry: '{}'", ReflectionUtil.typeToString(clazz), id)));
|
||||
logger.warn("TODO: Dead Entries");
|
||||
//registryMap.forEach((clazz, pair) -> ((RegistryImpl<?>) pair.getRegistry())
|
||||
// .getDeadEntries()
|
||||
// .forEach((id, value) -> logger.debug("Dead entry in '{}' registry: '{}'", ReflectionUtil.typeToString(clazz), id)));
|
||||
}
|
||||
|
||||
public ConfigPackTemplate getTemplate() {
|
||||
@@ -407,14 +403,14 @@ public class ConfigPackImpl implements ConfigPack {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> CheckedRegistry<T> getRegistry(Type type) {
|
||||
return (CheckedRegistry<T>) registryMap.get(type);
|
||||
public <T> Registry<T> getRegistry(Type type) {
|
||||
return (Registry<T>) registryMap.get(type);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> CheckedRegistry<T> getCheckedRegistry(Type type) throws IllegalStateException {
|
||||
return (CheckedRegistry<T>) registryMap.get(type);
|
||||
public <T> Registry<T> getRegistry(Type type) throws IllegalStateException {
|
||||
return (Registry<T>) registryMap.get(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,108 +0,0 @@
|
||||
/*
|
||||
* This file is part of Terra.
|
||||
*
|
||||
* Terra is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Terra is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Terra. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.registry;
|
||||
|
||||
import com.dfsek.tectonic.api.depth.DepthTracker;
|
||||
import com.dfsek.tectonic.api.exception.LoadException;
|
||||
import com.dfsek.tectonic.api.loader.ConfigLoader;
|
||||
import org.jetbrains.annotations.ApiStatus.Internal;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.AnnotatedType;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.registry.OpenRegistry;
|
||||
import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
|
||||
|
||||
/**
|
||||
* Wrapper for a registry that ensures checked additions.
|
||||
*
|
||||
* @param <T> Type in registry
|
||||
*/
|
||||
public class CheckedRegistryImpl<T> implements CheckedRegistry<T> {
|
||||
private final OpenRegistry<T> registry;
|
||||
|
||||
public CheckedRegistryImpl(OpenRegistry<T> registry) {
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
@Internal
|
||||
public OpenRegistry<T> getRegistry() {
|
||||
return registry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(@NotNull RegistryKey identifier, @NotNull T value) throws DuplicateEntryException {
|
||||
registry.registerChecked(identifier, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<T> get(@NotNull RegistryKey key) {
|
||||
return registry.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(@NotNull RegistryKey key) {
|
||||
return registry.contains(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEach(@NotNull Consumer<T> consumer) {
|
||||
registry.forEach(consumer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEach(@NotNull BiConsumer<RegistryKey, T> consumer) {
|
||||
registry.forEach(consumer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Collection<T> entries() {
|
||||
return registry.entries();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Set<RegistryKey> keys() {
|
||||
return registry.keys();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeKey<T> getType() {
|
||||
return registry.getType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<RegistryKey, T> getMatches(String id) {
|
||||
return registry.getMatches(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T load(@NotNull AnnotatedType t, @NotNull Object c, @NotNull ConfigLoader loader, DepthTracker depthTracker)
|
||||
throws LoadException {
|
||||
return registry.load(t, c, loader, depthTracker);
|
||||
}
|
||||
}
|
||||
@@ -1,95 +0,0 @@
|
||||
/*
|
||||
* This file is part of Terra.
|
||||
*
|
||||
* Terra is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Terra is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Terra. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.registry;
|
||||
|
||||
import com.dfsek.tectonic.api.depth.DepthTracker;
|
||||
import com.dfsek.tectonic.api.exception.LoadException;
|
||||
import com.dfsek.tectonic.api.loader.ConfigLoader;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.AnnotatedType;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
|
||||
|
||||
/**
|
||||
* Wrapper for a registry that forbids all write access.
|
||||
*
|
||||
* @param <T> Type in registry
|
||||
*/
|
||||
public class LockedRegistryImpl<T> implements Registry<T> {
|
||||
private final Registry<T> registry;
|
||||
|
||||
public LockedRegistryImpl(Registry<T> registry) {
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<T> get(@NotNull RegistryKey key) {
|
||||
return registry.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(@NotNull RegistryKey key) {
|
||||
return registry.contains(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEach(@NotNull Consumer<T> consumer) {
|
||||
registry.forEach(consumer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEach(@NotNull BiConsumer<RegistryKey, T> consumer) {
|
||||
registry.forEach(consumer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Collection<T> entries() {
|
||||
return registry.entries();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Set<RegistryKey> keys() {
|
||||
return registry.keys();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeKey<T> getType() {
|
||||
return registry.getType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<RegistryKey, T> getMatches(String id) {
|
||||
return registry.getMatches(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T load(@NotNull AnnotatedType t, @NotNull Object c, @NotNull ConfigLoader loader, DepthTracker depthTracker)
|
||||
throws LoadException {
|
||||
return registry.load(t, c, loader, depthTracker);
|
||||
}
|
||||
}
|
||||
@@ -1,185 +0,0 @@
|
||||
/*
|
||||
* This file is part of Terra.
|
||||
*
|
||||
* Terra is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Terra is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Terra. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.registry;
|
||||
|
||||
import com.dfsek.tectonic.api.depth.DepthTracker;
|
||||
import com.dfsek.tectonic.api.exception.LoadException;
|
||||
import com.dfsek.tectonic.api.loader.ConfigLoader;
|
||||
import com.google.common.collect.ListMultimap;
|
||||
import com.google.common.collect.Multimaps;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.AnnotatedType;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.dfsek.terra.api.registry.OpenRegistry;
|
||||
import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
import com.dfsek.terra.api.util.generic.pair.Pair;
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
|
||||
|
||||
/**
|
||||
* Registry implementation with read/write access. For internal use only.
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public class OpenRegistryImpl<T> implements OpenRegistry<T> {
|
||||
private static final Entry<?> NULL = new Entry<>(null);
|
||||
private final Map<RegistryKey, Entry<T>> objects;
|
||||
private final ListMultimap<String, Pair<RegistryKey, Entry<T>>> objectIDs = Multimaps.newListMultimap(new HashMap<>(), ArrayList::new);
|
||||
private final TypeKey<T> typeKey;
|
||||
|
||||
public OpenRegistryImpl(TypeKey<T> typeKey) {
|
||||
this(new HashMap<>(), typeKey);
|
||||
}
|
||||
|
||||
protected OpenRegistryImpl(Map<RegistryKey, Entry<T>> init, TypeKey<T> typeKey) {
|
||||
this.objects = init;
|
||||
this.typeKey = typeKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T load(@NotNull AnnotatedType type, @NotNull Object o, @NotNull ConfigLoader configLoader, DepthTracker depthTracker)
|
||||
throws LoadException {
|
||||
return getByID((String) o).orElseThrow(() -> new LoadException("No such " + type.getType().getTypeName() + " matching \"" + o +
|
||||
"\" was found in this registry. Registry contains items: " +
|
||||
getItemsFormatted(), depthTracker));
|
||||
}
|
||||
|
||||
private String getItemsFormatted() {
|
||||
if(objects.isEmpty()) {
|
||||
return "[ ]";
|
||||
}
|
||||
return objects
|
||||
.keySet()
|
||||
.stream()
|
||||
.map(RegistryKey::toString)
|
||||
.sorted()
|
||||
.reduce("", (a, b) -> a + "\n - " + b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean register(@NotNull RegistryKey identifier, @NotNull T value) {
|
||||
return register(identifier, new Entry<>(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerChecked(@NotNull RegistryKey identifier, @NotNull T value) throws DuplicateEntryException {
|
||||
if(objects.containsKey(identifier))
|
||||
throw new DuplicateEntryException("Value with identifier \"" + identifier + "\" is already defined in registry.");
|
||||
register(identifier, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
objects.clear();
|
||||
objectIDs.clear();
|
||||
}
|
||||
|
||||
private boolean register(RegistryKey identifier, Entry<T> value) {
|
||||
boolean exists = objects.containsKey(identifier);
|
||||
objects.put(identifier, value);
|
||||
objectIDs.put(identifier.getID(), Pair.of(identifier, value));
|
||||
return exists;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Optional<T> get(@NotNull RegistryKey key) {
|
||||
return Optional.ofNullable(objects.getOrDefault(key, (Entry<T>) NULL).getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(@NotNull RegistryKey key) {
|
||||
return objects.containsKey(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEach(@NotNull Consumer<T> consumer) {
|
||||
objects.forEach((id, obj) -> consumer.accept(obj.getRaw()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEach(@NotNull BiConsumer<RegistryKey, T> consumer) {
|
||||
objects.forEach((id, entry) -> consumer.accept(id, entry.getRaw()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Collection<T> entries() {
|
||||
return objects.values().stream().map(Entry::getRaw).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Set<RegistryKey> keys() {
|
||||
return objects.keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeKey<T> getType() {
|
||||
return typeKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<RegistryKey, T> getMatches(String id) {
|
||||
return objectIDs
|
||||
.get(id)
|
||||
.stream()
|
||||
.collect(HashMap::new, (map, pair) -> map.put(pair.getLeft(), pair.getRight().getValue()), Map::putAll);
|
||||
}
|
||||
|
||||
public Map<RegistryKey, T> getDeadEntries() {
|
||||
Map<RegistryKey, T> dead = new HashMap<>();
|
||||
objects.forEach((id, entry) -> {
|
||||
if(entry.dead()) dead.put(id, entry.value); // dont increment value here.
|
||||
});
|
||||
return dead;
|
||||
}
|
||||
|
||||
|
||||
private static final class Entry<T> {
|
||||
private final T value;
|
||||
private final AtomicInteger access = new AtomicInteger(0);
|
||||
|
||||
public Entry(T value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public boolean dead() {
|
||||
return access.get() == 0;
|
||||
}
|
||||
|
||||
public T getValue() {
|
||||
access.incrementAndGet();
|
||||
return value;
|
||||
}
|
||||
|
||||
private T getRaw() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package com.dfsek.terra.registry;
|
||||
|
||||
import com.dfsek.tectonic.api.depth.DepthTracker;
|
||||
import com.dfsek.tectonic.api.exception.LoadException;
|
||||
import com.dfsek.tectonic.api.loader.ConfigLoader;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.AnnotatedType;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import com.dfsek.terra.api.registry.Registry;
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
|
||||
|
||||
public class RegistryImpl<T> implements Registry<T> {
|
||||
private final io.vavr.collection.Map<RegistryKey, T> contents;
|
||||
private final TypeKey<T> type;
|
||||
|
||||
private RegistryImpl(io.vavr.collection.Map<RegistryKey, T> contents, TypeKey<T> type) {
|
||||
this.contents = contents;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public static <T> RegistryImpl<T> empty(TypeKey<T> key) {
|
||||
return new RegistryImpl<>(io.vavr.collection.HashMap.empty(), key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<T> get(@NotNull RegistryKey key) {
|
||||
return contents.get(key).toJavaOptional();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(@NotNull RegistryKey key) {
|
||||
return contents.containsKey(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEach(@NotNull Consumer<T> consumer) {
|
||||
contents.values().forEach(consumer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEach(@NotNull BiConsumer<RegistryKey, T> consumer) {
|
||||
contents.forEach(consumer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Set<T> entries() {
|
||||
return contents.values().toJavaSet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Registry<T> register(@NotNull RegistryKey identifier, @NotNull T value) {
|
||||
return new RegistryImpl<>(contents.put(identifier, value), type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Set<RegistryKey> keys() {
|
||||
return contents.keySet().toJavaSet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeKey<T> getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<RegistryKey, T> getID(String id) {
|
||||
return contents.filterKeys(key -> key.getID().equals(id)).toJavaMap();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<RegistryKey, T> getNamespace(String namespace) {
|
||||
return contents.filterKeys(key -> key.getNamespace().equals(namespace)).toJavaMap();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public T load(@NotNull AnnotatedType annotatedType, @NotNull Object o, @NotNull ConfigLoader configLoader, DepthTracker depthTracker)
|
||||
throws LoadException {
|
||||
return get(RegistryKey.parse((String) o)).orElseThrow(() -> new LoadException("No such " + type.getType().getTypeName() + " matching \"" + o +
|
||||
"\" was found in this registry. Registry contains items: " +
|
||||
getItemsFormatted(), depthTracker));
|
||||
|
||||
}
|
||||
|
||||
private String getItemsFormatted() {
|
||||
if(contents.isEmpty()) {
|
||||
return "[ ]";
|
||||
}
|
||||
return contents
|
||||
.keySet()
|
||||
.map(RegistryKey::toString)
|
||||
.toSortedSet()
|
||||
.reduce((a, b) -> a + "\n - " + b);
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,9 @@
|
||||
package com.dfsek.terra.registry.master;
|
||||
|
||||
import com.dfsek.tectonic.api.exception.ConfigException;
|
||||
|
||||
import com.dfsek.terra.registry.RegistryImpl;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -30,13 +33,12 @@ import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
import com.dfsek.terra.config.pack.ConfigPackImpl;
|
||||
import com.dfsek.terra.registry.OpenRegistryImpl;
|
||||
|
||||
|
||||
/**
|
||||
* Class to hold config packs
|
||||
*/
|
||||
public class ConfigRegistry extends OpenRegistryImpl<ConfigPack> {
|
||||
public class ConfigRegistry extends RegistryImpl<ConfigPack> {
|
||||
private static final Logger logger = LoggerFactory.getLogger(ConfigRegistry.class);
|
||||
|
||||
public ConfigRegistry() {
|
||||
|
||||
@@ -1,96 +0,0 @@
|
||||
/*
|
||||
* This file is part of Terra.
|
||||
*
|
||||
* Terra is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Terra is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Terra. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package registry;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.registry.OpenRegistry;
|
||||
import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
import com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
import com.dfsek.terra.registry.CheckedRegistryImpl;
|
||||
import com.dfsek.terra.registry.OpenRegistryImpl;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
|
||||
public class RegistryTest {
|
||||
@Test
|
||||
public void openRegistry() {
|
||||
OpenRegistry<String> test = new OpenRegistryImpl<>(TypeKey.of(String.class));
|
||||
|
||||
test.register(RegistryKey.parse("test:test"), "bazinga");
|
||||
|
||||
assertEquals(test.get(RegistryKey.parse("test:test")).orElseThrow(), "bazinga");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void openRegistryChecked() {
|
||||
OpenRegistry<String> test = new OpenRegistryImpl<>(TypeKey.of(String.class));
|
||||
|
||||
test.registerChecked(RegistryKey.parse("test:test"), "bazinga");
|
||||
|
||||
try {
|
||||
test.registerChecked(RegistryKey.parse("test:test"), "bazinga2");
|
||||
fail("Shouldn't be able to re-register with #registerChecked!");
|
||||
} catch(DuplicateEntryException ignore) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkedRegistry() {
|
||||
CheckedRegistry<String> test = new CheckedRegistryImpl<>(new OpenRegistryImpl<>(TypeKey.of(String.class)));
|
||||
|
||||
test.register(RegistryKey.parse("test:test"), "bazinga");
|
||||
|
||||
assertEquals(test.get(RegistryKey.parse("test:test")).orElseThrow(), "bazinga");
|
||||
|
||||
try {
|
||||
test.register(RegistryKey.parse("test:test"), "bazinga2");
|
||||
fail("Shouldn't be able to re-register in CheckedRegistry!");
|
||||
} catch(DuplicateEntryException ignore) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getID() {
|
||||
OpenRegistry<String> test = new OpenRegistryImpl<>(TypeKey.of(String.class));
|
||||
|
||||
test.register(RegistryKey.parse("test:test"), "bazinga");
|
||||
|
||||
assertEquals(test.getByID("test").orElseThrow(), "bazinga");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getIDAmbiguous() {
|
||||
OpenRegistry<String> test = new OpenRegistryImpl<>(TypeKey.of(String.class));
|
||||
|
||||
test.registerChecked(RegistryKey.parse("test:test"), "bazinga");
|
||||
test.registerChecked(RegistryKey.parse("test2:test"), "bazinga");
|
||||
|
||||
try {
|
||||
test.getByID("test");
|
||||
fail("Shouldn't be able to get with ambiguous ID!");
|
||||
} catch(IllegalArgumentException ignore) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user