Search in sources :

Example 21 with LocalizedStringImpl

use of net.glowstone.i18n.LocalizedStringImpl in project Glowstone by GlowstoneMC.

the class SpawnPointCommand 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 && args.length != 1 && args.length < 4) {
        sendUsageMessage(sender, commandMessages);
        return false;
    }
    final String playerPattern = args.length >= 1 ? args[0] : null;
    List<Player> targets;
    Location spawnLocation;
    // Manage player(s)
    if (playerPattern == null) {
        // Default player, set to current one (if a player)
        if (sender instanceof Player) {
            targets = ImmutableList.of((Player) sender);
        } else {
            new LocalizedStringImpl("spawnpoint.no-player", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender);
            return false;
        }
    } else if (playerPattern.startsWith("@") && playerPattern.length() > 1 && CommandUtils.isPhysical(sender)) {
        // Manage selectors
        final Location location = sender instanceof Entity ? ((Entity) sender).getLocation() : ((BlockCommandSender) sender).getBlock().getLocation();
        final Entity[] entities = new CommandTarget(sender, args[0]).getMatched(location);
        targets = new ArrayList<>(entities.length);
        for (final Entity entity : entities) {
            if (entity instanceof Player) {
                targets.add((Player) entity);
            }
        }
    } else {
        // One player given (with or without coordinates)
        final Player 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);
        }
    }
    // Manage coordinates
    if (args.length == 4) {
        // Coordinates are given
        final Location currentLocation;
        final World world = CommandUtils.getWorld(sender);
        // If we are using relative coordinates, we need to get the sender location
        if (args[1].startsWith("~") || args[2].startsWith("~") || args[3].startsWith("~")) {
            if (!CommandUtils.isPhysical(sender)) {
                commandMessages.getGeneric(GenericMessage.NOT_PHYSICAL_COORDS).sendInColor(ChatColor.RED, sender);
                return false;
            } else {
                currentLocation = sender instanceof Entity ? ((Entity) sender).getLocation() : ((BlockCommandSender) sender).getBlock().getLocation();
            }
        } else {
            // Otherwise, the current location can be set to 0/0/0 (since it's absolute)
            currentLocation = new Location(world, 0, 0, 0);
        }
        spawnLocation = CommandUtils.getLocation(currentLocation, args[1], args[2], args[3]);
        if (spawnLocation.getY() < 0) {
            commandMessages.getGeneric(GenericMessage.TOO_LOW).sendInColor(ChatColor.RED, sender);
            return false;
        } else if (spawnLocation.getBlockY() > world.getMaxHeight()) {
            commandMessages.getGeneric(GenericMessage.TOO_HIGH).sendInColor(ChatColor.RED, sender, world.getMaxHeight());
            return false;
        }
    } else {
        // Use the sender coordinates
        if (CommandUtils.isPhysical(sender)) {
            spawnLocation = sender instanceof Entity ? ((Entity) sender).getLocation() : ((BlockCommandSender) sender).getBlock().getLocation();
        } else {
            commandMessages.getGeneric(GenericMessage.NOT_PHYSICAL_COORDS).sendInColor(ChatColor.RED, sender);
            return false;
        }
    }
    // Update spawn location
    LocalizedStringImpl doneForOne = new LocalizedStringImpl("spawnpoint.done", commandMessages.getResourceBundle());
    int newX = spawnLocation.getBlockX();
    int newY = spawnLocation.getBlockY();
    int newZ = spawnLocation.getBlockZ();
    for (final Player target : targets) {
        target.setBedSpawnLocation(spawnLocation, true);
        doneForOne.send(sender, target.getName(), newX, newY, newZ);
    }
    return true;
}
Also used : Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) CommandTarget(net.glowstone.command.CommandTarget) LocalizedStringImpl(net.glowstone.i18n.LocalizedStringImpl) ArrayList(java.util.ArrayList) World(org.bukkit.World) Location(org.bukkit.Location)

Example 22 with LocalizedStringImpl

use of net.glowstone.i18n.LocalizedStringImpl in project Glowstone by GlowstoneMC.

