Config fixes, reusing the config objects
This commit is contained in:
parent
b89391f84c
commit
9923f26698
4 changed files with 27 additions and 24 deletions
|
@ -25,9 +25,9 @@ import java.util.Stack;
|
||||||
@HasConfig(global = true)
|
@HasConfig(global = true)
|
||||||
public abstract class ButtonPlugin extends JavaPlugin {
|
public abstract class ButtonPlugin extends JavaPlugin {
|
||||||
@Getter //Needs to be static as we don't know the plugin when a command is handled
|
@Getter //Needs to be static as we don't know the plugin when a command is handled
|
||||||
private static Command2MC command2MC = new Command2MC();
|
private static final Command2MC command2MC = new Command2MC();
|
||||||
@Getter(AccessLevel.PROTECTED)
|
@Getter(AccessLevel.PROTECTED)
|
||||||
private IHaveConfig iConfig;
|
private final IHaveConfig iConfig = new IHaveConfig(this::saveConfig);
|
||||||
private CommentedConfiguration yaml;
|
private CommentedConfiguration yaml;
|
||||||
@Getter(AccessLevel.PROTECTED)
|
@Getter(AccessLevel.PROTECTED)
|
||||||
private IHaveConfig data; //TODO
|
private IHaveConfig data; //TODO
|
||||||
|
@ -35,8 +35,7 @@ public abstract class ButtonPlugin extends JavaPlugin {
|
||||||
* Used to unregister components in the right order - and to reload configs
|
* Used to unregister components in the right order - and to reload configs
|
||||||
*/
|
*/
|
||||||
@Getter
|
@Getter
|
||||||
private Stack<Component<?>> componentStack = new Stack<>();
|
private final Stack<Component<?>> componentStack = new Stack<>();
|
||||||
;
|
|
||||||
|
|
||||||
protected abstract void pluginEnable();
|
protected abstract void pluginEnable();
|
||||||
|
|
||||||
|
@ -72,8 +71,7 @@ public abstract class ButtonPlugin extends JavaPlugin {
|
||||||
return false;
|
return false;
|
||||||
var section = config.getConfigurationSection("global");
|
var section = config.getConfigurationSection("global");
|
||||||
if (section == null) section = config.createSection("global");
|
if (section == null) section = config.createSection("global");
|
||||||
if (iConfig != null) iConfig.reset(section);
|
iConfig.reset(section);
|
||||||
else iConfig = new IHaveConfig(section, this::saveConfig);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,7 +83,6 @@ public abstract class ButtonPlugin extends JavaPlugin {
|
||||||
pluginDisable();
|
pluginDisable();
|
||||||
if (ConfigData.saveNow(getConfig()))
|
if (ConfigData.saveNow(getConfig()))
|
||||||
getLogger().info("Saved configuration changes.");
|
getLogger().info("Saved configuration changes.");
|
||||||
iConfig = null; //Clearing the hashmap is not enough, we need to update the section as well
|
|
||||||
getCommand2MC().unregisterCommands(this);
|
getCommand2MC().unregisterCommands(this);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
TBMCCoreAPI.SendException("Error while disabling plugin " + getName() + "!", e);
|
TBMCCoreAPI.SendException("Error while disabling plugin " + getName() + "!", e);
|
||||||
|
|
|
@ -31,9 +31,7 @@ public abstract class Component<TP extends JavaPlugin> {
|
||||||
@Getter
|
@Getter
|
||||||
@NonNull
|
@NonNull
|
||||||
private TP plugin;
|
private TP plugin;
|
||||||
@NonNull
|
private @Getter final IHaveConfig config = new IHaveConfig(null);
|
||||||
private @Getter
|
|
||||||
IHaveConfig config;
|
|
||||||
private @Getter IHaveConfig data; //TODO
|
private @Getter IHaveConfig data; //TODO
|
||||||
|
|
||||||
public final ConfigData<Boolean> shouldBeEnabled() {
|
public final ConfigData<Boolean> shouldBeEnabled() {
|
||||||
|
@ -83,6 +81,7 @@ public abstract class Component<TP extends JavaPlugin> {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
component.plugin = plugin;
|
component.plugin = plugin;
|
||||||
|
component.config.setSaveAction(plugin::saveConfig);
|
||||||
updateConfig(plugin, component);
|
updateConfig(plugin, component);
|
||||||
component.register(plugin);
|
component.register(plugin);
|
||||||
components.put(component.getClass(), component);
|
components.put(component.getClass(), component);
|
||||||
|
@ -161,11 +160,8 @@ public abstract class Component<TP extends JavaPlugin> {
|
||||||
var configSect = compconf.getConfigurationSection(component.getClassName());
|
var configSect = compconf.getConfigurationSection(component.getClassName());
|
||||||
if (configSect == null)
|
if (configSect == null)
|
||||||
configSect = compconf.createSection(component.getClassName());
|
configSect = compconf.createSection(component.getClassName());
|
||||||
if (component.config != null) component.config.reset(configSect);
|
component.config.reset(configSect);
|
||||||
else component.config = new IHaveConfig(configSect, plugin::saveConfig);
|
} //Testing: it's already set
|
||||||
} else //Testing
|
|
||||||
if (component.config == null)
|
|
||||||
component.config = new IHaveConfig(null, plugin::saveConfig);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -192,7 +188,7 @@ public abstract class Component<TP extends JavaPlugin> {
|
||||||
*
|
*
|
||||||
* @param plugin Plugin object
|
* @param plugin Plugin object
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings({"unused", "WeakerAccess"})
|
@SuppressWarnings({"unused"})
|
||||||
protected void register(JavaPlugin plugin) {
|
protected void register(JavaPlugin plugin) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -203,7 +199,7 @@ public abstract class Component<TP extends JavaPlugin> {
|
||||||
*
|
*
|
||||||
* @param plugin Plugin object
|
* @param plugin Plugin object
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings({"WeakerAccess", "unused"})
|
@SuppressWarnings({"unused"})
|
||||||
protected void unregister(JavaPlugin plugin) {
|
protected void unregister(JavaPlugin plugin) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -257,10 +253,15 @@ public abstract class Component<TP extends JavaPlugin> {
|
||||||
var cs = c.getConfigurationSection(key);
|
var cs = c.getConfigurationSection(key);
|
||||||
if (cs == null) cs = c.createSection(key);
|
if (cs == null) cs = c.createSection(key);
|
||||||
val res = cs.getValues(false).entrySet().stream().filter(e -> e.getValue() instanceof ConfigurationSection)
|
val res = cs.getValues(false).entrySet().stream().filter(e -> e.getValue() instanceof ConfigurationSection)
|
||||||
.collect(Collectors.toMap(Map.Entry::getKey, kv -> new IHaveConfig((ConfigurationSection) kv.getValue(), getPlugin()::saveConfig)));
|
.collect(Collectors.toMap(Map.Entry::getKey, kv -> {
|
||||||
|
var conf = new IHaveConfig(getPlugin()::saveConfig);
|
||||||
|
conf.reset((ConfigurationSection) kv.getValue());
|
||||||
|
return conf;
|
||||||
|
}));
|
||||||
if (res.size() == 0) {
|
if (res.size() == 0) {
|
||||||
for (val entry : defaultProvider.entrySet()) {
|
for (val entry : defaultProvider.entrySet()) {
|
||||||
val conf = new IHaveConfig(cs.createSection(entry.getKey()), getPlugin()::saveConfig);
|
val conf = new IHaveConfig(getPlugin()::saveConfig);
|
||||||
|
conf.reset(cs.createSection(entry.getKey()));
|
||||||
entry.getValue().accept(conf);
|
entry.getValue().accept(conf);
|
||||||
res.put(entry.getKey(), conf);
|
res.put(entry.getKey(), conf);
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,8 @@ public class ConfigData<T> {
|
||||||
private String path;
|
private String path;
|
||||||
protected final T def;
|
protected final T def;
|
||||||
private final Object primitiveDef;
|
private final Object primitiveDef;
|
||||||
private final Runnable saveAction;
|
@Setter(AccessLevel.PACKAGE)
|
||||||
|
private Runnable saveAction;
|
||||||
/**
|
/**
|
||||||
* 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)}
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -23,15 +23,14 @@ public final class IHaveConfig {
|
||||||
*/
|
*/
|
||||||
@Getter
|
@Getter
|
||||||
private ConfigurationSection config;
|
private ConfigurationSection config;
|
||||||
private final Runnable saveAction;
|
private Runnable saveAction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* May be used in testing.
|
* May be used in testing.
|
||||||
*
|
*
|
||||||
* @param section May be null for testing
|
* @param saveAction What to do to save the config to disk. Don't use get methods until it's non-null.
|
||||||
*/
|
*/
|
||||||
IHaveConfig(ConfigurationSection section, Runnable saveAction) {
|
IHaveConfig(Runnable saveAction) {
|
||||||
config = section;
|
|
||||||
this.saveAction = saveAction;
|
this.saveAction = saveAction;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -183,6 +182,11 @@ public final class IHaveConfig {
|
||||||
datamap.forEach((path, data) -> data.reset(config));
|
datamap.forEach((path, data) -> data.reset(config));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setSaveAction(Runnable saveAction) {
|
||||||
|
this.saveAction = saveAction;
|
||||||
|
datamap.forEach((path, data) -> data.setSaveAction(saveAction));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates the config YAML.
|
* Generates the config YAML.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue