Search in sources :

Example 11 with CommandTarget

use of net.glowstone.command.CommandTarget in project Glowstone by GlowstoneMC.

the class FunctionCommand method anyMatch.

private boolean anyMatch(CommandSender sender, String selector, Location location) {
    CommandTarget target = new CommandTarget(sender, selector);
    Entity[] matched = target.getMatched(location);
    return matched.length > 0;
}
Also used : Entity(org.bukkit.entity.Entity) CommandTarget(net.glowstone.command.CommandTarget)

Example 12 with CommandTarget

use of net.glowstone.command.CommandTarget in project Glowstone by GlowstoneMC.

the class TellCommand method execute.

@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages commandMessages) {
    if (!testPermission(sender, commandMessages.getPermissionMessage())) {
        return true;
    }
    if (args.length <= 1) {
        sendUsageMessage(sender, commandMessages);
        return false;
    }
    String name = args[0];
    Player[] players;
    if (name.charAt(0) == '@' && CommandUtils.isPhysical(sender)) {
        Location location = CommandUtils.getLocation(sender);
        CommandTarget target = new CommandTarget(sender, name);
        target.getArguments().put("type", // NON-NLS; only players
        new CommandTarget.SelectorValue("player"));
        Entity[] matched = target.getMatched(location);
        if (matched.length == 0) {
            commandMessages.getGeneric(GenericMessage.NO_MATCHES).sendInColor(ChatColor.RED, sender, name);
            return false;
        }
        players = new Player[matched.length];
        for (int i = 0; i < matched.length; i++) {
            players[i] = (Player) matched[i];
        }
    } else {
        Player player = Bukkit.getPlayer(name);
        if (player == null) {
            commandMessages.getGeneric(GenericMessage.NO_SUCH_PLAYER).sendInColor(ChatColor.RED, sender, name);
            return false;
        }
        players = new Player[] { player };
    }
    String senderName = CommandUtils.getName(sender);
    String message = StringUtils.join(args, ' ', 1, args.length);
    ResourceBundle bundle = commandMessages.getResourceBundle();
    LocalizedStringImpl senderMessage = new LocalizedStringImpl("tell.sender", bundle);
    for (Player player : players) {
        if (sender.equals(player)) {
            new LocalizedStringImpl("tell.self", bundle).sendInColor(ChatColor.RED, sender);
            continue;
        }
        new LocalizedStringImpl("tell.recipient", getBundle(player)).send(player, senderName, message);
        senderMessage.send(sender, senderName, player.getName(), message);
    }
    return true;
}
Also used : Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) CommandTarget(net.glowstone.command.CommandTarget) LocalizedStringImpl(net.glowstone.i18n.LocalizedStringImpl) ResourceBundle(java.util.ResourceBundle) Location(org.bukkit.Location)

Example 13 with CommandTarget

use of net.glowstone.command.CommandTarget in project Glowstone by GlowstoneMC.

the class TpCommand method execute.

