Search in sources :

Example 41 with CommandException

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

the class NPCCommands method item.

@Command(aliases = { "npc" }, usage = "item [item] (data)", desc = "Sets the NPC's item", modifiers = { "item" }, min = 2, max = 3, flags = "", permission = "citizens.npc.item")
@Requirements(selected = true, ownership = true, types = { EntityType.DROPPED_ITEM, EntityType.ITEM_FRAME, EntityType.FALLING_BLOCK })
public void item(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    Material mat = Material.matchMaterial(args.getString(1));
    if (mat == null)
        throw new CommandException(Messages.UNKNOWN_MATERIAL);
    int data = args.getInteger(2, 0);
    npc.data().setPersistent(NPC.ITEM_ID_METADATA, mat.name());
    npc.data().setPersistent(NPC.ITEM_DATA_METADATA, data);
    switch(npc.getEntity().getType()) {
        case DROPPED_ITEM:
            ((org.bukkit.entity.Item) npc.getEntity()).getItemStack().setType(mat);
            break;
        case ITEM_FRAME:
            ((ItemFrame) npc.getEntity()).getItem().setType(mat);
            break;
        default:
            break;
    }
    if (npc.isSpawned()) {
        npc.despawn();
        npc.spawn(npc.getStoredLocation());
    }
    Messaging.sendTr(sender, Messages.ITEM_SET, Util.prettyEnum(mat));
}
Also used : Material(org.bukkit.Material) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException) Command(net.citizensnpcs.api.command.Command) Requirements(net.citizensnpcs.api.command.Requirements)

Example 42 with CommandException

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

the class NPCCommands method copy.

@Command(aliases = { "npc" }, usage = "copy (--name newname)", desc = "Copies an NPC", modifiers = { "copy" }, min = 1, max = 1, permission = "citizens.npc.copy")
public void copy(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    String name = args.getFlag("name", npc.getFullName());
    NPC copy = npc.clone();
    if (!copy.getFullName().equals(name)) {
        copy.setName(name);
    }
    if (copy.isSpawned() && args.getSenderLocation() != null) {
        Location location = args.getSenderLocation();
        location.getChunk().load();
        copy.teleport(location, TeleportCause.COMMAND);
        copy.getTrait(CurrentLocation.class).setLocation(location);
    }
    CommandSenderCreateNPCEvent event = sender instanceof Player ? new PlayerCreateNPCEvent((Player) sender, copy) : new CommandSenderCreateNPCEvent(sender, copy);
    Bukkit.getPluginManager().callEvent(event);
    if (event.isCancelled()) {
        event.getNPC().destroy();
        String reason = "Couldn't create NPC.";
        if (!event.getCancelReason().isEmpty())
            reason += " Reason: " + event.getCancelReason();
        throw new CommandException(reason);
    }
    Messaging.sendTr(sender, Messages.NPC_COPIED, npc.getName());
    selector.select(sender, copy);
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) PlayerCreateNPCEvent(net.citizensnpcs.api.event.PlayerCreateNPCEvent) Player(org.bukkit.entity.Player) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException) CurrentLocation(net.citizensnpcs.trait.CurrentLocation) CommandSenderCreateNPCEvent(net.citizensnpcs.api.event.CommandSenderCreateNPCEvent) Location(org.bukkit.Location) CurrentLocation(net.citizensnpcs.trait.CurrentLocation) Command(net.citizensnpcs.api.command.Command)

Example 43 with CommandException

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

the class TemplateCommands method apply.

@Command(aliases = { "template", "tpl" }, usage = "apply [template name] (id id2...)", desc = "Applies a template to the selected NPC", modifiers = { "apply" }, min = 2, permission = "citizens.templates.apply")
@Requirements
public void apply(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    Template template = Template.byName(args.getString(1));
    if (template == null)
        throw new CommandException(Messages.TEMPLATE_MISSING);
    int appliedCount = 0;
    if (args.argsLength() == 2) {
        if (npc == null)
            throw new CommandException(Messaging.tr(Messages.COMMAND_MUST_HAVE_SELECTED));
        template.apply(npc);
        appliedCount++;
    } else {
        String joined = args.getJoinedStrings(2, ',');
        List<Integer> ids = Lists.newArrayList();
        for (String id : Splitter.on(',').trimResults().split(joined)) {
            int parsed = Integer.parseInt(id);
            ids.add(parsed);
        }
        Iterable<NPC> transformed = Iterables.transform(ids, new Function<Integer, NPC>() {

            @Override
            public NPC apply(@Nullable Integer arg0) {
                if (arg0 == null)
                    return null;
                return CitizensAPI.getNPCRegistry().getById(arg0);
            }
        });
        for (NPC toApply : transformed) {
            template.apply(toApply);
            appliedCount++;
        }
    }
    Messaging.sendTr(sender, Messages.TEMPLATE_APPLIED, appliedCount);
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) CommandException(net.citizensnpcs.api.command.exception.CommandException) Template(net.citizensnpcs.npc.Template) Command(net.citizensnpcs.api.command.Command) Requirements(net.citizensnpcs.api.command.Requirements)

Example 44 with CommandException

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

the class TemplateCommands method create.

@Command(aliases = { "template", "tpl" }, usage = "create [template name] (-o)", desc = "Creates a template from the selected NPC", modifiers = { "create" }, min = 2, max = 2, flags = "o", permission = "citizens.templates.create")
public void create(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    String name = args.getString(1);
    if (Template.byName(name) != null)
        throw new CommandException(Messages.TEMPLATE_CONFLICT);
    TemplateBuilder.create(name).from(npc).override(args.hasFlag('o')).buildAndSave();
    Messaging.sendTr(sender, Messages.TEMPLATE_CREATED);
}
Also used : CommandException(net.citizensnpcs.api.command.exception.CommandException) Command(net.citizensnpcs.api.command.Command)

Example 45 with CommandException

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

the class TemplateCommands method delete.

@Command(aliases = { "template", "tpl" }, usage = "delete [template name]", desc = "Deletes a template", modifiers = { "delete" }, min = 2, max = 2, permission = "citizens.templates.delete")
public void delete(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    String name = args.getString(1);
    if (Template.byName(name) == null)
        throw new CommandException(Messages.TEMPLATE_MISSING);
    Template.byName(name).delete();
    Messaging.sendTr(sender, Messages.TEMPLATE_DELETED, name);
}
Also used : CommandException(net.citizensnpcs.api.command.exception.CommandException) Command(net.citizensnpcs.api.command.Command)

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