aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/cowtipper/cowlection/command/TabCompletableCommand.java
blob: 43ed2b64115f996cc810cabb914ae80dc2118a90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package de.cowtipper.cowlection.command;

import de.cowtipper.cowlection.Cowlection;
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 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 Cowlection main;
    private final String cmdName;

    public TabCompletableCommand(Cowlection main, String cmdName) {
        this.main = main;
        this.cmdName = cmdName;
    }

    @Override
    public String getCommandName() {
        return cmdName;
    }

    @Override
    public String getCommandUsage(ICommandSender sender) {
        return "/" + cmdName + " [additional parameters]. This client-command provides username tab-completion for the equivalent server-side command.";
    }

    @Override
    public void processCommand(ICommandSender sender, String[] args) throws CommandException {
        String prefix;
        String arguments;
        if (args.length >= 1 && args[0].equalsIgnoreCase("say")) {
            // work-around so you can still say '[commandName]' in chat without triggering the server-side command
            prefix = "";
            arguments = CommandBase.buildString(args, 1);
        } else {
            // send client-command to server
            prefix = "/";
            arguments = CommandBase.buildString(args, 0);
        }
        Minecraft.getMinecraft().thePlayer.sendChatMessage(prefix + getCommandName() + (!arguments.isEmpty() ? " " + arguments : ""));
    }

    @Override
    public int getRequiredPermissionLevel() {
        return 0;
    }

    @Override
    public List<String> 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;
    }
}