aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/thatgravyboat/skyblockhud/SkyblockHud.java
blob: 92564b86206291d39a03b20f6777d16ffd3baad4 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package com.thatgravyboat.skyblockhud;

import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.thatgravyboat.skyblockhud.api.LeaderboardGetter;
import com.thatgravyboat.skyblockhud.api.events.ProfileSwitchedEvent;
import com.thatgravyboat.skyblockhud.commands.Commands;
import com.thatgravyboat.skyblockhud.config.KeyBindings;
import com.thatgravyboat.skyblockhud.config.SBHConfig;
import com.thatgravyboat.skyblockhud.dungeons.DungeonHandler;
import com.thatgravyboat.skyblockhud.handlers.*;
import com.thatgravyboat.skyblockhud.location.FarmingIslandHandler;
import com.thatgravyboat.skyblockhud.location.IslandHandler;
import com.thatgravyboat.skyblockhud.location.LocationHandler;
import com.thatgravyboat.skyblockhud.location.MinesHandler;
import com.thatgravyboat.skyblockhud.overlay.DungeonOverlay;
import com.thatgravyboat.skyblockhud.overlay.MiningHud;
import com.thatgravyboat.skyblockhud.overlay.OverlayHud;
import com.thatgravyboat.skyblockhud.overlay.RPGHud;
import com.thatgravyboat.skyblockhud.playerstats.ActionBarParsing;
import com.thatgravyboat.skyblockhud.seasons.SeasonDateHandler;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Set;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.scoreboard.ScoreObjective;
import net.minecraft.scoreboard.Scoreboard;
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
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.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import org.lwjgl.input.Keyboard;

@Mod(modid = SkyblockHud.MODID, version = SkyblockHud.VERSION)
public class SkyblockHud {

    public static final String MODID = "skyblockhud";
    public static final String VERSION = "1.12";

    public static SBHConfig config;

    private File configFile;

    private static final Set<String> SKYBLOCK_IN_ALL_LANGUAGES = Sets.newHashSet("SKYBLOCK", "\u7A7A\u5C9B\u751F\u5B58");

    private final Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create();

    private static File configDirectory;

    @EventHandler
    public void preInit(FMLPreInitializationEvent event) {
        MinecraftForge.EVENT_BUS.register(this);
        MinecraftForge.EVENT_BUS.register(new LeaderboardGetter());
        MinecraftForge.EVENT_BUS.register(new SeasonDateHandler());
        MinecraftForge.EVENT_BUS.register(new LocationHandler());
        MinecraftForge.EVENT_BUS.register(new IslandHandler());
        MinecraftForge.EVENT_BUS.register(new TimeHandler());
        MinecraftForge.EVENT_BUS.register(new CurrencyHandler());
        MinecraftForge.EVENT_BUS.register(new SlayerHandler());
        MinecraftForge.EVENT_BUS.register(new DungeonHandler());
        MinecraftForge.EVENT_BUS.register(new MinesHandler());
        MinecraftForge.EVENT_BUS.register(new FarmingIslandHandler());

        /* DISABLE UNTIL NEW SYSTEM
        MinecraftForge.EVENT_BUS.register(new TrackerHandler());
        MinecraftForge.EVENT_BUS.register(new KillTrackerHandler());
         */
        MinecraftForge.EVENT_BUS.register(new HeldItemHandler());

        ClientRegistry.registerKeyBinding(KeyBindings.map);

        MinecraftForge.EVENT_BUS.register(new ComponentHandler());
        MinecraftForge.EVENT_BUS.register(new ActionBarParsing());
        MinecraftForge.EVENT_BUS.register(new CrystalWaypoints());
        Commands.init();

        configFile = new File(event.getModConfigurationDirectory(), "sbh-config.json");

        if (configFile.exists()) {
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(configFile), StandardCharsets.UTF_8))) {
                config = gson.fromJson(reader, SBHConfig.class);
            } catch (Exception ignored) {}
        }

        if (config == null) {
            config = new SBHConfig();
            saveConfig();
        }

        configDirectory = event.getModConfigurationDirectory();

        Runtime.getRuntime().addShutdownHook(new Thread(this::saveConfig));
        //Runtime.getRuntime().addShutdownHook(new Thread(() -> TrackerFileLoader.saveTrackerStatsFile(event.getModConfigurationDirectory())));
    }

    public void saveConfig() {
        try {
            configFile.createNewFile();

            try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(configFile), StandardCharsets.UTF_8))) {
                writer.write(gson.toJson(config));
            }
        } catch (IOException ignored) {}
    }

    @EventHandler
    public void postInit(FMLPostInitializationEvent event) {
        MinecraftForge.EVENT_BUS.register(new OverlayHud());
        MinecraftForge.EVENT_BUS.register(new RPGHud());
        MinecraftForge.EVENT_BUS.register(new DungeonOverlay());
        MinecraftForge.EVENT_BUS.register(new BossbarHandler());
        MinecraftForge.EVENT_BUS.register(new MapHandler());
        MinecraftForge.EVENT_BUS.register(new MiningHud());
    }

    /* DISABLE UNTIL NEW SYSTEM

    @EventHandler
    public void loadComplete(FMLLoadCompleteEvent event){
        TrackerFileLoader.loadTrackersFile();

        if (TrackerFileLoader.loadTrackerStatsFile(configDirectory)){
            TrackerFileLoader.saveTrackerStatsFile(configDirectory);
        }
    }

    @SubscribeEvent
    public void onLeaveServer(FMLNetworkEvent.ClientDisconnectionFromServerEvent event){
        TrackerFileLoader.saveTrackerStatsFile(configDirectory);
    }

     */

    public static boolean hasSkyblockScoreboard() {
        Minecraft mc = Minecraft.getMinecraft();

        if (mc != null && mc.theWorld != null) {
            Scoreboard scoreboard = mc.theWorld.getScoreboard();
            ScoreObjective sidebarObjective = scoreboard.getObjectiveInDisplaySlot(1);
            if (sidebarObjective != null) {
                String objectiveName = sidebarObjective.getDisplayName().replaceAll("(?i)\\u00A7.", "");
                for (String skyblock : SKYBLOCK_IN_ALL_LANGUAGES) {
                    if (objectiveName.startsWith(skyblock)) {
                        return true;
                    }
                }
            }
        }

        return false;
    }

    @SubscribeEvent
    public void onTooltip(ItemTooltipEvent event) {
        if (event.itemStack != null && Keyboard.isKeyDown(Keyboard.KEY_BACKSLASH)) {
            try {
                StringSelection clipboard = new StringSelection(event.itemStack.serializeNBT().toString());
                Toolkit.getDefaultToolkit().getSystemClipboard().setContents(clipboard, clipboard);
            } catch (Exception ignored) {}
        }
    }

    @SubscribeEvent(priority = EventPriority.HIGHEST)
    public void onStatusBar(ClientChatReceivedEvent event) {
        if (Utils.removeColor(event.message.getUnformattedText()).toLowerCase().trim().startsWith("your profile was changed to:")) {
            MinecraftForge.EVENT_BUS.post(new ProfileSwitchedEvent());
        }
    }

    public static GuiScreen screenToOpen = null;
    private static int screenTicks = 0;

    @SubscribeEvent
    public void onClientTick(TickEvent.ClientTickEvent event) {
        if (screenToOpen != null) {
            screenTicks++;
            if (screenTicks == 5) {
                Minecraft.getMinecraft().displayGuiScreen(screenToOpen);
                screenTicks = 0;
                screenToOpen = null;
            }
        }
    }
}