Search in sources :

Example 1 with Anchors

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

the class NPCCommands method anchor.

@Command(aliases = { "npc" }, usage = "anchor (--save [name]|--assume [name]|--remove [name]) (-a)(-c)", desc = "Changes/Saves/Lists NPC's location anchor(s)", flags = "ac", modifiers = { "anchor" }, min = 1, max = 3, permission = "citizens.npc.anchor")
public void anchor(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    Anchors trait = npc.getTrait(Anchors.class);
    if (args.hasValueFlag("save")) {
        if (args.getFlag("save").isEmpty())
            throw new CommandException(Messages.INVALID_ANCHOR_NAME);
        if (args.getSenderLocation() == null)
            throw new ServerCommandException();
        if (args.hasFlag('c')) {
            if (trait.addAnchor(args.getFlag("save"), args.getSenderTargetBlockLocation())) {
                Messaging.sendTr(sender, Messages.ANCHOR_ADDED);
            } else
                throw new CommandException(Messages.ANCHOR_ALREADY_EXISTS, args.getFlag("save"));
        } else {
            if (trait.addAnchor(args.getFlag("save"), args.getSenderLocation())) {
                Messaging.sendTr(sender, Messages.ANCHOR_ADDED);
            } else
                throw new CommandException(Messages.ANCHOR_ALREADY_EXISTS, args.getFlag("save"));
        }
    } else if (args.hasValueFlag("assume")) {
        if (args.getFlag("assume").isEmpty())
            throw new CommandException(Messages.INVALID_ANCHOR_NAME);
        Anchor anchor = trait.getAnchor(args.getFlag("assume"));
        if (anchor == null)
            throw new CommandException(Messages.ANCHOR_MISSING, args.getFlag("assume"));
        npc.teleport(anchor.getLocation(), TeleportCause.COMMAND);
    } else if (args.hasValueFlag("remove")) {
        if (args.getFlag("remove").isEmpty())
            throw new CommandException(Messages.INVALID_ANCHOR_NAME);
        if (trait.removeAnchor(trait.getAnchor(args.getFlag("remove"))))
            Messaging.sendTr(sender, Messages.ANCHOR_REMOVED);
        else
            throw new CommandException(Messages.ANCHOR_MISSING, args.getFlag("remove"));
    } else if (!args.hasFlag('a')) {
        Paginator paginator = new Paginator().header("Anchors");
        paginator.addLine("<e>Key: <a>ID  <b>Name  <c>World  <d>Location (X,Y,Z)");
        for (int i = 0; i < trait.getAnchors().size(); i++) {
            if (trait.getAnchors().get(i).isLoaded()) {
                String line = "<a>" + i + "<b>  " + trait.getAnchors().get(i).getName() + "<c>  " + trait.getAnchors().get(i).getLocation().getWorld().getName() + "<d>  " + trait.getAnchors().get(i).getLocation().getBlockX() + ", " + trait.getAnchors().get(i).getLocation().getBlockY() + ", " + trait.getAnchors().get(i).getLocation().getBlockZ();
                paginator.addLine(line);
            } else {
                String[] parts = trait.getAnchors().get(i).getUnloadedValue();
                String line = "<a>" + i + "<b>  " + trait.getAnchors().get(i).getName() + "<c>  " + parts[0] + "<d>  " + parts[1] + ", " + parts[2] + ", " + parts[3] + " <f>(unloaded)";
                paginator.addLine(line);
            }
        }
        int page = args.getInteger(1, 1);
        if (!paginator.sendPage(sender, page))
            throw new CommandException(Messages.COMMAND_PAGE_MISSING);
    }
    // Assume Player's position
    if (!args.hasFlag('a'))
        return;
    if (sender instanceof ConsoleCommandSender)
        throw new ServerCommandException();
    npc.teleport(args.getSenderLocation(), TeleportCause.COMMAND);
}
Also used : Anchors(net.citizensnpcs.trait.Anchors) Anchor(net.citizensnpcs.util.Anchor) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) Paginator(net.citizensnpcs.api.util.Paginator) Command(net.citizensnpcs.api.command.Command)

