Skip to content

Commit

Permalink
Debug ultraverbose
Browse files Browse the repository at this point in the history
CommandManager code update yoinked from Citizens source.
Also minor typo fix in anvil event
  • Loading branch information
mcmonkey4eva committed Sep 19, 2022
1 parent 3839aa8 commit 622b2f4
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 22 deletions.
Expand Up @@ -17,7 +17,7 @@ public class AnvilBlockDamagedScriptEvent extends BukkitScriptEvent implements L
// @Events
// anvil block damaged|breaks
//
// @Group paper
// @Group Paper
//
// @Location true
//
Expand Down
8 changes: 8 additions & 0 deletions plugin/src/main/java/com/denizenscript/denizen/Denizen.java
Expand Up @@ -70,6 +70,7 @@
import java.io.*;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -580,7 +581,14 @@ public boolean onCommand(CommandSender sender, Command cmd, String alias, String

Object[] methodArgs = {sender};
return commandManager.executeSafe(cmd, args, sender, methodArgs);
}

@Override
public List<String> onTabComplete(CommandSender commandSender, Command command, String alias, String[] strings) {
if (alias.equals("denizen")) {
return commandManager.onTabComplete(commandSender, command, alias, strings);
}
return null;
}

private boolean suggestClosestModifier(CommandSender sender, String command, String modifier) {
Expand Down
Expand Up @@ -132,30 +132,32 @@ else if (s.equals("disabled")) {
* DENIZEN DEBUG
*/
@Command(
aliases = {"denizen"}, usage = "debug",
aliases = {"denizen"}, usage = "debug (--verbose on) (--ultraverbose on)",
desc = "Toggles debug mode for Denizen.", modifiers = {"debug", "de", "db", "dbug"},
min = 1, max = 5, permission = "denizen.debug", flags = "scbroevnipfl")
public void debug(CommandContext args, CommandSender sender) throws CommandException {
if (args.getFlags().isEmpty() && args.getValueFlags().isEmpty()) {
CoreConfiguration.shouldShowDebug = !CoreConfiguration.shouldShowDebug;
Messaging.sendInfo(sender, "Denizen debugger is now: " + (CoreConfiguration.shouldShowDebug ? "<a>ENABLED" : "<c>DISABLED") + "<f>.");
return;
}
CoreConfiguration.shouldShowDebug = true;
if (args.hasFlag('s')) {
CoreConfiguration.shouldShowDebug = true;
CoreConfiguration.debugStackTraces = !CoreConfiguration.debugStackTraces;
Messaging.sendInfo(sender, (CoreConfiguration.debugStackTraces ? "Denizen debugger is now showing caught " +
"exception stack traces." : "Denizen debugger is no longer showing caught stack traces."));
}
if (args.hasFlag('c')) {
CoreConfiguration.shouldShowDebug = true;
DebugConsoleSender.showColor = !DebugConsoleSender.showColor;
Messaging.sendInfo(sender, (DebugConsoleSender.showColor ? "Denizen debugger will now show color."
: "Denizen debugger will no longer show color."));
}
if (args.hasFlag('o')) {
CoreConfiguration.shouldShowDebug = true;
CoreConfiguration.debugOverride = !CoreConfiguration.debugOverride;
Messaging.sendInfo(sender, (CoreConfiguration.debugOverride ? "Denizen debugger is now overriding 'debug: false'."
: "Denizen debugger will no longer override 'debug: false'."));
}
if (args.hasFlag('b')) {
CoreConfiguration.shouldShowDebug = true;
CoreConfiguration.debugScriptBuilder = !CoreConfiguration.debugScriptBuilder;
Messaging.sendInfo(sender, (CoreConfiguration.debugScriptBuilder ? "Denizen debugger is now logging the " +
"ScriptBuilder." : "Denizen debugger is now hiding ScriptBuilder logging."));
Expand All @@ -165,69 +167,61 @@ public void debug(CommandContext args, CommandSender sender) throws CommandExcep
Messaging.sendError(sender, "Not allowed to record debug currently.");
return;
}
CoreConfiguration.shouldShowDebug = true;
CoreConfiguration.shouldRecordDebug = !CoreConfiguration.shouldRecordDebug;
Debug.debugRecording = new StringBuilder();
Messaging.sendInfo(sender, (CoreConfiguration.shouldRecordDebug ? "Denizen debugger is now recording. Use /denizen " +
"submit to finish." : "Denizen debugger recording disabled."));
}
if (args.hasFlag('e')) {
CoreConfiguration.shouldShowDebug = true;
CoreConfiguration.debugExtraInfo = !CoreConfiguration.debugExtraInfo;
Messaging.sendInfo(sender, (CoreConfiguration.debugExtraInfo ? "Denizen debugger is now showing extra internal information." :
"Denizen debugger is no longer showing extra internal information."));
}
if (args.hasFlag('v')) {
CoreConfiguration.shouldShowDebug = true;
if (args.hasFlag('v') || args.hasValueFlag("verbose")) {
CoreConfiguration.debugVerbose = !CoreConfiguration.debugVerbose;
Messaging.sendInfo(sender, (CoreConfiguration.debugVerbose ? "Denizen debugger is now verbose." :
"Denizen debugger is no longer verbose."));
}
if (args.hasValueFlag("ultraverbose")) {
CoreConfiguration.debugUltraVerbose = !CoreConfiguration.debugUltraVerbose;
CoreConfiguration.debugVerbose = CoreConfiguration.debugUltraVerbose;
Messaging.sendInfo(sender, (CoreConfiguration.debugVerbose ? "Denizen debugger is now ultra-verbose." :
"Denizen debugger is no longer ultra-verbose."));
}
if (args.hasFlag('f')) {
CoreConfiguration.shouldShowDebug = true;
CoreConfiguration.futureWarningsEnabled = !CoreConfiguration.futureWarningsEnabled;
Messaging.sendInfo(sender, (CoreConfiguration.futureWarningsEnabled ? "Denizen debugger is now showing future warnings." :
"Denizen debugger will no longer show future warnings."));
}
if (args.hasFlag('n')) {
CoreConfiguration.shouldShowDebug = true;
CoreConfiguration.debugShouldTrim = !CoreConfiguration.debugShouldTrim;
Messaging.sendInfo(sender, (CoreConfiguration.debugShouldTrim ? "Denizen debugger is now trimming long messages."
: "Denizen debugger is no longer trimming long messages."));
}
if (args.hasFlag('i')) {
CoreConfiguration.shouldShowDebug = true;
CoreConfiguration.debugShowSources = !CoreConfiguration.debugShowSources;
Messaging.sendInfo(sender, (CoreConfiguration.debugShowSources ? "Denizen debugger is now showing source information."
: "Denizen debugger is no longer showing source information."));
}
if (args.hasFlag('p')) {
CoreConfiguration.shouldShowDebug = true;
NetworkInterceptHelper.enable();
NMSHandler.debugPackets = !NMSHandler.debugPackets;
NMSHandler.debugPacketFilter = "";
Messaging.sendInfo(sender, (NMSHandler.debugPackets ? "Denizen debugger is now showing unfiltered packet logs."
: "Denizen debugger is no longer showing packet logs."));
}
if (args.hasValueFlag("pfilter")) {
CoreConfiguration.shouldShowDebug = true;
NetworkInterceptHelper.enable();
NMSHandler.debugPackets = true;
NMSHandler.debugPacketFilter = CoreUtilities.toLowerCase(args.getFlag("pfilter"));
Messaging.sendInfo(sender, "Denizen debug packet log now enabled and filtered.");
return;
}
if (args.hasFlag('l')) {
CoreConfiguration.shouldShowDebug = true;
CoreConfiguration.debugLoadingInfo = !CoreConfiguration.debugLoadingInfo;
Messaging.sendInfo(sender, (CoreConfiguration.debugLoadingInfo ? "Denizen debugger is now showing script loading information."
: "Denizen debugger is no longer showing script loading information."));
}
if (args.getFlags().isEmpty()) {
CoreConfiguration.shouldShowDebug = !CoreConfiguration.shouldShowDebug;
Messaging.sendInfo(sender, "Denizen debugger is now: " + (CoreConfiguration.shouldShowDebug ? "<a>ENABLED" : "<c>DISABLED") + "<f>.");
}

}

/*
Expand Down
Expand Up @@ -8,6 +8,7 @@
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;

import java.lang.annotation.Annotation;
Expand All @@ -16,7 +17,7 @@
import java.lang.reflect.Modifier;
import java.util.*;

public class CommandManager {
public class CommandManager implements TabCompleter {

private final Map<Class<? extends Annotation>, CommandAnnotationProcessor> annotationProcessors =
new HashMap<>();
Expand Down Expand Up @@ -314,6 +315,47 @@ private boolean hasPermission(Method method, CommandSender sender) {
return cmd.permission().isEmpty() || hasPermission(sender, cmd.permission()) || hasPermission(sender, "admin");
}

@Override
public List<String> onTabComplete(CommandSender sender, org.bukkit.command.Command command, String alias, String[] args) {
List<String> results = new ArrayList<>();
if (args.length <= 1) {
String search = args.length == 1 ? args[0] : "";
for (String base : commands.keySet()) {
String[] parts = base.split(" ");
String cmd = parts[0];
if (!cmd.equalsIgnoreCase(command.getName()) || parts.length < 2)
continue;
String modifier = parts[1];
if (modifier.startsWith(search)) {
results.add(modifier);
}
}
return results;
}
CommandInfo internalCommand = getCommand(command.getName().toLowerCase(), args[0]);
if (internalCommand == null) {
return results;
}
String[] newArgs = new String[args.length + 1];
System.arraycopy(args, 0, newArgs, 1, args.length);
newArgs[0] = command.getName().toLowerCase();
CommandContext context = new CommandContext(sender, newArgs); // partial parse
String flags = internalCommand.commandAnnotation.flags();
for (int i = 0; i < flags.length(); i++) {
char c = flags.charAt(i);
if (!context.hasFlag(c)) {
results.add("-" + c);
}
}
Collection<String> valueFlags = internalCommand.valueFlags();
for (String valueFlag : valueFlags) {
if (!context.hasValueFlag(valueFlag)) {
results.add("--" + valueFlag);
}
}
return results;
}

/**
* Register a class that contains commands (methods annotated with
* {@link Command}). If no dependency {@link Injector} is specified, then
Expand Down Expand Up @@ -444,11 +486,23 @@ public void setInjector(Injector injector) {

public static class CommandInfo {
private final Command commandAnnotation;
private List<String> valueFlags;

public CommandInfo(Command commandAnnotation) {
this.commandAnnotation = commandAnnotation;
}

private Collection<String> calculateValueFlags() {
valueFlags = new ArrayList<>();
String[] usage = commandAnnotation.usage().replace("(", "").replace(")", "").split(" ");
for (String part : usage) {
if (part.startsWith("--")) {
valueFlags.add(part.split("\\|")[0].replace("--", ""));
}
}
return valueFlags;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
Expand Down Expand Up @@ -477,6 +531,10 @@ public Command getCommandAnnotation() {
public int hashCode() {
return 31 + ((commandAnnotation == null) ? 0 : commandAnnotation.hashCode());
}

public Collection<String> valueFlags() {
return valueFlags == null ? calculateValueFlags() : valueFlags;
}
}

private static String capitalize(Object string) {
Expand Down

0 comments on commit 622b2f4

Please sign in to comment.