From e4191129a9a3624b4d69862e4bd5f498827e08c8 Mon Sep 17 00:00:00 2001 From: Cow Date: Wed, 29 Apr 2020 19:23:24 +0200 Subject: Simplified command handling with Tab-completable usernames - Replaced having just one command with several aliases for each supported command with having one command for each supported command - Fixes chat components with run_command click events to be executed (e.g. join a party) --- .../command/TabCompletableCommand.java | 50 +++++----------------- 1 file changed, 11 insertions(+), 39 deletions(-) (limited to 'src/main/java/eu/olli/cowmoonication/command/TabCompletableCommand.java') diff --git a/src/main/java/eu/olli/cowmoonication/command/TabCompletableCommand.java b/src/main/java/eu/olli/cowmoonication/command/TabCompletableCommand.java index b1ffa33..470f93c 100644 --- a/src/main/java/eu/olli/cowmoonication/command/TabCompletableCommand.java +++ b/src/main/java/eu/olli/cowmoonication/command/TabCompletableCommand.java @@ -1,63 +1,40 @@ package eu.olli.cowmoonication.command; -import com.mojang.realmsclient.util.Pair; import eu.olli.cowmoonication.Cowmoonication; -import eu.olli.cowmoonication.config.MooConfig; import net.minecraft.client.Minecraft; import net.minecraft.command.CommandBase; import net.minecraft.command.CommandException; import net.minecraft.command.ICommandSender; import net.minecraft.util.BlockPos; -import net.minecraft.util.EnumChatFormatting; -import org.apache.commons.lang3.StringUtils; -import java.util.Arrays; import java.util.List; +/** + * This is not a real command. Its sole purpose is to add tab completion for usernames to server-side commands that do not provide tab completion for usernames by default. + */ public class TabCompletableCommand extends CommandBase { private final Cowmoonication main; + private final String cmdName; - public TabCompletableCommand(Cowmoonication main) { + public TabCompletableCommand(Cowmoonication main, String cmdName) { this.main = main; + this.cmdName = cmdName; } @Override public String getCommandName() { - return "tabcompletablecommand"; + return cmdName; } @Override public String getCommandUsage(ICommandSender sender) { - return EnumChatFormatting.YELLOW + "Commands where player names can be Tab-completed: " + EnumChatFormatting.GOLD + StringUtils.join(getCommandAliases(), EnumChatFormatting.YELLOW + ", " + EnumChatFormatting.GOLD) - + EnumChatFormatting.YELLOW + ". Use " + EnumChatFormatting.GOLD + "/moo config " + EnumChatFormatting.YELLOW + " to edit the list of commands with tab-completable usernames."; + return null; } @Override public void processCommand(ICommandSender sender, String[] args) throws CommandException { - Pair lastCommand = getLastCommand(); - if (lastCommand == null) { - main.getChatHelper().sendMessage(EnumChatFormatting.RED, "Something went wrong trying to process this command."); - } else if (lastCommand.first().equalsIgnoreCase(getCommandName())) { - main.getChatHelper().sendMessage(EnumChatFormatting.YELLOW, getCommandUsage(sender)); - } else { - Minecraft.getMinecraft().thePlayer.sendChatMessage(lastCommand.second()); - } - } - - /** - * Work-around to get last used command name or alias (by default it's impossible to detect the used alias) - * - * @return 1st: last command used by thePlayer, 2nd: (full) last message sent thePlayer; or null if no command was sent as the last message - */ - private Pair getLastCommand() { - List sentMessages = Minecraft.getMinecraft().ingameGUI.getChatGUI().getSentMessages(); - String lastMessage = sentMessages.get(sentMessages.size() - 1); - if (lastMessage.startsWith("/")) { - int endOfCommandName = lastMessage.indexOf(" "); - return Pair.of(lastMessage.substring(1, endOfCommandName == -1 ? lastMessage.length() : endOfCommandName), - lastMessage); - } - return null; + // send client-command to server + Minecraft.getMinecraft().thePlayer.sendChatMessage("/" + getCommandName() + " " + CommandBase.buildString(args, 0)); } @Override @@ -65,15 +42,10 @@ public class TabCompletableCommand extends CommandBase { return 0; } - @Override - public List getCommandAliases() { - // list of commands that require a player name as one their 1st or 2nd argument - return Arrays.asList(MooConfig.tabCompletableNamesCommands); - } - @Override public List addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos) { if (args.length == 1 || args.length == 2) { + // suggest recently 'seen' usernames as tab-completion options. return getListOfStringsMatchingLastWord(args, main.getPlayerCache().getAllNamesSorted()); } return null; -- cgit