Skip to content

Commit

Permalink
new: added rank type enum (#75)
Browse files Browse the repository at this point in the history
* fix: resolve offline Mojang API crashing the game and fixed ads in tab not being correctly toggleable

* new: added rank type enum
  • Loading branch information
xthe-dev committed Mar 20, 2023
1 parent 17d4b49 commit ddd79b5
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 27 deletions.
10 changes: 8 additions & 2 deletions src/main/java/cc/woverflow/hytils/HytilsReborn.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -228,4 +230,8 @@ public Logger getLogger() {
public FriendCache getFriendCache() {
return friendCache;
}

public RankType getRank() {
return rank;
}
}
11 changes: 7 additions & 4 deletions src/main/java/cc/woverflow/hytils/command/HytilsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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;
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,6 +45,10 @@ public class MysteryBoxStar {

private final Pattern mysteryBoxStarPattern = Pattern.compile("\\u00a75\\u00a7o\\u00a77\\u00a77Quality: \\u00a7e(?<stars>\\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()) {
Expand Down Expand Up @@ -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;
}

Expand Down
32 changes: 14 additions & 18 deletions src/main/java/cc/woverflow/hytils/util/HypixelAPIUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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) {
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/cc/woverflow/hytils/util/ranks/RankType.java
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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;
}
}

0 comments on commit ddd79b5

Please sign in to comment.