Search in sources :

Example 6 with LocalizedStringImpl

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

the class TitleCommand method execute.

// CAUTION: Most usage messages in this method can't be replaced with sendUsageMessage, because
// they're subcommand-specific.
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args, CommandMessages commandMessages) {
    if (!testPermission(sender, commandMessages.getPermissionMessage())) {
        return true;
    }
    if (args.length < 2) {
        sendUsageMessage(sender, commandMessages);
        return false;
    }
    Player player = Bukkit.getPlayerExact(args[0]);
    if (player == null || sender instanceof Player && !((Player) sender).canSee(player)) {
        commandMessages.getGeneric(GenericMessage.NO_SUCH_PLAYER).send(sender, args[0]);
        return false;
    }
    @NonNls String action = args[1];
    if (action.equalsIgnoreCase("clear")) {
        ((GlowPlayer) player).clearTitle();
        new LocalizedStringImpl("title.done.clear", commandMessages.getResourceBundle()).send(sender, player.getName());
    } else if (action.equalsIgnoreCase("reset")) {
        player.resetTitle();
        new LocalizedStringImpl("title.done.reset", commandMessages.getResourceBundle()).send(sender, player.getName());
    } else if (action.equalsIgnoreCase("title")) {
        if (args.length < 3) {
            sendUsageMessage(sender, commandMessages);
            sender.sendMessage(ChatColor.RED + "Usage: /title <player> " + action + " <raw json>");
            return false;
        }
        StringBuilder message = new StringBuilder();
        for (int i = 2; i < args.length; i++) {
            message.append(args[i]);
        }
        String raw = message.toString().trim();
        if (!validJson(raw)) {
            sender.sendMessage(ChatColor.RED + "Invalid JSON: Could not parse, invalid format?");
            return false;
        }
        String component = raw;
        Map<String, Object> parsed = getJson(raw);
        if (parsed != null) {
            component = convertJson(parsed);
        }
        ((GlowPlayer) player).updateTitle(TitleMessage.Action.TITLE, component);
        ((GlowPlayer) player).sendTitle();
        sender.sendMessage("Updated " + player.getName() + "'s title");
    } else if (action.equalsIgnoreCase("subtitle")) {
        if (args.length < 3) {
            sender.sendMessage(ChatColor.RED + "Usage: /title <player> " + action + " <raw json>");
            return false;
        }
        StringBuilder message = new StringBuilder();
        for (int i = 2; i < args.length; i++) {
            message.append(args[i]);
        }
        String raw = message.toString().trim();
        if (!validJson(raw)) {
            sender.sendMessage(ChatColor.RED + "Invalid JSON: Could not parse, invalid format?");
            return false;
        }
        String component = raw;
        Object parsed = JSONValue.parse(raw);
        if (parsed instanceof JSONObject) {
            component = convertJson((JSONObject) parsed);
        }
        ((GlowPlayer) player).updateTitle(TitleMessage.Action.SUBTITLE, component);
        sender.sendMessage("Updated " + player.getName() + "'s subtitle");
    } else if (action.equalsIgnoreCase("times")) {
        if (args.length != 5) {
            sender.sendMessage(ChatColor.RED + "Usage: /title <player> " + action + " <fade in> <stay time> <fade out>");
            return false;
        }
        if (!tryParseInt(args[2])) {
            sender.sendMessage(ChatColor.RED + "'" + args[2] + "' is not a number");
            return false;
        }
        if (!tryParseInt(args[3])) {
            sender.sendMessage(ChatColor.RED + "'" + args[3] + "' is not a number");
            return false;
        }
        if (!tryParseInt(args[4])) {
            sender.sendMessage(ChatColor.RED + "'" + args[4] + "' is not a number");
            return false;
        }
        ((GlowPlayer) player).updateTitle(TitleMessage.Action.TIMES, toInt(args[2]), toInt(args[3]), toInt(args[4]));
        sender.sendMessage("Updated " + player.getName() + "'s times");
    } else {
        sendUsageMessage(sender, commandMessages);
        return false;
    }
    return true;
}
Also used : NonNls(org.jetbrains.annotations.NonNls) GlowPlayer(net.glowstone.entity.GlowPlayer) Player(org.bukkit.entity.Player) JSONObject(org.json.simple.JSONObject) GlowPlayer(net.glowstone.entity.GlowPlayer) LocalizedStringImpl(net.glowstone.i18n.LocalizedStringImpl) JSONObject(org.json.simple.JSONObject)

