Skip to content

Commit

Permalink
Minor changes in command tab completion
Browse files Browse the repository at this point in the history
  • Loading branch information
danae committed Aug 28, 2024
1 parent 150f076 commit dd2b4b9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.danae.creativesuite.plugin.commands;

import dev.danae.commons.commands.Command;
import dev.danae.commons.commands.CommandContext;
import dev.danae.commons.commands.CommandUtils;
import dev.danae.commons.messages.MessageManager;
import dev.danae.commons.messages.NamespacedKeyFormatter;
Expand All @@ -11,6 +12,7 @@
import java.util.List;
import java.util.Map;
import net.md_5.bungee.api.chat.BaseComponent;
import org.bukkit.Bukkit;


public abstract class ManagerCommand extends Command implements MessageManager
Expand Down Expand Up @@ -89,4 +91,17 @@ public List<String> handleHotbarTabCompletion(String arg)
.map(key -> key.toString())
.toList());
}

// Handle tab completion for a server command
public List<String> handleCommandTabCompletion(CommandContext context, int startIndex)
{
// Arguments of the command, so normal handling
if (context.hasAtLeastArgumentsCount(startIndex + 2))
return Bukkit.getCommandMap().tabComplete(context.getSender(), context.getJoinedArguments(startIndex));

// The command itself, so strip the first slash
return Bukkit.getCommandMap().tabComplete(context.getSender(), context.getJoinedArguments(startIndex)).stream()
.map(command -> command.startsWith("/") ? command.substring(1) : command)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private MessageFunction createCommandComponent(Alias alias)
{
return (String content) -> {
var formattedCommand = "/" + Parameter.replace(alias.getCommand(), createParameterFormatter(content));
var command = "/" + Parameter.replace(alias.getCommand(), Map.of());
var command = Parameter.replace(alias.getCommand(), Map.of());

return new ComponentBuilder()
.append("")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void handle(CommandContext context) throws CommandException, CommandUsage
public List<String> handleTabCompletion(CommandContext context)
{
if (context.hasAtLeastArgumentsCount(2))
return Bukkit.getCommandMap().tabComplete(context.getSender(), context.getJoinedArguments(1));
return this.handleCommandTabCompletion(context, 1);
if (context.hasArgumentsCount(1))
return this.handleAliasTabCompletion(context.getArgument(0));
else
Expand Down

0 comments on commit dd2b4b9

Please sign in to comment.