@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages commandMessages) {
    if (!testPermission(sender, commandMessages.getPermissionMessage())) {
        return true;
    }
    switch(args.length) {
        case 0:
            sendUsageMessage(sender, commandMessages);
            return false;
        case 1:
            Entity from;
            if (sender instanceof Player) {
                from = (Entity) sender;
            } else {
                sender.sendMessage(ChatColor.RED + "Only entities can be teleported");
                return false;
            }
            String name = args[0];
            if (name.startsWith("@") && !name.startsWith("@e") && name.length() >= 2 && CommandUtils.isPhysical(sender)) {
                Location location = from.getLocation();
                CommandTarget target = new CommandTarget(sender, name);
                Entity[] matched = target.getMatched(location);
                if (matched.length == 0) {
                    commandMessages.getGeneric(GenericMessage.NO_MATCHES).sendInColor(ChatColor.RED, sender, name);
                    return false;
                }
                for (Entity entity : matched) {
                    from.teleport(entity);
                    sender.sendMessage("Teleported " + CommandUtils.getName(from) + " to " + CommandUtils.getName(entity));
                }
                return true;
            } else {
                Player player = Bukkit.getPlayerExact(name);
                if (player == null) {
                    commandMessages.getGeneric(GenericMessage.NO_SUCH_PLAYER).sendInColor(ChatColor.RED, sender, name);
                    return false;
                } else {
                    from.teleport(player);
                    sender.sendMessage("Teleported " + CommandUtils.getName(from) + " to " + player.getName());
                    return true;
                }
            }
        case 2:
            Entity destination;
            String fromName = args[0];
            String destName = args[1];
            if (fromName.startsWith("@") && fromName.length() >= 2 && CommandUtils.isPhysical(sender)) {
                Location location = CommandUtils.getLocation(sender);
                CommandTarget target = new CommandTarget(sender, fromName);
                Entity[] matched = target.getMatched(location);
                if (matched.length == 0) {
                    commandMessages.getGeneric(GenericMessage.NO_MATCHES).sendInColor(ChatColor.RED, sender, fromName);
                    return false;
                }
                for (Entity entity : matched) {
                    if (destName.startsWith("@") && !destName.startsWith("@e") && destName.length() >= 2 && CommandUtils.isPhysical(sender)) {
                        Location location2 = CommandUtils.getLocation(sender);
                        CommandTarget target2 = new CommandTarget(sender, destName);
                        Entity[] matched2 = target2.getMatched(location2);
                        if (matched2.length == 0) {
                            commandMessages.getGeneric(GenericMessage.NO_MATCHES).sendInColor(ChatColor.RED, sender, destName);
                            return false;
                        }
                        destination = matched2[0];
                    } else {
                        Player player = Bukkit.getPlayerExact(destName);
                        if (player == null) {
                            commandMessages.getGeneric(GenericMessage.NO_MATCHES).sendInColor(ChatColor.RED, sender, destName);
                            return false;
                        } else {
                            destination = player;
                        }
                    }
                    entity.teleport(destination);
                    sender.sendMessage("Teleported " + CommandUtils.getName(entity) + " to " + CommandUtils.getName(destination));
                }
                return true;
            } else {
                Player player = Bukkit.getPlayerExact(fromName);
                if (player == null) {
                    sender.sendMessage(ChatColor.RED + "Player '" + fromName + "' is not online");
                    return false;
                } else {
                    if (destName.startsWith("@") && !destName.startsWith("@e") && destName.length() >= 2 && CommandUtils.isPhysical(sender)) {
                        Location location2 = CommandUtils.getLocation(sender);
                        CommandTarget target2 = new CommandTarget(sender, destName);
                        Entity[] matched2 = target2.getMatched(location2);
                        if (matched2.length == 0) {
                            commandMessages.getGeneric(GenericMessage.NO_MATCHES).sendInColor(ChatColor.RED, sender, destName);
                            return false;
                        }
                        destination = matched2[0];
                    } else {
                        Player player2 = Bukkit.getPlayerExact(destName);
                        if (player2 == null) {
                            commandMessages.getGeneric(GenericMessage.NO_SUCH_PLAYER).sendInColor(ChatColor.RED, sender, destName);
                            return false;
                        } else {
                            destination = player2;
                        }
                    }
                }
                player.teleport(destination);
                sender.sendMessage("Teleported " + player.getName() + " to " + CommandUtils.getName(destination));
                return true;
            }
        default:
            sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
            return false;
    }
}
Also used : Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) CommandTarget(net.glowstone.command.CommandTarget) Location(org.bukkit.Location)

Example 14 with CommandTarget

use of net.glowstone.command.CommandTarget 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 15 with CommandTarget

use of net.glowstone.command.CommandTarget in project Glowstone by GlowstoneMC.

the class SayCommand 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;
    }
    StringBuilder message = new StringBuilder("[").append(sender.getName() == null ? GlowstoneMessages.Command.SAY_SERVER.get() : sender.getName()).append("] ");
    for (String arg : args) {
        if (arg.startsWith("@") && arg.length() >= 2 && CommandUtils.isPhysical(sender)) {
            // command targets
            Location location = CommandUtils.getLocation(sender);
            CommandTarget target = new CommandTarget(sender, arg);
            Entity[] matched = target.getMatched(location);
            if (matched.length == 0) {
                commandMessages.getGeneric(GenericMessage.NO_MATCHES).sendInColor(ChatColor.RED, sender, arg);
                return false;
            }
            message.append(commandMessages.joinList(Arrays.stream(matched).map(Entity::getName))).append(' ');
        } else {
            message.append(arg).append(' ');
        }
    }
    Bukkit.broadcastMessage(message.toString());
    return true;
}
Also used : Entity(org.bukkit.entity.Entity) CommandTarget(net.glowstone.command.CommandTarget) Location(org.bukkit.Location)

Aggregations

CommandTarget (net.glowstone.command.CommandTarget)16 Entity (org.bukkit.entity.Entity)14 Location (org.bukkit.Location)12 LocalizedStringImpl (net.glowstone.i18n.LocalizedStringImpl)11 Player (org.bukkit.entity.Player)10 GlowPlayer (net.glowstone.entity.GlowPlayer)4 ArrayList (java.util.ArrayList)3 ResourceBundle (java.util.ResourceBundle)3 Material (org.bukkit.Material)3 BlockCommandSender (org.bukkit.command.BlockCommandSender)3 World (org.bukkit.World)2 ItemStack (org.bukkit.inventory.ItemStack)2 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Stream (java.util.stream.Stream)1 CommandUtils (net.glowstone.command.CommandUtils)1 GlowEnchantment (net.glowstone.constants.GlowEnchantment)1 GlowPotionEffect (net.glowstone.constants.GlowPotionEffect)1 GlowSound (net.glowstone.constants.GlowSound)1