Search in sources :

Example 1 with CommandTarget

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

the class EffectCommand method execute.

@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages commandMessages) {
    if (!testPermission(sender, commandMessages.getPermissionMessage())) {
        return true;
    }
    if (args.length < 2) {
        sendUsageMessage(sender, commandMessages);
        return false;
    }
    String name = args[0];
    List<GlowPlayer> players;
    if (name.startsWith("@") && name.length() >= 2) {
        CommandTarget target = new CommandTarget(sender, name);
        players = Arrays.stream(target.getMatched(CommandUtils.getLocation(sender))).filter(GlowPlayer.class::isInstance).map(GlowPlayer.class::cast).collect(Collectors.toList());
    } else {
        GlowPlayer player = (GlowPlayer) Bukkit.getPlayerExact(args[0]);
        if (player == null) {
            commandMessages.getGeneric(GenericMessage.NO_SUCH_PLAYER).sendInColor(ChatColor.RED, sender, name);
            return false;
        } else {
            players = Collections.singletonList(player);
        }
    }
    if (args[1].equals("clear")) {
        // NON-NLS
        for (GlowPlayer player : players) {
            for (PotionEffect potionEffect : player.getActivePotionEffects()) {
                player.removePotionEffect(potionEffect.getType());
            }
            new LocalizedStringImpl("effect.cleared", commandMessages.getResourceBundle()).send(sender, player.getName());
        }
        return true;
    } else {
        PotionEffectType effectType = GlowPotionEffect.parsePotionEffectId(args[1]);
        if (effectType == null) {
            new LocalizedStringImpl("effect.unknown", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender, args[1]);
            return false;
        }
        int duration = TickUtil.secondsToTicks(30);
        if (args.length >= 3 && args[2] != null) {
            try {
                duration = TickUtil.secondsToTicks(Integer.parseInt(args[2]));
            } catch (NumberFormatException exc) {
                commandMessages.getGeneric(GenericMessage.NAN).sendInColor(ChatColor.RED, sender, args[2]);
                return false;
            }
        }
        int amplifier = 0;
        if (args.length >= 4 && args[3] != null) {
            try {
                amplifier = Integer.parseInt(args[3]);
            } catch (NumberFormatException exc) {
                commandMessages.getGeneric(GenericMessage.NAN).sendInColor(ChatColor.RED, sender, args[3]);
                return false;
            }
        }
        boolean hideParticles = false;
        if (args.length >= 5 && args[4] != null) {
            hideParticles = Boolean.parseBoolean(args[4]);
        }
        LocalizedStringImpl doneMessage = new LocalizedStringImpl("effect.done", commandMessages.getResourceBundle());
        for (GlowPlayer player : players) {
            player.addPotionEffect(new PotionEffect(effectType, duration, amplifier, false, !hideParticles));
            doneMessage.send(sender, effectType.getName(), effectType.getId(), amplifier + 1, player.getName(), duration / 20);
        }
        return true;
    }
}
Also used : CommandTarget(net.glowstone.command.CommandTarget) PotionEffect(org.bukkit.potion.PotionEffect) GlowPotionEffect(net.glowstone.constants.GlowPotionEffect) GlowPlayer(net.glowstone.entity.GlowPlayer) PotionEffectType(org.bukkit.potion.PotionEffectType) LocalizedStringImpl(net.glowstone.i18n.LocalizedStringImpl)

Example 2 with CommandTarget

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

the class KillCommand 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) {
        if (sender instanceof Entity) {
            Entity entity = (Entity) sender;
            if (entity.isDead()) {
                new LocalizedStringImpl("kill.self-dead", commandMessages.getResourceBundle()).send(entity);
            } else if (entity instanceof LivingEntity) {
                LivingEntity living = (LivingEntity) entity;
                living.damage(Double.MAX_VALUE, EntityDamageEvent.DamageCause.SUICIDE);
                new LocalizedStringImpl("kill.done", commandMessages.getResourceBundle()).send(sender, CommandUtils.getName(entity));
            } else {
                entity.remove();
                new LocalizedStringImpl("kill.done", commandMessages.getResourceBundle()).send(sender, CommandUtils.getName(entity));
            }
            return true;
        } else {
            new LocalizedStringImpl("kill.self-not-entity", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender);
            return false;
        }
    }
    if (args.length == 1) {
        String name = args[0];
        if (name.startsWith("@") && name.length() >= 2 && CommandUtils.isPhysical(sender)) {
            Location location = CommandUtils.getLocation(sender);
            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;
            }
            LocalizedStringImpl killDoneMessage = new LocalizedStringImpl("kill.done", commandMessages.getResourceBundle());
            for (Entity entity : matched) {
                if (entity instanceof LivingEntity) {
                    LivingEntity living = (LivingEntity) entity;
                    living.damage(Double.MAX_VALUE, EntityDamageEvent.DamageCause.VOID);
                } else {
                    entity.remove();
                }
                killDoneMessage.send(sender, 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 {
                player.damage(Double.MAX_VALUE, EntityDamageEvent.DamageCause.VOID);
                new LocalizedStringImpl("kill.done", commandMessages.getResourceBundle()).send(sender, player.getName());
                return true;
            }
        }
    }
    sendUsageMessage(sender, commandMessages);
    return false;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) CommandTarget(net.glowstone.command.CommandTarget) LocalizedStringImpl(net.glowstone.i18n.LocalizedStringImpl) Location(org.bukkit.Location)

