Search in sources :

Example 36 with CommandException

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

the class NPCCommands method skin.

@Command(aliases = { "npc" }, usage = "skin (-c -l(atest)) [name]", desc = "Sets an NPC's skin name. Use -l to set the skin to always update to the latest", modifiers = { "skin" }, min = 1, max = 2, flags = "cl", permission = "citizens.npc.skin")
@Requirements(types = EntityType.PLAYER, selected = true, ownership = true)
public void skin(final CommandContext args, final CommandSender sender, final NPC npc) throws CommandException {
    String skinName = npc.getName();
    if (args.hasFlag('c')) {
        npc.data().remove(NPC.PLAYER_SKIN_UUID_METADATA);
    } else {
        if (args.argsLength() != 2)
            throw new CommandException(Messages.SKIN_REQUIRED);
        npc.data().setPersistent(NPC.PLAYER_SKIN_UUID_METADATA, args.getString(1));
        if (args.hasFlag('l')) {
            npc.data().setPersistent(NPC.PLAYER_SKIN_USE_LATEST, true);
        }
        skinName = args.getString(1);
    }
    Messaging.sendTr(sender, Messages.SKIN_SET, npc.getName(), skinName);
    if (npc.isSpawned()) {
        SkinnableEntity skinnable = npc.getEntity() instanceof SkinnableEntity ? (SkinnableEntity) npc.getEntity() : null;
        if (skinnable != null) {
            skinnable.setSkinName(skinName, true);
        }
    }
}
Also used : SkinnableEntity(net.citizensnpcs.npc.skin.SkinnableEntity) 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 37 with CommandException

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

the class NPCCommands method owner.

@Command(aliases = { "npc" }, usage = "owner [name]", desc = "Set the owner of an NPC", modifiers = { "owner" }, min = 1, max = 2, permission = "citizens.npc.owner")
public void owner(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    Owner ownerTrait = npc.getTrait(Owner.class);
    if (args.argsLength() == 1) {
        Messaging.sendTr(sender, Messages.NPC_OWNER, npc.getName(), ownerTrait.getOwner());
        return;
    }
    String name = args.getString(1);
    if (ownerTrait.isOwnedBy(name))
        throw new CommandException(Messages.ALREADY_OWNER, name, npc.getName());
    ownerTrait.setOwner(name);
    boolean serverOwner = name.equalsIgnoreCase(Owner.SERVER);
    Messaging.sendTr(sender, serverOwner ? Messages.OWNER_SET_SERVER : Messages.OWNER_SET, npc.getName(), name);
}
Also used : Owner(net.citizensnpcs.api.trait.trait.Owner) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException) Command(net.citizensnpcs.api.command.Command)

Example 38 with CommandException

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

the class NPCCommands method tpto.

@Command(aliases = { "npc" }, usage = "tpto [player name|npc id] [player name|npc id]", desc = "Teleport an NPC or player to another NPC or player", modifiers = { "tpto" }, min = 3, max = 3, permission = "citizens.npc.tpto")
@Requirements
public void tpto(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    Entity from = null, to = null;
    if (npc != null) {
        from = npc.getEntity();
    }
    boolean firstWasPlayer = false;
    try {
        int id = args.getInteger(1);
        NPC fromNPC = CitizensAPI.getNPCRegistry().getById(id);
        if (fromNPC != null) {
            from = fromNPC.getEntity();
        }
    } catch (NumberFormatException e) {
        from = Bukkit.getPlayerExact(args.getString(1));
        firstWasPlayer = true;
    }
    try {
        int id = args.getInteger(2);
        NPC toNPC = CitizensAPI.getNPCRegistry().getById(id);
        if (toNPC != null) {
            to = toNPC.getEntity();
        }
    } catch (NumberFormatException e) {
        if (!firstWasPlayer) {
            to = Bukkit.getPlayerExact(args.getString(2));
        }
    }
    if (from == null)
        throw new CommandException(Messages.FROM_ENTITY_NOT_FOUND);
    if (to == null)
        throw new CommandException(Messages.TO_ENTITY_NOT_FOUND);
    from.teleport(to);
    Messaging.sendTr(sender, Messages.TPTO_SUCCESS);
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) Entity(org.bukkit.entity.Entity) SkinnableEntity(net.citizensnpcs.npc.skin.SkinnableEntity) 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 39 with CommandException

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

