Search in sources :

Example 1 with ClanService

use of nl.riebie.mcclans.api.ClanService in project UltimateChat by FabioZumbi12.

the class UCMessages method formatTags.

public static String formatTags(String tag, String text, Object cmdSender, Object receiver, String msg, UCChannel ch) {
    if (receiver instanceof CommandSource && tag.equals("message")) {
        text = text.replace("{message}", mention(cmdSender, receiver, msg));
        if (UChat.get().getConfig().root().general.item_hand.enable) {
            text = text.replace(UChat.get().getConfig().root().general.item_hand.placeholder, formatTags("", UCUtil.toColor(UChat.get().getConfig().root().general.item_hand.format), cmdSender, receiver, msg, ch));
        }
    } else {
        text = text.replace("{message}", msg);
    }
    if (tag.equals("message") && !UChat.get().getConfig().root().general.enable_tags_on_messages) {
        return text;
    }
    text = text.replace("{ch-color}", ch.getColor()).replace("{ch-name}", ch.getName()).replace("{ch-alias}", ch.getAlias());
    if (UChat.get().getConfig().root().jedis.enable && ch.useJedis()) {
        text = text.replace("{jedis-id}", UChat.get().getConfig().root().jedis.server_id);
    }
    if (cmdSender instanceof CommandSource) {
        text = text.replace("{playername}", ((CommandSource) cmdSender).getName());
        if (receiver instanceof CommandSource) {
            text = text.replace("{receivername}", ((CommandSource) receiver).getName());
        }
        if (receiver instanceof String) {
            text = text.replace("{receivername}", receiver.toString());
        }
    } else {
        text = text.replace("{playername}", (String) cmdSender);
        if (receiver instanceof CommandSource)
            text = text.replace("{receivername}", ((CommandSource) receiver).getName());
        if (receiver instanceof String)
            text = text.replace("{receivername}", (String) receiver);
    }
    for (String repl : registeredReplacers.keySet()) {
        if (registeredReplacers.get(repl).equals(repl)) {
            text = text.replace(repl, "");
            continue;
        }
        text = text.replace(repl, registeredReplacers.get(repl));
    }
    if (defFormat.length > 0) {
        StringBuilder all = new StringBuilder();
        for (int i = 0; i < defFormat.length; i++) {
            if (i == 0)
                text = text.replace("{chat_header}", defFormat[i]);
            if (i == 1)
                text = text.replace("{chat_body}", defFormat[i]);
            if (i == 2)
                text = text.replace("{chat_footer}", defFormat[i]);
            all.append(defFormat[i]);
        }
        text = text.replace("{chat_all}", all.toString());
    }
    if (text.contains("{time-now}")) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        text = text.replace("{time-now}", sdf.format(cal.getTime()));
    }
    if (cmdSender instanceof Player) {
        Player sender = (Player) cmdSender;
        if (text.contains("{nickname}") && sender.get(Keys.DISPLAY_NAME).isPresent()) {
            String nick = sender.get(Keys.DISPLAY_NAME).get().toPlain();
            if (!nick.equals(sender.getName())) {
                text = text.replace("{nickname}", nick);
                text = text.replace("{nick-symbol}", UChat.get().getConfig().root().general.nick_symbol);
            } else if (Sponge.getPluginManager().getPlugin("nucleus").isPresent() && NucleusAPI.getNicknameService().isPresent()) {
                Optional<Text> opNick = NucleusAPI.getNicknameService().get().getNickname(sender);
                if (opNick.isPresent()) {
                    nick = TextSerializers.FORMATTING_CODE.serialize(opNick.get());
                    text = text.replace("{nick-symbol}", UChat.get().getConfig().root().general.nick_symbol);
                }
            }
            text = text.replace("{nickname}", nick);
        }
        // replace item hand
        text = text.replace(UChat.get().getConfig().root().general.item_hand.placeholder, UCUtil.toColor(UChat.get().getConfig().root().general.item_hand.format));
        ItemStack item = null;
        if (sender.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
            item = sender.getItemInHand(HandTypes.MAIN_HAND).get();
        } else if (sender.getItemInHand(HandTypes.OFF_HAND).isPresent()) {
            item = sender.getItemInHand(HandTypes.OFF_HAND).get();
        }
        if (text.contains("{hand-") && item != null) {
            text = text.replace("{hand-durability}", item.get(Keys.ITEM_DURABILITY).isPresent() ? String.valueOf(item.get(Keys.ITEM_DURABILITY).get()) : "").replace("{hand-name}", item.getItem().getTranslation().get());
            if (item.get(Keys.ITEM_LORE).isPresent()) {
                StringBuilder lorestr = new StringBuilder();
                for (Text line : item.get(Keys.ITEM_LORE).get()) {
                    lorestr.append("\n ").append(line.toPlain());
                }
                if (lorestr.length() >= 2) {
                    text = text.replace("{hand-lore}", lorestr.toString().substring(0, lorestr.length() - 1));
                }
            }
            if (item.get(Keys.ITEM_ENCHANTMENTS).isPresent()) {
                StringBuilder str = new StringBuilder();
                str.append(UChat.get().getVHelper().getEnchantments(str, item));
                if (str.length() >= 2) {
                    text = text.replace("{hand-enchants}", str.toString().substring(0, str.length() - 1));
                }
            }
            text = text.replace("{hand-amount}", String.valueOf(item.getQuantity()));
            text = text.replace("{hand-name}", item.getItem().getName());
            text = text.replace("{hand-type}", item.getItem().getTranslation().get());
        } else {
            text = text.replace("{hand-name}", UChat.get().getLang().get("chat.emptyslot"));
            text = text.replace("{hand-type}", "Air");
        }
        if (text.contains("{world}")) {
            String world = sender.getWorld().getName();
            text = text.replace("{world}", UChat.get().getConfig().root().general.world_names.getOrDefault(world, world));
        }
        if (text.contains("{balance}") && UChat.get().getEco() != null) {
            UniqueAccount acc = UChat.get().getEco().getOrCreateAccount(sender.getUniqueId()).get();
            text = text.replace("{balance}", "" + acc.getBalance(UChat.get().getEco().getDefaultCurrency()).intValue());
        }
        // parse permissions options
        try {
            // player options
            Pattern pp = Pattern.compile("\\{player_option_(.+?)\\}");
            Matcher pm = pp.matcher(text);
            while (pm.find()) {
                if (sender.getOption(pm.group(1)).isPresent() && !text.contains(sender.getOption(pm.group(1)).get())) {
                    text = text.replace("{player_option_" + pm.group(1) + "}", sender.getOption(pm.group(1)).get());
                    pm = pp.matcher(text);
                }
            }
            // group options
            Subject sub = UChat.get().getPerms().getGroupAndTag(sender);
            if (sub != null) {
                text = text.replace("{option_group}", UChat.get().getConfig().root().general.group_names.getOrDefault(sub.getIdentifier(), sub.getIdentifier()));
                if (sub.getOption("display_name").isPresent()) {
                    text = text.replace("{option_display_name}", sub.getOption("display_name").get());
                } else {
                    text = text.replace("{option_display_name}", UChat.get().getConfig().root().general.group_names.getOrDefault(sub.getIdentifier(), sub.getIdentifier()));
                }
                Pattern gp = Pattern.compile("\\{option_(.+?)\\}");
                Matcher gm = gp.matcher(text);
                while (gm.find()) {
                    if (sub.getOption(gm.group(1)).isPresent() && !text.contains(sub.getOption(gm.group(1)).get())) {
                        text = text.replace("{option_" + gm.group(1) + "}", sub.getOption(gm.group(1)).get());
                        gm = gp.matcher(text);
                    }
                }
            }
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        if (text.contains("{clan_") && UChat.get().getConfig().root().hooks.MCClans.enable) {
            Optional<ClanService> clanServiceOpt = Sponge.getServiceManager().provide(ClanService.class);
            if (clanServiceOpt.isPresent()) {
                ClanService clan = clanServiceOpt.get();
                ClanPlayer cp = clan.getClanPlayer(sender.getUniqueId());
                if (cp != null && cp.isMemberOfAClan()) {
                    text = text.replace("{clan_name}", checkEmpty(cp.getClan().getName())).replace("{clan_tag}", cp.getClan().getTag()).replace("{clan_tag_color}", TextSerializers.FORMATTING_CODE.serialize(cp.getClan().getTagColored())).replace("{clan_kdr}", "" + cp.getClan().getKDR()).replace("{clan_player_rank}", checkEmpty(cp.getRank().getName())).replace("{clan_player_kdr}", "" + cp.getKillDeath().getKDR()).replace("{clan_player_ffprotected}", String.valueOf(cp.isFfProtected())).replace("{clan_player_isowner}", String.valueOf(cp.getClan().getOwner().equals(cp)));
                }
            }
        }
        if (Sponge.getPluginManager().getPlugin("placeholderapi").isPresent()) {
            Optional<PlaceholderService> phapiOpt = Sponge.getServiceManager().provide(PlaceholderService.class);
            Pattern p = Pattern.compile("\\%([^\\%]*)\\%");
            if (phapiOpt.isPresent() && p.matcher(text).find()) {
                PlaceholderService phapi = phapiOpt.get();
                // fix last color from string
                String lastcolor = text.length() > 1 ? text.substring(text.length() - 2, text.length()) : "";
                if (!Pattern.compile("(&([A-Fa-fK-Ok-oRr0-9]))").matcher(lastcolor).find())
                    lastcolor = "";
                text = TextSerializers.FORMATTING_CODE.serialize(phapi.replacePlaceholders(text, sender, receiver, p)) + lastcolor;
            }
        }
    }
    text = text.replace("{option_suffix}", "&r: ");
    if (cmdSender instanceof CommandSource) {
        text = text.replace("{nickname}", UChat.get().getConfig().root().general.console_tag.replace("{console}", ((CommandSource) cmdSender).getName()));
    } else {
        text = text.replace("{nickname}", UChat.get().getConfig().root().general.console_tag.replace("{console}", (String) cmdSender));
    }
    // remove blank items
    text = text.replaceAll("\\{.*\\}", "");
    if (!tag.equals("message")) {
        for (String rpl : UChat.get().getConfig().root().general.remove_from_chat) {
            text = text.replace(UCUtil.toColor(rpl), "");
        }
    }
    if (text.equals(" ") || text.equals("  ")) {
        return text = "";
    }
    return text;
}
Also used : Pattern(java.util.regex.Pattern) Player(org.spongepowered.api.entity.living.player.Player) ClanPlayer(nl.riebie.mcclans.api.ClanPlayer) UniqueAccount(org.spongepowered.api.service.economy.account.UniqueAccount) Matcher(java.util.regex.Matcher) Text(org.spongepowered.api.text.Text) CommandSource(org.spongepowered.api.command.CommandSource) ClanPlayer(nl.riebie.mcclans.api.ClanPlayer) PlaceholderService(me.rojo8399.placeholderapi.PlaceholderService) Subject(org.spongepowered.api.service.permission.Subject) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ExecutionException(java.util.concurrent.ExecutionException) SimpleDateFormat(java.text.SimpleDateFormat) ClanService(nl.riebie.mcclans.api.ClanService)

Aggregations

SimpleDateFormat (java.text.SimpleDateFormat)1 ExecutionException (java.util.concurrent.ExecutionException)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 PlaceholderService (me.rojo8399.placeholderapi.PlaceholderService)1 ClanPlayer (nl.riebie.mcclans.api.ClanPlayer)1 ClanService (nl.riebie.mcclans.api.ClanService)1 CommandSource (org.spongepowered.api.command.CommandSource)1 Player (org.spongepowered.api.entity.living.player.Player)1 ItemStack (org.spongepowered.api.item.inventory.ItemStack)1 UniqueAccount (org.spongepowered.api.service.economy.account.UniqueAccount)1 Subject (org.spongepowered.api.service.permission.Subject)1 Text (org.spongepowered.api.text.Text)1