Search in sources :

Example 1 with StaticQuoteConsumer

use of com.easterlyn.util.text.StaticQuoteConsumer in project Easterlyn by Easterlyn.

the class EasterlynChat method register.

@Override
protected void register(EasterlynCore plugin) {
    StringUtil.addQuoteConsumer(new StaticQuoteConsumer(CHANNEL_PATTERN) {

        @Override
        public void addComponents(@NotNull ParsedText components, @NotNull Supplier<Matcher> matcherSupplier) {
            Matcher matcher = matcherSupplier.get();
            String channelName = matcher.group(1);
            Channel channel = getChannels().get(channelName.toLowerCase().substring(1));
            TextComponent component;
            if (channel != null) {
                component = channel.getMention();
            } else {
                int end = matcher.end(1);
                component = new TextComponent(matcher.group().substring(0, end));
                component.setColor(Colors.CHANNEL);
                component.setUnderlined(true);
                component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(TextComponent.fromLegacyText(Colors.COMMAND + "/join " + Colors.CHANNEL + channelName))));
                component.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/join " + channelName));
            }
            components.addComponent(component);
            String trailingPunctuation = matcher.group(2);
            if (trailingPunctuation != null && !trailingPunctuation.isEmpty()) {
                components.addText(trailingPunctuation, component.getHoverEvent(), component.getClickEvent());
            }
        }
    });
    IssuerAwareContextResolver<Channel, BukkitCommandExecutionContext> channelContext = context -> {
        String firstArg = context.getFirstArg();
        // Current channel or unspecified, defaulting to current
        if (context.hasFlag(ChannelFlag.CURRENT) || (context.hasFlag(ChannelFlag.LISTENING_OR_CURRENT) || context.hasFlag(ChannelFlag.VISIBLE_OR_CURRENT)) && (firstArg == null || firstArg.indexOf('#') != 0)) {
            if (!context.getIssuer().isPlayer()) {
                // Console never has a current channel
                throw new InvalidCommandArgument(CoreLang.NO_CONSOLE);
            }
            String channelName = plugin.getUserManager().getUser(context.getPlayer().getUniqueId()).getStorage().getString(USER_CURRENT);
            Channel channel = channels.get(channelName);
            if (channel == null) {
                throw new InvalidCommandArgument(MessageKey.of("chat.common.no_current_channel"));
            }
            return channel;
        }
        // Channel name must be specified for anything beyond this point, pop argument
        context.popFirstArg();
        if (firstArg == null) {
            throw new InvalidCommandArgument(MessageKey.of("chat.common.no_specified_channel"));
        }
        if (firstArg.length() == 0 || firstArg.charAt(0) != '#') {
            throw new InvalidCommandArgument(MessageKey.of("chat.commands.channel.create.error.naming_conventions"));
        }
        Channel channel = channels.get(firstArg.substring(1).toLowerCase());
        if (channel == null) {
            throw new InvalidCommandArgument(MessageKey.of("chat.common.no_matching_channel"), "{value}", firstArg);
        }
        User user;
        if (context.getIssuer().isPlayer()) {
            user = getCore().getUserManager().getUser(context.getIssuer().getUniqueId());
        } else {
            user = null;
        }
        if (context.hasFlag(ChannelFlag.VISIBLE) || context.hasFlag(ChannelFlag.VISIBLE_OR_CURRENT)) {
            if (user == null || !channel.isPrivate() || channel.isWhitelisted(user)) {
                return channel;
            }
            throw new InvalidCommandArgument(MessageKey.of("chat.common.no_channel_access"), "{value}", channel.getDisplayName());
        }
        if (context.hasFlag(ChannelFlag.LISTENING) || context.hasFlag(ChannelFlag.LISTENING_OR_CURRENT)) {
            if (user == null) {
                return channel;
            }
            List<String> channels = user.getStorage().getStringList(EasterlynChat.USER_CHANNELS);
            if (!channels.contains(channel.getName())) {
                throw new InvalidCommandArgument(MessageKey.of("chat.common.not_listening_to_channel"), "{value}", channel.getDisplayName());
            }
            return channel;
        }
        if (user == null) {
            throw new InvalidCommandArgument(CoreLang.NO_CONSOLE);
        }
        if (context.hasFlag(ChannelFlag.NOT_LISTENING)) {
            List<String> channels = user.getStorage().getStringList(EasterlynChat.USER_CHANNELS);
            if (channels.contains(channel.getName())) {
                throw new InvalidCommandArgument(MessageKey.of("chat.common.listening_to_channel"), "{value}", channel.getDisplayName());
            }
            return channel;
        }
        ReportableEvent.call("Missing Channel context flag!", 10);
        throw new InvalidCommandArgument(CoreLang.ERROR_LOGGED);
    };
    plugin.getCommandManager().getCommandContexts().registerIssuerAwareContext(Channel.class, channelContext);
    plugin.getCommandManager().getCommandContexts().registerIssuerAwareContext(NormalChannel.class, context -> {
        Channel channel = channelContext.getContext(context);
        if (!(channel instanceof NormalChannel)) {
            throw new InvalidCommandArgument(MessageKey.of("chat.common.channel_not_modifiable"));
        }
        return (NormalChannel) channel;
    });
    // TODO ACF is removing leading # from channel display names in completions
    plugin.getCommandManager().getCommandCompletions().registerCompletion("channels", getUserHandler(user -> channels.values().stream().distinct().filter(channel -> channel.isWhitelisted(user)).map(Channel::getDisplayName).collect(Collectors.toSet())));
    plugin.getCommandManager().getCommandCompletions().setDefaultCompletion("channels", Channel.class, NormalChannel.class);
    plugin.getCommandManager().getCommandCompletions().registerCompletion("channelsJoinable", getUserHandler(user -> {
        List<String> channelsJoined = user.getStorage().getStringList(EasterlynChat.USER_CHANNELS);
        return channels.values().stream().distinct().filter(channel -> !channelsJoined.contains(channel.getName()) && channel.isWhitelisted(user)).map(Channel::getDisplayName).collect(Collectors.toSet());
    }));
    plugin.getCommandManager().getCommandCompletions().registerCompletion("channelsListening", getUserHandler(user -> user.getStorage().getStringList(EasterlynChat.USER_CHANNELS)));
    plugin.getCommandManager().getCommandCompletions().registerCompletion("channelsModerated", getUserHandler(user -> channels.values().stream().distinct().filter(channel -> channel.isModerator(user)).map(Channel::getDisplayName).collect(Collectors.toSet())));
    plugin.getCommandManager().getCommandCompletions().registerCompletion("channelsOwned", getUserHandler(user -> channels.values().stream().distinct().filter(channel -> channel.isOwner(user)).map(Channel::getDisplayName).collect(Collectors.toSet())));
    plugin.getLocaleManager().addLocaleSupplier(this);
    plugin.registerCommands(this, getClassLoader(), "com.easterlyn.chat.command");
}
Also used : TextComponent(net.md_5.bungee.api.chat.TextComponent) EasterlynPlugin(com.easterlyn.plugin.EasterlynPlugin) MessageKey(co.aikar.locales.MessageKey) Text(net.md_5.bungee.api.chat.hover.content.Text) InvalidCommandArgument(co.aikar.commands.InvalidCommandArgument) ClickEvent(net.md_5.bungee.api.chat.ClickEvent) NormalChannel(com.easterlyn.chat.channel.NormalChannel) ConfigurationSection(org.bukkit.configuration.ConfigurationSection) PermissionUtil(com.easterlyn.util.PermissionUtil) Constructor(java.lang.reflect.Constructor) Function(java.util.function.Function) Supplier(java.util.function.Supplier) BukkitCommandExecutionContext(co.aikar.commands.BukkitCommandExecutionContext) HashSet(java.util.HashSet) TextComponent(net.md_5.bungee.api.chat.TextComponent) IssuerAwareContextResolver(co.aikar.commands.contexts.IssuerAwareContextResolver) FileConfiguration(org.bukkit.configuration.file.FileConfiguration) Matcher(java.util.regex.Matcher) ChannelFlag(com.easterlyn.chat.command.ChannelFlag) BukkitCommandCompletionContext(co.aikar.commands.BukkitCommandCompletionContext) StringUtil(com.easterlyn.util.StringUtil) StaticQuoteConsumer(com.easterlyn.util.text.StaticQuoteConsumer) Map(java.util.Map) ParsedText(com.easterlyn.util.text.ParsedText) HoverEvent(net.md_5.bungee.api.chat.HoverEvent) User(com.easterlyn.user.User) MuteListener(com.easterlyn.chat.listener.MuteListener) Collection(java.util.Collection) PermissionDefault(org.bukkit.permissions.PermissionDefault) Channel(com.easterlyn.chat.channel.Channel) ReportableEvent(com.easterlyn.event.ReportableEvent) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) UUID(java.util.UUID) SecretChannel(com.easterlyn.chat.channel.SecretChannel) Collectors(java.util.stream.Collectors) ChannelManagementListener(com.easterlyn.chat.listener.ChannelManagementListener) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) UserRank(com.easterlyn.user.UserRank) ChatColor(org.bukkit.ChatColor) Pattern(java.util.regex.Pattern) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) RegisteredServiceProvider(org.bukkit.plugin.RegisteredServiceProvider) CommandCompletions(co.aikar.commands.CommandCompletions) CoreLang(com.easterlyn.command.CoreLang) Colors(com.easterlyn.util.Colors) HoverEvent(net.md_5.bungee.api.chat.HoverEvent) User(com.easterlyn.user.User) Matcher(java.util.regex.Matcher) NormalChannel(com.easterlyn.chat.channel.NormalChannel) Channel(com.easterlyn.chat.channel.Channel) SecretChannel(com.easterlyn.chat.channel.SecretChannel) ClickEvent(net.md_5.bungee.api.chat.ClickEvent) ParsedText(com.easterlyn.util.text.ParsedText) Text(net.md_5.bungee.api.chat.hover.content.Text) ParsedText(com.easterlyn.util.text.ParsedText) StaticQuoteConsumer(com.easterlyn.util.text.StaticQuoteConsumer) InvalidCommandArgument(co.aikar.commands.InvalidCommandArgument) List(java.util.List) NormalChannel(com.easterlyn.chat.channel.NormalChannel) BukkitCommandExecutionContext(co.aikar.commands.BukkitCommandExecutionContext)