Example 2 with Anchors

use of net.citizensnpcs.trait.Anchors in project Denizen-For-Bukkit by DenizenScript.

the class AnchorCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    Action action = (Action) scriptEntry.getObject("action");
    LocationTag location = scriptEntry.getObjectTag("location");
    ElementTag range = scriptEntry.getElement("range");
    ElementTag id = scriptEntry.getElement("id");
    NPCTag npc = Utilities.getEntryNPC(scriptEntry);
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), npc, db("action", action.name()), id, location, range);
    }
    Anchors anchors = npc.getCitizen().getOrAddTrait(Anchors.class);
    switch(action) {
        case ADD:
            {
                if (location == null) {
                    Debug.echoError("Must specify a location!");
                    return;
                }
                Anchor existing = anchors.getAnchor(id.asString());
                if (existing != null) {
                    anchors.removeAnchor(existing);
                }
                anchors.addAnchor(id.asString(), location);
                break;
            }
        case REMOVE:
            {
                Anchor n = anchors.getAnchor(id.asString());
                if (n == null) {
                    Debug.echoError(scriptEntry, "Invalid anchor name '" + id.asString() + "'");
                } else {
                    anchors.removeAnchor(n);
                }
                break;
            }
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) Anchors(net.citizensnpcs.trait.Anchors) Anchor(net.citizensnpcs.util.Anchor) NPCTag(com.denizenscript.denizen.objects.NPCTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 3 with Anchors

use of net.citizensnpcs.trait.Anchors in project Denizen-For-Bukkit by DenizenScript.

the class NPCCommandHandler method sleeping.

@Command(aliases = { "npc" }, usage = "sleep (--location x,y,z,world) (--anchor anchor_name)", desc = "Makes the NPC sleep.", modifiers = { "sleep" }, min = 1, max = 3, permission = "denizen.npc.sleep")
@Requirements(selected = true, ownership = true, types = { EntityType.VILLAGER, EntityType.PLAYER })
public void sleeping(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    if (npc.hasTrait(SneakingTrait.class)) {
        npc.getOrAddTrait(SneakingTrait.class).stand();
        npc.removeTrait(SneakingTrait.class);
    }
    if (npc.hasTrait(SittingTrait.class)) {
        npc.getOrAddTrait(SittingTrait.class).stand();
        npc.removeTrait(SittingTrait.class);
    }
    SleepingTrait trait = npc.getOrAddTrait(SleepingTrait.class);
    if (trait.isSleeping()) {
        Messaging.send(sender, npc.getName() + " was already sleeping, and is now standing!");
        trait.wakeUp();
        npc.removeTrait(SleepingTrait.class);
        return;
    }
    if (args.hasValueFlag("location")) {
        LocationTag location = LocationTag.valueOf(args.getFlag("location"), CoreUtilities.basicContext);
        if (location == null) {
            Messaging.sendError(sender, "Usage: /npc sleep --location x,y,z,world");
            return;
        }
        trait.toSleep(location);
    } else if (args.hasValueFlag("anchor")) {
        if (npc.hasTrait(Anchors.class)) {
            Anchors anchors = npc.getOrAddTrait(Anchors.class);
            if (anchors.getAnchor(args.getFlag("anchor")) != null) {
                trait.toSleep(anchors.getAnchor(args.getFlag("anchor")).getLocation());
                Messaging.send(sender, npc.getName() + " is now sleeping.");
                return;
            }
        }
        Messaging.sendError(sender, "The NPC does not have the specified anchor!");
        return;
    } else {
        trait.toSleep();
    }
    if (!trait.isSleeping()) {
        npc.removeTrait(SleepingTrait.class);
    }
    Messaging.send(sender, npc.getName() + " is now sleeping.");
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) Anchors(net.citizensnpcs.trait.Anchors) Command(net.citizensnpcs.api.command.Command) Requirements(net.citizensnpcs.api.command.Requirements)

Example 4 with Anchors

use of net.citizensnpcs.trait.Anchors in project Denizen-For-Bukkit by DenizenScript.

the class NPCCommandHandler method sitting.

@Command(aliases = { "npc" }, usage = "sit (--location x,y,z,world) (--anchor anchor_name) (-c)", desc = "Makes the NPC sit.", flags = "c", modifiers = { "sit" }, min = 1, max = 3, permission = "denizen.npc.sit")
@Requirements(selected = true, ownership = true)
public void sitting(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    if (npc.hasTrait(SneakingTrait.class)) {
        npc.getOrAddTrait(SneakingTrait.class).stand();
        npc.removeTrait(SneakingTrait.class);
    }
    if (npc.hasTrait(SleepingTrait.class)) {
        npc.getOrAddTrait(SleepingTrait.class).wakeUp();
        npc.removeTrait(SleepingTrait.class);
    }
    SittingTrait trait = npc.getOrAddTrait(SittingTrait.class);
    if (args.hasValueFlag("location")) {
        LocationTag location = LocationTag.valueOf(args.getFlag("location"), CoreUtilities.basicContext);
        if (location == null) {
            Messaging.sendError(sender, "Usage: /npc sit --location x,y,z,world");
            return;
        }
        trait.sit(location);
        return;
    } else if (args.hasValueFlag("anchor")) {
        if (npc.hasTrait(Anchors.class)) {
            Anchors anchors = npc.getOrAddTrait(Anchors.class);
            if (anchors.getAnchor(args.getFlag("anchor")) != null) {
                trait.sit(anchors.getAnchor(args.getFlag("anchor")).getLocation());
                Messaging.send(sender, npc.getName() + " is now sitting.");
                return;
            }
        }
        Messaging.sendError(sender, "The NPC does not have the specified anchor!");
        return;
    }
    Location targetLocation;
    if (args.hasFlag('c')) {
        targetLocation = args.getSenderTargetBlockLocation().clone().add(0.5, 0, 0.5);
        targetLocation.setYaw(npc.getStoredLocation().getYaw());
    } else {
        targetLocation = npc.getStoredLocation().clone();
        targetLocation.add(0, -0.2, 0);
    }
    if (trait.isSitting()) {
        Messaging.send(sender, npc.getName() + " is already sitting, use '/npc stand' to stand the NPC back up.");
        return;
    }
    Block block = targetLocation.getBlock();
    BlockData data = block.getBlockData();
    if (data instanceof Stairs || data instanceof Bed || (data instanceof Slab && ((Slab) data).getType() == Slab.Type.BOTTOM)) {
        targetLocation.setY(targetLocation.getBlockY() + 0.3);
    } else if (data instanceof Campfire) {
        targetLocation.setY(targetLocation.getBlockY() + 0.2);
    } else if (block.getType().name().endsWith("CARPET")) {
        targetLocation.setY(targetLocation.getBlockY());
    } else if (block.getType().isSolid()) {
        targetLocation.setY(targetLocation.getBlockY() + 0.8);
    }
    trait.sit(targetLocation);
    Messaging.send(sender, npc.getName() + " is now sitting.");
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) Anchors(net.citizensnpcs.trait.Anchors) Bed(org.bukkit.block.data.type.Bed) Campfire(org.bukkit.block.data.type.Campfire) Stairs(org.bukkit.block.data.type.Stairs) Block(org.bukkit.block.Block) BlockData(org.bukkit.block.data.BlockData) Slab(org.bukkit.block.data.type.Slab) Location(org.bukkit.Location) Command(net.citizensnpcs.api.command.Command) Requirements(net.citizensnpcs.api.command.Requirements)

Example 5 with Anchors

use of net.citizensnpcs.trait.Anchors in project Denizen-For-Bukkit by DenizenScript.

the class NPCCommandHandler method startFishing.

@Command(aliases = { "npc" }, usage = "fish (--location x,y,z,world) (--anchor anchor_name) (-c) (--reel_time <duration>) (--cast_time <duration>)", desc = "Makes the NPC fish, casting at the given location.", flags = "c", modifiers = { "fish" }, min = 1, max = 3, permission = "denizen.npc.fish")
@Requirements(selected = true, ownership = true)
public void startFishing(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    FishingTrait trait = npc.getOrAddTrait(FishingTrait.class);
    if (trait.isFishing()) {
        Messaging.sendError(sender, npc.getName() + " is already fishing! Use '/npc stopfishing' to stop.");
        return;
    }
    if (args.hasValueFlag("percent")) {
        trait.setCatchPercent(args.getFlagInteger("percent"));
    }
    if (args.hasValueFlag("reel_time")) {
        DurationTag duration = DurationTag.valueOf(args.getFlag("reel_time"), CoreUtilities.basicContext);
        if (duration == null) {
            Messaging.sendError(sender, "Invalid reel duration.");
            return;
        }
        trait.reelTickRate = duration.getTicksAsInt();
        Messaging.send(sender, "Set reel rate to " + duration.formatted(true));
    }
    if (args.hasValueFlag("cast_time")) {
        DurationTag duration = DurationTag.valueOf(args.getFlag("cast_time"), CoreUtilities.basicContext);
        if (duration == null) {
            Messaging.sendError(sender, "Invalid cast duration.");
            return;
        }
        trait.reelTickRate = duration.getTicksAsInt();
        Messaging.send(sender, "Set cast rate to " + duration.formatted(true));
    }
    if (args.hasFlag('c')) {
        trait.startFishing(args.getSenderTargetBlockLocation());
    } else if (args.hasValueFlag("location")) {
        String[] argsArray = args.getFlag("location").split(",");
        if (argsArray.length != 4) {
            Messaging.sendError(sender, "Usage: /npc fish --location x,y,z,world");
            return;
        }
        trait.startFishing(LocationTag.valueOf(argsArray[0] + "," + argsArray[1] + "," + argsArray[2] + "," + argsArray[3], CoreUtilities.basicContext));
    } else if (args.hasValueFlag("anchor")) {
        if (npc.hasTrait(Anchors.class)) {
            Anchors anchors = npc.getOrAddTrait(Anchors.class);
            if (anchors.getAnchor(args.getFlag("anchor")) != null) {
                trait.startFishing(anchors.getAnchor(args.getFlag("anchor")).getLocation());
            }
        }
        Messaging.sendError(sender, "The NPC does not have the specified anchor!");
    } else {
        trait.startFishing();
    }
    Messaging.send(sender, npc.getName() + " is now fishing.");
}
Also used : Anchors(net.citizensnpcs.trait.Anchors) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) Command(net.citizensnpcs.api.command.Command) Requirements(net.citizensnpcs.api.command.Requirements)

Aggregations

Anchors (net.citizensnpcs.trait.Anchors)5 Command (net.citizensnpcs.api.command.Command)4 LocationTag (com.denizenscript.denizen.objects.LocationTag)3 Requirements (net.citizensnpcs.api.command.Requirements)3 Anchor (net.citizensnpcs.util.Anchor)2 NPCTag (com.denizenscript.denizen.objects.NPCTag)1 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)1 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)1 CommandException (net.citizensnpcs.api.command.exception.CommandException)1 ServerCommandException (net.citizensnpcs.api.command.exception.ServerCommandException)1 Paginator (net.citizensnpcs.api.util.Paginator)1 Location (org.bukkit.Location)1 Block (org.bukkit.block.Block)1 BlockData (org.bukkit.block.data.BlockData)1 Bed (org.bukkit.block.data.type.Bed)1 Campfire (org.bukkit.block.data.type.Campfire)1 Slab (org.bukkit.block.data.type.Slab)1 Stairs (org.bukkit.block.data.type.Stairs)1 ConsoleCommandSender (org.bukkit.command.ConsoleCommandSender)1