Search in sources :

Example 36 with Command

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

the class NPCCommands method pose.

@Command(aliases = { "npc" }, usage = "pose (--save [name]|--assume [name]|--remove [name]) (-a)", desc = "Changes/Saves/Lists NPC's head pose(s)", flags = "a", modifiers = { "pose" }, min = 1, max = 2, permission = "citizens.npc.pose")
public void pose(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    Poses trait = npc.getTrait(Poses.class);
    if (args.hasValueFlag("save")) {
        if (args.getFlag("save").isEmpty())
            throw new CommandException(Messages.INVALID_POSE_NAME);
        if (args.getSenderLocation() == null)
            throw new ServerCommandException();
        if (trait.addPose(args.getFlag("save"), args.getSenderLocation())) {
            Messaging.sendTr(sender, Messages.POSE_ADDED);
        } else
            throw new CommandException(Messages.POSE_ALREADY_EXISTS, args.getFlag("save"));
    } else if (args.hasValueFlag("assume")) {
        String pose = args.getFlag("assume");
        if (pose.isEmpty())
            throw new CommandException(Messages.INVALID_POSE_NAME);
        if (!trait.hasPose(pose))
            throw new CommandException(Messages.POSE_MISSING, pose);
        trait.assumePose(pose);
    } else if (args.hasValueFlag("remove")) {
        if (args.getFlag("remove").isEmpty())
            throw new CommandException(Messages.INVALID_POSE_NAME);
        if (trait.removePose(args.getFlag("remove"))) {
            Messaging.sendTr(sender, Messages.POSE_REMOVED);
        } else
            throw new CommandException(Messages.POSE_MISSING, args.getFlag("remove"));
    } else if (!args.hasFlag('a')) {
        trait.describe(sender, args.getInteger(1, 1));
    }
    // Assume Player's pose
    if (!args.hasFlag('a'))
        return;
    if (args.getSenderLocation() == null)
        throw new ServerCommandException();
    Location location = args.getSenderLocation();
    trait.assumePose(location);
}
Also used : ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) Poses(net.citizensnpcs.trait.Poses) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException) Location(org.bukkit.Location) CurrentLocation(net.citizensnpcs.trait.CurrentLocation) Command(net.citizensnpcs.api.command.Command)

Example 37 with Command

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

the class NPCCommands method tp.

@Command(aliases = { "npc" }, usage = "tp", desc = "Teleport to a NPC", modifiers = { "tp", "teleport" }, min = 1, max = 1, permission = "citizens.npc.tp")
public void tp(CommandContext args, Player player, NPC npc) {
    Location to = npc.getTrait(CurrentLocation.class).getLocation();
    if (to == null) {
        Messaging.sendError(player, Messages.TELEPORT_NPC_LOCATION_NOT_FOUND);
        return;
    }
    player.teleport(to, TeleportCause.COMMAND);
    Messaging.sendTr(player, Messages.TELEPORTED_TO_NPC, npc.getName());
}
Also used : CurrentLocation(net.citizensnpcs.trait.CurrentLocation) Location(org.bukkit.Location) CurrentLocation(net.citizensnpcs.trait.CurrentLocation) Command(net.citizensnpcs.api.command.Command)

Example 38 with Command

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

the class NPCCommands method create.

