document and refactor config events

This commit is contained in:
dfsek
2021-07-15 14:34:48 -07:00
parent 2705e7d6a2
commit cb4b537a2f
20 changed files with 100 additions and 94 deletions

View File

@@ -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;
}
}

View File

@@ -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);
}
}

View File

@@ -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;
}
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;