the class NPCCommands method spawn.

@Command(aliases = { "npc" }, usage = "spawn (id|name) -l(oad chunks)", desc = "Spawn an existing NPC", modifiers = { "spawn" }, min = 1, max = 2, flags = "l", permission = "citizens.npc.spawn")
@Requirements(ownership = true)
public void spawn(final CommandContext args, final CommandSender sender, NPC npc) throws CommandException {
    NPCCommandSelector.Callback callback = new NPCCommandSelector.Callback() {

        @Override
        public void run(NPC respawn) throws CommandException {
            if (respawn == null) {
                if (args.argsLength() > 1) {
                    throw new CommandException(Messages.NO_NPC_WITH_ID_FOUND, args.getString(1));
                } else {
                    throw new CommandException(CommandMessages.MUST_HAVE_SELECTED);
                }
            }
            if (respawn.isSpawned()) {
                throw new CommandException(Messages.NPC_ALREADY_SPAWNED, respawn.getName());
            }
            Location location = respawn.getTrait(CurrentLocation.class).getLocation();
            if (location == null || args.hasValueFlag("location")) {
                if (args.getSenderLocation() == null)
                    throw new CommandException(Messages.NO_STORED_SPAWN_LOCATION);
                location = args.getSenderLocation();
            }
            if (args.hasFlag('l') && !Util.isLoaded(location)) {
                location.getChunk().load();
            }
            if (respawn.spawn(location)) {
                selector.select(sender, respawn);
                Messaging.sendTr(sender, Messages.NPC_SPAWNED, respawn.getName());
            }
        }
    };
    if (args.argsLength() > 1) {
        NPCCommandSelector.startWithCallback(callback, npcRegistry, sender, args, args.getString(1));
    } else {
        callback.run(npc);
    }
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException) CurrentLocation(net.citizensnpcs.trait.CurrentLocation) Location(org.bukkit.Location) CurrentLocation(net.citizensnpcs.trait.CurrentLocation) Command(net.citizensnpcs.api.command.Command) Requirements(net.citizensnpcs.api.command.Requirements)

Example 40 with CommandException

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

the class NPCCommands method sheep.

@Command(aliases = { "npc" }, usage = "sheep (--color [color]) (--sheared [sheared])", desc = "Sets sheep modifiers", modifiers = { "sheep" }, min = 1, max = 1, permission = "citizens.npc.sheep")
@Requirements(selected = true, ownership = true, types = { EntityType.SHEEP })
public void sheep(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    SheepTrait trait = npc.getTrait(SheepTrait.class);
    boolean hasArg = false;
    if (args.hasValueFlag("sheared")) {
        trait.setSheared(Boolean.valueOf(args.getFlag("sheared")));
        hasArg = true;
    }
    if (args.hasValueFlag("color")) {
        DyeColor color = Util.matchEnum(DyeColor.values(), args.getFlag("color"));
        if (color != null) {
            trait.setColor(color);
            Messaging.sendTr(sender, Messages.SHEEP_COLOR_SET, color.toString().toLowerCase());
        } else {
            Messaging.sendErrorTr(sender, Messages.INVALID_SHEEP_COLOR, Util.listValuesPretty(DyeColor.values()));
        }
        hasArg = true;
    }
    if (!hasArg) {
        throw new CommandException();
    }
}
Also used : ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException) DyeColor(org.bukkit.DyeColor) SheepTrait(net.citizensnpcs.trait.SheepTrait) Command(net.citizensnpcs.api.command.Command) Requirements(net.citizensnpcs.api.command.Requirements)

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