Example 7 with LocalizedStringImpl

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

the class SetBlockCommand method execute.

@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages commandMessages) {
    if (!testPermission(sender, commandMessages.getPermissionMessage())) {
        return true;
    }
    if (args.length < 4) {
        sendUsageMessage(sender, commandMessages);
        return false;
    }
    String itemName = CommandUtils.toNamespaced(args[3].toLowerCase());
    Material type = ItemIds.getBlock(itemName);
    if (type == null) {
        new LocalizedStringImpl("setblock.invalid.type", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender, itemName);
        return false;
    }
    Location location = CommandUtils.getLocation(CommandUtils.getLocation(sender), args[0], args[1], args[2]);
    GlowBlock block = (GlowBlock) location.getBlock();
    byte dataValue = 0;
    if (args.length > 4) {
        String state = args[4];
        BlockStateData data = CommandUtils.readState(sender, type, state);
        if (data == null) {
            return false;
        }
        if (data.isNumeric()) {
            dataValue = data.getNumericValue();
        } else {
            try {
                dataValue = StateSerialization.parseData(type, data).getData();
            } catch (InvalidBlockStateException e) {
                sender.sendMessage(ChatColor.RED + e.getMessage());
                return false;
            }
        }
    }
    block.setType(type, dataValue, true);
    if (args.length > 5 && block.getBlockEntity() != null) {
        String dataTag = String.join(" ", new ArrayList<>(Arrays.asList(args)).subList(5, args.length));
        try {
            CompoundTag prev = new CompoundTag();
            block.getBlockEntity().saveNbt(prev);
            CompoundTag tag = Mojangson.parseCompound(dataTag);
            tag.mergeInto(prev, true);
            block.getBlockEntity().loadNbt(prev);
        } catch (MojangsonParseException e) {
            commandMessages.getGeneric(GenericMessage.INVALID_JSON).sendInColor(ChatColor.RED, sender, e.getMessage());
            return false;
        }
    }
    new LocalizedStringImpl("setblock.done", commandMessages.getResourceBundle()).send(sender);
    return true;
}
Also used : GlowBlock(net.glowstone.block.GlowBlock) LocalizedStringImpl(net.glowstone.i18n.LocalizedStringImpl) ArrayList(java.util.ArrayList) MojangsonParseException(net.glowstone.util.mojangson.ex.MojangsonParseException) InvalidBlockStateException(net.glowstone.block.state.InvalidBlockStateException) Material(org.bukkit.Material) BlockStateData(net.glowstone.block.state.BlockStateData) CompoundTag(net.glowstone.util.nbt.CompoundTag) Location(org.bukkit.Location)

Example 8 with LocalizedStringImpl

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

the class SetWorldSpawnCommand method execute.

