Search in sources :

Example 1 with CommandException

use of net.citizensnpcs.api.command.exception.CommandException in project Denizen-For-Bukkit by DenizenScript.

the class NPCCommandHandler method constants.

// <--[language]
// @name /npc constant command
// @group Console Commands
// @description
// The /npc constants command configures a NPC's constants. Uses Denizen's ConstantTrait to keep track of
// NPC-specific constants. This provides seamless integration with an assignment script's 'Default Constants' in
// which string variables can be stored and retrieved with the use of 'replaceable tags', or API. Constants set at
// the NPC level override any constants from the NPC's assignment script.
//
// Constants may be used in several ways: Setting, Removing, and Viewing
//
// To set a constant, all that is required is a name and value. Use the Bukkit command in the
// following manner: (Note the use of quotes on multi world values)
// /npc constant --set constant_name --value 'multi word value'
//
// Removing a constant from an NPC only requires a name. Note: It is not possible to remove a
// constant set by the NPCs Assignment Script, except by modifying the script itself.
// /npc constant --remove constant_name
//
// Viewing constants is easy, just use '/npc constant #', specifying a page number. Constants which
// have been overridden by the NPC are formatted with a strike-through to indicate this case.
//
// To reference a constant value, use the replaceable tag to get the NPCs 'constant' attribute. For example:
// <npc.constant[constant_name]>. Constants may also have other tags in their value, which will be replaced
// whenever the constant is used, allowing the use of dynamic information.
// -->
@Command(aliases = { "npc" }, usage = "constant --set|remove name --value constant value", desc = "Views/adds/removes NPC string constants.", flags = "r", modifiers = { "constants", "constant", "cons" }, min = 1, max = 3, permission = "denizen.npc.constants")
@Requirements(selected = true, ownership = true)
public void constants(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    if (!npc.hasTrait(ConstantsTrait.class)) {
        npc.addTrait(ConstantsTrait.class);
    }
    ConstantsTrait trait = npc.getTrait(ConstantsTrait.class);
    if (args.hasValueFlag("set")) {
        if (!args.hasValueFlag("value")) {
            throw new CommandException("--SET requires use of the '--VALUE \"constant value\"' argument.");
        }
        trait.setConstant(args.getFlag("set"), args.getFlag("value"));
        Messaging.sendInfo(sender, npc.getName() + " has added constant '" + args.getFlag("set") + "'.");
        return;
    } else if (args.hasValueFlag("remove")) {
        trait.removeConstant(args.getFlag("remove"));
        Messaging.sendInfo(sender, npc.getName() + " has removed constant '" + args.getFlag("remove") + "'.");
        return;
    } else if (args.length() > 2 && args.getInteger(1, 0) < 1) {
        Messaging.send(sender, "");
        Messaging.send(sender, "<f>Use '--set name' to add/set a new NPC-specific constant.");
        Messaging.send(sender, "<f>Must also specify '--value \"constant value\"'.");
        Messaging.send(sender, "<b>Example: /npc constant --set constant_1 --value \"test value\"");
        Messaging.send(sender, "<f>Remove NPC-specific constants with '--remove name'");
        Messaging.send(sender, "<f>Note: Constants set will override any specified in an");
        Messaging.send(sender, "<f>assignment. Constants specified in assignments cannot be");
        Messaging.send(sender, "<f>removed with this command.");
        Messaging.send(sender, "");
        return;
    }
    try {
        trait.describe(sender, args.getInteger(1, 1));
    } catch (net.citizensnpcs.api.command.exception.CommandException e) {
        throw new CommandException(e.getMessage());
    }
}
Also used : CommandException(net.citizensnpcs.api.command.exception.CommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException) Command(net.citizensnpcs.api.command.Command) Requirements(net.citizensnpcs.api.command.Requirements)

Example 2 with CommandException

use of net.citizensnpcs.api.command.exception.CommandException in project Denizen-For-Bukkit by DenizenScript.

the class NPCCommandHandler method trigger.

/*
     * TRIGGER
     */