@Command(aliases = { "npc" }, usage = "create [name] ((-b,u) --at (x:y:z:world) --type (type) --trait ('trait1, trait2...') --b (behaviours))", desc = "Create a new NPC", flags = "bu", modifiers = { "create" }, min = 2, permission = "citizens.npc.create")
@Requirements
public void create(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    String name = Colorizer.parseColors(args.getJoinedStrings(1).trim());
    EntityType type = EntityType.PLAYER;
    if (args.hasValueFlag("type")) {
        String inputType = args.getFlag("type");
        type = Util.matchEntityType(inputType);
        if (type == null) {
            throw new CommandException(Messaging.tr(Messages.NPC_CREATE_INVALID_MOBTYPE, inputType));
        } else if (!EntityControllers.controllerExistsForType(type)) {
            throw new CommandException(Messaging.tr(Messages.NPC_CREATE_MISSING_MOBTYPE, inputType));
        }
    }
    int nameLength = type == EntityType.PLAYER ? 46 : 64;
    if (name.length() > nameLength) {
        Messaging.sendErrorTr(sender, Messages.NPC_NAME_TOO_LONG);
        name = name.substring(0, nameLength);
    }
    if (name.length() == 0)
        throw new CommandException();
    if (!sender.hasPermission("citizens.npc.create.*") && !sender.hasPermission("citizens.npc.createall") && !sender.hasPermission("citizens.npc.create." + type.name().toLowerCase().replace("_", "")))
        throw new NoPermissionsException();
    npc = npcRegistry.createNPC(type, name);
    String msg = "You created [[" + npc.getName() + "]]";
    int age = 0;
    if (args.hasFlag('b')) {
        if (!Ageable.class.isAssignableFrom(type.getEntityClass()))
            Messaging.sendErrorTr(sender, Messages.MOBTYPE_CANNOT_BE_AGED, type.name().toLowerCase().replace("_", "-"));
        else {
            age = -24000;
            msg += " as a baby";
        }
    }
    // Initialize necessary traits
    if (!Setting.SERVER_OWNS_NPCS.asBoolean()) {
        npc.getTrait(Owner.class).setOwner(sender);
    }
    npc.getTrait(MobType.class).setType(type);
    Location spawnLoc = null;
    if (sender instanceof Player) {
        spawnLoc = args.getSenderLocation();
    } else if (sender instanceof BlockCommandSender) {
        spawnLoc = args.getSenderLocation();
    }
    CommandSenderCreateNPCEvent event = sender instanceof Player ? new PlayerCreateNPCEvent((Player) sender, npc) : new CommandSenderCreateNPCEvent(sender, npc);
    Bukkit.getPluginManager().callEvent(event);
    if (event.isCancelled()) {
        npc.destroy();
        String reason = "Couldn't create NPC.";
        if (!event.getCancelReason().isEmpty())
            reason += " Reason: " + event.getCancelReason();
        throw new CommandException(reason);
    }
    if (args.hasValueFlag("at")) {
        spawnLoc = CommandContext.parseLocation(args.getSenderLocation(), args.getFlag("at"));
    }
    if (spawnLoc == null) {
        npc.destroy();
        throw new CommandException(Messages.INVALID_SPAWN_LOCATION);
    }
    if (!args.hasFlag('u')) {
        npc.spawn(spawnLoc);
    }
    if (args.hasValueFlag("trait")) {
        Iterable<String> parts = Splitter.on(',').trimResults().split(args.getFlag("trait"));
        StringBuilder builder = new StringBuilder();
        for (String tr : parts) {
            Trait trait = CitizensAPI.getTraitFactory().getTrait(tr);
            if (trait == null)
                continue;
            npc.addTrait(trait);
            builder.append(StringHelper.wrap(tr) + ", ");
        }
        if (builder.length() > 0)
            builder.delete(builder.length() - 2, builder.length());
        msg += " with traits " + builder.toString();
    }
    if (args.hasValueFlag("template")) {
        Iterable<String> parts = Splitter.on(',').trimResults().split(args.getFlag("template"));
        StringBuilder builder = new StringBuilder();
        for (String part : parts) {
            Template template = Template.byName(part);
            if (template == null)
                continue;
            template.apply(npc);
            builder.append(StringHelper.wrap(part) + ", ");
        }
        if (builder.length() > 0)
            builder.delete(builder.length() - 2, builder.length());
        msg += " with templates " + builder.toString();
    }
    // Set age after entity spawns
    if (npc.getEntity() instanceof Ageable) {
        npc.getTrait(Age.class).setAge(age);
    }
    selector.select(sender, npc);
    Messaging.send(sender, msg + '.');
}
Also used : PlayerCreateNPCEvent(net.citizensnpcs.api.event.PlayerCreateNPCEvent) Owner(net.citizensnpcs.api.trait.trait.Owner) Player(org.bukkit.entity.Player) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException) Ageable(org.bukkit.entity.Ageable) MobType(net.citizensnpcs.api.trait.trait.MobType) CommandSenderCreateNPCEvent(net.citizensnpcs.api.event.CommandSenderCreateNPCEvent) Template(net.citizensnpcs.npc.Template) EntityType(org.bukkit.entity.EntityType) NoPermissionsException(net.citizensnpcs.api.command.exception.NoPermissionsException) Trait(net.citizensnpcs.api.trait.Trait) WitherTrait(net.citizensnpcs.trait.WitherTrait) SheepTrait(net.citizensnpcs.trait.SheepTrait) ScriptTrait(net.citizensnpcs.trait.ScriptTrait) ArmorStandTrait(net.citizensnpcs.trait.ArmorStandTrait) Age(net.citizensnpcs.trait.Age) Location(org.bukkit.Location) CurrentLocation(net.citizensnpcs.trait.CurrentLocation) BlockCommandSender(org.bukkit.command.BlockCommandSender) Command(net.citizensnpcs.api.command.Command) Requirements(net.citizensnpcs.api.command.Requirements)

Example 39 with Command

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

the class NPCCommands method mount.

