Search in sources :

Example 1 with Controllable

use of net.citizensnpcs.trait.Controllable in project Citizens2 by CitizensDev.

the class NPCCommands method controllable.

@Command(aliases = { "npc" }, usage = "controllable|control (-m(ount),-y,-n,-o)", desc = "Toggles whether the NPC can be ridden and controlled", modifiers = { "controllable", "control" }, min = 1, max = 1, flags = "myno")
public void controllable(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    if ((npc.isSpawned() && !sender.hasPermission("citizens.npc.controllable." + npc.getEntity().getType().name().toLowerCase().replace("_", ""))) || !sender.hasPermission("citizens.npc.controllable"))
        throw new NoPermissionsException();
    if (!npc.hasTrait(Controllable.class)) {
        npc.addTrait(new Controllable(false));
    }
    Controllable trait = npc.getTrait(Controllable.class);
    boolean enabled = trait.toggle();
    if (args.hasFlag('y')) {
        enabled = trait.setEnabled(true);
    } else if (args.hasFlag('n')) {
        enabled = trait.setEnabled(false);
    }
    trait.setOwnerRequired(args.hasFlag('o'));
    String key = enabled ? Messages.CONTROLLABLE_SET : Messages.CONTROLLABLE_REMOVED;
    Messaging.sendTr(sender, key, npc.getName());
    if (enabled && args.hasFlag('m') && sender instanceof Player) {
        trait.mount((Player) sender);
    }
}
Also used : Player(org.bukkit.entity.Player) Controllable(net.citizensnpcs.trait.Controllable) NoPermissionsException(net.citizensnpcs.api.command.exception.NoPermissionsException) Command(net.citizensnpcs.api.command.Command)

Example 2 with Controllable

use of net.citizensnpcs.trait.Controllable 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)

Aggregations

Command (net.citizensnpcs.api.command.Command)2 Controllable (net.citizensnpcs.trait.Controllable)2 Player (org.bukkit.entity.Player)2 UUID (java.util.UUID)1 CommandException (net.citizensnpcs.api.command.exception.CommandException)1 NoPermissionsException (net.citizensnpcs.api.command.exception.NoPermissionsException)1 ServerCommandException (net.citizensnpcs.api.command.exception.ServerCommandException)1 NPC (net.citizensnpcs.api.npc.NPC)1