aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/torui/coflsky/CoflSky.java
blob: 83896764aea7c85ef73a4bf4902281a3fbd881d7 (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
116
117
118
119
120
121
122
123
124
package de.torui.coflsky;


import java.io.File;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;

import com.google.gson.Gson;
import de.torui.coflsky.configuration.LocalConfig;
import de.torui.coflsky.gui.GUIType;
import de.torui.coflsky.handlers.EventRegistry;
import de.torui.coflsky.listeners.ChatListener;
import de.torui.coflsky.proxy.APIKeyManager;
import de.torui.coflsky.gui.tfm.ButtonRemapper;
import de.torui.coflsky.gui.tfm.ChatMessageSendHandler;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import org.lwjgl.input.Keyboard;
import de.torui.coflsky.network.WSClientWrapper;
import net.minecraft.client.settings.KeyBinding;
import net.minecraftforge.client.ClientCommandHandler;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.client.registry.ClientRegistry;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.relauncher.Side;

@Mod(modid = CoflSky.MODID, version = CoflSky.VERSION)
public class CoflSky {
    public static final String MODID = "CoflSky";
    public static final String VERSION = "1.5.2-Alpha";

    public static WSClientWrapper Wrapper;
    public static KeyBinding[] keyBindings;

    public static EventRegistry Events;
    public static File configFile;
    private File coflDir;
    public static LocalConfig config;

    public static final String[] webSocketURIPrefix = new String[]{
        "wss://sky.coflnet.com/modsocket",
        // fallback for old java versions not supporting new tls certificates
        "ws://sky-mod.coflnet.com/modsocket",
    };

    public static String CommandUri = Config.BaseUrl + "/api/mod/commands";
    private final static APIKeyManager apiKeyManager = new APIKeyManager();


    @EventHandler
    public void preInit(FMLPreInitializationEvent event) {
        String configString = null;
        Gson gson = new Gson();
        coflDir = new File(event.getModConfigurationDirectory(), "CoflSky");
        coflDir.mkdirs();
        configFile = new File(coflDir, "config.json");
        try {
            if (configFile.isFile()) {
                configString = new String(Files.readAllBytes(Paths.get(configFile.getPath())));
                config = gson.fromJson(configString, LocalConfig.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (config == null) {
            config = LocalConfig.createDefaultConfig();
        }

        try {
            this.apiKeyManager.loadIfExists();
        } catch (Exception exception) {
            exception.printStackTrace();
        }

        MinecraftForge.EVENT_BUS.register(new ChatListener());

        // Cache all the mods on load
        WSCommandHandler.cacheMods();
    }

    @EventHandler
    public void init(FMLInitializationEvent event) {
        CoflSky.Wrapper = new WSClientWrapper(webSocketURIPrefix);

        keyBindings = new KeyBinding[]{
                new KeyBinding("key.replay_last.onclick", Keyboard.KEY_NONE, "SkyCofl"),
                new KeyBinding("key.start_highest_bid", Keyboard.KEY_NONE, "SkyCofl")
        };

        if (event.getSide() == Side.CLIENT) {
            ClientCommandHandler.instance.registerCommand(new CoflSkyCommand());
            ClientCommandHandler.instance.registerCommand(new ColfCommand());
            ClientCommandHandler.instance.registerCommand(new FlipperChatCommand());

            for (int i = 0; i < keyBindings.length; ++i) {
                ClientRegistry.registerKeyBinding(keyBindings[i]);
            }


        }
        Events = new EventRegistry();
        MinecraftForge.EVENT_BUS.register(Events);
        if (config.purchaseOverlay == GUIType.TFM) {
            MinecraftForge.EVENT_BUS.register(ButtonRemapper.getInstance());
        }
        MinecraftForge.EVENT_BUS.register(new ChatMessageSendHandler());
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            config.saveConfig(configFile, config);
            try {
                apiKeyManager.saveKey();
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }));
    }

    public static APIKeyManager getAPIKeyManager() {
        return apiKeyManager;
    }

}