diff --git a/gradle.properties b/gradle.properties index 08a1d1da..bc9ae14e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,6 +6,6 @@ org.gradle.jvmargs=-Xmx2G mod_name = Hytils Reborn mod_id = hytils-reborn -mod_version = 1.6.0-beta4 +mod_version = 1.6.0-beta5 loom.platform = forge \ No newline at end of file diff --git a/src/main/java/cc/woverflow/hytils/HytilsReborn.java b/src/main/java/cc/woverflow/hytils/HytilsReborn.java index 8a68c903..98ad6a10 100644 --- a/src/main/java/cc/woverflow/hytils/HytilsReborn.java +++ b/src/main/java/cc/woverflow/hytils/HytilsReborn.java @@ -138,7 +138,7 @@ public void postInit(FMLPostInitializationEvent event) { isPatcher = Loader.isModLoaded("patcher"); isChatting = Loader.isModLoaded("chatting"); - Multithreading.runAsync(() -> rank = HypixelAPIUtils.getRank(Minecraft.getMinecraft().getSession().getUsername())); + Multithreading.runAsync(() -> rank = HypixelAPIUtils.getRank()); } @Mod.EventHandler diff --git a/src/main/java/cc/woverflow/hytils/command/HytilsCommand.java b/src/main/java/cc/woverflow/hytils/command/HytilsCommand.java index cfc98e28..3d13d812 100644 --- a/src/main/java/cc/woverflow/hytils/command/HytilsCommand.java +++ b/src/main/java/cc/woverflow/hytils/command/HytilsCommand.java @@ -25,16 +25,13 @@ import cc.polyfrost.oneconfig.utils.commands.annotations.Description; import cc.polyfrost.oneconfig.utils.commands.annotations.Main; import cc.polyfrost.oneconfig.utils.commands.annotations.SubCommand; -import cc.polyfrost.oneconfig.utils.hypixel.HypixelUtils; import cc.woverflow.hytils.HytilsReborn; import cc.woverflow.hytils.command.parser.GEXPType; import cc.woverflow.hytils.command.parser.GEXPTypeParser; import cc.woverflow.hytils.command.parser.WinstreakType; import cc.woverflow.hytils.command.parser.WinstreakTypeParser; -import cc.woverflow.hytils.config.HytilsConfig; import cc.woverflow.hytils.util.HypixelAPIUtils; import com.mojang.authlib.GameProfile; -import net.minecraft.util.EnumChatFormatting; import org.jetbrains.annotations.Nullable; import java.util.Locale; @@ -55,10 +52,6 @@ private void handleDefault() { @SuppressWarnings("SameParameterValue") private void gexp(@Description("username") @Nullable GameProfile player, @Description("type") @Nullable GEXPType type) { Multithreading.runAsync(() -> { - if (HytilsConfig.apiKey.isEmpty() || !HypixelUtils.INSTANCE.isValidKey(HytilsConfig.apiKey)) { - HytilsReborn.INSTANCE.sendMessage(EnumChatFormatting.RED + "You need to provide a valid API key to run this command! Type /api new to autoset a key."); - return; - } if (player != null) { if (type == null) { if (HypixelAPIUtils.getGEXP(player.getName())) { @@ -112,10 +105,6 @@ private void gexp(@Description("username") @Nullable GameProfile player, @Descri @SuppressWarnings("SameParameterValue") private void winstreak(@Description("username") @Nullable GameProfile player, @Description("type") @Nullable WinstreakType gamemode) { Multithreading.runAsync(() -> { - if (HytilsConfig.apiKey.isEmpty() || !HypixelUtils.INSTANCE.isValidKey(HytilsConfig.apiKey)) { - HytilsReborn.INSTANCE.sendMessage(EnumChatFormatting.RED + "You need to provide a valid API key to run this command! Type /api new to autoset a key."); - return; - } if (player != null) { if (gamemode != null) { if (HypixelAPIUtils.getWinstreak(player.getName(), gamemode.name())) { @@ -154,17 +143,4 @@ private void winstreak(@Description("username") @Nullable GameProfile player, @D } }); } - - @SubCommand(description = "Sets your API key.", aliases = "setkey") - private static void key(@Description("API Key") String apiKey) { - Multithreading.runAsync(() -> { - if (HypixelUtils.INSTANCE.isValidKey(apiKey)) { - HytilsConfig.apiKey = apiKey; - HytilsReborn.INSTANCE.getConfig().save(); - HytilsReborn.INSTANCE.sendMessage(EnumChatFormatting.GREEN + "Saved API key successfully!"); - } else { - HytilsReborn.INSTANCE.sendMessage(EnumChatFormatting.RED + "Invalid API key! Please try again."); - } - }); - } } diff --git a/src/main/java/cc/woverflow/hytils/config/HytilsConfig.java b/src/main/java/cc/woverflow/hytils/config/HytilsConfig.java index 07755e90..62b5cb72 100644 --- a/src/main/java/cc/woverflow/hytils/config/HytilsConfig.java +++ b/src/main/java/cc/woverflow/hytils/config/HytilsConfig.java @@ -44,17 +44,6 @@ @SuppressWarnings("unused") public class HytilsConfig extends Config { - // API - - @Text( - name = "API Key", - description = "Automatically get the API Key from /api new.", - category = "API", - secure = true - ) - @HypixelKey - public static String apiKey = ""; - // Automatic @Switch( diff --git a/src/main/java/cc/woverflow/hytils/util/HypixelAPIUtils.java b/src/main/java/cc/woverflow/hytils/util/HypixelAPIUtils.java index 49782e1b..a6b0ff9a 100644 --- a/src/main/java/cc/woverflow/hytils/util/HypixelAPIUtils.java +++ b/src/main/java/cc/woverflow/hytils/util/HypixelAPIUtils.java @@ -23,35 +23,46 @@ import cc.polyfrost.oneconfig.events.event.TickEvent; import cc.polyfrost.oneconfig.libs.eventbus.Subscribe; import cc.polyfrost.oneconfig.utils.NetworkUtils; -import cc.polyfrost.oneconfig.utils.hypixel.HypixelUtils; import cc.polyfrost.oneconfig.utils.hypixel.LocrawInfo; import cc.woverflow.hytils.HytilsReborn; -import cc.woverflow.hytils.config.HytilsConfig; import cc.woverflow.hytils.handlers.cache.HeightHandler; import cc.woverflow.hytils.util.ranks.RankType; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import com.mojang.authlib.exceptions.AuthenticationException; import net.minecraft.client.Minecraft; import net.minecraft.util.EnumChatFormatting; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Locale; -import java.util.Map; -import java.util.TimeZone; +import java.math.BigInteger; +import java.util.*; public class HypixelAPIUtils { public static String gexp; public static String winstreak; public static LocrawInfo locraw; private int ticks = 0; - private static final String[] rankValues = {"rank", "monthlyPackageRank", "newPackageRank", "packageRank"}; - private static String getCurrentESTTime() { - SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); - simpleDateFormat.setTimeZone(TimeZone.getTimeZone("EST")); - return simpleDateFormat.format(new Date(System.currentTimeMillis())); + public HypixelAPIUtils() { + authorize(); + } + + private static boolean authorize() { + Random r1 = new Random(); + Random r2 = new Random(System.identityHashCode(new Object())); + BigInteger random1Bi = new BigInteger(128, r1); + BigInteger random2Bi = new BigInteger(128, r2); + BigInteger serverBi = random1Bi.xor(random2Bi); + String serverId = serverBi.toString(16); + try { + Minecraft.getMinecraft().getSessionService().joinServer(Minecraft + .getMinecraft() + .getSession() + .getProfile(), Minecraft.getMinecraft().getSession().getToken(), serverId); + } catch (AuthenticationException e) { + e.printStackTrace(); + return false; + } + JsonObject response = NetworkUtils.getJsonElement("https://api.polyfrost.cc/hypixel/authentication?username=" + Minecraft.getMinecraft().getSession().getUsername() + "&serverId=" + serverId).getAsJsonObject(); + return !response.has("error"); } @@ -61,17 +72,19 @@ private static String getCurrentESTTime() { * @return Whether the "getting" was successful. */ public static boolean getGEXP() { - String gexp = null; - String uuid = Minecraft.getMinecraft().thePlayer.getGameProfile().getId().toString().replace("-", ""); - JsonArray guildMembers = NetworkUtils.getJsonElement("https://api.hypixel.net/guild?key=" + HytilsConfig.apiKey + ";player=" + uuid).getAsJsonObject().getAsJsonObject("guild").getAsJsonArray("members"); - for (JsonElement e : guildMembers) { - if (e.getAsJsonObject().get("uuid").getAsString().equals(uuid)) { - gexp = Integer.toString(e.getAsJsonObject().getAsJsonObject("expHistory").get(getCurrentESTTime()).getAsInt()); - break; + JsonObject exp = NetworkUtils.getJsonElement("https://api.polyfrost.cc/hypixel/guild-exp").getAsJsonObject(); + if (exp.has("error")) { + if (Objects.equals(exp.get("error").getAsString(), "INVALID_AUTHORIZATION")) { + if (authorize()) { + return getGEXP(); + } else { + return false; + } + } else { + return false; } } - if (gexp == null) return false; - HypixelAPIUtils.gexp = gexp; + gexp = exp.get("exp").getAsString(); return true; } @@ -82,17 +95,19 @@ public static boolean getGEXP() { * @return Whether the "getting" was successful. */ public static boolean getGEXP(String username) { - String gexp = null; - String uuid = getUUID(username); - JsonArray guildMembers = NetworkUtils.getJsonElement("https://api.hypixel.net/guild?key=" + HytilsConfig.apiKey + ";player=" + uuid).getAsJsonObject().getAsJsonObject("guild").getAsJsonArray("members"); - for (JsonElement e : guildMembers) { - if (e.getAsJsonObject().get("uuid").getAsString().equals(uuid)) { - gexp = Integer.toString(e.getAsJsonObject().getAsJsonObject("expHistory").get(getCurrentESTTime()).getAsInt()); - break; + JsonObject exp = NetworkUtils.getJsonElement("https://api.polyfrost.cc/hypixel/guild-exp?username=" + username).getAsJsonObject(); + if (exp.has("error")) { + if (Objects.equals(exp.get("error").getAsString(), "INVALID_AUTHORIZATION")) { + if (authorize()) { + return getGEXP(username); + } else { + return false; + } + } else { + return false; } } - if (gexp == null) return false; - HypixelAPIUtils.gexp = gexp; + gexp = exp.get("exp").getAsString(); return true; } @@ -102,21 +117,19 @@ public static boolean getGEXP(String username) { * @return Whether the "getting" was successful. */ public static boolean getWeeklyGEXP() { - String gexp = null; - String uuid = Minecraft.getMinecraft().thePlayer.getGameProfile().getId().toString().replace("-", ""); - JsonArray guildMembers = NetworkUtils.getJsonElement("https://api.hypixel.net/guild?key=" + HytilsConfig.apiKey + ";player=" + uuid).getAsJsonObject().getAsJsonObject("guild").getAsJsonArray("members"); - for (JsonElement e : guildMembers) { - if (e.getAsJsonObject().get("uuid").getAsString().equals(uuid)) { - int addGEXP = 0; - for (Map.Entry set : e.getAsJsonObject().get("expHistory").getAsJsonObject().entrySet()) { - addGEXP += set.getValue().getAsInt(); + JsonObject exp = NetworkUtils.getJsonElement("https://api.polyfrost.cc/hypixel/guild-exp").getAsJsonObject(); + if (exp.has("error")) { + if (Objects.equals(exp.get("error").getAsString(), "INVALID_AUTHORIZATION")) { + if (authorize()) { + return getWeeklyGEXP(); + } else { + return false; } - gexp = Integer.toString(addGEXP); - break; + } else { + return false; } } - if (gexp == null) return false; - HypixelAPIUtils.gexp = gexp; + gexp = exp.get("weeklyExp").getAsString(); return true; } @@ -127,21 +140,19 @@ public static boolean getWeeklyGEXP() { * @return Whether the "getting" was successful. */ public static boolean getWeeklyGEXP(String username) { - String gexp = null; - String uuid = getUUID(username); - JsonArray guildMembers = NetworkUtils.getJsonElement("https://api.hypixel.net/guild?key=" + HytilsConfig.apiKey + ";player=" + uuid).getAsJsonObject().getAsJsonObject("guild").getAsJsonArray("members"); - for (JsonElement e : guildMembers) { - if (e.getAsJsonObject().get("uuid").getAsString().equals(uuid)) { - int addGEXP = 0; - for (Map.Entry set : e.getAsJsonObject().get("expHistory").getAsJsonObject().entrySet()) { - addGEXP += set.getValue().getAsInt(); + JsonObject exp = NetworkUtils.getJsonElement("https://api.polyfrost.cc/hypixel/guild-exp?username=" + username).getAsJsonObject(); + if (exp.has("error")) { + if (Objects.equals(exp.get("error").getAsString(), "INVALID_AUTHORIZATION")) { + if (authorize()) { + return getWeeklyGEXP(username); + } else { + return false; } - gexp = Integer.toString(addGEXP); - break; + } else { + return false; } } - if (gexp == null) return false; - HypixelAPIUtils.gexp = gexp; + gexp = exp.get("weeklyExp").getAsString(); return true; } @@ -151,38 +162,36 @@ public static boolean getWeeklyGEXP(String username) { * @return Whether the "getting" was successful. */ public static boolean getWinstreak() { - String uuid = Minecraft.getMinecraft().thePlayer.getGameProfile().getId().toString().replace("-", ""); - JsonObject playerStats = - NetworkUtils.getJsonElement("https://api.hypixel.net/player?key=" + HytilsConfig.apiKey + ";uuid=" + uuid).getAsJsonObject().getAsJsonObject("player").getAsJsonObject("stats"); + String gameType = "bedwars"; if (locraw != null) { switch (locraw.getGameType()) { case BEDWARS: - try { - winstreak = Integer.toString(playerStats.getAsJsonObject("Bedwars").get("winstreak").getAsInt()); - return true; - } catch (Exception e) { - e.printStackTrace(); - } + gameType = "bedwars"; break; case SKYWARS: - try { - winstreak = Integer.toString(playerStats.getAsJsonObject("SkyWars").get("win_streak").getAsInt()); - return true; - } catch (Exception e) { - e.printStackTrace(); - } + gameType = "skywars"; break; case DUELS: - try { - winstreak = Integer.toString(playerStats.getAsJsonObject("Duels").get("current_winstreak").getAsInt()); - return true; - } catch (Exception e) { - e.printStackTrace(); - } + gameType = "duels"; break; + default: + return false; } } - return false; + JsonObject winstreak = NetworkUtils.getJsonElement("https://api.polyfrost.cc/hypixel/winstreak?game=" + gameType).getAsJsonObject(); + if (winstreak.has("error")) { + if (Objects.equals(winstreak.get("error").getAsString(), "INVALID_AUTHORIZATION")) { + if (authorize()) { + return getWinstreak(); + } else { + return false; + } + } else { + return false; + } + } + HypixelAPIUtils.winstreak = String.valueOf(winstreak.get("winstreak").getAsInt()); + return true; } /** @@ -192,38 +201,36 @@ public static boolean getWinstreak() { * @return Whether the "getting" was successful. */ public static boolean getWinstreak(String username) { - String uuid = getUUID(username); - JsonObject playerStats = - NetworkUtils.getJsonElement("https://api.hypixel.net/player?key=" + HytilsConfig.apiKey + ";uuid=" + uuid).getAsJsonObject().getAsJsonObject("player").getAsJsonObject("stats"); + String gameType = "bedwars"; if (locraw != null) { switch (locraw.getGameType()) { case BEDWARS: - try { - winstreak = Integer.toString(playerStats.getAsJsonObject("Bedwars").get("winstreak").getAsInt()); - return true; - } catch (Exception e) { - e.printStackTrace(); - } + gameType = "bedwars"; break; case SKYWARS: - try { - winstreak = Integer.toString(playerStats.getAsJsonObject("SkyWars").get("win_streak").getAsInt()); - return true; - } catch (Exception e) { - e.printStackTrace(); - } + gameType = "skywars"; break; case DUELS: - try { - winstreak = Integer.toString(playerStats.getAsJsonObject("Duels").get("current_winstreak").getAsInt()); - return true; - } catch (Exception e) { - e.printStackTrace(); - } + gameType = "duels"; break; + default: + return false; } } - return false; + JsonObject winstreak = NetworkUtils.getJsonElement("https://api.polyfrost.cc/hypixel/winstreak?game=" + gameType + "&username=" + username).getAsJsonObject(); + if (winstreak.has("error")) { + if (Objects.equals(winstreak.get("error").getAsString(), "INVALID_AUTHORIZATION")) { + if (authorize()) { + return getWinstreak(username); + } else { + return false; + } + } else { + return false; + } + } + HypixelAPIUtils.winstreak = String.valueOf(winstreak.get("winstreak").getAsInt()); + return true; } /** @@ -234,63 +241,45 @@ public static boolean getWinstreak(String username) { * @return Whether the "getting" was successful. */ public static boolean getWinstreak(String username, String game) { - String uuid = getUUID(username); - JsonObject playerStats = - NetworkUtils.getJsonElement("https://api.hypixel.net/player?key=" + HytilsConfig.apiKey + ";uuid=" + uuid).getAsJsonObject().getAsJsonObject("player").getAsJsonObject("stats"); - if (game != null) { - switch (game.toLowerCase(Locale.ENGLISH)) { - case "bedwars": - try { - winstreak = Integer.toString(playerStats.getAsJsonObject("Bedwars").get("winstreak").getAsInt()); - return true; - } catch (Exception e) { - e.printStackTrace(); - } - break; - case "skywars": - try { - winstreak = Integer.toString(playerStats.getAsJsonObject("SkyWars").get("win_streak").getAsInt()); - return true; - } catch (Exception e) { - e.printStackTrace(); - } - break; - case "duels": - try { - winstreak = Integer.toString(playerStats.getAsJsonObject("Duels").get("current_winstreak").getAsInt()); - return true; - } catch (Exception e) { - e.printStackTrace(); - } - break; + if (!Objects.equals(game, "bedwars") && !Objects.equals(game, "skywars") && !Objects.equals(game, "duels")) { + return false; + } + JsonObject winstreak = NetworkUtils.getJsonElement("https://api.polyfrost.cc/hypixel/winstreak?game=" + game + "&username=" + username).getAsJsonObject(); + if (winstreak.has("error")) { + if (Objects.equals(winstreak.get("error").getAsString(), "INVALID_AUTHORIZATION")) { + if (authorize()) { + return getWinstreak(username, game); + } else { + return false; + } + } else { + return false; } } - return false; + HypixelAPIUtils.winstreak = String.valueOf(winstreak.get("winstreak").getAsInt()); + return true; } /** * Gets the Hypixel rank of the specified player. * - * @param username The username of the player. * @return Player rank, RankType.UNKNOWN if the player does not exist or the API key is empty */ - public static RankType getRank(String username) { - if (!HytilsConfig.apiKey.isEmpty() && HypixelUtils.INSTANCE.isValidKey(HytilsConfig.apiKey)) { - String uuid = getUUID(username); - try { - JsonObject playerRank = - NetworkUtils.getJsonElement("https://api.hypixel.net/player?key=" + HytilsConfig.apiKey + ";uuid=" + uuid).getAsJsonObject().getAsJsonObject("player"); - for (String value : rankValues) { - if (playerRank.has(value) && !playerRank.get(value).getAsString().matches("NONE|NORMAL")) { - return RankType.getRank(playerRank.get(value).getAsString()); - } + public static RankType getRank() { + JsonObject playerRank = + NetworkUtils.getJsonElement("https://api.polyfrost.cc/hypixel/rank").getAsJsonObject(); + if (playerRank.has("error")) { + if (Objects.equals(playerRank.get("error").getAsString(), "INVALID_AUTHORIZATION")) { + if (authorize()) { + return getRank(); + } else { + return RankType.UNKNOWN; } - return RankType.NON; - } catch (Exception e) { - e.printStackTrace(); + } else { + return RankType.UNKNOWN; } } - return RankType.UNKNOWN; + return RankType.getRank(playerRank.get("rank").getAsString()); } /** @@ -318,7 +307,7 @@ public static String getUUID(String username) { } @Subscribe - public void onTick(TickEvent event) { + private void onTick(TickEvent event) { if (event.stage == Stage.START) { if (ticks % 20 == 0) { if (Minecraft.getMinecraft().thePlayer != null) {