Add permanent component enable/disable

#81
This commit is contained in:
Norbi Peti 2020-03-10 16:06:57 +01:00
parent e676ea516c
commit b77871bb8c

View file

@ -25,9 +25,9 @@ public class ComponentCommand extends ICommand2MC {
@Subcommand(helpText = { @Subcommand(helpText = {
"Enable component", "Enable component",
"Temporarily enables a component. If you want to permanently enable a component, change it's 'enabled' config option.\"" "Temporarily or permanently enables a component."
}) })
public boolean enable(CommandSender sender, Plugin plugin, String component) { public boolean enable(CommandSender sender, Plugin plugin, String component, @Command2.OptionalArg boolean permanent) {
if (plugin instanceof ButtonPlugin) { if (plugin instanceof ButtonPlugin) {
if (!((ButtonPlugin) plugin).justReload()) { if (!((ButtonPlugin) plugin).justReload()) {
sender.sendMessage("§cCouldn't reload config, check console."); sender.sendMessage("§cCouldn't reload config, check console.");
@ -35,15 +35,15 @@ public class ComponentCommand extends ICommand2MC {
} }
} else } else
plugin.reloadConfig(); //Reload config so the new config values are read - All changes are saved to disk on disable plugin.reloadConfig(); //Reload config so the new config values are read - All changes are saved to disk on disable
return enable_disable(sender, plugin, component, true); return enable_disable(sender, plugin, component, true, permanent);
} }
@Subcommand(helpText = { @Subcommand(helpText = {
"Disable component", "Disable component",
"Temporarily disables a component. If you want to permanently disable a component, change it's 'enabled' config option." "Temporarily or permanently disables a component."
}) })
public boolean disable(CommandSender sender, Plugin plugin, String component) { public boolean disable(CommandSender sender, Plugin plugin, String component, @Command2.OptionalArg boolean permanent) {
return enable_disable(sender, plugin, component, false); return enable_disable(sender, plugin, component, false, permanent);
} }
@Subcommand(helpText = { @Subcommand(helpText = {
@ -57,13 +57,15 @@ public class ComponentCommand extends ICommand2MC {
return true; return true;
} }
private boolean enable_disable(CommandSender sender, Plugin plugin, String component, boolean enable) { private boolean enable_disable(CommandSender sender, Plugin plugin, String component, boolean enable, boolean permanent) {
try { try {
val oc = getComponentOrError(plugin, component, sender); val oc = getComponentOrError(plugin, component, sender);
if (!oc.isPresent()) if (!oc.isPresent())
return true; return true;
Component.setComponentEnabled(oc.get(), enable); Component.setComponentEnabled(oc.get(), enable);
sender.sendMessage(oc.get().getClass().getSimpleName() + " " + (enable ? "en" : "dis") + "abled."); if (permanent)
oc.get().shouldBeEnabled().set(enable);
sender.sendMessage(oc.get().getClass().getSimpleName() + " " + (enable ? "en" : "dis") + "abled " + (permanent ? "permanently" : "temporarily") + ".");
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("Couldn't " + (enable ? "en" : "dis") + "able component " + component + "!", e); TBMCCoreAPI.SendException("Couldn't " + (enable ? "en" : "dis") + "able component " + component + "!", e);
} }
@ -77,5 +79,5 @@ public class ComponentCommand extends ICommand2MC {
if (!oc.isPresent()) if (!oc.isPresent())
sender.sendMessage("§cComponent not found!"); //^ Much simpler to solve in the new command system sender.sendMessage("§cComponent not found!"); //^ Much simpler to solve in the new command system
return oc; return oc;
} //TODO: Tabcompletion for the new command system }
} }