the class TestForCommand 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];
    Entity[] entities;
    if (name.startsWith("@")) {
        CommandTarget target = new CommandTarget(sender, name);
        entities = target.getMatched(CommandUtils.getLocation(sender));
        if (entities.length == 0) {
            commandMessages.getGeneric(GenericMessage.NO_MATCHES).sendInColor(ChatColor.RED, sender, name);
            return false;
        }
    } else {
        // TODO: Select custom-named non-player entities?
        GlowPlayer player = (GlowPlayer) Bukkit.getPlayerExact(args[0]);
        if (player == null) {
            commandMessages.getGeneric(GenericMessage.NO_SUCH_PLAYER).sendInColor(ChatColor.RED, sender, name);
            return false;
        } else {
            entities = new Entity[] { player };
        }
    }
    LocalizedStringImpl foundMessage = new LocalizedStringImpl("testfor.found", commandMessages.getResourceBundle());
    if (args.length >= 2) {
        String data = String.join(" ", Arrays.copyOfRange(args, 1, args.length));
        CompoundTag tag;
        try {
            tag = Mojangson.parseCompound(data);
        } catch (MojangsonParseException e) {
            commandMessages.getGeneric(GenericMessage.INVALID_JSON).sendInColor(ChatColor.RED, sender, e.getMessage());
            return false;
        }
        LocalizedStringImpl wrongDataMessage = new LocalizedStringImpl("testfor.wrong-data", commandMessages.getResourceBundle());
        for (Entity entity : entities) {
            if (entity instanceof GlowEntity) {
                CompoundTag entityTag = new CompoundTag();
                EntityStorage.save((GlowEntity) entity, entityTag);
                if (tag.matches(entityTag)) {
                    foundMessage.send(sender, CommandUtils.getName(entity));
                } else {
                    wrongDataMessage.sendInColor(ChatColor.RED, sender, CommandUtils.getName(entity));
                }
            }
        }
    } else {
        for (Entity entity : entities) {
            foundMessage.send(sender, CommandUtils.getName(entity));
        }
    }
    // matching entities.
    return true;
}
Also used : Entity(org.bukkit.entity.Entity) GlowEntity(net.glowstone.entity.GlowEntity) CommandTarget(net.glowstone.command.CommandTarget) GlowPlayer(net.glowstone.entity.GlowPlayer) LocalizedStringImpl(net.glowstone.i18n.LocalizedStringImpl) MojangsonParseException(net.glowstone.util.mojangson.ex.MojangsonParseException) GlowEntity(net.glowstone.entity.GlowEntity) CompoundTag(net.glowstone.util.nbt.CompoundTag)

Example 23 with LocalizedStringImpl

use of net.glowstone.i18n.LocalizedStringImpl 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 24 with LocalizedStringImpl

use of net.glowstone.i18n.LocalizedStringImpl in project Glowstone by GlowstoneMC.

the class DefaultGameModeCommand 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) {
        sendUsageMessage(sender, messages);
        return false;
    }
    final String inputMode = args[0];
    final ResourceBundle bundle = messages.getResourceBundle();
    final GameMode gamemode = GameModeUtils.build(inputMode, messages.getLocale());
    if (gamemode == null) {
        new LocalizedStringImpl("defaultgamemode.unknown", bundle).sendInColor(ChatColor.RED, sender, inputMode);
        return false;
    }
    ServerProvider.getServer().setDefaultGameMode(gamemode);
    new LocalizedStringImpl("defaultgamemode.done", bundle).send(sender, ChatColor.GRAY + "" + ChatColor.ITALIC + GameModeUtils.prettyPrint(gamemode, messages.getLocale()));
    return true;
}
Also used : GameMode(org.bukkit.GameMode) LocalizedStringImpl(net.glowstone.i18n.LocalizedStringImpl) ResourceBundle(java.util.ResourceBundle)

Example 25 with LocalizedStringImpl

use of net.glowstone.i18n.LocalizedStringImpl in project Glowstone by GlowstoneMC.

the class EnchantCommand method execute.

