From 4fc751988074db5b8ae9c225047379fde3a0ce64 Mon Sep 17 00:00:00 2001 From: benwoo1110 <30431861+benwoo1110@users.noreply.github.com> Date: Wed, 10 Mar 2021 22:23:48 +0800 Subject: [PATCH] Add debug command. --- .../MultiverseCore/commands/DebugCommand.java | 56 +++++++++++++++++++ .../commands/MultiverseCoreCommand.java | 11 ++++ .../commandtools/MVCommandManager.java | 7 +++ .../commandtools/MVCommandParser.java | 21 +++++++ .../MultiverseCore/TestDebugMode.java | 12 +--- 5 files changed, 96 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/onarandombox/MultiverseCore/commands/DebugCommand.java diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/DebugCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/DebugCommand.java new file mode 100644 index 00000000..dfd76e6b --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/DebugCommand.java @@ -0,0 +1,56 @@ +package com.onarandombox.MultiverseCore.commands; + +import co.aikar.commands.annotation.CommandAlias; +import co.aikar.commands.annotation.CommandCompletion; +import co.aikar.commands.annotation.CommandPermission; +import co.aikar.commands.annotation.Conditions; +import co.aikar.commands.annotation.Description; +import co.aikar.commands.annotation.Subcommand; +import co.aikar.commands.annotation.Syntax; +import com.dumptruckman.minecraft.util.Logging; +import com.onarandombox.MultiverseCore.MultiverseCore; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +@CommandAlias("mv") +public class DebugCommand extends MultiverseCoreCommand { + + public DebugCommand(MultiverseCore plugin) { + super(plugin); + } + + @Subcommand("debug") + @CommandPermission("multiverse.core.debug") + @Description("Show the current debug level.") + public void onShowDebugCommand(@NotNull CommandSender sender) { + this.displayDebugMode(sender); + } + + @Subcommand("debug") + @CommandPermission("multiverse.core.debug") + @Syntax("") + @CommandCompletion("@toggles|@range:3") + @Description("Change debug level.") + public void onChangeDebugCommand(@NotNull CommandSender sender, + + @Conditions("debuglevel") + @Syntax("") + @Description("Debug level to set to.") + int level) { + + this.plugin.getMVConfig().setGlobalDebug(level); + this.saveMVConfigs(sender); + this.displayDebugMode(sender); + } + + private void displayDebugMode(@NotNull CommandSender sender) { + final int debugLevel = this.plugin.getMVConfig().getGlobalDebug(); + if (debugLevel == 0) { + sender.sendMessage("Multiverse Debug mode is " + ChatColor.RED + "OFF"); + return; + } + sender.sendMessage("Multiverse Debug mode is at level " + ChatColor.GREEN + debugLevel); + Logging.fine("Multiverse Debug ENABLED"); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/MultiverseCoreCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/MultiverseCoreCommand.java index e7f2adb4..ebf7134d 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/MultiverseCoreCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/MultiverseCoreCommand.java @@ -2,6 +2,8 @@ package com.onarandombox.MultiverseCore.commands; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; /** * Generic multiverse core command with handy reference to the plugin instance. @@ -13,4 +15,13 @@ public abstract class MultiverseCoreCommand extends MultiverseCommand { protected MultiverseCoreCommand(MultiverseCore plugin) { this.plugin = plugin; } + + protected boolean saveMVConfigs(CommandSender sender) { + if (this.plugin.saveMVConfigs()) { + return true; + } + sender.sendMessage(ChatColor.RED + "An error occurred while trying to save Multiverse-Core config.yml. " + + "You changes will be temporary!"); + return false; + } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandManager.java b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandManager.java index 260e67c3..b066dbf0 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandManager.java @@ -7,14 +7,21 @@ import co.aikar.commands.CommandCompletions; import co.aikar.commands.CommandContexts; import co.aikar.commands.PaperCommandManager; import com.onarandombox.MultiverseCore.MultiverseCore; +import com.onarandombox.MultiverseCore.commands.DebugCommand; /** * Main class to manage permissions. */ public class MVCommandManager extends PaperCommandManager { + private final MultiverseCore plugin; + public MVCommandManager(MultiverseCore plugin) { super(plugin); + this.plugin = plugin; + + // Register commands + this.registerCommand(new DebugCommand(plugin)); } /** diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandParser.java b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandParser.java index 300a61ef..4bac883b 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandParser.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandParser.java @@ -1,6 +1,12 @@ package com.onarandombox.MultiverseCore.commandtools; +import co.aikar.commands.BukkitCommandExecutionContext; +import co.aikar.commands.BukkitCommandIssuer; +import co.aikar.commands.ConditionContext; +import co.aikar.commands.ConditionFailedException; import co.aikar.commands.PaperCommandContexts; +import org.bukkit.ChatColor; +import org.jetbrains.annotations.NotNull; /** * Class to parse command arguments into its object and validate them. @@ -9,5 +15,20 @@ public class MVCommandParser extends PaperCommandContexts { public MVCommandParser(MVCommandManager manager) { super(manager); + + // Contexts + + // Conditions + manager.getCommandConditions().addCondition(int.class, "debuglevel", this::checkDebugLevel); + } + + private void checkDebugLevel(@NotNull ConditionContext context, + @NotNull BukkitCommandExecutionContext executionContext, + int level) { + + if (level < 0 || level > 3) { + throw new ConditionFailedException("Invalid debug mode level. Please use a number 0-3 " + + ChatColor.AQUA + "(3 being many many messages!)"); + } } } diff --git a/src/test/java/com/onarandombox/MultiverseCore/TestDebugMode.java b/src/test/java/com/onarandombox/MultiverseCore/TestDebugMode.java index 308bb21e..3354853f 100644 --- a/src/test/java/com/onarandombox/MultiverseCore/TestDebugMode.java +++ b/src/test/java/com/onarandombox/MultiverseCore/TestDebugMode.java @@ -11,14 +11,12 @@ import com.onarandombox.MultiverseCore.api.Core; import com.onarandombox.MultiverseCore.utils.TestInstanceCreator; import junit.framework.Assert; import org.bukkit.Server; -import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.java.JavaPluginLoader; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.PowerMockIgnore; @@ -29,13 +27,10 @@ import java.io.File; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; @RunWith(PowerMockRunner.class) @PrepareForTest({ MultiverseCore.class, PluginDescriptionFile.class, JavaPluginLoader.class}) @PowerMockIgnore("javax.script.*") -@Ignore public class TestDebugMode { TestInstanceCreator creator; Server mockServer; @@ -70,16 +65,11 @@ public class TestDebugMode { File serverDirectory = new File(creator.getCore().getServerFolder(), "world"); serverDirectory.mkdirs(); - // Initialize a fake command - Command mockCommand = mock(Command.class); - when(mockCommand.getName()).thenReturn("mv"); - // Assert debug mode is off Assert.assertEquals(0, core.getMVConfig().getGlobalDebug()); // Send the debug command. - String[] debugArgs = new String[] { "debug", "3" }; - plugin.onCommand(mockCommandSender, mockCommand, "", debugArgs); + this.creator.dispatch(mockCommandSender, "mv debug 3"); Assert.assertEquals(3, core.getMVConfig().getGlobalDebug()); }