@Command(aliases = { "npc" }, usage = "trigger [trigger name] [(--cooldown [seconds])|(--radius [radius])|(-t)]", desc = "Controls the various triggers for an NPC.", flags = "t", modifiers = { "trigger", "tr" }, min = 1, max = 3, permission = "denizen.npc.trigger")
@Requirements(selected = true, ownership = true)
public void trigger(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    if (!npc.hasTrait(TriggerTrait.class)) {
        npc.addTrait(TriggerTrait.class);
    }
    TriggerTrait trait = npc.getTrait(TriggerTrait.class);
    if ((args.hasValueFlag("name") || (args.argsLength() > 1 && (args.getJoinedStrings(1) != null) && !args.getString(1).matches("\\d+")))) {
        // Get the name of the trigger
        String triggerName;
        if (args.hasValueFlag("name")) {
            triggerName = args.getFlag("name");
        } else {
            triggerName = args.getJoinedStrings(1);
        }
        // Check to make sure trigger exists
        if (DenizenAPI.getCurrentInstance().getTriggerRegistry().get(triggerName) == null) {
            Messaging.sendError(sender, "'" + triggerName.toUpperCase() + "' trigger does not exist.");
            Messaging.send(sender, "<f>Usage: /npc trigger [trigger_name] [(--cooldown #)|(--radius #)|(-t)]");
            Messaging.send(sender, "");
            Messaging.send(sender, "<f>Use '--name trigger_name' to specify a specific trigger, and '-t' to toggle.");
            Messaging.send(sender, "<b>Example: /npc trigger --name damage -t");
            Messaging.send(sender, "<f>You may also use '--cooldown #' to specify a new cooldown time, and '--radius #' to specify a specific radius, when applicable.");
            Messaging.send(sender, "");
            return;
        }
        // If toggling
        if (args.hasFlag('t')) {
            trait.toggleTrigger(triggerName);
        }
        // If setting cooldown
        if (args.hasValueFlag("cooldown")) {
            trait.setLocalCooldown(triggerName, args.getFlagDouble("cooldown"));
        }
        // If specifying radius
        if (args.hasValueFlag("radius")) {
            trait.setLocalRadius(triggerName, args.getFlagInteger("radius"));
            Messaging.sendInfo(sender, triggerName.toUpperCase() + " trigger radius now " + args.getFlag("radius") + ".");
        }
        // Show current status of the trigger
        Messaging.sendInfo(sender, triggerName.toUpperCase() + " trigger " + (trait.isEnabled(triggerName) ? "is" : "is not") + " currently enabled" + (trait.isEnabled(triggerName) ? " with a cooldown of '" + trait.getCooldownDuration(triggerName) + "' seconds." : "."));
        return;
    }
    try {
        trait.describe(sender, args.getInteger(1, 1));
    } catch (net.citizensnpcs.api.command.exception.CommandException e) {
        throw new CommandException(e.getMessage());
    }
}
Also used : CommandException(net.citizensnpcs.api.command.exception.CommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException) Command(net.citizensnpcs.api.command.Command) Requirements(net.citizensnpcs.api.command.Requirements)

Example 3 with CommandException

use of net.citizensnpcs.api.command.exception.CommandException in project Denizen-For-Bukkit by DenizenScript.

the class ConstantsTrait method describe.

public void describe(CommandSender sender, int page) throws CommandException {
    Paginator paginator = new Paginator().header("Constants for " + npc.getName());
    paginator.addLine("<e>NPC-specific constants: " + (hasNPCConstants() ? "" : "None.") + "");
    if (hasNPCConstants()) {
        paginator.addLine("<e>Key: <a>Name  <b>Value");
    }
    for (Entry<String, String> constant : constants.entrySet()) {
        paginator.addLine("<a> " + String.valueOf(constant.getKey().charAt(0)).toUpperCase() + constant.getKey().substring(1) + "<b>  " + constant.getValue());
    }
    paginator.addLine("");
    if (npc.hasTrait(AssignmentTrait.class) && npc.getTrait(AssignmentTrait.class).hasAssignment()) {
        getAssignmentConstants();
        // List constants inherited from an Assignment.
        paginator.addLine("<e>Constants for assignment '" + assignment.toUpperCase() + "':");
        paginator.addLine("<e>Key: <a>Name  <b>Value");
        for (Entry<String, String> constant : getAssignmentConstants().entrySet()) {
            // change formatting to indicate so.
            if (constants.containsKey(constant.getKey())) {
                paginator.addLine("<m>" + String.valueOf(constant.getKey().charAt(0)).toUpperCase() + constant.getKey().substring(1) + "<r>  <m>" + constant.getValue());
            } else {
                paginator.addLine("<a>" + String.valueOf(constant.getKey().charAt(0)).toUpperCase() + constant.getKey().substring(1) + "<b>  " + constant.getValue());
            }
        }
        paginator.addLine("");
    }
    if (!paginator.sendPage(sender, page)) {
        throw new CommandException(Messages.COMMAND_PAGE_MISSING, page);
    }
}
Also used : CommandException(net.citizensnpcs.api.command.exception.CommandException) Paginator(net.citizensnpcs.api.util.Paginator)

