aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/utils/hypixel
diff options
context:
space:
mode:
authorWyvest <45589059+Wyvest@users.noreply.github.com>2022-05-28 12:02:04 +0700
committerGitHub <noreply@github.com>2022-05-28 12:02:04 +0700
commit01e4c0dfb2a9726a9bf1c4d1a1fe28edbc3d094c (patch)
tree2949a7d1a9e6a5e9fbf6db983491ab813614b889 /src/main/java/cc/polyfrost/oneconfig/utils/hypixel
parent2f45fed981d7fcdf6e39791fecfa1dd410614fbf (diff)
downloadOneConfig-01e4c0dfb2a9726a9bf1c4d1a1fe28edbc3d094c.tar.gz
OneConfig-01e4c0dfb2a9726a9bf1c4d1a1fe28edbc3d094c.tar.bz2
OneConfig-01e4c0dfb2a9726a9bf1c4d1a1fe28edbc3d094c.zip
OC-32 OC-34 (#24)
* OC-34 hypixel utils class (#21) * OC-26 Build Workflow * OC-26 oops * OC-33 some networking utils * OC-34 idk * OC-34 idk * OC-34 restructure hypixel utils * hypixelutils and multithreading implement more events remove vcal icon stuff * more javadocs Co-authored-by: Ethan <git@ethanlibs.co>
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/utils/hypixel')
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/utils/hypixel/HypixelUtils.java159
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/utils/hypixel/LocrawInfo.java110
2 files changed, 269 insertions, 0 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/hypixel/HypixelUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/hypixel/HypixelUtils.java
new file mode 100644
index 0000000..d7a9b0d
--- /dev/null
+++ b/src/main/java/cc/polyfrost/oneconfig/utils/hypixel/HypixelUtils.java
@@ -0,0 +1,159 @@
+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.libs.universal.UMinecraft;
+import cc.polyfrost.oneconfig.libs.universal.wrappers.UPlayer;
+import cc.polyfrost.oneconfig.libs.universal.wrappers.message.UTextComponent;
+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 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;
+ }
+}
diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/hypixel/LocrawInfo.java b/src/main/java/cc/polyfrost/oneconfig/utils/hypixel/LocrawInfo.java
new file mode 100644
index 0000000..4a946f0
--- /dev/null
+++ b/src/main/java/cc/polyfrost/oneconfig/utils/hypixel/LocrawInfo.java
@@ -0,0 +1,110 @@
+package cc.polyfrost.oneconfig.utils.hypixel;
+
+import com.google.gson.annotations.SerializedName;
+
+import java.util.Objects;
+
+/**
+ * Represents the location of the player in 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>
+ *
+ * @see HypixelUtils
+ */
+public class LocrawInfo {
+ @SerializedName("server")
+ private String serverId;
+ @SerializedName("mode")
+ private String gameMode = "lobby";
+ @SerializedName("map")
+ private String mapName;
+ @SerializedName("gametype")
+ private String rawGameType;
+ private GameType gameType;
+
+ /**
+ * @return The serverID of the server you are currently on, ex: mini121
+ */
+ public String getServerId() {
+ return serverId;
+ }
+
+ /**
+ * @return The GameType of the server as a String.
+ */
+ public String getRawGameType() {
+ return rawGameType;
+ }
+
+ /**
+ * @return The GameMode of the server, ex: solo_insane
+ */
+ public String getGameMode() {
+ return gameMode;
+ }
+
+ /**
+ * @return The GameType of the server as an Enum.
+ */
+ public GameType getGameType() {
+ return gameType;
+ }
+
+ /**
+ * @param gameType The GameType to set it to.
+ */
+ public void setGameType(GameType gameType) {
+ this.gameType = gameType;
+ }
+
+ /**
+ * @return The map of the server, ex: Shire.
+ */
+ public String getMapName() {
+ return mapName;
+ }
+
+ @Override
+ public String toString() {
+ return "LocrawInfo{" + "serverId='" + serverId + '\'' + ", gameMode='" + gameMode + '\'' + ", mapName='" + mapName + '\'' + ", rawGameType='" + rawGameType + '\'' + '}';
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ LocrawInfo that = (LocrawInfo) o;
+ return Objects.equals(serverId, that.serverId) && Objects.equals(gameMode, that.gameMode) && Objects.equals(mapName, that.mapName) && Objects.equals(rawGameType, that.rawGameType);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(serverId, gameMode, mapName, rawGameType);
+ }
+
+ public enum GameType {
+ UNKNOWN(""), LIMBO("LIMBO"), BEDWARS("BEDWARS"), SKYWARS("SKYWARS"), PROTOTYPE("PROTOTYPE"), SKYBLOCK("SKYBLOCK"), MAIN("MAIN"), MURDER_MYSTERY("MURDER_MYSTERY"), HOUSING("HOUSING"), ARCADE_GAMES("ARCADE"), BUILD_BATTLE("BUILD_BATTLE"), DUELS("DUELS"), PIT("PIT"), UHC_CHAMPIONS("UHC"), SPEED_UHC("SPEED_UHC"), TNT_GAMES("TNTGAMES"), CLASSIC_GAMES("LEGACY"), COPS_AND_CRIMS("MCGO"), BLITZ_SG("SURVIVAL_GAMES"), MEGA_WALLS("WALLS3"), SMASH_HEROES("SUPER_SMASH"), WARLORDS("BATTLEGROUND");
+
+ private final String serverName;
+
+ GameType(String serverName) {
+ this.serverName = serverName;
+ }
+
+ public static GameType getFromLocraw(String gameType) {
+ for (GameType value : values()) {
+ if (value.serverName.equals(gameType)) {
+ return value;
+ }
+ }
+
+ return UNKNOWN;
+ }
+
+ public String getServerName() {
+ return serverName;
+ }
+ }
+}