refactor: improve the CommandHandlerPlugin a bit
This commit is contained in:
@@ -74,7 +74,7 @@ public class HelpCommand extends Command {
|
||||
|
||||
List<String> commandNames = new ArrayList<>();
|
||||
|
||||
for (Command command : CommandHandlerPlugin.commands) {
|
||||
for (Command command : CommandHandlerPlugin.COMMANDS) {
|
||||
if (command.trustLevel != trustLevel || (command.consoleOnly && !(context instanceof ConsoleCommandContext)))
|
||||
continue;
|
||||
|
||||
@@ -108,7 +108,7 @@ public class HelpCommand extends Command {
|
||||
|
||||
final String prefix = context.prefix;
|
||||
|
||||
for (Command command : CommandHandlerPlugin.commands) {
|
||||
for (Command command : CommandHandlerPlugin.COMMANDS) {
|
||||
if (
|
||||
!command.name.equalsIgnoreCase(commandName) &&
|
||||
!Arrays.stream(command.aliases).toList().contains(commandName.toLowerCase())
|
||||
|
||||
@@ -23,7 +23,7 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class CommandHandlerPlugin implements TickPlugin.Listener {
|
||||
public static final List<Command> commands = new ArrayList<>();
|
||||
public static final List<Command> COMMANDS = new ArrayList<>();
|
||||
|
||||
static {
|
||||
registerCommand(new CommandBlockCommand());
|
||||
@@ -68,7 +68,22 @@ public class CommandHandlerPlugin implements TickPlugin.Listener {
|
||||
}
|
||||
|
||||
public static void registerCommand (Command command) {
|
||||
commands.add(command);
|
||||
COMMANDS.add(command);
|
||||
}
|
||||
|
||||
public static Command findCommand (String searchTerm) {
|
||||
if (searchTerm.isBlank()) return null;
|
||||
|
||||
for (Command command : COMMANDS) {
|
||||
if (
|
||||
command.name.equals(searchTerm.toLowerCase()) ||
|
||||
Arrays.stream(command.aliases).toList().contains(searchTerm.toLowerCase())
|
||||
) {
|
||||
return command;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean disabled = false;
|
||||
@@ -110,7 +125,7 @@ public class CommandHandlerPlugin implements TickPlugin.Listener {
|
||||
|
||||
final String commandName = splitInput[0];
|
||||
|
||||
final Command command = findCommand(commands, commandName);
|
||||
final Command command = findCommand(commandName);
|
||||
|
||||
// I think this is kinda annoying when you correct spelling mistakes or something,
|
||||
// so I made it return nothing if it's in game
|
||||
@@ -217,19 +232,4 @@ public class CommandHandlerPlugin implements TickPlugin.Listener {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Command findCommand (List<Command> commands, String searchTerm) {
|
||||
for (Command command : commands) {
|
||||
if (
|
||||
(
|
||||
command.name.equals(searchTerm.toLowerCase()) ||
|
||||
Arrays.stream(command.aliases).toList().contains(searchTerm.toLowerCase())
|
||||
) &&
|
||||
!searchTerm.isEmpty() // ig yup
|
||||
) {
|
||||
return command;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ public class CommandSuggestionPlugin implements ChatPlugin.Listener {
|
||||
final List<Component> output = new ArrayList<>();
|
||||
output.add(Component.text(ID));
|
||||
|
||||
for (Command command : CommandHandlerPlugin.commands) {
|
||||
for (Command command : CommandHandlerPlugin.COMMANDS) {
|
||||
if (command.consoleOnly) continue;
|
||||
|
||||
final boolean hasAliases = command.aliases.length != 0;
|
||||
|
||||
Reference in New Issue
Block a user