Example 3 with CommandTarget

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

the class TpCommand method matchEntities.

private Entity[] matchEntities(CommandSender sender, String selector) {
    if (selector.startsWith("@") && CommandUtils.isPhysical(sender)) {
        Location location = CommandUtils.getLocation(sender);
        CommandTarget target = new CommandTarget(sender, selector);
        Entity[] matched = target.getMatched(location);
        if (matched.length == 0) {
            sender.sendMessage(ChatColor.RED + "Selector " + selector + " found nothing");
        }
        return matched;
    } else {
        Player player = Bukkit.getPlayerExact(selector);
        if (player == null) {
            sender.sendMessage(ChatColor.RED + "Player '" + selector + "' is not online");
            return new Entity[0];
        } else {
            return new Entity[] { player };
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) CommandTarget(net.glowstone.command.CommandTarget) Location(org.bukkit.Location)

Example 4 with CommandTarget

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

the class GameModeCommand method execute.

@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages messages) {
    if (!testPermission(sender, messages.getPermissionMessage())) {
        return true;
    }
    if (args.length == 0 || args.length == 1 && !(sender instanceof Player)) {
        sendUsageMessage(sender, messages);
        return false;
    }
    final ResourceBundle bundle = messages.getResourceBundle();
    String gm = args[0];
    GameMode gamemode = GameModeUtils.build(gm, messages.getLocale());
    if (gamemode == null) {
        new LocalizedStringImpl("gamemode.unknown", bundle).sendInColor(ChatColor.RED, sender, gm);
        return false;
    }
    if (args.length == 1) {
        // self
        Player player = (Player) sender;
        updateGameMode(sender, player, gamemode, bundle);
        return true;
    }
    String name = args[1];
    if (name.startsWith("@") && name.length() >= 2 && CommandUtils.isPhysical(sender)) {
        Location location = CommandUtils.getLocation(sender);
        CommandTarget target = new CommandTarget(sender, name);
        Entity[] matched = target.getMatched(location);
        for (Entity entity : matched) {
            if (entity instanceof Player) {
                Player player = (Player) entity;
                updateGameMode(sender, player, gamemode, bundle);
            }
        }
    } else {
        Player player = Bukkit.getPlayerExact(name);
        if (player == null) {
            messages.getGeneric(GenericMessage.OFFLINE).sendInColor(ChatColor.RED, sender, name);
        } else {
            updateGameMode(sender, player, gamemode, bundle);
        }
    }
    return true;
}
Also used : GameMode(org.bukkit.GameMode) 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 5 with CommandTarget

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

the class GiveCommand method execute.

@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages commandMessages) {
    if (!testPermission(sender, commandMessages.getPermissionMessage())) {
        return true;
    }
    if (args.length < 2) {
        sendUsageMessage(sender, commandMessages);
        return false;
    }
    String itemName = CommandUtils.toNamespaced(args[1]);
    Material type = ItemIds.getItem(itemName);
    if (type == null) {
        new LocalizedStringImpl("give.unknown", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender, itemName);
        return false;
    }
    ItemStack stack = new ItemStack(type);
    if (args.length >= 3) {
        String amountString = args[2];
        try {
            int amount = Integer.valueOf(amountString);
            if (amount > 64) {
                new LocalizedStringImpl("give.too-many", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender, amount);
                return false;
            } else if (amount < 1) {
                new LocalizedStringImpl("give.too-few", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender, amount);
                return false;
            }
            stack.setAmount(amount);
        } catch (NumberFormatException ex) {
            commandMessages.getGeneric(GenericMessage.NAN).sendInColor(ChatColor.RED, sender, amountString);
            return false;
        }
    }
    String name = args[0];
    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) {
                Player player = (Player) entity;
                giveItem(sender, player, stack, commandMessages.getResourceBundle());
            }
        }
    } else {
        Player player = Bukkit.getPlayerExact(name);
        if (player == null) {
            commandMessages.getGeneric(GenericMessage.OFFLINE).sendInColor(ChatColor.RED, sender, name);
            return false;
        } else {
            giveItem(sender, player, stack, commandMessages.getResourceBundle());
        }
    }
    return true;
}
Also used : Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) CommandTarget(net.glowstone.command.CommandTarget) LocalizedStringImpl(net.glowstone.i18n.LocalizedStringImpl) Material(org.bukkit.Material) ItemStack(org.bukkit.inventory.ItemStack) Location(org.bukkit.Location) BlockCommandSender(org.bukkit.command.BlockCommandSender)

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