@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages commandMessages) {
    if (!testPermission(sender, commandMessages.getPermissionMessage())) {
        return true;
    }
    Location spawnLocation;
    final World world = CommandUtils.getWorld(sender);
    if (args.length == 0) {
        // Get the player current location
        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;
        }
    } else if (args.length >= 3) {
        // manage arguments
        final Location senderLocation;
        // Get the sender coordinates if relative is used
        if (args[0].startsWith("~") || args[1].startsWith("~") || args[2].startsWith("~")) {
            if (!CommandUtils.isPhysical(sender)) {
                commandMessages.getGeneric(GenericMessage.NOT_PHYSICAL_COORDS).sendInColor(ChatColor.RED, sender);
                return false;
            } else {
                senderLocation = 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)
            senderLocation = new Location(world, 0, 0, 0);
        }
        spawnLocation = CommandUtils.getLocation(senderLocation, args[0], args[1], args[2]);
    } else {
        sendUsageMessage(sender, commandMessages);
        return false;
    }
    int newY = spawnLocation.getBlockY();
    if (newY < 0) {
        commandMessages.getGeneric(GenericMessage.TOO_LOW).sendInColor(ChatColor.RED, sender);
        return false;
    } else if (newY > world.getMaxHeight()) {
        commandMessages.getGeneric(GenericMessage.TOO_HIGH).sendInColor(ChatColor.RED, sender, world.getMaxHeight());
        return false;
    }
    int newX = spawnLocation.getBlockX();
    int newZ = spawnLocation.getBlockZ();
    world.setSpawnLocation(newX, newY, newZ);
    new LocalizedStringImpl("setworldspawn.done", commandMessages.getResourceBundle()).send(sender, newX, newY, newZ);
    return true;
}
Also used : Entity(org.bukkit.entity.Entity) LocalizedStringImpl(net.glowstone.i18n.LocalizedStringImpl) World(org.bukkit.World) Location(org.bukkit.Location) BlockCommandSender(org.bukkit.command.BlockCommandSender)

Example 9 with LocalizedStringImpl

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

the class BanIpCommand method execute.

@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args, CommandMessages messages) {
    if (!testPermission(sender, messages.getPermissionMessage())) {
        return true;
    }
    final ResourceBundle resourceBundle = messages.getResourceBundle();
    if (args.length > 0) {
        String target = null;
        if (InetAddresses.isInetAddress(args[0])) {
            target = args[0];
        } else {
            Player player = Bukkit.getPlayer(args[0]);
            if (player != null) {
                target = player.getAddress().getAddress().getHostAddress();
            }
        }
        if (target != null) {
            if (args.length == 1) {
                Bukkit.getBanList(BanList.Type.IP).addBan(target, null, null, null);
            } else {
                StringBuilder reason = new StringBuilder();
                for (int i = 1; i < args.length; i++) {
                    reason.append(args[i]).append(" ");
                }
                Bukkit.getBanList(BanList.Type.IP).addBan(target, reason.toString(), null, null);
            }
            new LocalizedStringImpl("ban-ip.done", resourceBundle).send(sender, target);
            return true;
        }
        new LocalizedStringImpl("ban-ip.invalid", resourceBundle).send(sender);
        return false;
    }
    sendUsageMessage(sender, messages);
    return false;
}
Also used : Player(org.bukkit.entity.Player) LocalizedStringImpl(net.glowstone.i18n.LocalizedStringImpl) ResourceBundle(java.util.ResourceBundle)

Example 10 with LocalizedStringImpl

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

the class ClearCommand method clearAll.

private boolean clearAll(CommandSender sender, Player player, Material material, int data, int maxCount, ResourceBundle resourceBundle) {
    int count = countAllItems(player.getInventory(), material, data, maxCount);
    if (maxCount == 0) {
        new LocalizedStringImpl("clear.count", resourceBundle).send(sender, player.getName(), count);
        return true;
    }
    if (count == 0) {
        new LocalizedStringImpl("clear.empty", resourceBundle).send(sender, player.getName());
        return false;
    }
    if (material == null) {
        player.getInventory().clear();
    } else {
        int remaining = maxCount;
        for (ItemStack stack : player.getInventory().getContents()) {
            if (stack.getType() == material && (data == -1 || data == stack.getData().getData())) {
                // matches type and data
                if (maxCount == -1) {
                    player.getInventory().remove(stack);
                } else {
                    int oldAmount = stack.getAmount();
                    int removed = Math.min(oldAmount, remaining);
                    stack.setAmount(oldAmount - removed);
                    remaining -= removed;
                }
            }
            if (remaining == 0) {
                break;
            }
        }
    }
    if (count == 1) {
        new LocalizedStringImpl("clear.done.singular", resourceBundle).send(sender, player.getName());
    } else {
        new LocalizedStringImpl("clear.done", resourceBundle).send(sender, player.getName(), count);
    }
    return true;
}
Also used : LocalizedStringImpl(net.glowstone.i18n.LocalizedStringImpl) 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