Search in sources :

Example 16 with BlockCommandSender

use of org.bukkit.command.BlockCommandSender in project Glowstone by GlowstoneMC.

the class ClearCommand method execute.

@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages messages) {
    if (!testPermission(sender, messages.getPermissionMessage())) {
        return true;
    }
    final ResourceBundle resourceBundle = messages.getResourceBundle();
    if (args.length == 0) {
        if ((sender instanceof Player)) {
            Player player = (Player) sender;
            return clearAll(sender, player, null, -1, -1, resourceBundle);
        } else {
            sendUsageMessage(sender, messages);
            return false;
        }
    }
    String name = args[0];
    List<Player> players = new ArrayList<>();
    if (name.startsWith("@") && name.length() >= 2 && CommandUtils.isPhysical(sender)) {
        Location location = sender instanceof Entity ? ((Entity) sender).getLocation() : ((BlockCommandSender) sender).getBlock().getLocation();
        CommandTarget target = new CommandTarget(sender, name);
        Entity[] matched = target.getMatched(location);
        for (Entity entity : matched) {
            if (entity instanceof Player) {
                players.add((Player) entity);
            }
        }
    } else {
        Player player = Bukkit.getPlayerExact(name);
        if (player == null) {
            messages.getGeneric(GenericMessage.NO_SUCH_PLAYER).sendInColor(ChatColor.RED, sender, name);
            return false;
        } else {
            players.add(player);
        }
    }
    if (players.size() == 0) {
        messages.getGeneric(GenericMessage.NO_SUCH_PLAYER).sendInColor(ChatColor.RED, sender, name);
        return false;
    }
    if (args.length >= 2) {
        String itemName = CommandUtils.toNamespaced(args[1]);
        Material type = ItemIds.getItem(itemName);
        if (type == null) {
            new LocalizedStringImpl("clear.no-such-item", resourceBundle).sendInColor(ChatColor.RED, sender, itemName);
            return false;
        }
        if (args.length >= 3) {
            String dataString = args[2];
            int data;
            try {
                data = Integer.valueOf(dataString);
            } catch (NumberFormatException ex) {
                messages.getGeneric(GenericMessage.NAN).sendInColor(ChatColor.RED, sender, dataString);
                return false;
            }
            if (data < -1) {
                new LocalizedStringImpl("clear.negative", resourceBundle).sendInColor(ChatColor.RED, sender, data);
                return false;
            }
            if (args.length >= 4) {
                String amountString = args[3];
                int amount;
                try {
                    amount = Integer.valueOf(amountString);
                } catch (NumberFormatException ex) {
                    messages.getGeneric(GenericMessage.NAN).sendInColor(ChatColor.RED, sender, amountString);
                    return false;
                }
                if (amount < -1) {
                    new LocalizedStringImpl("clear.negative", resourceBundle).sendInColor(ChatColor.RED, sender, amount);
                    return false;
                }
                if (args.length >= 5) {
                    new LocalizedStringImpl("clear.tag-unsupported", resourceBundle).sendInColor(ChatColor.RED, sender);
                    return false;
                } else {
                    boolean success = true;
                    for (Player player : players) {
                        if (!clearAll(sender, player, type, data, amount, resourceBundle)) {
                            success = false;
                        }
                    }
                    return success;
                }
            } else {
                boolean success = true;
                for (Player player : players) {
                    if (!clearAll(sender, player, type, data, -1, resourceBundle)) {
                        success = false;
                    }
                }
                return success;
            }
        } else {
            boolean success = true;
            for (Player player : players) {
                if (!clearAll(sender, player, type, -1, -1, resourceBundle)) {
                    success = false;
                }
            }
            return success;
        }
    } else {
        boolean success = true;
        for (Player player : players) {
            if (!clearAll(sender, player, null, -1, -1, resourceBundle)) {
                success = false;
            }
        }
        return success;
    }
}
Also used : Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) CommandTarget(net.glowstone.command.CommandTarget) ArrayList(java.util.ArrayList) Material(org.bukkit.Material) LocalizedStringImpl(net.glowstone.i18n.LocalizedStringImpl) ResourceBundle(java.util.ResourceBundle) Location(org.bukkit.Location) BlockCommandSender(org.bukkit.command.BlockCommandSender)

Example 17 with BlockCommandSender

