Search in sources :

Example 1 with BukkitCommandExecutionContext

use of co.aikar.commands.BukkitCommandExecutionContext 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 VoxelGameLibException("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 VoxelGameLibException("Unknown user " + ((Player) sender).getDisplayName()));
                        } else {
                            throw new InvalidCommandArgument(MessageKeys.NOT_ALLOWED_ON_CONSOLE, false);
                        }
                    }
                }
                return null;
            } else {
                return userHandler.getUser(arg).orElseThrow(() -> new VoxelGameLibException("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.lang.Locale) GameMode(com.voxelgameslib.voxelgameslib.game.GameMode) Player(org.bukkit.entity.Player) InvalidCommandArgument(co.aikar.commands.InvalidCommandArgument) VoxelGameLibException(com.voxelgameslib.voxelgameslib.exception.VoxelGameLibException) CommandSender(org.bukkit.command.CommandSender) Trackable(com.voxelgameslib.voxelgameslib.stats.Trackable) BukkitCommandExecutionContext(co.aikar.commands.BukkitCommandExecutionContext)

Aggregations

BukkitCommandExecutionContext (co.aikar.commands.BukkitCommandExecutionContext)1 InvalidCommandArgument (co.aikar.commands.InvalidCommandArgument)1 VoxelGameLibException (com.voxelgameslib.voxelgameslib.exception.VoxelGameLibException)1 GameMode (com.voxelgameslib.voxelgameslib.game.GameMode)1 Locale (com.voxelgameslib.voxelgameslib.lang.Locale)1 Trackable (com.voxelgameslib.voxelgameslib.stats.Trackable)1 CommandSender (org.bukkit.command.CommandSender)1 Player (org.bukkit.entity.Player)1