Add support for disabling/enabling the plugin

And autostart can be disabled too
This commit is contained in:
Norbi Peti 2020-11-28 00:14:26 +01:00
parent 6058842b61
commit 2c6eb3a610
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
5 changed files with 77 additions and 3 deletions

View file

@ -364,5 +364,6 @@ public final class Computer {
}
session.unlockMachine();
}
instance = null; //Allow setting it again
}
}

View file

@ -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;
}
}

View file

@ -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<IRenderer> 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<String> 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<Boolean> 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();
}
}

View file

@ -8,4 +8,4 @@ commands:
aliases: c
api-version: '1.13'
depend:
- ChromaCore
- Chroma-Core

View file

@ -83,7 +83,7 @@
<dependency>
<groupId>com.github.TBMCPlugins.ChromaCore</groupId>
<artifactId>Chroma-Core</artifactId>
<version>master-SNAPSHOT</version>
<version>v1.0.0</version>
</dependency>
</dependencies>