@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages commandMessages) {
    if (!testPermission(sender, commandMessages.getPermissionMessage())) {
        return true;
    }
    if (args.length < 3) {
        sendUsageMessage(sender, commandMessages);
        return false;
    }
    String name = args[0];
    Stream<GlowPlayer> players;
    if (name.startsWith("@")) {
        CommandTarget target = new CommandTarget(sender, name);
        players = Arrays.stream(target.getMatched(CommandUtils.getLocation(sender))).filter(GlowPlayer.class::isInstance).map(GlowPlayer.class::cast);
    } 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).stream();
        }
    }
    Enchantment enchantment = GlowEnchantment.parseEnchantment(args[1]);
    if (enchantment == null) {
        new LocalizedStringImpl("enchant.unknown", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender, args[1]);
        return false;
    }
    int level;
    try {
        level = Integer.parseInt(args[2]);
    } catch (NumberFormatException exc) {
        commandMessages.getGeneric(GenericMessage.NAN).sendInColor(ChatColor.RED, sender, args[2]);
        return false;
    }
    LocalizedStringImpl successMessage = new LocalizedStringImpl("enchant.done", commandMessages.getResourceBundle());
    players.filter(player -> player.getItemInHand() != null).filter(player -> player.getItemInHand().getData().getItemType() != Material.AIR).filter(player -> enchantment.canEnchantItem(player.getItemInHand())).forEach(player -> {
        ItemStack itemInHand = player.getItemInHand();
        itemInHand.addUnsafeEnchantment(enchantment, level);
        player.setItemInHand(itemInHand);
        successMessage.send(sender);
    });
    return true;
}
Also used : CommandSender(org.bukkit.command.CommandSender) Arrays(java.util.Arrays) NamespacedKey(org.bukkit.NamespacedKey) Enchantment(org.bukkit.enchantments.Enchantment) StringUtil(org.bukkit.util.StringUtil) NonNls(org.jetbrains.annotations.NonNls) GlowPlayer(net.glowstone.entity.GlowPlayer) LocalizedStringImpl(net.glowstone.i18n.LocalizedStringImpl) ItemStack(org.bukkit.inventory.ItemStack) ArrayList(java.util.ArrayList) List(java.util.List) Stream(java.util.stream.Stream) ChatColor(org.bukkit.ChatColor) CommandUtils(net.glowstone.command.CommandUtils) CommandTarget(net.glowstone.command.CommandTarget) GlowEnchantment(net.glowstone.constants.GlowEnchantment) Collections(java.util.Collections) Material(org.bukkit.Material) Bukkit(org.bukkit.Bukkit) CommandTarget(net.glowstone.command.CommandTarget) GlowPlayer(net.glowstone.entity.GlowPlayer) LocalizedStringImpl(net.glowstone.i18n.LocalizedStringImpl) Enchantment(org.bukkit.enchantments.Enchantment) GlowEnchantment(net.glowstone.constants.GlowEnchantment) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

LocalizedStringImpl (net.glowstone.i18n.LocalizedStringImpl)37 Location (org.bukkit.Location)15 ResourceBundle (java.util.ResourceBundle)11 CommandTarget (net.glowstone.command.CommandTarget)11 Entity (org.bukkit.entity.Entity)10 Player (org.bukkit.entity.Player)10 ArrayList (java.util.ArrayList)7 Material (org.bukkit.Material)6 GlowWorld (net.glowstone.GlowWorld)5 GlowPlayer (net.glowstone.entity.GlowPlayer)5 CompoundTag (net.glowstone.util.nbt.CompoundTag)5 World (org.bukkit.World)5 GlowServer (net.glowstone.GlowServer)4 GlowBlock (net.glowstone.block.GlowBlock)4 MojangsonParseException (net.glowstone.util.mojangson.ex.MojangsonParseException)4 BlockCommandSender (org.bukkit.command.BlockCommandSender)3 ItemStack (org.bukkit.inventory.ItemStack)3 BlockStateData (net.glowstone.block.state.BlockStateData)2 InvalidBlockStateException (net.glowstone.block.state.InvalidBlockStateException)2 GlowEntity (net.glowstone.entity.GlowEntity)2