use of org.bukkit.command.BlockCommandSender in project Glowstone by GlowstoneMC.

the class XpCommand method execute.

@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages commandMessages) {
    if (!testPermission(sender, commandMessages.getPermissionMessage())) {
        return true;
    }
    if (args.length == 0) {
        sendUsageMessage(sender, commandMessages);
        return false;
    }
    String stringAmount = args[0];
    final String playerPattern = (args.length > 1) ? args[1] : null;
    final boolean addLevels = // NON-NLS
    stringAmount.endsWith("l") || // NON-NLS
    stringAmount.endsWith("L");
    int amount;
    List<Player> targets;
    // Handle the amount
    if (addLevels) {
        if (stringAmount.length() == 1) {
            sendUsageMessage(sender, commandMessages);
            return false;
        }
        stringAmount = stringAmount.substring(0, stringAmount.length() - 1);
    }
    try {
        amount = Integer.parseInt(stringAmount);
    } catch (NumberFormatException ex) {
        commandMessages.getGeneric(GenericMessage.NAN).send(sender, stringAmount);
        return false;
    }
    if (amount < 0 && !addLevels) {
        sender.sendMessage(ChatColor.RED + "Cannot give player negative experience points.");
        return false;
    }
    // Handle the player(s)
    if (playerPattern != null && playerPattern.startsWith("@") && playerPattern.length() > 1 && CommandUtils.isPhysical(sender)) {
        final Location location = sender instanceof Entity ? ((Entity) sender).getLocation() : ((BlockCommandSender) sender).getBlock().getLocation();
        final Entity[] entities = new CommandTarget(sender, args[1]).getMatched(location);
        targets = new ArrayList<>();
        for (final Entity entity : entities) {
            if (entity instanceof Player) {
                targets.add((Player) entity);
            }
        }
    } else {
        Player player;
        if (playerPattern == null) {
            // If no players, get the current one
            player = sender instanceof Player ? (Player) sender : null;
        } else {
            player = Bukkit.getPlayerExact(playerPattern);
        }
        if (player == null) {
            commandMessages.getGeneric(GenericMessage.NO_SUCH_PLAYER).sendInColor(ChatColor.RED, sender, playerPattern);
            return false;
        } else {
            targets = Collections.singletonList(player);
        }
    }
    if (targets.isEmpty()) {
        commandMessages.getGeneric(GenericMessage.NO_MATCHES).sendInColor(ChatColor.RED, sender, playerPattern);
        return false;
    }
    // Give (or take) experience to the target(s)
    for (final Player player : targets) {
        if (addLevels) {
            player.giveExpLevels(amount);
            if (amount < 0) {
                sender.sendMessage("Taken " + (-amount) + " levels to " + player.getName());
            } else {
                sender.sendMessage("Given " + amount + " levels to " + player.getName());
            }
        } else {
            player.giveExp(amount);
            sender.sendMessage("Given " + amount + " experience to " + player.getName());
        }
    }
    return true;
}
Also used : Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) CommandTarget(net.glowstone.command.CommandTarget) Location(org.bukkit.Location) BlockCommandSender(org.bukkit.command.BlockCommandSender)

Example 18 with BlockCommandSender

use of org.bukkit.command.BlockCommandSender 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 19 with BlockCommandSender

use of org.bukkit.command.BlockCommandSender in project Citizens2 by CitizensDev.

the class NPCSelector method select.

public void select(CommandSender sender, NPC npc) {
    // Remove existing selection if any
    List<String> selectors = npc.data().get("selectors");
    if (selectors == null) {
        selectors = Lists.newArrayList();
        npc.data().set("selectors", selectors);
    }
    if (sender instanceof Player) {
        Player player = (Player) sender;
        setMetadata(npc, player);
        selectors.add(sender.getName());
        // Remove editor if the player has one
        Editor.leave(player);
    } else if (sender instanceof BlockCommandSender) {
        Block block = ((BlockCommandSender) sender).getBlock();
        setMetadata(npc, block);
        selectors.add(toName(block));
    } else if (sender instanceof ConsoleCommandSender) {
        consoleSelectedNPC = npc.getUniqueId();
        selectors.add("console");
    }
    Bukkit.getPluginManager().callEvent(new NPCSelectEvent(npc, sender));
}
Also used : Player(org.bukkit.entity.Player) Block(org.bukkit.block.Block) NPCSelectEvent(net.citizensnpcs.api.event.NPCSelectEvent) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) BlockCommandSender(org.bukkit.command.BlockCommandSender)