Example 2 with StaticQuoteConsumer

use of com.easterlyn.util.text.StaticQuoteConsumer in project Easterlyn by Easterlyn.

the class UserChatEvent method send.

public void send(Collection<UUID> additionalRecipients) {
    Bukkit.getPluginManager().callEvent(this);
    if (this.isCancelled()) {
        return;
    }
    TextComponent channelElement = new TextComponent();
    TextComponent[] channelHover = new TextComponent[] { new TextComponent("/join "), new TextComponent(channel.getDisplayName()) };
    channelHover[0].setColor(Colors.COMMAND);
    channelHover[1].setColor(Colors.CHANNEL);
    channelElement.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(channelHover)));
    channelElement.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/join " + channel.getDisplayName()));
    TextComponent channelName = new TextComponent(channel.getDisplayName());
    channelName.setColor((channel.isOwner(getUser()) ? Colors.CHANNEL_OWNER : channel.isModerator(getUser()) ? Colors.CHANNEL_MODERATOR : Colors.CHANNEL_MEMBER));
    TextComponent nameElement = new TextComponent(thirdPerson ? "> " : " <");
    TextComponent userElement = getUser().getMention();
    nameElement.setHoverEvent(userElement.getHoverEvent());
    nameElement.setClickEvent(userElement.getClickEvent());
    TextComponent nameText = new TextComponent(userElement.getText().substring(1));
    nameText.setColor(userElement.getColor());
    nameElement.addExtra(nameText);
    nameElement.addExtra(new TextComponent(thirdPerson ? " " : "> "));
    Collection<TextComponent> messageComponents;
    Player player = getUser().getPlayer();
    if (player == null) {
        messageComponents = StringUtil.toJSON(message);
    } else {
        messageComponents = StringUtil.toJSON(message, Collections.singleton(new StaticQuoteConsumer(PLAYER_ITEMS) {

            @Override
            public void addComponents(@NotNull ParsedText components, @NotNull Supplier<Matcher> matcherSupplier) {
                Matcher matcher = matcherSupplier.get();
                int slot;
                try {
                    slot = Integer.parseInt(matcher.group(1));
                } catch (NumberFormatException e) {
                    components.addText(matcher.group());
                    return;
                }
                if (slot < 0 || slot >= player.getInventory().getSize()) {
                    components.addText(matcher.group());
                    return;
                }
                ItemStack item = player.getInventory().getItem(slot);
                if (item == null) {
                    item = ItemUtil.AIR;
                }
                components.addComponent(ItemUtil.getItemComponent(item));
            }
        }));
    }
    Stream.concat(additionalRecipients.stream(), channel.getMembers().stream()).distinct().map(uuid -> getUser().getPlugin().getUserManager().getUser(uuid)).forEach(user -> {
        boolean highlight = false;
        // Copy and convert TextComponents from parsed message
        List<TextComponent> highlightedComponents = new LinkedList<>();
        for (TextComponent textComponent : messageComponents) {
            String text = textComponent.getText();
            Matcher matcher = getHighlightPattern(user).matcher(text);
            int previousMatch = 0;
            while (matcher.find()) {
                highlight = true;
                if (matcher.start() > 0) {
                    TextComponent start = new TextComponent(textComponent);
                    start.setText(text.substring(previousMatch, matcher.start()));
                    highlightedComponents.add(start);
                }
                TextComponent mention = user.getMention();
                mention.setColor(Colors.HIGHLIGHT);
                highlightedComponents.add(mention);
                // Set previous match to end of group 1 so next will pick up group 2 if it exists.
                previousMatch = matcher.end(1);
            }
            if (previousMatch == 0) {
                // No matches, no need to modify component for user.
                highlightedComponents.add(textComponent);
                continue;
            }
            if (previousMatch == text.length()) {
                // Last match coincided with the end of the text.
                continue;
            }
            TextComponent end = new TextComponent(textComponent);
            end.setText(text.substring(previousMatch));
            highlightedComponents.add(end);
        }
        TextComponent finalMessage = new TextComponent();
        // Set text a nice relaxing grey if not focused or explicitly set
        finalMessage.setColor(channel.getName().equals("pm") || channel.getName().equals(user.getStorage().getString(EasterlynChat.USER_CURRENT)) ? ChatColor.WHITE : ChatColor.GRAY);
        TextComponent channelBrace;
        if (highlight) {
            channelBrace = new TextComponent("!!");
            channelBrace.setColor(Colors.HIGHLIGHT);
        } else {
            channelBrace = new TextComponent("[");
        }
        BaseComponent finalChannel = channelElement.duplicate();
        finalChannel.addExtra(channelBrace);
        finalChannel.addExtra(channelName);
        if (!highlight) {
            channelBrace = new TextComponent("]");
        }
        finalChannel.addExtra(channelBrace);
        finalMessage.addExtra(finalChannel);
        finalMessage.addExtra(nameElement);
        for (TextComponent component : highlightedComponents) {
            finalMessage.addExtra(component);
        }
        user.sendMessage(getUser() instanceof AutoUser ? null : getUser().getUniqueId(), finalMessage);
    });
    Bukkit.getConsoleSender().sendMessage(ChatColor.stripColor(String.format("[%1$s]" + (thirdPerson ? "> %2$s " : " <%2$s> ") + "%3$s", channel.getDisplayName(), getUser().getDisplayName(), message)));
}
Also used : TextComponent(net.md_5.bungee.api.chat.TextComponent) HandlerList(org.bukkit.event.HandlerList) Cancellable(org.bukkit.event.Cancellable) Text(net.md_5.bungee.api.chat.hover.content.Text) ClickEvent(net.md_5.bungee.api.chat.ClickEvent) Player(org.bukkit.entity.Player) Supplier(java.util.function.Supplier) TextComponent(net.md_5.bungee.api.chat.TextComponent) AutoUser(com.easterlyn.user.AutoUser) BaseComponent(net.md_5.bungee.api.chat.BaseComponent) Matcher(java.util.regex.Matcher) StringUtil(com.easterlyn.util.StringUtil) StaticQuoteConsumer(com.easterlyn.util.text.StaticQuoteConsumer) ParsedText(com.easterlyn.util.text.ParsedText) LinkedList(java.util.LinkedList) ChatColor(net.md_5.bungee.api.ChatColor) Bukkit(org.bukkit.Bukkit) HoverEvent(net.md_5.bungee.api.chat.HoverEvent) User(com.easterlyn.user.User) ItemUtil(com.easterlyn.util.inventory.ItemUtil) Collection(java.util.Collection) Channel(com.easterlyn.chat.channel.Channel) UUID(java.util.UUID) UserEvent(com.easterlyn.event.UserEvent) ItemStack(org.bukkit.inventory.ItemStack) List(java.util.List) Stream(java.util.stream.Stream) Pattern(java.util.regex.Pattern) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) EasterlynChat(com.easterlyn.EasterlynChat) Colors(com.easterlyn.util.Colors) HoverEvent(net.md_5.bungee.api.chat.HoverEvent) Player(org.bukkit.entity.Player) BaseComponent(net.md_5.bungee.api.chat.BaseComponent) Matcher(java.util.regex.Matcher) AutoUser(com.easterlyn.user.AutoUser) ClickEvent(net.md_5.bungee.api.chat.ClickEvent) ParsedText(com.easterlyn.util.text.ParsedText) Text(net.md_5.bungee.api.chat.hover.content.Text) ParsedText(com.easterlyn.util.text.ParsedText) StaticQuoteConsumer(com.easterlyn.util.text.StaticQuoteConsumer) LinkedList(java.util.LinkedList) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

Channel (com.easterlyn.chat.channel.Channel)2 User (com.easterlyn.user.User)2 Colors (com.easterlyn.util.Colors)2 StringUtil (com.easterlyn.util.StringUtil)2 ParsedText (com.easterlyn.util.text.ParsedText)2 StaticQuoteConsumer (com.easterlyn.util.text.StaticQuoteConsumer)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 List (java.util.List)2 UUID (java.util.UUID)2 Supplier (java.util.function.Supplier)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 ClickEvent (net.md_5.bungee.api.chat.ClickEvent)2 HoverEvent (net.md_5.bungee.api.chat.HoverEvent)2 TextComponent (net.md_5.bungee.api.chat.TextComponent)2 Text (net.md_5.bungee.api.chat.hover.content.Text)2 NotNull (org.jetbrains.annotations.NotNull)2 BukkitCommandCompletionContext (co.aikar.commands.BukkitCommandCompletionContext)1 BukkitCommandExecutionContext (co.aikar.commands.BukkitCommandExecutionContext)1