aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/Danker/features/ChatAliases.java
blob: ae9e0f78fef9902f8572d4290461fb44c141b5a4 (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
package me.Danker.features;

import com.google.gson.GsonBuilder;
import me.Danker.events.PacketWriteEvent;
import net.minecraft.client.Minecraft;
import net.minecraft.network.play.client.C01PacketChatMessage;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ChatAliases {

    public static List<Alias> aliases = new ArrayList<>();
    public static String configFile;

    @SubscribeEvent
    public void onPacketWrite(PacketWriteEvent event) {
        if (event.packet instanceof C01PacketChatMessage) {
            C01PacketChatMessage packet = (C01PacketChatMessage) event.packet;
            String message = packet.getMessage();

            for (Alias alias : aliases) {
                if (!alias.toggled) continue;
                message = message.replace(alias.text, alias.alias);
            }

            if (!packet.getMessage().equals(message)) {
                event.setCanceled(true);
                Minecraft.getMinecraft().thePlayer.sendQueue.addToSendQueue(new C01PacketChatMessage(message));
            }
        }
    }

    public static void save() {
        try (FileWriter writer = new FileWriter(configFile)) {
            new GsonBuilder().create().toJson(aliases, writer);
            writer.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static class Alias {

        public String text;
        public String alias;
        public boolean toggled;

        public Alias(String text, String alias, boolean toggled) {
            this.text = text;
            this.alias = alias;
            this.toggled = toggled;
        }

        public void toggle() {
            toggled = !toggled;
        }

    }

}