Search in sources :

Example 1 with PlayerUtil

use of com.easterlyn.util.PlayerUtil in project Easterlyn by Easterlyn.

the class CoreContexts method register.

public static void register(EasterlynCore plugin) {
    ContextResolver<Long, BukkitCommandExecutionContext> longResolver = context -> {
        String firstArg = context.popFirstArg();
        Matcher matcher = INTEGER_PATTERN.matcher(firstArg);
        if (matcher.find()) {
            try {
                return Long.valueOf(matcher.group(1));
            } catch (NumberFormatException e) {
                throw new InvalidCommandArgument(CoreLang.WHOLE_NUMBER);
            }
        }
        firstArg = firstArg.toUpperCase();
        if (firstArg.matches("[IVXLCDM]+")) {
            return (long) NumberUtil.intFromRoman(firstArg);
        }
        throw new InvalidCommandArgument(CoreLang.WHOLE_NUMBER);
    };
    plugin.getCommandManager().getCommandContexts().registerContext(long.class, longResolver);
    plugin.getCommandManager().getCommandContexts().registerContext(Long.class, longResolver);
    ContextResolver<Integer, BukkitCommandExecutionContext> intResolver = context -> Math.toIntExact(longResolver.getContext(context));
    plugin.getCommandManager().getCommandContexts().registerContext(int.class, intResolver);
    plugin.getCommandManager().getCommandContexts().registerContext(Integer.class, intResolver);
    plugin.getCommandManager().getCommandContexts().registerContext(UUID.class, context -> {
        String firstArg = context.popFirstArg();
        if (firstArg == null) {
            // TODO lang
            throw new InvalidCommandArgument("UUID required");
        }
        try {
            return UUID.fromString(firstArg);
        } catch (IllegalArgumentException e) {
            throw new InvalidCommandArgument("UUID required");
        }
    // TODO allow fetching by player after
    });
    plugin.getCommandManager().getCommandContexts().registerIssuerAwareContext(BukkitCommandIssuer.class, CommandExecutionContext::getIssuer);
    plugin.getCommandManager().getCommandContexts().registerIssuerAwareContext(Player.class, new IssuerAwareContextResolver<>() {

        @Override
        public Player getContext(BukkitCommandExecutionContext context) throws InvalidCommandArgument {
            // noinspection unchecked
            if (context.hasFlag(SELF) || context.hasFlag(ONLINE_WITH_PERM) && context.getIssuer().isPlayer() && context.getCmd().getRequiredPermissions().stream().noneMatch(perm -> context.getIssuer().hasPermission(perm.toString().replace(".self", ".other")))) {
                return getSelf(context.getIssuer());
            }
            if (context.hasFlag(ONLINE)) {
                return getOnline(context.getIssuer(), context.popFirstArg());
            }
            if (context.hasFlag(OFFLINE)) {
                return getOffline(context.getIssuer(), context.popFirstArg());
            }
            // Default/ONLINE_WITH_PERM behavior: Attempt to match online, fall through to self.
            Player player = null;
            String firstArg = context.getFirstArg();
            if (firstArg != null && firstArg.length() > 3) {
                try {
                    player = getOnline(context.getIssuer(), firstArg);
                } catch (InvalidCommandArgument ignored) {
                // If user is not specified, fall through to self.
                }
            }
            if (player != null) {
                context.popFirstArg();
                return player;
            }
            return getSelf(context.getIssuer());
        }

        @NotNull
        private Player getSelf(@NotNull BukkitCommandIssuer issuer) throws InvalidCommandArgument {
            if (issuer.isPlayer()) {
                return issuer.getPlayer();
            }
            throw new InvalidCommandArgument(CoreLang.NO_CONSOLE);
        }

        @NotNull
        private Player getOnline(@NotNull BukkitCommandIssuer issuer, @Nullable String argument) throws InvalidCommandArgument {
            if (argument == null) {
                throw new InvalidCommandArgument(CoreLang.INVALID_PLAYER, "{value}", "null");
            }
            Player player = PlayerUtil.matchOnlinePlayer(issuer.getIssuer(), argument);
            if (player == null) {
                throw new InvalidCommandArgument(CoreLang.INVALID_PLAYER, "{value}", argument);
            }
            return player;
        }

        @NotNull
        private Player getOffline(@NotNull BukkitCommandIssuer issuer, @Nullable String argument) throws InvalidCommandArgument {
            if (argument == null) {
                throw new InvalidCommandArgument(CoreLang.INVALID_PLAYER, "{value}", "null");
            }
            Player player;
            try {
                player = PlayerUtil.matchPlayer(issuer.getIssuer(), argument, true, plugin);
            } catch (IllegalAccessException e) {
                ReportableEvent.call("Called PlayerUtil#matchPlayer on the main thread while executing!", e, 5);
                player = PlayerUtil.matchOnlinePlayer(issuer.getIssuer(), argument);
            }
            if (player == null) {
                throw new InvalidCommandArgument(CoreLang.INVALID_PLAYER, "{value}", argument);
            }
            return player;
        }
    });
    plugin.getCommandManager().getCommandContexts().registerIssuerAwareContext(ConsoleCommandSender.class, context -> {
        if (!(context.getIssuer().getIssuer() instanceof ConsoleCommandSender)) {
            throw new InvalidCommandArgument(CoreLang.ONLY_CONSOLE);
        }
        return (ConsoleCommandSender) context.getIssuer().getIssuer();
    });
    plugin.getCommandManager().getCommandContexts().registerIssuerAwareContext(User.class, context -> {
        Player player = (Player) plugin.getCommandManager().getCommandContexts().getResolver(Player.class).getContext(context);
        return plugin.getUserManager().getUser(player.getUniqueId());
    });
    plugin.getCommandManager().getCommandContexts().registerContext(Date.class, context -> {
        String firstArg = context.popFirstArg();
        long duration = NumberUtil.parseDuration(firstArg);
        return new Date(Math.addExact(System.currentTimeMillis(), duration));
    });
    plugin.getCommandManager().getCommandContexts().registerContext(ChatColor.class, new ContextResolver<>() {

        @Override
        public ChatColor getContext(BukkitCommandExecutionContext context1) throws InvalidCommandArgument {
            ChatColor matched = Colors.getOrDefault(context1.popFirstArg(), null);
            if (matched == null) {
                invalid(context1);
            }
            if (matched == ChatColor.RESET || !context1.hasFlag("colour") && !context1.hasFlag("format")) {
                // Reset is a special case - used to clear colour settings
                return matched;
            }
            boolean format = matched == ChatColor.BOLD || matched == ChatColor.UNDERLINE || matched == ChatColor.STRIKETHROUGH || matched == ChatColor.MAGIC;
            if (context1.hasFlag("colour") && format || context1.hasFlag("format") && !format) {
                invalid(context1);
            }
            return matched;
        }

        private void invalid(BukkitCommandExecutionContext context1) {
            throw new InvalidCommandArgument(MessageKeys.PLEASE_SPECIFY_ONE_OF, "{valid}", Arrays.stream(org.bukkit.ChatColor.values()).filter(chatColor -> context1.hasFlag("format") ? chatColor.isFormat() : !context1.hasFlag("colour") || chatColor.isColor()).map(Enum::name).collect(Collectors.joining(", ", "[", "]")));
        }
    });
    // TODO lang for invalid args
    plugin.getCommandManager().getCommandContexts().registerIssuerAwareContext(World.class, context -> {
        String worldName = context.getFirstArg();
        if (worldName == null) {
            if (context.isOptional() && context.getIssuer().isPlayer()) {
                return context.getIssuer().getPlayer().getWorld();
            }
            throw new InvalidCommandArgument("No world specified!");
        }
        World world = plugin.getServer().getWorld(worldName.toLowerCase());
        if (world == null) {
            if (context.isOptional() && context.getIssuer().isPlayer()) {
                return context.getIssuer().getPlayer().getWorld();
            }
            throw new InvalidCommandArgument("No world specified!");
        }
        context.popFirstArg();
        return world;
    });
}
Also used : Arrays(java.util.Arrays) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) InvalidCommandArgument(co.aikar.commands.InvalidCommandArgument) Date(java.util.Date) ContextResolver(co.aikar.commands.contexts.ContextResolver) Player(org.bukkit.entity.Player) BukkitCommandExecutionContext(co.aikar.commands.BukkitCommandExecutionContext) IssuerAwareContextResolver(co.aikar.commands.contexts.IssuerAwareContextResolver) Matcher(java.util.regex.Matcher) PlayerUtil(com.easterlyn.util.PlayerUtil) World(org.bukkit.World) ChatColor(net.md_5.bungee.api.ChatColor) CommandExecutionContext(co.aikar.commands.CommandExecutionContext) User(com.easterlyn.user.User) NumberUtil(com.easterlyn.util.NumberUtil) MessageKeys(co.aikar.commands.MessageKeys) EasterlynCore(com.easterlyn.EasterlynCore) ReportableEvent(com.easterlyn.event.ReportableEvent) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Nullable(org.jetbrains.annotations.Nullable) BukkitCommandIssuer(co.aikar.commands.BukkitCommandIssuer) Pattern(java.util.regex.Pattern) NotNull(org.jetbrains.annotations.NotNull) Colors(com.easterlyn.util.Colors) Player(org.bukkit.entity.Player) Matcher(java.util.regex.Matcher) BukkitCommandIssuer(co.aikar.commands.BukkitCommandIssuer) World(org.bukkit.World) NotNull(org.jetbrains.annotations.NotNull) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) Date(java.util.Date) InvalidCommandArgument(co.aikar.commands.InvalidCommandArgument) ChatColor(net.md_5.bungee.api.ChatColor) BukkitCommandExecutionContext(co.aikar.commands.BukkitCommandExecutionContext) BukkitCommandExecutionContext(co.aikar.commands.BukkitCommandExecutionContext) CommandExecutionContext(co.aikar.commands.CommandExecutionContext)

Aggregations

BukkitCommandExecutionContext (co.aikar.commands.BukkitCommandExecutionContext)1 BukkitCommandIssuer (co.aikar.commands.BukkitCommandIssuer)1 CommandExecutionContext (co.aikar.commands.CommandExecutionContext)1 InvalidCommandArgument (co.aikar.commands.InvalidCommandArgument)1 MessageKeys (co.aikar.commands.MessageKeys)1 ContextResolver (co.aikar.commands.contexts.ContextResolver)1 IssuerAwareContextResolver (co.aikar.commands.contexts.IssuerAwareContextResolver)1 EasterlynCore (com.easterlyn.EasterlynCore)1 ReportableEvent (com.easterlyn.event.ReportableEvent)1 User (com.easterlyn.user.User)1 Colors (com.easterlyn.util.Colors)1 NumberUtil (com.easterlyn.util.NumberUtil)1 PlayerUtil (com.easterlyn.util.PlayerUtil)1 Arrays (java.util.Arrays)1 Date (java.util.Date)1 UUID (java.util.UUID)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 ChatColor (net.md_5.bungee.api.ChatColor)1