refactor: improve the CommandHandlerPlugin a bit

This commit is contained in:
ChomeNS
2025-04-03 09:46:37 +07:00
parent 9d6b0d8dc4
commit 13385de5d5
4 changed files with 22 additions and 22 deletions

View File

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

View File

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

View File

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