Example 20 with BlockCommandSender

use of org.bukkit.command.BlockCommandSender in project Denizen-For-Bukkit by DenizenScript.

the class DenizenCommand method execute.

@Override
public boolean execute(CommandSender commandSender, String commandLabel, String[] arguments) {
    if (!testPermission(commandSender)) {
        return true;
    }
    Map<String, ObjectTag> context = new HashMap<>();
    String raw_args = "";
    if (arguments.length > 0) {
        StringBuilder rawArgsBuilder = new StringBuilder();
        for (String arg : arguments) {
            rawArgsBuilder.append(arg).append(' ');
        }
        raw_args = rawArgsBuilder.substring(0, rawArgsBuilder.length() - 1);
    }
    List<String> args = Arrays.asList(ArgumentHelper.buildArgs(raw_args));
    context.put("args", new ListTag(args, true));
    context.put("raw_args", new ElementTag(raw_args, true));
    context.put("alias", new ElementTag(commandLabel, true));
    PlayerTag player = null;
    NPCTag npc = null;
    if (commandSender instanceof Player) {
        Player pl = (Player) commandSender;
        if (EntityTag.isCitizensNPC(pl)) {
            npc = NPCTag.fromEntity(pl);
        } else {
            player = PlayerTag.mirrorBukkitPlayer(pl);
        }
        context.put("server", new ElementTag(false));
        context.put("source_type", new ElementTag("player"));
    } else {
        if (commandSender instanceof BlockCommandSender) {
            context.put("command_block_location", new LocationTag(((BlockCommandSender) commandSender).getBlock().getLocation()));
            context.put("server", new ElementTag(false));
            context.put("source_type", new ElementTag("command_block"));
        } else if (commandSender instanceof CommandMinecart) {
            context.put("command_minecart", new EntityTag((CommandMinecart) commandSender));
            context.put("server", new ElementTag(false));
            context.put("source_type", new ElementTag("command_minecart"));
        } else {
            context.put("server", new ElementTag(true));
            context.put("source_type", new ElementTag("server"));
        }
    }
    if (Depends.citizens != null && npc == null) {
        NPC citizen = CitizensAPI.getDefaultNPCSelector().getSelected(commandSender);
        if (citizen != null) {
            npc = new NPCTag(citizen);
        }
    }
    script.runCommandScript(player, npc, context);
    return true;
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) Player(org.bukkit.entity.Player) HashMap(java.util.HashMap) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) CommandMinecart(org.bukkit.entity.minecart.CommandMinecart) LocationTag(com.denizenscript.denizen.objects.LocationTag) NPCTag(com.denizenscript.denizen.objects.NPCTag) EntityTag(com.denizenscript.denizen.objects.EntityTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) BlockCommandSender(org.bukkit.command.BlockCommandSender)

Aggregations

BlockCommandSender (org.bukkit.command.BlockCommandSender)25 Player (org.bukkit.entity.Player)19 Location (org.bukkit.Location)13 Entity (org.bukkit.entity.Entity)10 ConsoleCommandSender (org.bukkit.command.ConsoleCommandSender)8 Block (org.bukkit.block.Block)5 CommandSender (org.bukkit.command.CommandSender)5 EventHandler (org.bukkit.event.EventHandler)5 ArrayList (java.util.ArrayList)4 World (org.bukkit.World)4 CommandBlock (org.bukkit.block.CommandBlock)4 CommandMinecart (org.bukkit.entity.minecart.CommandMinecart)4 CommandTarget (net.glowstone.command.CommandTarget)3 LivingEntity (org.bukkit.entity.LivingEntity)3 CastSourceLocation (com.elmakers.mine.bukkit.api.magic.CastSourceLocation)2 Mage (com.elmakers.mine.bukkit.api.magic.Mage)2 Spell (com.elmakers.mine.bukkit.api.spell.Spell)2 MCCommandSender (com.laytonsmith.abstraction.MCCommandSender)2 List (java.util.List)2 LocalizedStringImpl (net.glowstone.i18n.LocalizedStringImpl)2