@Command(aliases = { "npc" }, usage = "mount (--onnpc <npc id>)", desc = "Mounts a controllable NPC", modifiers = { "mount" }, min = 1, max = 1, permission = "citizens.npc.controllable")
public void mount(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    if (args.hasValueFlag("onnpc")) {
        NPC mount;
        try {
            UUID uuid = UUID.fromString(args.getFlag("onnpc"));
            mount = CitizensAPI.getNPCRegistry().getByUniqueId(uuid);
        } catch (IllegalArgumentException ex) {
            mount = CitizensAPI.getNPCRegistry().getById(args.getFlagInteger("onnpc"));
        }
        if (mount == null || !mount.isSpawned()) {
            throw new CommandException(Messaging.tr(Messages.MOUNT_NPC_MUST_BE_SPAWNED, args.getFlag("onnpc")));
        }
        if (mount.equals(npc)) {
            throw new CommandException();
        }
        NMS.mount(mount.getEntity(), npc.getEntity());
        return;
    }
    boolean enabled = npc.hasTrait(Controllable.class) && npc.getTrait(Controllable.class).isEnabled();
    if (!enabled) {
        Messaging.sendTr(sender, Messages.NPC_NOT_CONTROLLABLE, npc.getName());
        return;
    }
    if (!(sender instanceof Player)) {
        throw new CommandException(CommandMessages.MUST_BE_INGAME);
    }
    Player player = (Player) sender;
    boolean success = npc.getTrait(Controllable.class).mount(player);
    if (!success) {
        Messaging.sendTr(player, Messages.FAILED_TO_MOUNT_NPC, npc.getName());
    }
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) Player(org.bukkit.entity.Player) Controllable(net.citizensnpcs.trait.Controllable) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException) UUID(java.util.UUID) Command(net.citizensnpcs.api.command.Command)

Example 40 with Command

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

the class NPCCommands method skinLayers.

@Command(aliases = { "npc" }, usage = "skinlayers (--cape [true|false]) (--hat [true|false]) (--jacket [true|false]) (--sleeves [true|false]) (--pants [true|false])", desc = "Sets an NPC's skin layers visibility.", modifiers = { "skinlayers" }, min = 1, max = 5, permission = "citizens.npc.skinlayers")
@Requirements(types = EntityType.PLAYER, selected = true, ownership = true)
public void skinLayers(final CommandContext args, final CommandSender sender, final NPC npc) throws CommandException {
    SkinLayers trait = npc.getTrait(SkinLayers.class);
    if (args.hasValueFlag("cape")) {
        trait.setVisible(Layer.CAPE, Boolean.valueOf(args.getFlag("cape")));
    }
    if (args.hasValueFlag("hat")) {
        trait.setVisible(Layer.HAT, Boolean.valueOf(args.getFlag("hat")));
    }
    if (args.hasValueFlag("jacket")) {
        trait.setVisible(Layer.JACKET, Boolean.valueOf(args.getFlag("jacket")));
    }
    if (args.hasValueFlag("sleeves")) {
        boolean hasSleeves = Boolean.valueOf(args.getFlag("sleeves"));
        trait.setVisible(Layer.LEFT_SLEEVE, hasSleeves);
        trait.setVisible(Layer.RIGHT_SLEEVE, hasSleeves);
    }
    if (args.hasValueFlag("pants")) {
        boolean hasPants = Boolean.valueOf(args.getFlag("pants"));
        trait.setVisible(Layer.LEFT_PANTS, hasPants);
        trait.setVisible(Layer.RIGHT_PANTS, hasPants);
    }
    Messaging.sendTr(sender, Messages.SKIN_LAYERS_SET, npc.getName(), trait.isVisible(Layer.CAPE), trait.isVisible(Layer.HAT), trait.isVisible(Layer.JACKET), trait.isVisible(Layer.LEFT_SLEEVE) || trait.isVisible(Layer.RIGHT_SLEEVE), trait.isVisible(Layer.LEFT_PANTS) || trait.isVisible(Layer.RIGHT_PANTS));
}
Also used : SkinLayers(net.citizensnpcs.trait.SkinLayers) Command(net.citizensnpcs.api.command.Command) Requirements(net.citizensnpcs.api.command.Requirements)

Aggregations

Command (net.citizensnpcs.api.command.Command)60 CommandException (net.citizensnpcs.api.command.exception.CommandException)40 Requirements (net.citizensnpcs.api.command.Requirements)39 ServerCommandException (net.citizensnpcs.api.command.exception.ServerCommandException)27 Player (org.bukkit.entity.Player)11 Location (org.bukkit.Location)10 NPC (net.citizensnpcs.api.npc.NPC)9 CurrentLocation (net.citizensnpcs.trait.CurrentLocation)9 DyeColor (org.bukkit.DyeColor)8 BarColor (org.bukkit.boss.BarColor)5 NoPermissionsException (net.citizensnpcs.api.command.exception.NoPermissionsException)4 Owner (net.citizensnpcs.api.trait.trait.Owner)4 Anchors (net.citizensnpcs.trait.Anchors)4 CommandUsageException (net.citizensnpcs.api.command.exception.CommandUsageException)3 MobType (net.citizensnpcs.api.trait.trait.MobType)3 SkinnableEntity (net.citizensnpcs.npc.skin.SkinnableEntity)3 ScriptTrait (net.citizensnpcs.trait.ScriptTrait)3 SheepTrait (net.citizensnpcs.trait.SheepTrait)3 LocationTag (com.denizenscript.denizen.objects.LocationTag)2 ArrayList (java.util.ArrayList)2