Search in sources :

Example 1 with Locale

use of com.voxelgameslib.voxelgameslib.internal.lang.Locale in project VoxelGamesLibv2 by VoxelGamesLib.

the class VoxelGamesLib method registerCommandContexts.

private void registerCommandContexts() {
    CommandContexts<BukkitCommandExecutionContext> con = commandManager.getCommandContexts();
    con.registerIssuerAwareContext(User.class, c -> {
        boolean isOptional = c.hasAnnotation(Optional.class);
        CommandSender sender = c.getSender();
        boolean isPlayerSender = sender instanceof Player;
        if (!c.hasFlag("other")) {
            Player player = isPlayerSender ? (Player) sender : null;
            if (player == null) {
                if (!isOptional) {
                    throw new InvalidCommandArgument(MessageKeys.NOT_ALLOWED_ON_CONSOLE, false);
                } else {
                    return null;
                }
            }
            return userHandler.getUser(player.getUniqueId()).orElseThrow(() -> new InvalidCommandArgument("Unknown user " + player.getDisplayName()));
        } else {
            String arg = c.popFirstArg();
            if (arg == null) {
                if (isOptional) {
                    if (c.hasFlag("defaultself")) {
                        if (isPlayerSender) {
                            return userHandler.getUser(((Player) sender).getUniqueId()).orElseThrow(() -> new InvalidCommandArgument("Unknown user " + ((Player) sender).getDisplayName()));
                        } else {
                            throw new InvalidCommandArgument(MessageKeys.NOT_ALLOWED_ON_CONSOLE, false);
                        }
                    }
                }
                return null;
            } else {
                return userHandler.getUser(arg).orElseThrow(() -> new InvalidCommandArgument("Unknown user " + arg));
            }
        }
    });
    con.registerContext(int.class, c -> Integer.parseInt(c.popFirstArg()));
    con.registerContext(GameMode.class, c -> {
        GameMode mode = gameHandler.getGameModes().stream().filter(gameMode -> gameMode.getName().equalsIgnoreCase(c.getFirstArg())).findAny().orElseThrow(() -> new InvalidCommandArgument("Unknown gamemode " + c.getFirstArg()));
        // pop later so that we can get a nice error message
        c.popLastArg();
        return mode;
    });
    con.registerContext(Locale.class, c -> {
        Locale locale = Locale.fromName(c.getFirstArg()).orElse(Locale.fromTag(c.getFirstArg()).orElseThrow(() -> new InvalidCommandArgument("Unknown locale " + c.getFirstArg())));
        // pop later so that we can get a nice error message
        c.popFirstArg();
        return locale;
    });
    con.registerContext(Role.class, c -> Role.fromName(c.popFirstArg()));
    con.registerContext(UUID.class, c -> UUID.fromString(c.popFirstArg()));
    con.registerContext(Trackable.class, c -> {
        Trackable s = StatsHandler.fromName(c.getFirstArg()).orElseThrow(() -> new InvalidCommandArgument("Unknown stats type " + c.getFirstArg()));
        // pop later so that we can get a nice error message
        c.popFirstArg();
        return s;
    });
}
Also used : Locale(com.voxelgameslib.voxelgameslib.internal.lang.Locale) GameMode(com.voxelgameslib.voxelgameslib.api.game.GameMode) Player(org.bukkit.entity.Player) InvalidCommandArgument(co.aikar.commands.InvalidCommandArgument) CommandSender(org.bukkit.command.CommandSender) Trackable(com.voxelgameslib.voxelgameslib.api.stats.Trackable) BukkitCommandExecutionContext(co.aikar.commands.BukkitCommandExecutionContext)

Example 2 with Locale

use of com.voxelgameslib.voxelgameslib.internal.lang.Locale in project VoxelGamesLibv2 by VoxelGamesLib.

the class LangCommands method lang.

@Default
@CommandPermission("%user")
public void lang(@Nonnull User sender) {
    StringBuilder sb = new StringBuilder();
    for (Locale loc : langHandler.getInstalledLocales()) {
        sb.append(loc.getTag()).append(" (").append(loc.getName()).append("), ");
    }
    sb.setLength(sb.length() - 1);
    Lang.msg(sender, LangKey.LANG_INSTALLED, sb.toString());
    Lang.msg(sender, LangKey.LANG_CURRENT, sender.getLocale().getName());
    Lang.msg(sender, LangKey.TRANSLATED_BY);
}
Also used : Locale(com.voxelgameslib.voxelgameslib.internal.lang.Locale) Default(co.aikar.commands.annotation.Default) CommandPermission(co.aikar.commands.annotation.CommandPermission)

Aggregations

Locale (com.voxelgameslib.voxelgameslib.internal.lang.Locale)2 BukkitCommandExecutionContext (co.aikar.commands.BukkitCommandExecutionContext)1 InvalidCommandArgument (co.aikar.commands.InvalidCommandArgument)1 CommandPermission (co.aikar.commands.annotation.CommandPermission)1 Default (co.aikar.commands.annotation.Default)1 GameMode (com.voxelgameslib.voxelgameslib.api.game.GameMode)1 Trackable (com.voxelgameslib.voxelgameslib.api.stats.Trackable)1 CommandSender (org.bukkit.command.CommandSender)1 Player (org.bukkit.entity.Player)1