Search in sources :

Example 6 with Builder

use of org.spongepowered.api.text.Text.Builder in project UltimateChat by FabioZumbi12.

the class UCDiscord method onMessageReceived.

@Override
public void onMessageReceived(MessageReceivedEvent e) {
    if (e.getAuthor().getId().equals(e.getJDA().getSelfUser().getId()) || e.getMember().getUser().isFake())
        return;
    String message = e.getMessage().getContentRaw();
    int used = 0;
    for (UCChannel ch : this.uchat.getChannels().values()) {
        if (ch.isListenDiscord() && ch.matchDiscordID(e.getChannel().getId())) {
            if (e.getMember().getUser().isBot() && !ch.AllowBot()) {
                continue;
            }
            // check if is cmd
            if (message.startsWith(this.uchat.getConfig().root().discord.server_commands.alias) && ch.getDiscordAllowCmds()) {
                message = message.replace(this.uchat.getConfig().root().discord.server_commands.alias + " ", "");
                if (!this.uchat.getConfig().root().discord.server_commands.withelist.isEmpty()) {
                    int count = 0;
                    for (String cmd : this.uchat.getConfig().root().discord.server_commands.withelist) {
                        if (message.startsWith(cmd))
                            count++;
                    }
                    if (count == 0)
                        continue;
                }
                if (!this.uchat.getConfig().root().discord.server_commands.blacklist.isEmpty()) {
                    int count = 0;
                    for (String cmd : this.uchat.getConfig().root().discord.server_commands.blacklist) {
                        if (message.startsWith(cmd))
                            count++;
                    }
                    if (count > 0)
                        continue;
                }
                Sponge.getCommandManager().process(Sponge.getServer().getConsole(), message);
                used++;
            } else {
                Builder text = Text.builder();
                // format prefixes tags
                String formated = formatTags(ch.getDiscordtoMCFormat(), ch, e, "", "");
                // add dd channel name to hover
                String hovered = formatTags(ch.getDiscordHover(), ch, e, "", "");
                text.append(Text.of(formated));
                if (!hovered.isEmpty()) {
                    text.onHover(TextActions.showText(Text.of(hovered)));
                }
                // format message
                if (!e.getMessage().getAttachments().isEmpty()) {
                    try {
                        text.onClick(TextActions.openUrl(new URL(e.getMessage().getAttachments().get(0).getUrl())));
                        text.onHover(TextActions.showText(Text.of(e.getMessage().getAttachments().get(0).getFileName())));
                        if (message.isEmpty()) {
                            text.append(Text.of("- Attachment -"));
                        } else {
                            text.append(Text.of(message));
                        }
                    } catch (MalformedURLException ignored) {
                    }
                } else {
                    text.append(Text.of(message));
                }
                ch.sendMessage(Sponge.getServer().getConsole(), text.build(), true);
                used++;
            }
        }
    }
    // check if is from log command chanel
    if (used == 0 && e.getChannel().getId().equals(UChat.get().getConfig().root().discord.commands_channel_id)) {
        if (!this.uchat.getConfig().root().discord.server_commands.withelist.isEmpty()) {
            int count = 0;
            for (String cmd : this.uchat.getConfig().root().discord.server_commands.withelist) {
                if (message.startsWith(cmd))
                    count++;
            }
            if (count == 0)
                return;
        }
        if (!this.uchat.getConfig().root().discord.server_commands.blacklist.isEmpty()) {
            int count = 0;
            for (String cmd : this.uchat.getConfig().root().discord.server_commands.blacklist) {
                if (message.startsWith(cmd))
                    count++;
            }
            if (count > 0)
                return;
        }
        Sponge.getCommandManager().process(Sponge.getServer().getConsole(), message);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Builder(org.spongepowered.api.text.Text.Builder) JDABuilder(jdalib.jda.core.JDABuilder) URL(java.net.URL)

Example 7 with Builder

use of org.spongepowered.api.text.Text.Builder in project UltimateChat by FabioZumbi12.

the class UCMessages method sendMessage.

public static Text sendMessage(CommandSource sender, Object receiver, Text srcTxt, UCChannel ch, boolean isSpy) {
    Builder formatter = Text.builder();
    Builder playername = Text.builder();
    Builder message = Text.builder();
    if (srcTxt.getClickAction().isPresent()) {
        message.onClick(srcTxt.getClickAction().get());
    }
    if (srcTxt.getHoverAction().isPresent()) {
        message.onHover(srcTxt.getHoverAction().get());
    }
    if (srcTxt.getShiftClickAction().isPresent()) {
        message.onShiftClick(srcTxt.getShiftClickAction().get());
    }
    String msg = srcTxt.toPlain();
    if (!ch.getName().equals("tell")) {
        String[] defaultBuilder = UChat.get().getConfig().getDefBuilder();
        if (ch.useOwnBuilder()) {
            defaultBuilder = ch.getBuilder();
        }
        String lastColor = "";
        for (String tag : defaultBuilder) {
            Builder tagBuilder = Text.builder();
            Builder msgBuilder;
            if (UChat.get().getConfig().root().tags.get(tag) == null) {
                tagBuilder.append(Text.of(tag));
                continue;
            }
            String format = lastColor + UChat.get().getConfig().root().tags.get(tag).format;
            String perm = UChat.get().getConfig().root().tags.get(tag).permission;
            String execute = UChat.get().getConfig().root().tags.get(tag).click_cmd;
            String url = UChat.get().getConfig().root().tags.get(tag).click_url;
            String suggest = UChat.get().getConfig().root().tags.get(tag).suggest;
            List<String> messages = UChat.get().getConfig().root().tags.get(tag).hover_messages;
            List<String> showWorlds = UChat.get().getConfig().root().tags.get(tag).show_in_worlds;
            List<String> hideWorlds = UChat.get().getConfig().root().tags.get(tag).hide_in_worlds;
            // check perm
            if (perm != null && !perm.isEmpty() && !sender.hasPermission(perm)) {
                continue;
            }
            // check show or hide in world
            if (sender instanceof Player) {
                if (showWorlds != null && !showWorlds.contains(((Player) sender).getWorld().getName())) {
                    continue;
                }
                if (hideWorlds != null && hideWorlds.contains(((Player) sender).getWorld().getName())) {
                    continue;
                }
            }
            StringBuilder tooltip = new StringBuilder();
            if (messages != null && !messages.isEmpty()) {
                for (String tp : messages) {
                    tooltip.append("\n").append(tp);
                }
                if (tooltip.length() > 2) {
                    tooltip = new StringBuilder(tooltip.substring(1));
                }
            }
            if (suggest != null && !suggest.isEmpty()) {
                tagBuilder.onClick(TextActions.suggestCommand(formatTags(tag, "/" + suggest, sender, receiver, msg, ch)));
            }
            if (execute != null && !execute.isEmpty()) {
                tagBuilder.onClick(TextActions.runCommand(formatTags(tag, "/" + execute, sender, receiver, msg, ch)));
            }
            if (url != null && !url.isEmpty()) {
                try {
                    tagBuilder.onClick(TextActions.openUrl(new URL(formatTags(tag, url, sender, receiver, msg, ch))));
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
            }
            if (!tag.equals("message") || UChat.get().getPerms().hasPerm(sender, "chat.click-urls")) {
                for (String arg : msg.split(" ")) {
                    try {
                        tagBuilder.onClick(TextActions.openUrl(new URL(formatTags(tag, arg, sender, receiver, msg, ch))));
                        tagBuilder.onHover(TextActions.showText(UCUtil.toText(formatTags(tag, UChat.get().getConfig().root().general.URL_template.replace("{url}", arg), sender, receiver, msg, ch))));
                    } catch (MalformedURLException ignored) {
                    }
                }
            }
            msgBuilder = tagBuilder;
            if (tag.equals("message") && (!msg.equals(mention(sender, receiver, msg)) || msg.contains(UChat.get().getConfig().root().general.item_hand.placeholder))) {
                tooltip = new StringBuilder(formatTags("", tooltip.toString(), sender, receiver, msg, ch));
                format = formatTags(tag, format, sender, receiver, msg, ch);
                lastColor = getLastColor(format);
                // append text
                msgBuilder.append(UCUtil.toText(format));
                if (UChat.get().getConfig().root().general.item_hand.enable && msg.contains(UChat.get().getConfig().root().general.item_hand.placeholder) && sender instanceof Player) {
                    ItemStack hand = ItemStack.of(ItemTypes.NONE, 1);
                    if (((Player) sender).getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
                        hand = ((Player) sender).getItemInHand(HandTypes.MAIN_HAND).get();
                    } else if (((Player) sender).getItemInHand(HandTypes.OFF_HAND).isPresent()) {
                        hand = ((Player) sender).getItemInHand(HandTypes.OFF_HAND).get();
                    }
                    msgBuilder.onHover(TextActions.showItem(hand.createSnapshot()));
                } else if (UChat.get().getConfig().root().mention.hover_message.length() > 0 && StringUtils.containsIgnoreCase(msg, ((CommandSource) receiver).getName())) {
                    tooltip = new StringBuilder(formatTags("", UChat.get().getConfig().root().mention.hover_message, sender, receiver, msg, ch));
                    msgBuilder.onHover(TextActions.showText(UCUtil.toText(tooltip.toString())));
                } else if (tooltip.length() > 0) {
                    msgBuilder.onHover(TextActions.showText(UCUtil.toText(tooltip.toString())));
                }
                msgBuilder.applyTo(message);
            } else {
                format = formatTags(tag, format, sender, receiver, msg, ch);
                tooltip = new StringBuilder(formatTags("", tooltip.toString(), sender, receiver, msg, ch));
                lastColor = getLastColor(format);
                if (tooltip.length() > 0) {
                    tagBuilder.append(UCUtil.toText(format)).onHover(TextActions.showText(UCUtil.toText(tooltip.toString())));
                } else {
                    tagBuilder.append(UCUtil.toText(format));
                }
                if (tag.equals("{playername}") || tag.equals("{nickname}")) {
                    tagBuilder.applyTo(playername);
                } else {
                    tagBuilder.applyTo(formatter);
                }
            }
        }
    } else {
        // if tell
        String prefix = UChat.get().getConfig().root().tell.prefix;
        String format = UChat.get().getConfig().root().tell.format;
        List<String> messages = UChat.get().getConfig().root().tell.hover_messages;
        StringBuilder tooltip = new StringBuilder();
        if (!messages.isEmpty() && messages.get(0).length() > 1) {
            for (String tp : messages) {
                tooltip.append("\n").append(tp);
            }
            if (tooltip.length() > 2) {
                tooltip = new StringBuilder(tooltip.substring(1));
            }
        }
        prefix = formatTags("", prefix, sender, receiver, msg, ch);
        format = formatTags("tell", format, sender, receiver, msg, ch);
        tooltip = new StringBuilder(formatTags("", tooltip.toString(), sender, receiver, msg, ch));
        if (tooltip.length() > 0) {
            formatter.append(UCUtil.toText(prefix)).onHover(TextActions.showText(UCUtil.toText(tooltip.toString())));
        } else {
            formatter.append(UCUtil.toText(prefix));
        }
        message.append(UCUtil.toText(format));
    }
    playername.applyTo(formatter);
    message.applyTo(formatter);
    return formatter.build();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) ClanPlayer(nl.riebie.mcclans.api.ClanPlayer) MalformedURLException(java.net.MalformedURLException) Builder(org.spongepowered.api.text.Text.Builder) ItemStack(org.spongepowered.api.item.inventory.ItemStack) URL(java.net.URL)

Example 8 with Builder

use of org.spongepowered.api.text.Text.Builder in project UltimateChat by FabioZumbi12.

the class UCUtil method sendBroadcast.

static boolean sendBroadcast(CommandSource src, String[] args, boolean silent) {
    StringBuilder message = new StringBuilder();
    StringBuilder hover = new StringBuilder();
    StringBuilder cmdline = new StringBuilder();
    StringBuilder url = new StringBuilder();
    boolean isHover = false;
    boolean isCmd = false;
    boolean isUrl = false;
    for (String arg : args) {
        if (arg.contains(UChat.get().getConfig().root().broadcast.on_hover)) {
            hover.append(" ").append(arg.replace(UChat.get().getConfig().root().broadcast.on_hover, ""));
            isHover = true;
            isCmd = false;
            isUrl = false;
            continue;
        }
        if (arg.contains(UChat.get().getConfig().root().broadcast.on_click)) {
            cmdline.append(" ").append(arg.replace(UChat.get().getConfig().root().broadcast.on_click, ""));
            isCmd = true;
            isHover = false;
            isUrl = false;
            continue;
        }
        if (arg.contains(UChat.get().getConfig().root().broadcast.url)) {
            url.append(" ").append(arg.replace(UChat.get().getConfig().root().broadcast.url, ""));
            isCmd = false;
            isHover = false;
            isUrl = true;
            continue;
        }
        if (isCmd) {
            cmdline.append(" ").append(arg);
        } else if (isHover) {
            hover.append(" ").append(arg);
        } else if (isUrl) {
            url.append(" ").append(arg);
        } else {
            message.append(" ").append(arg);
        }
    }
    if (message.toString().length() <= 1) {
        return false;
    }
    String finalMsg = UCMessages.formatTags("", message.toString().substring(1), src, src, message.toString().substring(1), new UCChannel("broadcast"));
    if (!silent) {
        Sponge.getServer().getConsole().sendMessage(UCUtil.toText("> Broadcast: &r" + finalMsg));
    }
    Builder fanci = Text.builder();
    fanci.append(UCUtil.toText(finalMsg));
    if (hover.toString().length() > 1) {
        fanci.onHover(TextActions.showText(UCUtil.toText(hover.toString().substring(1))));
        if (!silent) {
            Sponge.getServer().getConsole().sendMessage(UCUtil.toText("&8> OnHover: &r" + hover.toString().substring(1)));
        }
    }
    if (cmdline.toString().length() > 1 && !silent) {
        Sponge.getServer().getConsole().sendMessage(UCUtil.toText("&8> OnClick: &r" + cmdline.toString().substring(1)));
    }
    if (url.toString().length() > 1) {
        try {
            fanci.onClick(TextActions.openUrl(new URL(url.toString().substring(1))));
        } catch (MalformedURLException ignored) {
        }
        if (!silent) {
            Sponge.getServer().getConsole().sendMessage(UCUtil.toText("&8> Url: &r" + url.toString().substring(1)));
        }
    }
    for (Player p : Sponge.getServer().getOnlinePlayers()) {
        if (cmdline.toString().length() > 1) {
            fanci.onClick(TextActions.runCommand("/" + cmdline.toString().substring(1).replace("{clicked}", p.getName())));
        }
        p.sendMessage(fanci.build());
    }
    return true;
}
Also used : MalformedURLException(java.net.MalformedURLException) Player(org.spongepowered.api.entity.living.player.Player) Builder(org.spongepowered.api.text.Text.Builder) URL(java.net.URL)

Example 9 with Builder

use of org.spongepowered.api.text.Text.Builder in project modules-extra by CubeEngine.

the class ReportUtil method name.

public static Text name(BlockSnapshot snapshot, Receiver receiver) {
    BlockType type = snapshot.getState().getType();
    Translation trans = type.getTranslation();
    if (snapshot.getState().getType().getItem().isPresent()) {
        trans = ItemStack.builder().fromBlockSnapshot(snapshot).build().getTranslation();
    }
    Builder builder = Text.builder();
    builder.append(Text.of(GOLD, trans).toBuilder().onHover(showText(Text.of(type.getName()))).build());
    Optional<List<DataView>> items = snapshot.toContainer().getViewList(BlockReport.BLOCK_ITEMS);
    if (items.isPresent() && !items.get().isEmpty()) {
        // TODO lookup config : detailed inventory? click on ∋ to activate/deactivate or using cmd
        builder.append(Text.of(" ∋ ["));
        if (receiver.getLookup().getSettings().showDetailedInventory()) {
            builder.append(Text.of(" "));
            for (DataView dataView : items.get()) {
                DataContainer itemData = DataContainer.createNew();
                itemData.set(DataQuery.of("Count"), dataView.get(DataQuery.of("Count")).get());
                itemData.set(DataQuery.of("ItemType"), dataView.get(DataQuery.of("id")).get());
                Optional<DataView> tag = dataView.getView(DataQuery.of("tag"));
                if (tag.isPresent()) {
                    itemData.set(DataQuery.of("UnsafeData"), tag.get().getValues(false));
                }
                itemData.set(DataQuery.of("UnsafeDamage"), dataView.get(DataQuery.of("Damage")).get());
                ItemStack item = ItemStack.builder().fromContainer(itemData).build();
                builder.append(Text.of(dataView.getInt(DataQuery.of("Slot")).get()).toBuilder().onHover(showItem(item.createSnapshot())).build());
                builder.append(Text.of(" "));
            }
        } else {
            builder.append(Text.of("..."));
        }
        builder.append(Text.of("]"));
    }
    Optional<List<Text>> sign = snapshot.get(Keys.SIGN_LINES);
    if (sign.isPresent()) {
        builder.append(Text.of(" "), Text.of("[I]").toBuilder().onHover(showText(Text.joinWith(Text.NEW_LINE, sign.get()))).build());
    }
    return builder.build();
}
Also used : DataView(org.spongepowered.api.data.DataView) Translation(org.spongepowered.api.text.translation.Translation) DataContainer(org.spongepowered.api.data.DataContainer) BlockType(org.spongepowered.api.block.BlockType) Builder(org.spongepowered.api.text.Text.Builder) List(java.util.List) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 10 with Builder

use of org.spongepowered.api.text.Text.Builder in project UltimateChat by FabioZumbi12.

the class UCCommands method sendHelp.

private void sendHelp(CommandSource p) {
    Builder fancy = Text.builder();
    fancy.append(UCUtil.toText("\n&7--------------- " + UChat.get().getLang().get("_UChat.prefix") + " Help &7---------------\n"));
    fancy.append(UChat.get().getLang().getText("help.channels.enter", "\n"));
    fancy.append(UChat.get().getLang().getText("help.channels.send", "\n"));
    fancy.append(UChat.get().getLang().getText("help.channels.list", "\n"));
    if (UChat.get().getPerms().cmdPerm(p, "uchat.cmd.tell")) {
        fancy.append(UChat.get().getLang().getText("help.tell.lock", "\n"));
        fancy.append(UChat.get().getLang().getText("help.tell.send", "\n"));
        fancy.append(UChat.get().getLang().getText("help.tell.respond", "\n"));
    }
    if (UChat.get().getPerms().cmdPerm(p, "uchat.cmd.broadcast")) {
        fancy.append(UChat.get().getLang().getText("help.cmd.broadcast", "\n"));
    }
    if (UChat.get().getPerms().cmdPerm(p, "uchat.cmd.umsg")) {
        fancy.append(UChat.get().getLang().getText("help.cmd.umsg", "\n"));
    }
    if (UChat.get().getPerms().cmdPerm(p, "uchat.cmd.clear")) {
        fancy.append(UChat.get().getLang().getText("help.cmd.clear", "\n"));
    }
    if (UChat.get().getPerms().cmdPerm(p, "uchat.cmd.clear-all")) {
        fancy.append(UChat.get().getLang().getText("help.cmd.clear-all", "\n"));
    }
    if (UChat.get().getPerms().cmdPerm(p, "uchat.cmd.spy")) {
        fancy.append(UChat.get().getLang().getText("help.cmd.spy", "\n"));
    }
    if (UChat.get().getPerms().cmdPerm(p, "uchat.cmd.mute")) {
        fancy.append(UChat.get().getLang().getText("help.cmd.mute", "\n"));
    }
    if (UChat.get().getPerms().cmdPerm(p, "uchat.cmd.tempmute")) {
        fancy.append(UChat.get().getLang().getText("help.cmd.tempmute", "\n"));
    }
    if (UChat.get().getPerms().cmdPerm(p, "uchat.cmd.ignore.player")) {
        fancy.append(UChat.get().getLang().getText("help.cmd.ignore.player", "\n"));
    }
    if (UChat.get().getPerms().cmdPerm(p, "uchat.cmd.ignore.channel")) {
        fancy.append(UChat.get().getLang().getText("help.cmd.ignore.channel", "\n"));
    }
    if (UChat.get().getPerms().cmdPerm(p, "uchat.cmd.chconfig")) {
        fancy.append(UChat.get().getLang().getText("help.cmd.chconfig", "\n"));
    }
    if (UChat.get().getPerms().cmdPerm(p, "uchat.cmd.reload")) {
        fancy.append(UChat.get().getLang().getText("help.cmd.reload", "\n"));
    }
    if (UChat.get().getPerms().cmdPerm(p, "newchannel")) {
        fancy.append(UChat.get().getLang().getText("help.cmd.newchannel", "\n"));
    }
    if (UChat.get().getPerms().cmdPerm(p, "delchannel")) {
        fancy.append(UChat.get().getLang().getText("help.cmd.delchannel", "\n"));
    }
    getHelpChannel(p).applyTo(fancy);
    p.sendMessage(fancy.build());
}
Also used : Builder(org.spongepowered.api.text.Text.Builder)

Aggregations

Builder (org.spongepowered.api.text.Text.Builder)11 Player (org.spongepowered.api.entity.living.player.Player)6 MalformedURLException (java.net.MalformedURLException)4 URL (java.net.URL)4 ItemStack (org.spongepowered.api.item.inventory.ItemStack)2 World (org.spongepowered.api.world.World)2 DefineRegionBuilder (br.net.fabiozumbi12.RedProtect.Sponge.actions.DefineRegionBuilder)1 RedefineRegionBuilder (br.net.fabiozumbi12.RedProtect.Sponge.actions.RedefineRegionBuilder)1 UCChannel (br.net.fabiozumbi12.UltimateChat.Sponge.UCChannel)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 JDABuilder (jdalib.jda.core.JDABuilder)1 ClanPlayer (nl.riebie.mcclans.api.ClanPlayer)1 Command (org.cubeengine.butler.parametric.Command)1 BlockType (org.spongepowered.api.block.BlockType)1 DataContainer (org.spongepowered.api.data.DataContainer)1 DataView (org.spongepowered.api.data.DataView)1 PaginationList (org.spongepowered.api.service.pagination.PaginationList)1 Text (org.spongepowered.api.text.Text)1 TextColor (org.spongepowered.api.text.format.TextColor)1