From 2c6eb3a6100eb37d715d69ea8fc6163beb3a6909 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 28 Nov 2020 00:14:26 +0100 Subject: [PATCH] Add support for disabling/enabling the plugin And autostart can be disabled too --- .../java/sznp/virtualcomputer/Computer.java | 1 + .../sznp/virtualcomputer/ComputerCommand.java | 44 +++++++++++++++++++ .../java/sznp/virtualcomputer/PluginMain.java | 31 ++++++++++++- .../src/main/resources/plugin.yml | 2 +- pom.xml | 2 +- 5 files changed, 77 insertions(+), 3 deletions(-) diff --git a/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/Computer.java b/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/Computer.java index 1b4fe9c..1f60dc5 100644 --- a/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/Computer.java +++ b/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/Computer.java @@ -364,5 +364,6 @@ public final class Computer { } session.unlockMachine(); } + instance = null; //Allow setting it again } } diff --git a/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/ComputerCommand.java b/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/ComputerCommand.java index 0964575..72e04e3 100644 --- a/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/ComputerCommand.java +++ b/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/ComputerCommand.java @@ -2,6 +2,7 @@ package sznp.virtualcomputer; import buttondevteam.lib.chat.Command2; import buttondevteam.lib.chat.CommandClass; +import buttondevteam.lib.chat.CustomTabComplete; import buttondevteam.lib.chat.ICommand2MC; import lombok.var; import net.md_5.bungee.api.ChatColor; @@ -30,6 +31,7 @@ import java.util.Objects; public class ComputerCommand extends ICommand2MC { @Command2.Subcommand public void status(CommandSender sender) { + if (checkDisabled(sender)) return; Computer.getInstance().Status(sender); } @@ -39,36 +41,43 @@ public class ComputerCommand extends ICommand2MC { "Use /c list to see the index of the machines." }) public void start(CommandSender sender, @Command2.OptionalArg int index) { + if (checkDisabled(sender)) return; Computer.getInstance().Start(sender, index); } @Command2.Subcommand public void list(CommandSender sender) { + if (checkDisabled(sender)) return; Computer.getInstance().List(sender); } @Command2.Subcommand(aliases = {"poweroff", "off", "kill"}) public void stop(CommandSender sender) { + if (checkDisabled(sender)) return; Computer.getInstance().Stop(sender); } @Command2.Subcommand(aliases = {"powerbtn", "pwrbtn"}) public void powerbutton(CommandSender sender, @Command2.OptionalArg int index) { + if (checkDisabled(sender)) return; Computer.getInstance().PowerButton(sender, index); } @Command2.Subcommand(aliases = "restart") public void reset(CommandSender sender) { + if (checkDisabled(sender)) return; Computer.getInstance().Reset(sender); } @Command2.Subcommand(aliases = "save state") public void save(CommandSender sender) { + if (checkDisabled(sender)) return; Computer.getInstance().SaveState(sender); } @Command2.Subcommand(aliases = "fix screen") public void fix(CommandSender sender) { + if (checkDisabled(sender)) return; Computer.getInstance().FixScreen(sender); } @@ -77,6 +86,7 @@ public class ComputerCommand extends ICommand2MC { "Presses the specified key. Valid values for the last param are 'down', 'up' and amount of ticks to hold." }) public void key(CommandSender sender, String key, @Command2.OptionalArg String stateorduration) { + if (checkDisabled(sender)) return; if (stateorduration == null) stateorduration = ""; Computer.getInstance().PressKey(sender, key, stateorduration); } @@ -86,6 +96,7 @@ public class ComputerCommand extends ICommand2MC { "Move the mouse by the specified offset or press the given button." }) public void mouse(CommandSender sender, String keyOrX, @Command2.OptionalArg int y, @Command2.OptionalArg int z, @Command2.OptionalArg int w) { + if (checkDisabled(sender)) return; try { if (y != 0 || z != 0 || w != 0) { int x = Integer.parseInt(keyOrX); @@ -139,6 +150,7 @@ public class ComputerCommand extends ICommand2MC { "Locks the mouse to where you're looking. If you move your mouse, the computer's mouse will be moved." }) public void lock_mouse(Player player) { + if (checkDisabled(player)) return; MouseLockerPlayerListener.toggleLock(player); } @@ -147,6 +159,7 @@ public class ComputerCommand extends ICommand2MC { "Sets the mouse speed when locked. The default is 5." }) public void lock_speed(CommandSender sender, float speed) { + if (checkDisabled(sender)) return; MouseLockerPlayerListener.LockedSpeed = speed; sender.sendMessage("§aMouse speed set to " + MouseLockerPlayerListener.LockedSpeed); } @@ -156,6 +169,7 @@ public class ComputerCommand extends ICommand2MC { "Spawns a computer screen near you. All of them show the same thing." }) public void spawn(Player player) { + if (checkDisabled(player)) return; var loc = player.getLocation(); var world = Objects.requireNonNull(loc.getWorld()); short id = PluginMain.Instance.startID.get(); @@ -200,4 +214,34 @@ public class ComputerCommand extends ICommand2MC { System.out.println("item: " + item); } } + + @Command2.Subcommand(helpText = { + "Plugin enable/disable", + "Use this command to enable or disable the plugin.", + "This can be useful to save resources if it is unused." + }) + public boolean plugin(CommandSender sender, @CustomTabComplete({"enable", "disable"}) String enableDisable) { + switch (enableDisable) { + case "enable": + sender.sendMessage("§bEnabling plugin..."); + PluginMain.Instance.pluginEnableInternal(); + sender.sendMessage("§bPlugin enabled! More info on console."); + break; + case "disable": + sender.sendMessage("§aDisabling plugin..."); + PluginMain.Instance.pluginDisableInternal(); + sender.sendMessage("§aPlugin disabled! More info on console."); + break; + default: + return false; + } + return true; + } + + private boolean checkDisabled(CommandSender sender) { + boolean disabled = !PluginMain.isPluginEnabled(); + if (disabled) + sender.sendMessage("The plugin is currently disabled. Do /c plugin enable to enable it."); + return disabled; + } } diff --git a/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/PluginMain.java b/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/PluginMain.java index f775f17..1d6ed42 100644 --- a/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/PluginMain.java +++ b/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/PluginMain.java @@ -3,6 +3,7 @@ package sznp.virtualcomputer; import buttondevteam.lib.architecture.ButtonPlugin; import buttondevteam.lib.architecture.ConfigData; import jnr.ffi.LibraryLoader; +import lombok.Getter; import lombok.val; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; @@ -28,6 +29,7 @@ public class PluginMain extends ButtonPlugin { public static final int MCY = 4; private BukkitTask mousetask; private VBoxEventHandler listener; + private VirtualBoxManager manager; public static PluginMain Instance; /** @@ -37,6 +39,8 @@ public class PluginMain extends ButtonPlugin { private static final ArrayList renderers = new ArrayList<>(); public static boolean direct; public static boolean sendAll; + @Getter + private static boolean pluginEnabled; //The Bukkit plugin has to be enabled for the enable command to work /** * The first map ID to use for the screen. @@ -52,13 +56,28 @@ public class PluginMain extends ButtonPlugin { */ private final ConfigData kbLayout = getIConfig().getData("kbLayout", "en"); public File layoutFolder = new File(getDataFolder(), "layouts"); + /** + * When set to false, the plugin will not initialize on server startup and its only valid command will be /c plugin enable. + * This can be useful to save resources as the plugin keeps the VirtualBox interface running while enabled. + */ + private final ConfigData autoEnable = getIConfig().getData("autoEnable", true); @Override public void pluginEnable() { + registerCommand(new ComputerCommand()); + if (autoEnable.get()) + pluginEnableInternal(); + else + getLogger().info("Auto-enable is disabled. Enable with /c plugin enable."); + } + + void pluginEnableInternal() { + if (pluginEnabled) + return; + pluginEnabled = true; Instance = this; try { ConsoleCommandSender ccs = getServer().getConsoleSender(); - registerCommand(new ComputerCommand()); sendAll = getConfig().getBoolean("sendAll", true); ccs.sendMessage("§bInitializing VirtualBox..."); String osname = System.getProperty("os.name").toLowerCase(); @@ -95,6 +114,7 @@ public class PluginMain extends ButtonPlugin { IVirtualBox vbox = manager.getVBox(); (listener = new VBoxEventHandler()).registerTo(vbox.getEventSource()); new Computer(this, manager, vbox); //Saves itself + this.manager = manager; ccs.sendMessage("§bLoading Screen..."); try { if (useGPU.get()) @@ -140,6 +160,13 @@ public class PluginMain extends ButtonPlugin { @Override public void pluginDisable() { + pluginDisableInternal(); + } + + void pluginDisableInternal() { + if (!pluginEnabled) + return; + pluginEnabled = false; ConsoleCommandSender ccs = getServer().getConsoleSender(); if (mousetask != null) mousetask.cancel(); @@ -154,6 +181,8 @@ public class PluginMain extends ButtonPlugin { Computer.getInstance().pluginDisable(ccs); ccs.sendMessage("§aHuh."); saveConfig(); + renderers.clear(); + manager.cleanup(); } } diff --git a/VirtualComputer-Core/src/main/resources/plugin.yml b/VirtualComputer-Core/src/main/resources/plugin.yml index 25aade5..bbd4a43 100644 --- a/VirtualComputer-Core/src/main/resources/plugin.yml +++ b/VirtualComputer-Core/src/main/resources/plugin.yml @@ -8,4 +8,4 @@ commands: aliases: c api-version: '1.13' depend: - - ChromaCore \ No newline at end of file + - Chroma-Core \ No newline at end of file diff --git a/pom.xml b/pom.xml index 597d6a7..da43039 100644 --- a/pom.xml +++ b/pom.xml @@ -83,7 +83,7 @@ com.github.TBMCPlugins.ChromaCore Chroma-Core - master-SNAPSHOT + v1.0.0