From ddd79b5370e3dd3b1f4ec079784b6c1f962dcd5f Mon Sep 17 00:00:00 2001 From: XThe <101992755+SirXThe@users.noreply.github.com> Date: Mon, 20 Mar 2023 17:44:03 +0100 Subject: [PATCH] new: added rank type enum (#75) * fix: resolve offline Mojang API crashing the game and fixed ads in tab not being correctly toggleable * new: added rank type enum --- .../cc/woverflow/hytils/HytilsReborn.java | 10 ++++- .../hytils/command/HytilsCommand.java | 11 ++++-- .../modules/blockers/NonCooldownBlocker.java | 4 +- .../lobby/mysterybox/MysteryBoxStar.java | 9 ++++- .../hytils/util/HypixelAPIUtils.java | 32 +++++++-------- .../woverflow/hytils/util/ranks/RankType.java | 39 +++++++++++++++++++ 6 files changed, 78 insertions(+), 27 deletions(-) create mode 100644 src/main/java/cc/woverflow/hytils/util/ranks/RankType.java diff --git a/src/main/java/cc/woverflow/hytils/HytilsReborn.java b/src/main/java/cc/woverflow/hytils/HytilsReborn.java index 2389a6ef..762326ff 100644 --- a/src/main/java/cc/woverflow/hytils/HytilsReborn.java +++ b/src/main/java/cc/woverflow/hytils/HytilsReborn.java @@ -21,6 +21,7 @@ import cc.polyfrost.oneconfig.events.EventManager; import cc.polyfrost.oneconfig.libs.universal.ChatColor; import cc.polyfrost.oneconfig.libs.universal.UChat; +import cc.polyfrost.oneconfig.utils.Multithreading; import cc.polyfrost.oneconfig.utils.commands.CommandManager; import cc.woverflow.hytils.command.*; import cc.woverflow.hytils.config.HytilsConfig; @@ -53,6 +54,7 @@ import cc.woverflow.hytils.handlers.render.ChestHighlighter; import cc.woverflow.hytils.util.HypixelAPIUtils; import cc.woverflow.hytils.util.friends.FriendCache; +import cc.woverflow.hytils.util.ranks.RankType; import net.minecraft.client.Minecraft; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.fml.common.Loader; @@ -96,7 +98,7 @@ public class HytilsReborn { public boolean isChatting; private boolean loadedCall; - public String rank; + private RankType rank; @Mod.EventHandler public void init(FMLInitializationEvent event) { @@ -134,7 +136,7 @@ public void postInit(FMLPostInitializationEvent event) { isPatcher = Loader.isModLoaded("patcher"); isChatting = Loader.isModLoaded("chatting"); - rank = HypixelAPIUtils.getRank(Minecraft.getMinecraft().getSession().getUsername()); + Multithreading.runAsync(() -> rank = HypixelAPIUtils.getRank(Minecraft.getMinecraft().getSession().getUsername())); } @Mod.EventHandler @@ -228,4 +230,8 @@ public Logger getLogger() { public FriendCache getFriendCache() { return friendCache; } + + public RankType getRank() { + return rank; + } } diff --git a/src/main/java/cc/woverflow/hytils/command/HytilsCommand.java b/src/main/java/cc/woverflow/hytils/command/HytilsCommand.java index fc74a640..cfc98e28 100644 --- a/src/main/java/cc/woverflow/hytils/command/HytilsCommand.java +++ b/src/main/java/cc/woverflow/hytils/command/HytilsCommand.java @@ -25,6 +25,7 @@ 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; @@ -54,7 +55,7 @@ private void handleDefault() { @SuppressWarnings("SameParameterValue") private void gexp(@Description("username") @Nullable GameProfile player, @Description("type") @Nullable GEXPType type) { Multithreading.runAsync(() -> { - if (HytilsConfig.apiKey.isEmpty() || !HypixelAPIUtils.isValidKey(HytilsConfig.apiKey)) { + 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; } @@ -80,6 +81,7 @@ private void gexp(@Description("username") @Nullable GameProfile player, @Descri Notifications.INSTANCE .send(HytilsReborn.MOD_NAME, "There was a problem trying to get " + player.getName() + "'s daily GEXP."); } + break; case WEEKLY: if (HypixelAPIUtils.getWeeklyGEXP(player.getName())) { Notifications.INSTANCE @@ -91,6 +93,7 @@ private void gexp(@Description("username") @Nullable GameProfile player, @Descri Notifications.INSTANCE .send(HytilsReborn.MOD_NAME, "There was a problem trying to get " + player.getName() + "'s weekly GEXP."); } + break; } } } else { @@ -109,7 +112,7 @@ 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() || !HypixelAPIUtils.isValidKey(HytilsConfig.apiKey)) { + 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; } @@ -125,7 +128,7 @@ private void winstreak(@Description("username") @Nullable GameProfile player, @D Notifications.INSTANCE .send( HytilsReborn.MOD_NAME, - "There was a problem trying to get " + player.getName() + "'s winstreak in $gamemode." + "There was a problem trying to get " + player.getName() + "'s winstreak in " + gamemode + "." ); } } else { @@ -155,7 +158,7 @@ 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 (HypixelAPIUtils.isValidKey(apiKey)) { + if (HypixelUtils.INSTANCE.isValidKey(apiKey)) { HytilsConfig.apiKey = apiKey; HytilsReborn.INSTANCE.getConfig().save(); HytilsReborn.INSTANCE.sendMessage(EnumChatFormatting.GREEN + "Saved API key successfully!"); diff --git a/src/main/java/cc/woverflow/hytils/handlers/chat/modules/blockers/NonCooldownBlocker.java b/src/main/java/cc/woverflow/hytils/handlers/chat/modules/blockers/NonCooldownBlocker.java index 77a801e2..c1f7e6ae 100644 --- a/src/main/java/cc/woverflow/hytils/handlers/chat/modules/blockers/NonCooldownBlocker.java +++ b/src/main/java/cc/woverflow/hytils/handlers/chat/modules/blockers/NonCooldownBlocker.java @@ -21,6 +21,7 @@ import cc.woverflow.hytils.HytilsReborn; import cc.woverflow.hytils.config.HytilsConfig; import cc.woverflow.hytils.handlers.chat.ChatSendModule; +import cc.woverflow.hytils.util.ranks.RankType; import net.minecraft.client.Minecraft; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -33,9 +34,8 @@ public class NonCooldownBlocker implements ChatSendModule { @Override public @Nullable String onMessageSend(@NotNull String message) { - String rank = HytilsReborn.INSTANCE.rank; if (message.startsWith("/")) return message; - if (rank != null && rank.equals("DEFAULT")) { + if (HytilsReborn.INSTANCE.getRank() == RankType.NON) { if (nonCooldown < System.currentTimeMillis()) { nonCooldown = System.currentTimeMillis() + (getCooldownLengthInSeconds() * 1000L); return message; diff --git a/src/main/java/cc/woverflow/hytils/handlers/lobby/mysterybox/MysteryBoxStar.java b/src/main/java/cc/woverflow/hytils/handlers/lobby/mysterybox/MysteryBoxStar.java index 18473ca8..cb9e9e98 100644 --- a/src/main/java/cc/woverflow/hytils/handlers/lobby/mysterybox/MysteryBoxStar.java +++ b/src/main/java/cc/woverflow/hytils/handlers/lobby/mysterybox/MysteryBoxStar.java @@ -20,7 +20,9 @@ import cc.polyfrost.oneconfig.utils.hypixel.HypixelUtils; import cc.polyfrost.oneconfig.utils.hypixel.LocrawUtil; +import cc.woverflow.hytils.HytilsReborn; import cc.woverflow.hytils.config.HytilsConfig; +import cc.woverflow.hytils.util.ranks.RankType; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.inventory.GuiChest; @@ -43,6 +45,10 @@ public class MysteryBoxStar { private final Pattern mysteryBoxStarPattern = Pattern.compile("\\u00a75\\u00a7o\\u00a77\\u00a77Quality: \\u00a7e(?\\u2730+).*"); + private boolean missingRank(int stars) { + return (HytilsReborn.INSTANCE.getRank() == RankType.NON && stars >= 4) || ((HytilsReborn.INSTANCE.getRank() == RankType.VIP || HytilsReborn.INSTANCE.getRank() == RankType.VIP_PLUS) && stars == 5); + } + @SubscribeEvent public void onDrawScreenPre(GuiScreenEvent.DrawScreenEvent.Pre event) { if (!HytilsConfig.mysteryBoxStar || !HypixelUtils.INSTANCE.isHypixel() || LocrawUtil.INSTANCE.getLocrawInfo() == null || LocrawUtil.INSTANCE.isInGame()) { @@ -99,7 +105,8 @@ public void drawStars(ItemStack item, int x, int y) { String rankedBoxLine = tooltip.get(tooltip.size() - 5); Matcher rankedBoxMatcher = mysteryBoxStarPattern.matcher(rankedBoxLine); if (rankedBoxMatcher.matches()) { - fontRenderer.drawStringWithShadow(rankedBoxMatcher.group("stars").length() + "\u2730", x, y, -14080); + // prints the star in red if you cant open it + fontRenderer.drawStringWithShadow(rankedBoxMatcher.group("stars").length() + "\u2730", x, y, missingRank(rankedBoxMatcher.group("stars").length()) ? -43691 : -14080); return; } diff --git a/src/main/java/cc/woverflow/hytils/util/HypixelAPIUtils.java b/src/main/java/cc/woverflow/hytils/util/HypixelAPIUtils.java index 66b6ad15..49782e1b 100644 --- a/src/main/java/cc/woverflow/hytils/util/HypixelAPIUtils.java +++ b/src/main/java/cc/woverflow/hytils/util/HypixelAPIUtils.java @@ -23,10 +23,12 @@ 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; @@ -42,9 +44,9 @@ public class HypixelAPIUtils { public static String gexp; public static String winstreak; - public static String rank; 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"); @@ -270,20 +272,25 @@ public static boolean getWinstreak(String username, String game) { * Gets the Hypixel rank of the specified player. * * @param username The username of the player. - * @return Player rank + * @return Player rank, RankType.UNKNOWN if the player does not exist or the API key is empty */ - public static String getRank(String username) { - if (!HytilsConfig.apiKey.isEmpty() && HypixelAPIUtils.isValidKey(HytilsConfig.apiKey)) { + 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"); - rank = playerRank.get("newPackageRank").toString(); + for (String value : rankValues) { + if (playerRank.has(value) && !playerRank.get(value).getAsString().matches("NONE|NORMAL")) { + return RankType.getRank(playerRank.get(value).getAsString()); + } + } + return RankType.NON; } catch (Exception e) { e.printStackTrace(); } } - return rank; + return RankType.UNKNOWN; } /** @@ -303,24 +310,13 @@ public static String getUUID(String username) { } return uuidResponse.get("id").getAsString(); } catch (Exception e) { - e.printStackTrace(); HytilsReborn.INSTANCE.sendMessage( - EnumChatFormatting.RED + "The Mojang API is currently down. Please try again later." + EnumChatFormatting.RED + "Failed to fetch " + username + "'s data. Please make sure this user exists." ); return null; } } - public static boolean isValidKey(String key) { - try { - JsonObject gotten = NetworkUtils.getJsonElement("https://api.hypixel.net/key?key=" + key).getAsJsonObject(); - return gotten.has("success") && gotten.get("success").getAsBoolean(); - } catch (Exception e) { - e.printStackTrace(); - } - return false; - } - @Subscribe public void onTick(TickEvent event) { if (event.stage == Stage.START) { diff --git a/src/main/java/cc/woverflow/hytils/util/ranks/RankType.java b/src/main/java/cc/woverflow/hytils/util/ranks/RankType.java new file mode 100644 index 00000000..56993f56 --- /dev/null +++ b/src/main/java/cc/woverflow/hytils/util/ranks/RankType.java @@ -0,0 +1,39 @@ +/* + * Hytils Reborn - Hypixel focused Quality of Life mod. + * Copyright (C) 2020, 2021, 2022, 2023 Polyfrost, Sk1er LLC and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package cc.woverflow.hytils.util.ranks; + +public enum RankType { + UNKNOWN("UNKNOWN"), NON("DEFAULT"), VIP("VIP"), VIP_PLUS("VIP_PLUS"), MVP("MVP"), MVP_PLUS("MVP_PLUS"), MVP_PLUS_PLUS("SUPERSTAR"), YOUTUBE("YOUTUBER"), GAME_MASTER("GAME_MASTER"), ADMIN("ADMIN"); + + private final String rank; + + RankType(String rank) { + this.rank = rank; + } + + public static RankType getRank(String rank) { + if (rank == null) return UNKNOWN; + for (RankType value : values()) { + if (value.rank.equals(rank)) { + return value; + } + } + return UNKNOWN; + } +}