Search in sources :

Example 1 with Requirements

use of net.citizensnpcs.api.command.Requirements 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 Requirements

use of net.citizensnpcs.api.command.Requirements 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 Requirements

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

the class NPCCommandHandler method assignment.

/*
     * ASSIGNMENT
     */
@Command(aliases = { "npc" }, usage = "assignment --set assignment_name (-r)", desc = "Controls the assignment for an NPC.", flags = "r", modifiers = { "assignment", "assign" }, min = 1, max = 3, permission = "denizen.npc.assign")
@Requirements(selected = true, ownership = true)
public void assignment(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    if (!npc.hasTrait(AssignmentTrait.class)) {
        npc.addTrait(AssignmentTrait.class);
    }
    Player player = null;
    if (sender instanceof Player) {
        player = (Player) sender;
    }
    AssignmentTrait trait = npc.getTrait(AssignmentTrait.class);
    if (args.hasValueFlag("set")) {
        String script = args.getFlag("set").replace("\"", "");
        if (trait.setAssignment(script, dPlayer.mirrorBukkitPlayer(player))) {
            if (trait.hasAssignment()) {
                Messaging.sendInfo(sender, npc.getName() + "'s assignment is now: '" + trait.getAssignment().getName() + "'.");
            } else {
                Messaging.sendInfo(sender, npc.getName() + "'s assignment was not able to be set.");
            }
        } else if (ScriptRegistry.containsScript(script)) {
            Messaging.sendError(sender, "A script with that name exists, but it is not an assignment script!");
        } else {
            Messaging.sendError(sender, "Invalid assignment! Has the script sucessfully loaded, or has it been mispelled?");
        }
        return;
    } else if (args.hasFlag('r')) {
        trait.removeAssignment(dPlayer.mirrorBukkitPlayer(player));
        Messaging.sendInfo(sender, npc.getName() + "'s assignment has been removed.");
        return;
    } else if (args.length() > 2 && args.getInteger(1, 0) < 1) {
        Messaging.send(sender, "");
        Messaging.send(sender, "<f>Use '--set name' to set an assignment script to this NPC.");
        Messaging.send(sender, "<b>Example: /npc assignment --set \"Magic Shop\"");
        Messaging.send(sender, "<f>Remove an assignment with '-r'.");
        Messaging.send(sender, "<f>Note: Assigning a script will fire an 'On Assignment:' action.");
        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 : net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) Player(org.bukkit.entity.Player) 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)

Aggregations

Command (net.citizensnpcs.api.command.Command)3 Requirements (net.citizensnpcs.api.command.Requirements)3 CommandException (net.citizensnpcs.api.command.exception.CommandException)3 net.aufdemrand.denizen.objects.dPlayer (net.aufdemrand.denizen.objects.dPlayer)1 Player (org.bukkit.entity.Player)1