aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/kr/syeyoung/dungeonsguide/chat/ChatProcessor.java
blob: 5fc666e21d056ff62355029d7692ce3b9bd63c81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Dungeons Guide - The most intelligent Hypixel Skyblock Dungeons Mod
 * Copyright (C) 2021  cyoung06
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package kr.syeyoung.dungeonsguide.chat;

import kr.syeyoung.dungeonsguide.DungeonsGuide;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiNewChat;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.Tuple;
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import net.minecraftforge.fml.common.eventhandler.Event;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.simple.SimpleLogger;

import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArrayList;

public class ChatProcessor {
    public static final ChatProcessor INSTANCE = new ChatProcessor();

    private static final Logger logger = LogManager.getLogger();
    private ChatProcessor() {
        Logger l = LogManager.getLogger(GuiNewChat.class);
        if (l instanceof SimpleLogger) {
            ((SimpleLogger) l).setLevel(Level.OFF);
        } else if (l instanceof org.apache.logging.log4j.core.Logger) {
            ((org.apache.logging.log4j.core.Logger) l).setLevel(Level.OFF);
        }
    }

    private Queue<ChatSubscriber> chatSubscriberQueue = new ConcurrentLinkedQueue<>();
    private Queue<Tuple<String, Runnable>> chatQueue = new ConcurrentLinkedQueue<>();

    public void subscribe(ChatSubscriber chatSubscribed) {
        chatSubscriberQueue.add(chatSubscribed);
    }
    public void addToChatQueue(String chat, Runnable onSend, boolean noDupe) {
        if (noDupe && chatQueue.stream().anyMatch(a -> a.getFirst().trim().equalsIgnoreCase(chat.trim()))) return;
        chatQueue.add(new Tuple<>(chat, onSend));
    }


    private long minimumNext = 0;

    @SubscribeEvent
    public void onTick(TickEvent.ClientTickEvent clientTickEvent) {
        try {
            if (clientTickEvent.phase == TickEvent.Phase.START && Minecraft.getMinecraft().thePlayer != null && minimumNext < System.currentTimeMillis()) {
                if (!chatQueue.isEmpty()) {
                    Tuple<String, Runnable> tuple = chatQueue.poll();
                    Minecraft.getMinecraft().thePlayer.sendChatMessage(tuple.getFirst());
                    if (tuple.getSecond() != null)
                        tuple.getSecond().run();
                    minimumNext = System.currentTimeMillis() + 200;
                    DungeonsGuide.sendDebugChat(new ChatComponentText("Sending " + tuple.getFirst() + " Secretly"));
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }


    @SubscribeEvent(priority = EventPriority.HIGHEST, receiveCanceled = true)
    public void onMessage(ClientChatReceivedEvent chatReceivedEvent) {
        if (chatReceivedEvent.type == 2) return;
        String txt = chatReceivedEvent.message.getFormattedText();
        logger.log(Level.INFO, "[CHAT] "+txt);

        int processed = 0;
        int listenened = 0;
        Map<String, Object> context = new HashMap<>();
        Iterator<ChatSubscriber> it = chatSubscriberQueue.iterator();
        while (it.hasNext()) {
            ChatSubscriber chatSubscribed = it.next();
            context.put("removed", processed);
            context.put("onceListenered", listenened);
            ChatProcessResult chatProcessResult = chatSubscribed.process(txt, context);
            if (chatProcessResult.isRemoveChat()) processed++;
            if (chatProcessResult.isRemoveListener()) listenened++;

            if (chatProcessResult.isRemoveChat()) chatReceivedEvent.setResult(Event.Result.DENY);
            if (chatProcessResult.isRemoveListener()) it.remove();
        }
    }


    @SubscribeEvent(priority = EventPriority.LOWEST)
    public void cancelMessage(ClientChatReceivedEvent chatReceivedEvent) {
        if (chatReceivedEvent.getResult() == Event.Result.DENY)
            chatReceivedEvent.setCanceled(true);
    }
}