Search in sources :

Example 26 with ICPlaceholder

use of com.loohp.interactivechat.objectholders.ICPlaceholder in project InteractiveChat by LOOHP.

the class InteractiveChatBungee method onBungeeChat.

@EventHandler(priority = EventPriority.HIGH)
public void onBungeeChat(ChatEvent event) {
    if (event.isCancelled()) {
        return;
    }
    ProxiedPlayer player = (ProxiedPlayer) event.getSender();
    UUID uuid = player.getUniqueId();
    String message = event.getMessage();
    String newMessage = event.getMessage();
    boolean hasInteractiveChat = false;
    Server server = player.getServer();
    if (server != null) {
        BackendInteractiveChatData data = serverInteractiveChatInfo.get(server.getInfo().getName());
        if (data != null) {
            hasInteractiveChat = data.hasInteractiveChat();
        }
    }
    boolean usage = false;
    outer: for (List<ICPlaceholder> serverPlaceholders : placeholderList.values()) {
        for (ICPlaceholder icplaceholder : serverPlaceholders) {
            Matcher matcher = icplaceholder.getKeyword().matcher(message);
            if (matcher.find()) {
                int start = matcher.start();
                if ((start < 1 || message.charAt(start - 1) != '\\') || (start > 1 && message.charAt(start - 1) == '\\' && message.charAt(start - 2) == '\\')) {
                    usage = true;
                    break outer;
                }
            }
        }
    }
    if (newMessage.startsWith("/")) {
        if (usage && hasInteractiveChat) {
            for (String parsecommand : InteractiveChatBungee.parseCommands) {
                if (newMessage.matches(parsecommand)) {
                    String command = newMessage.trim();
                    outer: for (List<ICPlaceholder> serverPlaceholders : placeholderList.values()) {
                        for (ICPlaceholder icplaceholder : serverPlaceholders) {
                            Pattern placeholder = icplaceholder.getKeyword();
                            Matcher matcher = placeholder.matcher(command);
                            if (matcher.find()) {
                                int start = matcher.start();
                                if ((start < 1 || command.charAt(start - 1) != '\\') || (start > 1 && command.charAt(start - 1) == '\\' && command.charAt(start - 2) == '\\')) {
                                    String uuidmatch = "<cmd=" + uuid + ":" + Registry.ID_ESCAPE_PATTERN.matcher(command.substring(matcher.start(), matcher.end())).replaceAll("\\>") + ":>";
                                    command = command.substring(0, matcher.start()) + uuidmatch + command.substring(matcher.end());
                                    if (command.length() > 256) {
                                        command = command.substring(0, 256);
                                    }
                                    event.setMessage(command);
                                    break outer;
                                }
                            }
                        }
                    }
                    break;
                }
            }
        }
    } else {
        if (usage && InteractiveChatBungee.useAccurateSenderFinder && hasInteractiveChat) {
            outer: for (List<ICPlaceholder> serverPlaceholders : placeholderList.values()) {
                for (ICPlaceholder icplaceholder : serverPlaceholders) {
                    Pattern placeholder = icplaceholder.getKeyword();
                    Matcher matcher = placeholder.matcher(message);
                    if (matcher.find()) {
                        int start = matcher.start();
                        if ((start < 1 || message.charAt(start - 1) != '\\') || (start > 1 && message.charAt(start - 1) == '\\' && message.charAt(start - 2) == '\\')) {
                            String uuidmatch = "<chat=" + uuid + ":" + Registry.ID_ESCAPE_PATTERN.matcher(message.substring(matcher.start(), matcher.end())).replaceAll("\\>") + ":>";
                            message = message.substring(0, matcher.start()) + uuidmatch + message.substring(matcher.end());
                            if (message.length() > 256) {
                                message = message.substring(0, 256);
                            }
                            event.setMessage(message);
                            break outer;
                        }
                    }
                }
            }
        }
        ProxyServer.getInstance().getScheduler().schedule(plugin, () -> {
            Map<String, Long> messages = forwardedMessages.get(uuid);
            if (messages != null && messages.remove(newMessage) != null) {
                try {
                    PluginMessageSendingBungee.sendMessagePair(uuid, newMessage);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }, 100, TimeUnit.MILLISECONDS);
    }
}
Also used : ProxiedPlayer(net.md_5.bungee.api.connection.ProxiedPlayer) Pattern(java.util.regex.Pattern) Server(net.md_5.bungee.api.connection.Server) ProxyServer(net.md_5.bungee.api.ProxyServer) Matcher(java.util.regex.Matcher) BackendInteractiveChatData(com.loohp.interactivechat.proxy.objectholders.BackendInteractiveChatData) IOException(java.io.IOException) ICPlaceholder(com.loohp.interactivechat.objectholders.ICPlaceholder) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) ArrayList(java.util.ArrayList) UUID(java.util.UUID) EventHandler(net.md_5.bungee.event.EventHandler)

Aggregations

ICPlaceholder (com.loohp.interactivechat.objectholders.ICPlaceholder)26 UUID (java.util.UUID)15 Pattern (java.util.regex.Pattern)13 IOException (java.io.IOException)12 Matcher (java.util.regex.Matcher)12 CustomPlaceholder (com.loohp.interactivechat.objectholders.CustomPlaceholder)11 ArrayList (java.util.ArrayList)11 List (java.util.List)10 ICPlayer (com.loohp.interactivechat.objectholders.ICPlayer)9 ParsePlayer (com.loohp.interactivechat.objectholders.CustomPlaceholder.ParsePlayer)8 Component (net.kyori.adventure.text.Component)8 Player (org.bukkit.entity.Player)8 InteractiveChat (com.loohp.interactivechat.InteractiveChat)7 ItemStack (org.bukkit.inventory.ItemStack)7 CustomPlaceholderClickEvent (com.loohp.interactivechat.objectholders.CustomPlaceholder.CustomPlaceholderClickEvent)6 CustomPlaceholderHoverEvent (com.loohp.interactivechat.objectholders.CustomPlaceholder.CustomPlaceholderHoverEvent)6 CustomPlaceholderReplaceText (com.loohp.interactivechat.objectholders.CustomPlaceholder.CustomPlaceholderReplaceText)6 BackendInteractiveChatData (com.loohp.interactivechat.proxy.objectholders.BackendInteractiveChatData)6 ChatColorUtils (com.loohp.interactivechat.utils.ChatColorUtils)6 Field (java.lang.reflect.Field)6