mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 10:30:42 +00:00
document and refactor config events
This commit is contained in:
@@ -19,7 +19,7 @@ import com.dfsek.terra.api.addon.annotations.Addon;
|
||||
import com.dfsek.terra.api.addon.annotations.Author;
|
||||
import com.dfsek.terra.api.addon.annotations.Version;
|
||||
import com.dfsek.terra.api.event.EventListener;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.injection.annotations.Inject;
|
||||
import com.dfsek.terra.api.util.seeded.BiomeProviderBuilder;
|
||||
import com.dfsek.terra.api.util.seeded.SourceSeeded;
|
||||
|
||||
@@ -7,7 +7,7 @@ import com.dfsek.terra.api.addon.annotations.Addon;
|
||||
import com.dfsek.terra.api.addon.annotations.Author;
|
||||
import com.dfsek.terra.api.addon.annotations.Version;
|
||||
import com.dfsek.terra.api.event.EventListener;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.injection.annotations.Inject;
|
||||
import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
|
||||
import com.dfsek.terra.api.world.generator.ChunkGeneratorProvider;
|
||||
|
||||
@@ -9,7 +9,7 @@ import com.dfsek.terra.api.addon.annotations.Addon;
|
||||
import com.dfsek.terra.api.addon.annotations.Author;
|
||||
import com.dfsek.terra.api.addon.annotations.Version;
|
||||
import com.dfsek.terra.api.event.EventListener;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.injection.annotations.Inject;
|
||||
|
||||
@Addon("config-biome")
|
||||
|
||||
@@ -9,8 +9,8 @@ import com.dfsek.terra.api.addon.annotations.Addon;
|
||||
import com.dfsek.terra.api.addon.annotations.Author;
|
||||
import com.dfsek.terra.api.addon.annotations.Version;
|
||||
import com.dfsek.terra.api.event.EventListener;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigurationLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.injection.annotations.Inject;
|
||||
import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
|
||||
import com.dfsek.terra.api.util.seeded.BiomeBuilder;
|
||||
@@ -43,7 +43,7 @@ public class FloraAddon extends TerraAddon implements EventListener {
|
||||
.applyLoader(BlockLayer.class, BlockLayerTemplate::new);
|
||||
}
|
||||
|
||||
public void onBiomeLoad(ConfigLoadEvent event) {
|
||||
public void onBiomeLoad(ConfigurationLoadEvent event) {
|
||||
if(BiomeBuilder.class.isAssignableFrom(event.getType().getTypeClass())) {
|
||||
flora.put(event.getConfiguration().getID(), event.load(new BiomeFloraTemplate()).getFlora());
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ import com.dfsek.terra.api.addon.annotations.Addon;
|
||||
import com.dfsek.terra.api.addon.annotations.Author;
|
||||
import com.dfsek.terra.api.addon.annotations.Version;
|
||||
import com.dfsek.terra.api.event.EventListener;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.injection.annotations.Inject;
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.util.seeded.NoiseProvider;
|
||||
|
||||
@@ -6,7 +6,7 @@ import com.dfsek.terra.api.addon.annotations.Addon;
|
||||
import com.dfsek.terra.api.addon.annotations.Author;
|
||||
import com.dfsek.terra.api.addon.annotations.Version;
|
||||
import com.dfsek.terra.api.event.EventListener;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.injection.annotations.Inject;
|
||||
import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
|
||||
import com.dfsek.terra.api.world.generator.GenerationStageProvider;
|
||||
|
||||
@@ -8,7 +8,7 @@ import com.dfsek.terra.api.addon.annotations.Addon;
|
||||
import com.dfsek.terra.api.addon.annotations.Author;
|
||||
import com.dfsek.terra.api.addon.annotations.Version;
|
||||
import com.dfsek.terra.api.event.EventListener;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.injection.annotations.Inject;
|
||||
|
||||
@Addon("config-palette")
|
||||
|
||||
@@ -6,7 +6,7 @@ import com.dfsek.terra.api.addon.annotations.Addon;
|
||||
import com.dfsek.terra.api.addon.annotations.Author;
|
||||
import com.dfsek.terra.api.addon.annotations.Version;
|
||||
import com.dfsek.terra.api.event.EventListener;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.injection.annotations.Inject;
|
||||
import com.dfsek.terra.api.structure.ConfiguredStructure;
|
||||
|
||||
|
||||
@@ -8,8 +8,8 @@ import com.dfsek.terra.api.addon.annotations.Addon;
|
||||
import com.dfsek.terra.api.addon.annotations.Author;
|
||||
import com.dfsek.terra.api.addon.annotations.Version;
|
||||
import com.dfsek.terra.api.event.EventListener;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigurationLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.injection.annotations.Inject;
|
||||
import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
|
||||
import com.dfsek.terra.api.util.seeded.BiomeBuilder;
|
||||
@@ -41,7 +41,7 @@ public class TreeAddon extends TerraAddon implements EventListener {
|
||||
event.getPack().applyLoader(TreeLayer.class, TreeLayerTemplate::new);
|
||||
}
|
||||
|
||||
public void onBiomeLoad(ConfigLoadEvent event) {
|
||||
public void onBiomeLoad(ConfigurationLoadEvent event) {
|
||||
if(BiomeBuilder.class.isAssignableFrom(event.getType().getTypeClass())) {
|
||||
trees.put(event.getConfiguration().getID(), event.load(new BiomeTreeTemplate()).getTrees());
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import com.dfsek.terra.api.addon.annotations.Addon;
|
||||
import com.dfsek.terra.api.addon.annotations.Author;
|
||||
import com.dfsek.terra.api.addon.annotations.Version;
|
||||
import com.dfsek.terra.api.event.EventListener;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigurationLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigurationDiscoveryEvent;
|
||||
import com.dfsek.terra.api.injection.annotations.Inject;
|
||||
|
||||
@Addon("language-yaml")
|
||||
@@ -22,7 +22,7 @@ public class YamlAddon extends TerraAddon implements EventListener {
|
||||
main.getEventManager().registerListener(this, this);
|
||||
}
|
||||
|
||||
public void loadYamlConfigs(ConfigurationLoadEvent event) {
|
||||
public void loadYamlConfigs(ConfigurationDiscoveryEvent event) {
|
||||
event.getLoader().open("", ".yml").thenEntries(entries -> entries.forEach(entry -> event.register(new YamlConfiguration(entry.getValue(), entry.getKey()))));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import com.dfsek.terra.api.addon.annotations.Addon;
|
||||
import com.dfsek.terra.api.addon.annotations.Author;
|
||||
import com.dfsek.terra.api.addon.annotations.Version;
|
||||
import com.dfsek.terra.api.event.EventListener;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.injection.annotations.Inject;
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
package com.dfsek.terra.api.event.events.config;
|
||||
|
||||
import com.dfsek.tectonic.abstraction.AbstractConfiguration;
|
||||
import com.dfsek.tectonic.config.ConfigTemplate;
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.config.ConfigType;
|
||||
import com.dfsek.terra.api.event.events.PackEvent;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Fired when each individual configuration is loaded.
|
||||
*/
|
||||
public class ConfigLoadEvent implements PackEvent {
|
||||
private final ConfigPack pack;
|
||||
private final AbstractConfiguration configuration;
|
||||
private final Consumer<ConfigTemplate> loader;
|
||||
private final ConfigType<?, ?> type;
|
||||
|
||||
private final Object loaded;
|
||||
|
||||
public ConfigLoadEvent(ConfigPack pack, AbstractConfiguration configuration, Consumer<ConfigTemplate> loader, ConfigType<?, ?> type, Object loaded) {
|
||||
this.pack = pack;
|
||||
this.configuration = configuration;
|
||||
this.loader = loader;
|
||||
this.type = type;
|
||||
this.loaded = loaded;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConfigPack getPack() {
|
||||
return pack;
|
||||
}
|
||||
|
||||
public AbstractConfiguration getConfiguration() {
|
||||
return configuration;
|
||||
}
|
||||
|
||||
public <T extends ConfigTemplate> T load(T template) {
|
||||
loader.accept(template);
|
||||
return template;
|
||||
}
|
||||
|
||||
public ConfigType<?, ?> getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T getLoadedObject(Class<T> clazz) {
|
||||
if(!clazz.isAssignableFrom(type.getTypeClass())) throw new ClassCastException("Cannot assign object from loader of type " + type.getTypeClass().getCanonicalName() + " to class " + clazz.getCanonicalName());
|
||||
return (T) loaded;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.dfsek.terra.api.event.events.config;
|
||||
|
||||
import com.dfsek.tectonic.config.Configuration;
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.config.Loader;
|
||||
import com.dfsek.terra.api.event.events.PackEvent;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Fired when a pack is searched for {@link Configuration}s.
|
||||
* <p>
|
||||
* Addons should listen to this event if they wish to add
|
||||
* another configuration format.
|
||||
*/
|
||||
public class ConfigurationDiscoveryEvent implements PackEvent {
|
||||
private final ConfigPack pack;
|
||||
private final Loader loader;
|
||||
|
||||
private final Consumer<Configuration> consumer;
|
||||
|
||||
public ConfigurationDiscoveryEvent(ConfigPack pack, Loader loader, Consumer<Configuration> consumer) {
|
||||
this.pack = pack;
|
||||
this.loader = loader;
|
||||
this.consumer = consumer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConfigPack getPack() {
|
||||
return pack;
|
||||
}
|
||||
|
||||
public Loader getLoader() {
|
||||
return loader;
|
||||
}
|
||||
|
||||
public void register(Configuration config) {
|
||||
consumer.accept(config);
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,33 @@
|
||||
package com.dfsek.terra.api.event.events.config;
|
||||
|
||||
import com.dfsek.tectonic.config.Configuration;
|
||||
import com.dfsek.tectonic.abstraction.AbstractConfiguration;
|
||||
import com.dfsek.tectonic.config.ConfigTemplate;
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.config.Loader;
|
||||
import com.dfsek.terra.api.config.ConfigType;
|
||||
import com.dfsek.terra.api.event.events.PackEvent;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Fired when each individual configuration is loaded.
|
||||
* <p>
|
||||
* Addons should listen to this event if they wish to add
|
||||
* config values to existing {@link ConfigType}s.
|
||||
*/
|
||||
public class ConfigurationLoadEvent implements PackEvent {
|
||||
private final ConfigPack pack;
|
||||
private final Loader loader;
|
||||
private final AbstractConfiguration configuration;
|
||||
private final Consumer<ConfigTemplate> loader;
|
||||
private final ConfigType<?, ?> type;
|
||||
|
||||
private final Consumer<Configuration> consumer;
|
||||
private final Object loaded;
|
||||
|
||||
public ConfigurationLoadEvent(ConfigPack pack, Loader loader, Consumer<Configuration> consumer) {
|
||||
public ConfigurationLoadEvent(ConfigPack pack, AbstractConfiguration configuration, Consumer<ConfigTemplate> loader, ConfigType<?, ?> type, Object loaded) {
|
||||
this.pack = pack;
|
||||
this.configuration = configuration;
|
||||
this.loader = loader;
|
||||
this.consumer = consumer;
|
||||
this.type = type;
|
||||
this.loaded = loaded;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -24,11 +35,23 @@ public class ConfigurationLoadEvent implements PackEvent {
|
||||
return pack;
|
||||
}
|
||||
|
||||
public Loader getLoader() {
|
||||
return loader;
|
||||
public AbstractConfiguration getConfiguration() {
|
||||
return configuration;
|
||||
}
|
||||
|
||||
public void register(Configuration config) {
|
||||
consumer.accept(config);
|
||||
public <T extends ConfigTemplate> T load(T template) {
|
||||
loader.accept(template);
|
||||
return template;
|
||||
}
|
||||
|
||||
public ConfigType<?, ?> getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T getLoadedObject(Class<T> clazz) {
|
||||
if(!clazz.isAssignableFrom(type.getTypeClass()))
|
||||
throw new ClassCastException("Cannot assign object from loader of type " + type.getTypeClass().getCanonicalName() + " to class " + clazz.getCanonicalName());
|
||||
return (T) loaded;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.dfsek.terra.api.event.events.config;
|
||||
package com.dfsek.terra.api.event.events.config.pack;
|
||||
|
||||
import com.dfsek.tectonic.config.ConfigTemplate;
|
||||
import com.dfsek.tectonic.exception.ConfigException;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.dfsek.terra.api.event.events.config;
|
||||
package com.dfsek.terra.api.event.events.config.pack;
|
||||
|
||||
import com.dfsek.tectonic.config.ConfigTemplate;
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.dfsek.terra.api.event.events.config;
|
||||
package com.dfsek.terra.api.event.events.config.pack;
|
||||
|
||||
import com.dfsek.tectonic.config.ConfigTemplate;
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
@@ -18,10 +18,10 @@ import com.dfsek.terra.api.config.ConfigFactory;
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.config.ConfigType;
|
||||
import com.dfsek.terra.api.config.Loader;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigPackPostLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigurationLoadEvent;
|
||||
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.ConfigurationDiscoveryEvent;
|
||||
import com.dfsek.terra.api.registry.CheckedRegistry;
|
||||
import com.dfsek.terra.api.registry.OpenRegistry;
|
||||
import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
|
||||
@@ -232,7 +232,7 @@ public class ConfigPackImpl implements ConfigPack {
|
||||
|
||||
List<Configuration> configurations = new ArrayList<>();
|
||||
|
||||
main.getEventManager().callEvent(new ConfigurationLoadEvent(this, loader, configurations::add)); // Create all the configs.
|
||||
main.getEventManager().callEvent(new ConfigurationDiscoveryEvent(this, loader, configurations::add)); // Create all the configs.
|
||||
|
||||
Map<ConfigType<? extends ConfigTemplate, ?>, List<Configuration>> configs = new HashMap<>();
|
||||
|
||||
@@ -248,7 +248,7 @@ public class ConfigPackImpl implements ConfigPack {
|
||||
try {
|
||||
Object loaded = ((ConfigFactory) configType.getFactory()).build(selfLoader.load(configType.getTemplate(this, main), config), main);
|
||||
registry.register(config.getID(), loaded);
|
||||
main.getEventManager().callEvent(new ConfigLoadEvent(this, config, template -> selfLoader.load(template, config), configType, loaded));
|
||||
main.getEventManager().callEvent(new ConfigurationLoadEvent(this, config, template -> selfLoader.load(template, config), configType, loaded));
|
||||
} catch(DuplicateEntryException e) {
|
||||
throw new LoadException("Duplicate registry entry: ", e);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import com.dfsek.terra.api.TerraPlugin;
|
||||
import com.dfsek.terra.api.event.EventListener;
|
||||
import com.dfsek.terra.api.event.annotations.Global;
|
||||
import com.dfsek.terra.api.event.annotations.Priority;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
|
||||
import com.dfsek.terra.api.world.Tree;
|
||||
import com.dfsek.terra.bukkit.world.BukkitAdapter;
|
||||
|
||||
@@ -2,9 +2,7 @@ package com.dfsek.terra.fabric;
|
||||
|
||||
import com.dfsek.tectonic.exception.ConfigException;
|
||||
import com.dfsek.tectonic.exception.LoadException;
|
||||
import com.dfsek.tectonic.loading.TypeLoader;
|
||||
import com.dfsek.tectonic.loading.TypeRegistry;
|
||||
import com.dfsek.tectonic.loading.object.ObjectTemplate;
|
||||
import com.dfsek.terra.api.Logger;
|
||||
import com.dfsek.terra.api.TerraPlugin;
|
||||
import com.dfsek.terra.api.addon.TerraAddon;
|
||||
@@ -20,8 +18,8 @@ import com.dfsek.terra.api.event.EventListener;
|
||||
import com.dfsek.terra.api.event.EventManager;
|
||||
import com.dfsek.terra.api.event.annotations.Global;
|
||||
import com.dfsek.terra.api.event.annotations.Priority;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigPackPostLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigPackPreLoadEvent;
|
||||
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.handle.ItemHandle;
|
||||
import com.dfsek.terra.api.handle.WorldHandle;
|
||||
import com.dfsek.terra.api.lang.Language;
|
||||
@@ -79,10 +77,8 @@ import org.apache.logging.log4j.LogManager;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
||||
public class TerraFabricPlugin implements TerraPlugin, ModInitializer {
|
||||
|
||||
Reference in New Issue
Block a user