aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/utils/hypixel/HypixelUtils.java
blob: a952053877bb2e26174b8cc0da75976bf132327c (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
package cc.polyfrost.oneconfig.utils.hypixel;

import cc.polyfrost.oneconfig.events.EventManager;
import cc.polyfrost.oneconfig.events.event.*;
import cc.polyfrost.oneconfig.utils.JsonUtils;
import cc.polyfrost.oneconfig.utils.Multithreading;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import gg.essential.universal.UChat;
import gg.essential.universal.UMinecraft;
import gg.essential.universal.wrappers.UPlayer;
import gg.essential.universal.wrappers.message.UTextComponent;
import me.kbrewster.eventbus.Subscribe;

import java.util.Locale;
import java.util.concurrent.TimeUnit;

/**
 * Various utilities for Hypixel.
 * <p>
 * Locraw utilities taken from Seraph by Scherso under LGPL-2.1
 * <a href="https://github.com/Scherso/Seraph/blob/master/LICENSE">https://github.com/Scherso/Seraph/blob/master/LICENSE</a>
 * </p>
 */
public class HypixelUtils {
    public static final HypixelUtils INSTANCE = new HypixelUtils();
    private final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
    private int tick = 0;
    private int limboLoop = 0;

    private boolean sentCommand = false;
    private boolean sendPermitted = false;
    private boolean inGame;

    private LocrawInfo locraw;
    private LocrawInfo previousLocraw;

    /**
     * Checks whether the player is on Hypixel.
     *
     * @return Whether the player is on Hypixel.
     * @see <a href="https://canary.discord.com/channels/864592657572560958/945075920664928276/978649312013725747">this discord link from jade / asbyth</a>
     */
    public boolean isHypixel() {
        if (UMinecraft.getWorld() == null || UMinecraft.getMinecraft().isIntegratedServerRunning()) return false;

        net.minecraft.client.entity.EntityPlayerSP player = UPlayer.getPlayer();
        if (player == null) return false;
        String serverBrand = player.getClientBrand();

        if (serverBrand == null) return false;

        return serverBrand.toLowerCase(Locale.ENGLISH).contains("hypixel");
    }

    /**
     * Queues a locraw update after the specified interval.
     *
     * @param interval The interval in milliseconds.
     */
    public void queueUpdate(long interval) {
        sendPermitted = true;
        Multithreading.schedule(() -> {
            if (sendPermitted) {
                UChat.say("/locraw");
            }
        }, interval, TimeUnit.MILLISECONDS);
    }

    @Subscribe
    private void onTick(TickEvent event) {
        if (event.stage == Stage.START) {
            tick++;

            if (tick % 20 == 0) {
                tick = 0;
                if (isHypixel() && !sentCommand) {
                    queueUpdate(500);
                    sentCommand = true;
                }
            }
        }
    }

    @Subscribe
    private void onWorldLoad(WorldLoadEvent event) {
        locraw = null;
        sendPermitted = false;
        sentCommand = false;
        limboLoop = 0;
    }

    @Subscribe
    private void onMessageReceived(ChatReceiveEvent event) {
        if (!sentCommand) return;
        try {
            final String msg = UTextComponent.Companion.stripFormatting(event.message.getUnformattedText());
            // Checking for rate limitation.
            if (!(msg.startsWith("{") && msg.endsWith("}"))) {
                if (msg.contains("You are sending too many commands! Please try again in a few seconds.")) // if you're being rate limited, the /locraw command will be resent in 5 seconds.
                    queueUpdate(5000);
                return;
            }

            JsonElement raw = JsonUtils.parseString(msg);
            if (!raw.isJsonObject()) return;
            JsonObject json = raw.getAsJsonObject();
            LocrawInfo parsed = GSON.fromJson(json, LocrawInfo.class);

            if (5 > limboLoop && parsed.getGameType() == LocrawInfo.GameType.LIMBO) {
                sentCommand = false;
                limboLoop++;
                queueUpdate(1000);
            } else locraw = parsed; // if the player isn't in limbo, the parsed info is used.

            if (locraw != null) {
                locraw.setGameType(LocrawInfo.GameType.getFromLocraw(this.locraw.getRawGameType()));
                if (parsed.getGameMode().equals("lobby")) {
                    inGame = false; // If your gamemode returns "lobby", boolean inGame is false.
                } else {
                    previousLocraw = parsed;
                    inGame = true; // If your gamemode does not return "lobby", boolean inGame is true.
                }
                EventManager.INSTANCE.post(new LocrawEvent(locraw));
                event.isCancelled = true;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Returns whether the player is in game.
     *
     * @return Whether the player is in game.
     */
    public boolean isInGame() {
        return this.inGame;
    }

    /**
     * Returns the current {@link LocrawInfo}.
     *
     * @return The current {@link LocrawInfo}.
     * @see LocrawInfo
     */
    public LocrawInfo getLocrawInfo() {
        return this.locraw;
    }

    /**
     * Returns the previous {@link LocrawInfo}.
     *
     * @return The previous {@link LocrawInfo}.
     * @see LocrawInfo
     */
    public LocrawInfo getPreviousLocraw() {
        return this.previousLocraw;
    }
}