aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/utils/hypixel/HypixelUtils.java
blob: c1dd91297f08df062556f58ed1bde431530625af (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
196
197
198
199
200
201
202
/*
 * This file is part of OneConfig.
 * OneConfig - Next Generation Config Library for Minecraft: Java Edition
 * Copyright (C) 2021, 2022 Polyfrost.
 *   <https://polyfrost.cc> <https://github.com/Polyfrost/>
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 *   OneConfig is licensed under the terms of version 3 of the GNU Lesser
 * General Public License as published by the Free Software Foundation, AND
 * under the Additional Terms Applicable to OneConfig, as published by Polyfrost,
 * either version 1.0 of the Additional Terms, 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
 * Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 * License.  If not, see <https://www.gnu.org/licenses/>. You should
 * have also received a copy of the Additional Terms Applicable
 * to OneConfig, as published by Polyfrost. If not, see
 * <https://polyfrost.cc/legal/oneconfig/additional-terms>
 */

package cc.polyfrost.oneconfig.utils.hypixel;

import cc.polyfrost.oneconfig.events.EventManager;
import cc.polyfrost.oneconfig.events.event.*;
import cc.polyfrost.oneconfig.libs.eventbus.Subscribe;
import cc.polyfrost.oneconfig.libs.universal.UChat;
import cc.polyfrost.oneconfig.platform.Platform;
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 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 cancel = false;
    private boolean sendPermitted = false;
    private boolean inGame;

    private LocrawInfo locraw;
    private LocrawInfo previousLocraw;

    private boolean initialized = false;
    private boolean isSeraph = false;

    public void initialize() {
        if (initialized) {
            return;
        }
        EventManager.INSTANCE.register(this);
        isSeraph = Platform.getLoaderPlatform().isModLoaded("seraph");
        initialized = true;
    }

    /**
     * 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 (!Platform.getServerPlatform().inMultiplayer()) return false;

        String serverBrand = Platform.getServerPlatform().getServerBrand();

        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) {
                cancel = true;
                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 && !isSeraph) {
                    queueUpdate(500);
                    sentCommand = true;
                }
            }
        }
    }

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

    @Subscribe
    private void onMessageReceived(ChatReceiveEvent event) {
        try {
            final boolean didSendCommand = sentCommand;
            final String msg = event.getFullyUnformattedMessage();
            // Checking for rate limitation.
            if (!(msg.startsWith("{") && msg.endsWith("}"))) {
                if (sentCommand && 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));
                if (cancel && didSendCommand) {
                    cancel = false;
                    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;
    }
}