Add debug command.

This commit is contained in:
benwoo1110 2021-03-10 22:23:48 +08:00
parent 0496905573
commit 4fc7519880
5 changed files with 96 additions and 11 deletions

View File

@ -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("<level>")
@CommandCompletion("@toggles|@range:3")
@Description("Change debug level.")
public void onChangeDebugCommand(@NotNull CommandSender sender,
@Conditions("debuglevel")
@Syntax("<level>")
@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");
}
}

View File

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

View File

@ -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));
}
/**

View File

@ -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<BukkitCommandIssuer> 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!)");
}
}
}

View File

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