Components, components everywhere #56

Merged
NorbiPeti merged 7 commits from dev into master 2019-01-20 21:36:56 +00:00
4 changed files with 21 additions and 30 deletions
Showing only changes of commit cbbb5572b8 - Show all commits

View file

@ -32,7 +32,7 @@ public abstract class ButtonPlugin extends JavaPlugin {
try {
pluginDisable();
saveConfig();
iConfig.resetConfigurationCache();
iConfig = null; //Clearing the hashmap is not enough, we need to update the section as well
TBMCChatAPI.RemoveCommands(this);
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while disabling plugin " + getName() + "!", e);

View file

@ -9,7 +9,6 @@ import lombok.Getter;
import lombok.NonNull;
import lombok.experimental.var;
import lombok.val;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
@ -29,8 +28,6 @@ public abstract class Component {
@NonNull
private JavaPlugin plugin;
@NonNull
private ConfigurationSection configSect;
@NonNull
private @Getter
IHaveConfig config;
@ -78,15 +75,7 @@ public abstract class Component {
}
if (register) {
component.plugin = plugin;
if (plugin.getConfig() != null) { //Production
var compconf = plugin.getConfig().getConfigurationSection("components");
if (compconf == null) compconf = plugin.getConfig().createSection("components");
component.configSect = compconf.getConfigurationSection(component.getClassName());
if (component.configSect == null)
component.configSect = compconf.createSection(component.getClassName());
component.config = new IHaveConfig(component.configSect);
} else //Testing
component.config = new IHaveConfig(null);
updateConfig(plugin, component);
component.register(plugin);
components.put(component.getClass(), component);
if (ComponentManager.areComponentsEnabled() && component.shouldBeEnabled().get()) {
@ -128,16 +117,28 @@ public abstract class Component {
if (!components.containsKey(component.getClass()))
throw new UnregisteredComponentException(component);
if (component.enabled == enabled) return; //Don't do anything
if (component.enabled = enabled)
if (component.enabled = enabled) {
updateConfig(component.getPlugin(), component);
component.enable();
else {
} else {
component.disable();
component.plugin.saveConfig();
component.config.resetConfigurationCache();
TBMCChatAPI.RemoveCommands(component);
}
}
private static void updateConfig(JavaPlugin plugin, Component component) {
if (plugin.getConfig() != null) { //Production
var compconf = plugin.getConfig().getConfigurationSection("components");
if (compconf == null) compconf = plugin.getConfig().createSection("components");
var configSect = compconf.getConfigurationSection(component.getClassName());
if (configSect == null)
configSect = compconf.createSection(component.getClassName());
component.config = new IHaveConfig(configSect);
} else //Testing
component.config = new IHaveConfig(null);
}
/**
* Returns the currently registered components<br>
*

View file

@ -5,7 +5,7 @@ import lombok.RequiredArgsConstructor;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import javax.annotation.Nullable;
import java.util.Objects;
import java.util.function.Function;
/**
@ -20,7 +20,7 @@ public class ConfigData<T> { //TODO: Save after a while
*/
private final ConfigurationSection config;
private final String path;
private @Nullable final T def;
private final T def;
private final Object primitiveDef;
/**
* The parameter is of a primitive type as returned by {@link YamlConfiguration#get(String)}
@ -53,11 +53,8 @@ public class ConfigData<T> { //TODO: Save after a while
if (val == null) {
val = primitiveDef;
}
if (val == primitiveDef && !saved) {
if (def != null)
set(def); //Save default value
else if (config != null) //config==null: testing
config.set(path, primitiveDef);
if (!saved && Objects.equals(val, primitiveDef)) { //String needs .equals()
set(def); //Save default value - def is always set
saved = true;
}
if (getter != null) {

View file

@ -71,11 +71,4 @@ public final class IHaveConfig {
datamap.put(path, data = new ConfigData<>(config, path, getter.apply(primitiveDef), primitiveDef, getter, setter));
return (ConfigData<T>) data;
}
/**
* Deletes the config cache. Call this on component/plugin disable so that new values are read.
*/ //Currently only the /component command reloads the config, otherwise this doesn't really have effect
public void resetConfigurationCache() {
datamap.clear();
}
}