Search in sources :

Example 1 with PlayerCreateNPCEvent

use of net.citizensnpcs.api.event.PlayerCreateNPCEvent 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 2 with PlayerCreateNPCEvent

use of net.citizensnpcs.api.event.PlayerCreateNPCEvent 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)

Aggregations

Command (net.citizensnpcs.api.command.Command)2 CommandException (net.citizensnpcs.api.command.exception.CommandException)2 ServerCommandException (net.citizensnpcs.api.command.exception.ServerCommandException)2 CommandSenderCreateNPCEvent (net.citizensnpcs.api.event.CommandSenderCreateNPCEvent)2 PlayerCreateNPCEvent (net.citizensnpcs.api.event.PlayerCreateNPCEvent)2 CurrentLocation (net.citizensnpcs.trait.CurrentLocation)2 Location (org.bukkit.Location)2 Player (org.bukkit.entity.Player)2 Requirements (net.citizensnpcs.api.command.Requirements)1 NoPermissionsException (net.citizensnpcs.api.command.exception.NoPermissionsException)1 NPC (net.citizensnpcs.api.npc.NPC)1 Trait (net.citizensnpcs.api.trait.Trait)1 MobType (net.citizensnpcs.api.trait.trait.MobType)1 Owner (net.citizensnpcs.api.trait.trait.Owner)1 Template (net.citizensnpcs.npc.Template)1 Age (net.citizensnpcs.trait.Age)1 ArmorStandTrait (net.citizensnpcs.trait.ArmorStandTrait)1 ScriptTrait (net.citizensnpcs.trait.ScriptTrait)1 SheepTrait (net.citizensnpcs.trait.SheepTrait)1 WitherTrait (net.citizensnpcs.trait.WitherTrait)1