Fixed data reloading
And string data caching
This commit is contained in:
parent
788c2e8a87
commit
cbbb5572b8
4 changed files with 21 additions and 30 deletions
|
@ -32,7 +32,7 @@ public abstract class ButtonPlugin extends JavaPlugin {
|
||||||
try {
|
try {
|
||||||
pluginDisable();
|
pluginDisable();
|
||||||
saveConfig();
|
saveConfig();
|
||||||
iConfig.resetConfigurationCache();
|
iConfig = null; //Clearing the hashmap is not enough, we need to update the section as well
|
||||||
TBMCChatAPI.RemoveCommands(this);
|
TBMCChatAPI.RemoveCommands(this);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
TBMCCoreAPI.SendException("Error while disabling plugin " + getName() + "!", e);
|
TBMCCoreAPI.SendException("Error while disabling plugin " + getName() + "!", e);
|
||||||
|
|
|
@ -9,7 +9,6 @@ import lombok.Getter;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import lombok.experimental.var;
|
import lombok.experimental.var;
|
||||||
import lombok.val;
|
import lombok.val;
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
@ -29,8 +28,6 @@ public abstract class Component {
|
||||||
@NonNull
|
@NonNull
|
||||||
private JavaPlugin plugin;
|
private JavaPlugin plugin;
|
||||||
@NonNull
|
@NonNull
|
||||||
private ConfigurationSection configSect;
|
|
||||||
@NonNull
|
|
||||||
private @Getter
|
private @Getter
|
||||||
IHaveConfig config;
|
IHaveConfig config;
|
||||||
|
|
||||||
|
@ -78,15 +75,7 @@ public abstract class Component {
|
||||||
}
|
}
|
||||||
if (register) {
|
if (register) {
|
||||||
component.plugin = plugin;
|
component.plugin = plugin;
|
||||||
if (plugin.getConfig() != null) { //Production
|
updateConfig(plugin, component);
|
||||||
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);
|
|
||||||
component.register(plugin);
|
component.register(plugin);
|
||||||
components.put(component.getClass(), component);
|
components.put(component.getClass(), component);
|
||||||
if (ComponentManager.areComponentsEnabled() && component.shouldBeEnabled().get()) {
|
if (ComponentManager.areComponentsEnabled() && component.shouldBeEnabled().get()) {
|
||||||
|
@ -128,16 +117,28 @@ public abstract class Component {
|
||||||
if (!components.containsKey(component.getClass()))
|
if (!components.containsKey(component.getClass()))
|
||||||
throw new UnregisteredComponentException(component);
|
throw new UnregisteredComponentException(component);
|
||||||
if (component.enabled == enabled) return; //Don't do anything
|
if (component.enabled == enabled) return; //Don't do anything
|
||||||
if (component.enabled = enabled)
|
if (component.enabled = enabled) {
|
||||||
|
updateConfig(component.getPlugin(), component);
|
||||||
component.enable();
|
component.enable();
|
||||||
else {
|
} else {
|
||||||
component.disable();
|
component.disable();
|
||||||
component.plugin.saveConfig();
|
component.plugin.saveConfig();
|
||||||
component.config.resetConfigurationCache();
|
|
||||||
TBMCChatAPI.RemoveCommands(component);
|
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>
|
* Returns the currently registered components<br>
|
||||||
*
|
*
|
||||||
|
|
|
@ -5,7 +5,7 @@ import lombok.RequiredArgsConstructor;
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import java.util.Objects;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -20,7 +20,7 @@ public class ConfigData<T> { //TODO: Save after a while
|
||||||
*/
|
*/
|
||||||
private final ConfigurationSection config;
|
private final ConfigurationSection config;
|
||||||
private final String path;
|
private final String path;
|
||||||
private @Nullable final T def;
|
private final T def;
|
||||||
private final Object primitiveDef;
|
private final Object primitiveDef;
|
||||||
/**
|
/**
|
||||||
* The parameter is of a primitive type as returned by {@link YamlConfiguration#get(String)}
|
* 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) {
|
if (val == null) {
|
||||||
val = primitiveDef;
|
val = primitiveDef;
|
||||||
}
|
}
|
||||||
if (val == primitiveDef && !saved) {
|
if (!saved && Objects.equals(val, primitiveDef)) { //String needs .equals()
|
||||||
if (def != null)
|
set(def); //Save default value - def is always set
|
||||||
set(def); //Save default value
|
|
||||||
else if (config != null) //config==null: testing
|
|
||||||
config.set(path, primitiveDef);
|
|
||||||
saved = true;
|
saved = true;
|
||||||
}
|
}
|
||||||
if (getter != null) {
|
if (getter != null) {
|
||||||
|
|
|
@ -71,11 +71,4 @@ public final class IHaveConfig {
|
||||||
datamap.put(path, data = new ConfigData<>(config, path, getter.apply(primitiveDef), primitiveDef, getter, setter));
|
datamap.put(path, data = new ConfigData<>(config, path, getter.apply(primitiveDef), primitiveDef, getter, setter));
|
||||||
return (ConfigData<T>) data;
|
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue