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)
|
||||
public abstract class ButtonPlugin extends JavaPlugin {
|
||||
@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)
|
||||
private IHaveConfig iConfig;
|
||||
private final IHaveConfig iConfig = new IHaveConfig(this::saveConfig);
|
||||
private CommentedConfiguration yaml;
|
||||
@Getter(AccessLevel.PROTECTED)
|
||||
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
|
||||
*/
|
||||
@Getter
|
||||
private Stack<Component<?>> componentStack = new Stack<>();
|
||||
;
|
||||
private final Stack<Component<?>> componentStack = new Stack<>();
|
||||
|
||||
protected abstract void pluginEnable();
|
||||
|
||||
|
@ -72,8 +71,7 @@ public abstract class ButtonPlugin extends JavaPlugin {
|
|||
return false;
|
||||
var section = config.getConfigurationSection("global");
|
||||
if (section == null) section = config.createSection("global");
|
||||
if (iConfig != null) iConfig.reset(section);
|
||||
else iConfig = new IHaveConfig(section, this::saveConfig);
|
||||
iConfig.reset(section);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -85,7 +83,6 @@ public abstract class ButtonPlugin extends JavaPlugin {
|
|||
pluginDisable();
|
||||
if (ConfigData.saveNow(getConfig()))
|
||||
getLogger().info("Saved configuration changes.");
|
||||
iConfig = null; //Clearing the hashmap is not enough, we need to update the section as well
|
||||
getCommand2MC().unregisterCommands(this);
|
||||
} catch (Exception e) {
|
||||
TBMCCoreAPI.SendException("Error while disabling plugin " + getName() + "!", e);
|
||||
|
|
|
@ -31,9 +31,7 @@ public abstract class Component<TP extends JavaPlugin> {
|
|||
@Getter
|
||||
@NonNull
|
||||
private TP plugin;
|
||||
@NonNull
|
||||
private @Getter
|
||||
IHaveConfig config;
|
||||
private @Getter final IHaveConfig config = new IHaveConfig(null);
|
||||
private @Getter IHaveConfig data; //TODO
|
||||
|
||||
public final ConfigData<Boolean> shouldBeEnabled() {
|
||||
|
@ -83,6 +81,7 @@ public abstract class Component<TP extends JavaPlugin> {
|
|||
return false;
|
||||
}
|
||||
component.plugin = plugin;
|
||||
component.config.setSaveAction(plugin::saveConfig);
|
||||
updateConfig(plugin, component);
|
||||
component.register(plugin);
|
||||
components.put(component.getClass(), component);
|
||||
|
@ -161,11 +160,8 @@ public abstract class Component<TP extends JavaPlugin> {
|
|||
var configSect = compconf.getConfigurationSection(component.getClassName());
|
||||
if (configSect == null)
|
||||
configSect = compconf.createSection(component.getClassName());
|
||||
if (component.config != null) component.config.reset(configSect);
|
||||
else component.config = new IHaveConfig(configSect, plugin::saveConfig);
|
||||
} else //Testing
|
||||
if (component.config == null)
|
||||
component.config = new IHaveConfig(null, plugin::saveConfig);
|
||||
component.config.reset(configSect);
|
||||
} //Testing: it's already set
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -192,7 +188,7 @@ public abstract class Component<TP extends JavaPlugin> {
|
|||
*
|
||||
* @param plugin Plugin object
|
||||
*/
|
||||
@SuppressWarnings({"unused", "WeakerAccess"})
|
||||
@SuppressWarnings({"unused"})
|
||||
protected void register(JavaPlugin plugin) {
|
||||
}
|
||||
|
||||
|
@ -203,7 +199,7 @@ public abstract class Component<TP extends JavaPlugin> {
|
|||
*
|
||||
* @param plugin Plugin object
|
||||
*/
|
||||
@SuppressWarnings({"WeakerAccess", "unused"})
|
||||
@SuppressWarnings({"unused"})
|
||||
protected void unregister(JavaPlugin plugin) {
|
||||
}
|
||||
|
||||
|
@ -257,10 +253,15 @@ public abstract class Component<TP extends JavaPlugin> {
|
|||
var cs = c.getConfigurationSection(key);
|
||||
if (cs == null) cs = c.createSection(key);
|
||||
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) {
|
||||
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);
|
||||
res.put(entry.getKey(), conf);
|
||||
}
|
||||
|
|
|
@ -30,7 +30,8 @@ public class ConfigData<T> {
|
|||
private String path;
|
||||
protected final T def;
|
||||
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)}
|
||||
*/
|
||||
|
|
|
@ -23,15 +23,14 @@ public final class IHaveConfig {
|
|||
*/
|
||||
@Getter
|
||||
private ConfigurationSection config;
|
||||
private final Runnable saveAction;
|
||||
private Runnable saveAction;
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
config = section;
|
||||
IHaveConfig(Runnable saveAction) {
|
||||
this.saveAction = saveAction;
|
||||
}
|
||||
|
||||
|
@ -183,6 +182,11 @@ public final class IHaveConfig {
|
|||
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.
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue