Search in sources :

Example 1 with RequiredRank

use of me.itsmas.network.server.command.annotations.RequiredRank in project Network-depr by Mas281.

the class CommandManager method handleMethod.

/**
 * Handles the registering of a command via its method
 *
 * @param object The object containing the method
 * @param method The method to handle
 */
private void handleMethod(Object object, Method method) {
    if (method.isAnnotationPresent(Command.class)) {
        if (method.getParameterCount() < 1) {
            // No parameters, invalid command
            return;
        }
        int paramIndex = -1;
        int minArgs = 0;
        int maxArgs = 0;
        boolean optional = false;
        for (Parameter parameter : method.getParameters()) {
            // There's already been an optional argument
            if (optional) {
                return;
            }
            paramIndex++;
            Class<?> paramClass = parameter.getType();
            if (paramIndex == 0) {
                if (paramClass != User.class) {
                    // First parameter is not a User, invalid command
                    return;
                }
                continue;
            }
            if (!isSupportedParameter(paramClass)) {
                // Parameter with no context found or not String
                return;
            }
            if (parameter.isAnnotationPresent(Optional.class)) {
                optional = true;
            } else {
                minArgs++;
            }
            maxArgs++;
        }
        if (method.getParameterCount() == 1) {
            maxArgs = Integer.MAX_VALUE;
        }
        String[] aliases = method.getDeclaredAnnotation(Command.class).value().split("\\|");
        String usage = getUsage(method);
        Rank requiredRank = getRequiredRank(method);
        Rank[] specificRanks = getSpecificRanks(method);
        CommandData data = new CommandData(object, method, aliases, usage, requiredRank, specificRanks, minArgs, maxArgs);
        commands.add(data);
    }
}
Also used : Parameter(java.lang.reflect.Parameter) RequiredRank(me.itsmas.network.server.command.annotations.RequiredRank) Rank(me.itsmas.network.server.rank.Rank)

Example 2 with RequiredRank

use of me.itsmas.network.server.command.annotations.RequiredRank in project Network-depr by Mas281.

the class UserCommands method onSetRankCommand.

@Command("setrank")
@Usage("<player> <rank> [prefix]")
@RequiredRank(Rank.ADMIN)
public void onSetRankCommand(User user, User target, Rank rank, @Optional String prefix) {
    if (prefix == null) {
        prefix = rank.getName();
    }
    target.setRank(rank, prefix);
    String formatted = target.getFormattedRank();
    core.getTaskManager().scheduleTask(new UpdateRankTask(target, rank, prefix, user.getName(), user.getUniqueId()));
    user.sendMessage("command.rank.executed", target.getName(), formatted);
    user.addLog("Updated rank of %s [%s] to %s [%s]", target.getName(), target.getUniqueId(), rank.getName(), prefix);
}
Also used : UpdateRankTask(me.itsmas.network.server.rank.UpdateRankTask) Usage(me.itsmas.network.server.command.annotations.Usage) Command(me.itsmas.network.server.command.annotations.Command) RequiredRank(me.itsmas.network.server.command.annotations.RequiredRank)

Example 3 with RequiredRank

use of me.itsmas.network.server.command.annotations.RequiredRank in project Network-depr by Mas281.

the class UserCommands method onGiveCommand.

@Command("give|item")
@Usage("<player> <item> [amount]")
@RequiredRank(Rank.DEV)
public void onGiveCommand(User user, User target, Material material, @Optional Integer amount) {
    if (amount == null || amount < 1) {
        amount = 1;
    }
    ItemStack stack = new ItemStack(material, amount);
    target.getPlayer().getInventory().addItem(stack);
    user.sendMessage("command.give.executed", amount, material.name(), target.getName());
    target.sendMessage("command.give.received", amount, material.name(), user.getName());
}
Also used : ItemStack(org.bukkit.inventory.ItemStack) Usage(me.itsmas.network.server.command.annotations.Usage) Command(me.itsmas.network.server.command.annotations.Command) RequiredRank(me.itsmas.network.server.command.annotations.RequiredRank)

Aggregations

RequiredRank (me.itsmas.network.server.command.annotations.RequiredRank)3 Command (me.itsmas.network.server.command.annotations.Command)2 Usage (me.itsmas.network.server.command.annotations.Usage)2 Parameter (java.lang.reflect.Parameter)1 Rank (me.itsmas.network.server.rank.Rank)1 UpdateRankTask (me.itsmas.network.server.rank.UpdateRankTask)1 ItemStack (org.bukkit.inventory.ItemStack)1