Search in sources :

Example 1 with UserTop

use of net.dzikoysk.funnyguilds.user.top.UserTop in project FunnyGuilds by FunnyGuilds.

the class RankUtils method parseTop.

/**
 * Parse top placeholders (PTOP/GTOP-type-x) in text
 *
 * @param targetUser user for which text will be parsed
 * @param text       text to parse
 * @return parsed text
 */
public static String parseTop(PluginConfiguration config, TablistConfiguration tablistConfig, MessageConfiguration messages, UserRankManager userRankManager, GuildRankManager guildRankManager, User targetUser, String text) {
    if (text == null) {
        return null;
    }
    if (!text.contains("TOP-")) {
        return text;
    }
    Matcher matcher = TOP_PATTERN.matcher(text);
    if (matcher.find()) {
        String topType = matcher.group(1);
        String comparatorType = matcher.group(2);
        String indexString = matcher.group(3);
        int index;
        try {
            index = Integer.parseInt(indexString);
        } catch (NumberFormatException ex) {
            FunnyGuilds.getPluginLogger().error(indexString + "is invalid " + topType + " index!");
            return text;
        }
        if (index < 1) {
            FunnyGuilds.getPluginLogger().error("Index in " + topType + " must be greater or equal to 1!");
            return text;
        }
        if (topType.equalsIgnoreCase("PTOP")) {
            Option<UserTop> userTopOption = userRankManager.getTop(comparatorType);
            if (userTopOption.isEmpty()) {
                return StringUtils.replace(text, "{PTOP-" + comparatorType + "-" + index + "}", messages.ptopNoValue);
            }
            UserTop userTop = userTopOption.get();
            Option<User> userOption = userTop.getUser(index);
            if (userOption.isEmpty()) {
                return StringUtils.replace(text, "{PTOP-" + comparatorType + "-" + index + "}", messages.ptopNoValue);
            }
            User user = userOption.get();
            Number topValue = userTop.getComparator().getValue(user.getRank());
            String topFormat = config.top.format.ptop.getValue();
            if (!topFormat.isEmpty()) {
                List<RangeFormatting> valueFormatting = config.top.format.ptopValueFormatting.get(comparatorType.toLowerCase());
                topFormat = topFormat.replace("{VALUE-FORMAT}", valueFormatting == null ? topValue.toString() : NumberRange.inRangeToString(topValue, valueFormatting));
                topFormat = topFormat.replace("{VALUE}", topValue.toString());
            }
            return formatUserRank(config, text, "{PTOP-" + comparatorType + "-" + index + "}", targetUser, user, topFormat);
        } else if (topType.equalsIgnoreCase("GTOP")) {
            Option<GuildTop> guildTopOption = guildRankManager.getTop(comparatorType);
            if (guildTopOption.isEmpty()) {
                return StringUtils.replace(text, "{GTOP-" + comparatorType + "-" + index + "}", messages.gtopNoValue);
            }
            GuildTop guildTop = guildTopOption.get();
            Option<Guild> guildOption = guildTop.getGuild(index);
            if (guildOption.isEmpty()) {
                return StringUtils.replace(text, "{GTOP-" + comparatorType + "-" + index + "}", messages.gtopNoValue);
            }
            Guild guild = guildOption.get();
            Number topValue = guildTop.getComparator().getValue(guild.getRank());
            String topFormat = config.top.format.gtop.getValue();
            List<RangeFormatting> valueFormatting = config.top.format.gtopValueFormatting.get(comparatorType.toLowerCase());
            topFormat = topFormat.replace("{VALUE-FORMAT}", valueFormatting == null ? topValue.toString() : NumberRange.inRangeToString(topValue, valueFormatting));
            topFormat = topFormat.replace("{VALUE}", topValue.toString());
            return formatGuildRank(config, tablistConfig, text, "{GTOP-" + comparatorType + "-" + index + "}", targetUser, guild, topFormat);
        }
    }
    return text;
}
Also used : User(net.dzikoysk.funnyguilds.user.User) Matcher(java.util.regex.Matcher) Guild(net.dzikoysk.funnyguilds.guild.Guild) GuildTop(net.dzikoysk.funnyguilds.guild.top.GuildTop) UserTop(net.dzikoysk.funnyguilds.user.top.UserTop) Option(panda.std.Option) List(java.util.List) RangeFormatting(net.dzikoysk.funnyguilds.config.RangeFormatting)

Aggregations

List (java.util.List)1 Matcher (java.util.regex.Matcher)1 RangeFormatting (net.dzikoysk.funnyguilds.config.RangeFormatting)1 Guild (net.dzikoysk.funnyguilds.guild.Guild)1 GuildTop (net.dzikoysk.funnyguilds.guild.top.GuildTop)1 User (net.dzikoysk.funnyguilds.user.User)1 UserTop (net.dzikoysk.funnyguilds.user.top.UserTop)1 Option (panda.std.Option)1