Search in sources :

Example 1 with Command

use of cn.nukkit.command.Command in project Nukkit by Nukkit.

the class Player method sendCommandData.

public void sendCommandData() {
    AvailableCommandsPacket pk = new AvailableCommandsPacket();
    Map<String, CommandDataVersions> data = new HashMap<>();
    int count = 0;
    for (Command command : this.server.getCommandMap().getCommands().values()) {
        if (!command.testPermissionSilent(this)) {
            continue;
        }
        ++count;
        CommandDataVersions data0 = command.generateCustomCommandData(this);
        data.put(command.getName(), data0);
    }
    if (count > 0) {
        // TODO: structure checking
        pk.commands = data;
        // We *need* ACK so we can be sure that the client received the packet or not
        int identifier = this.dataPacket(pk, true);
        Thread t = new Thread() {

            public void run() {
                // We are going to wait 3 seconds, if after 3 seconds we didn't receive a reply from the client, resend the packet.
                try {
                    Thread.sleep(3000);
                    Boolean status = needACK.get(identifier);
                    if ((status == null || !status) && isOnline()) {
                        sendCommandData();
                        return;
                    }
                } catch (InterruptedException e) {
                }
            }
        };
        t.start();
    }
}
Also used : Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) Long2ObjectLinkedOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap) Command(cn.nukkit.command.Command) CommandDataVersions(cn.nukkit.command.data.CommandDataVersions)

Example 2 with Command

use of cn.nukkit.command.Command in project Nukkit by Nukkit.

the class HelpCommand method execute.

@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }
    String command = "";
    int pageNumber = 1;
    int pageHeight = 5;
    if (args.length != 0) {
        try {
            pageNumber = Integer.valueOf(args[args.length - 1]);
            if (pageNumber <= 0) {
                pageNumber = 1;
            }
            String[] newargs = new String[args.length - 1];
            System.arraycopy(args, 0, newargs, 0, newargs.length);
            args = newargs;
            /*if (args.length > 1) {
                    args = Arrays.copyOfRange(args, 0, args.length - 2);
                } else {
                    args = new String[0];
                }*/
            for (String arg : args) {
                if (!command.equals("")) {
                    command += " ";
                }
                command += arg;
            }
        } catch (NumberFormatException e) {
            pageNumber = 1;
            for (String arg : args) {
                if (!command.equals("")) {
                    command += " ";
                }
                command += arg;
            }
        }
    }
    if (sender instanceof ConsoleCommandSender) {
        pageHeight = Integer.MAX_VALUE;
    }
    if (command.equals("")) {
        Map<String, Command> commands = new TreeMap<>();
        for (Command cmd : sender.getServer().getCommandMap().getCommands().values()) {
            if (cmd.testPermissionSilent(sender)) {
                commands.put(cmd.getName(), cmd);
            }
        }
        int totalPage = commands.size() % pageHeight == 0 ? commands.size() / pageHeight : commands.size() / pageHeight + 1;
        pageNumber = Math.min(pageNumber, totalPage);
        if (pageNumber < 1) {
            pageNumber = 1;
        }
        sender.sendMessage(new TranslationContainer("commands.help.header", new String[] { String.valueOf(pageNumber), String.valueOf(totalPage) }));
        int i = 1;
        for (Command command1 : commands.values()) {
            if (i >= (pageNumber - 1) * pageHeight + 1 && i <= Math.min(commands.size(), pageNumber * pageHeight)) {
                sender.sendMessage(TextFormat.DARK_GREEN + "/" + command1.getName() + ": " + TextFormat.WHITE + command1.getDescription());
            }
            i++;
        }
        return true;
    } else {
        Command cmd = sender.getServer().getCommandMap().getCommand(command.toLowerCase());
        if (cmd != null) {
            if (cmd.testPermissionSilent(sender)) {
                String message = TextFormat.YELLOW + "--------- " + TextFormat.WHITE + " Help: /" + cmd.getName() + TextFormat.YELLOW + " ---------\n";
                message += TextFormat.GOLD + "Description: " + TextFormat.WHITE + cmd.getDescription() + "\n";
                String usage = "";
                String[] usages = cmd.getUsage().split("\n");
                for (String u : usages) {
                    if (!usage.equals("")) {
                        usage += "\n" + TextFormat.WHITE;
                    }
                    usage += u;
                }
                message += TextFormat.GOLD + "Usage: " + TextFormat.WHITE + usage + "\n";
                sender.sendMessage(message);
                return true;
            }
        }
        sender.sendMessage(TextFormat.RED + "No help for " + command.toLowerCase());
        return true;
    }
}
Also used : Command(cn.nukkit.command.Command) TranslationContainer(cn.nukkit.lang.TranslationContainer) TreeMap(java.util.TreeMap) ConsoleCommandSender(cn.nukkit.command.ConsoleCommandSender)

Aggregations

Command (cn.nukkit.command.Command)2 ConsoleCommandSender (cn.nukkit.command.ConsoleCommandSender)1 CommandDataVersions (cn.nukkit.command.data.CommandDataVersions)1 TranslationContainer (cn.nukkit.lang.TranslationContainer)1 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)1 Long2ObjectLinkedOpenHashMap (it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap)1 Long2ObjectOpenHashMap (it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap)1 TreeMap (java.util.TreeMap)1