create config type loading events

This commit is contained in:
dfsek
2021-07-15 14:40:00 -07:00
parent cb4b537a2f
commit d94ddb3e76
5 changed files with 53 additions and 1 deletions

View File

@@ -4,7 +4,7 @@ import com.dfsek.tectonic.config.ConfigTemplate;
import com.dfsek.terra.api.config.ConfigPack;
/**
* Called before a config pack's registries are filled. At this point, the pack manifest has been loaded, and all registries are empty.
* Called before a config pack's registries are filled.
*/
public class ConfigPackPreLoadEvent extends ConfigPackLoadEvent {
public ConfigPackPreLoadEvent(ConfigPack pack, ExceptionalConsumer<ConfigTemplate> configLoader) {

View File

@@ -0,0 +1,28 @@
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.event.events.PackEvent;
import com.dfsek.terra.api.registry.CheckedRegistry;
public abstract class ConfigTypeLoadEvent implements PackEvent {
private final ConfigType<?, ?> type;
private final CheckedRegistry<?> registry;
public ConfigTypeLoadEvent(ConfigType<?, ?> type, CheckedRegistry<?> registry) {
this.type = type;
this.registry = registry;
}
@Override
public ConfigPack getPack() {
return null;
}
@SuppressWarnings("unchecked")
public <T> CheckedRegistry<T> getRegistry(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 (CheckedRegistry<T>) registry;
}
}

View File

@@ -0,0 +1,10 @@
package com.dfsek.terra.api.event.events.config.type;
import com.dfsek.terra.api.config.ConfigType;
import com.dfsek.terra.api.registry.CheckedRegistry;
public class ConfigTypePostLoadEvent extends ConfigTypeLoadEvent{
public ConfigTypePostLoadEvent(ConfigType<?, ?> type, CheckedRegistry<?> registry) {
super(type, registry);
}
}

View File

@@ -0,0 +1,10 @@
package com.dfsek.terra.api.event.events.config.type;
import com.dfsek.terra.api.config.ConfigType;
import com.dfsek.terra.api.registry.CheckedRegistry;
public class ConfigTypePreLoadEvent extends ConfigTypeLoadEvent{
public ConfigTypePreLoadEvent(ConfigType<?, ?> type, CheckedRegistry<?> registry) {
super(type, registry);
}
}

View File

@@ -22,6 +22,8 @@ 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.event.events.config.type.ConfigTypePostLoadEvent;
import com.dfsek.terra.api.event.events.config.type.ConfigTypePreLoadEvent;
import com.dfsek.terra.api.registry.CheckedRegistry;
import com.dfsek.terra.api.registry.OpenRegistry;
import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
@@ -244,6 +246,7 @@ public class ConfigPackImpl implements ConfigPack {
for(ConfigType<?, ?> configType : configTypeRegistry.entries()) { // Load the configs
CheckedRegistry registry = getCheckedRegistry(configType.getTypeClass());
main.getEventManager().callEvent(new ConfigTypePreLoadEvent(configType, registry));
for(AbstractConfiguration config : abstractConfigLoader.loadConfigs(configs.getOrDefault(configType, Collections.emptyList()))) {
try {
Object loaded = ((ConfigFactory) configType.getFactory()).build(selfLoader.load(configType.getTemplate(this, main), config), main);
@@ -253,6 +256,7 @@ public class ConfigPackImpl implements ConfigPack {
throw new LoadException("Duplicate registry entry: ", e);
}
}
main.getEventManager().callEvent(new ConfigTypePostLoadEvent(configType, registry));
}
main.getEventManager().callEvent(new ConfigPackPostLoadEvent(this, template -> selfLoader.load(template, configuration)));