Example 4 with CommandException

use of net.citizensnpcs.api.command.exception.CommandException in project Denizen-For-Bukkit by DenizenScript.

the class TriggerTrait method describe.

public void describe(CommandSender sender, int page) throws CommandException {
    Paginator paginator = new Paginator().header("Triggers");
    paginator.addLine("<e>Key: <a>Name  <b>Status  <c>Cooldown  <d>Cooldown Type  <e>(Radius)");
    for (Entry<String, Boolean> entry : enabled.entrySet()) {
        String line = "<a> " + entry.getKey() + "<b> " + (entry.getValue() ? "Enabled" : "Disabled") + "<c> " + getCooldownDuration(entry.getKey()) + "<d> " + getCooldownType(entry.getKey()).name() + "<e> " + (getRadius(entry.getKey()) == -1 ? "" : getRadius(entry.getKey()));
        paginator.addLine(line);
    }
    if (!paginator.sendPage(sender, page)) {
        throw new CommandException(Messages.COMMAND_PAGE_MISSING, page);
    }
}
Also used : CommandException(net.citizensnpcs.api.command.exception.CommandException) Paginator(net.citizensnpcs.api.util.Paginator)

Example 5 with CommandException

use of net.citizensnpcs.api.command.exception.CommandException in project CitizensAPI by CitizensDev.

the class CommandManager method sendSpecificHelp.

private void sendSpecificHelp(CommandSender sender, String rootCommand, String modifier) throws CommandException {
    CommandInfo info = getCommand(rootCommand, modifier);
    if (info == null)
        throw new CommandException(CommandMessages.COMMAND_MISSING, rootCommand + " " + modifier);
    Messaging.send(sender, format(info.getCommandAnnotation(), rootCommand));
    String help = Messaging.tryTranslate(info.getCommandAnnotation().help());
    if (help.isEmpty())
        return;
    Messaging.send(sender, ChatColor.AQUA + help);
}
Also used : UnhandledCommandException(net.citizensnpcs.api.command.exception.UnhandledCommandException) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) WrappedCommandException(net.citizensnpcs.api.command.exception.WrappedCommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException)

Aggregations

CommandException (net.citizensnpcs.api.command.exception.CommandException)49 Command (net.citizensnpcs.api.command.Command)40 ServerCommandException (net.citizensnpcs.api.command.exception.ServerCommandException)30 Requirements (net.citizensnpcs.api.command.Requirements)25 NPC (net.citizensnpcs.api.npc.NPC)9 Player (org.bukkit.entity.Player)8 Paginator (net.citizensnpcs.api.util.Paginator)7 Location (org.bukkit.Location)7 CurrentLocation (net.citizensnpcs.trait.CurrentLocation)6 DyeColor (org.bukkit.DyeColor)5 NoPermissionsException (net.citizensnpcs.api.command.exception.NoPermissionsException)4 Owner (net.citizensnpcs.api.trait.trait.Owner)4 UnhandledCommandException (net.citizensnpcs.api.command.exception.UnhandledCommandException)3 WrappedCommandException (net.citizensnpcs.api.command.exception.WrappedCommandException)3 SkinnableEntity (net.citizensnpcs.npc.skin.SkinnableEntity)3 CommandUsageException (net.citizensnpcs.api.command.exception.CommandUsageException)2 CommandSenderCreateNPCEvent (net.citizensnpcs.api.event.CommandSenderCreateNPCEvent)2 PlayerCreateNPCEvent (net.citizensnpcs.api.event.PlayerCreateNPCEvent)2 Template (net.citizensnpcs.npc.Template)2 Age (net.citizensnpcs.trait.Age)2