diff options
author | Cow <cow@volloeko.de> | 2023-07-23 09:31:45 +0200 |
---|---|---|
committer | Cow <cow@volloeko.de> | 2023-07-23 09:31:45 +0200 |
commit | c4a4eebe2d322b59638720760e1ff45f64b2fa5e (patch) | |
tree | be6f5d8c69aa9ede5c4ef9a4580794b82f9f5be0 /src | |
parent | 78fb1b936ef2efe8de4e0c48397022d595062ab1 (diff) | |
download | Cowlection-c4a4eebe2d322b59638720760e1ff45f64b2fa5e.tar.gz Cowlection-c4a4eebe2d322b59638720760e1ff45f64b2fa5e.tar.bz2 Cowlection-c4a4eebe2d322b59638720760e1ff45f64b2fa5e.zip |
Added link to 'note on API keys' to API key related error messages
Diffstat (limited to 'src')
5 files changed, 31 insertions, 21 deletions
diff --git a/src/main/java/de/cowtipper/cowlection/command/MooCommand.java b/src/main/java/de/cowtipper/cowlection/command/MooCommand.java index 04c1eb1..ae79603 100644 --- a/src/main/java/de/cowtipper/cowlection/command/MooCommand.java +++ b/src/main/java/de/cowtipper/cowlection/command/MooCommand.java @@ -202,7 +202,7 @@ public class MooCommand extends CommandBase { //region sub commands: Best friends, friends & other players private void handleStalking(String[] args) throws CommandException { if (!CredentialStorage.isMooValid) { - throw new MooCommandException("[Cowlection] You haven't set your Hypixel API key yet or the API key is invalid. Use " + EnumChatFormatting.DARK_RED + "/" + this.getCommandName() + " apikey <key>" + EnumChatFormatting.RED + " to manually set your existing API key."); + throw new MooCommandException("[Cowlection] You haven't set your Hypixel API key yet or the API key is invalid. Use " + EnumChatFormatting.DARK_RED + "/" + this.getCommandName() + " apikey " + EnumChatFormatting.RED + "to manually set your existing API key."); } if (args.length != 2) { throw new WrongUsageException("/" + getCommandName() + " stalk <playerName>"); @@ -296,7 +296,7 @@ public class MooCommand extends CommandBase { //region sub commands: SkyBlock private void handleStalkingSkyBlock(String[] args) throws CommandException { if (!CredentialStorage.isMooValid) { - throw new MooCommandException("[Cowlection] You haven't set your Hypixel API key yet or the API key is invalid. Use " + EnumChatFormatting.DARK_RED + "/" + this.getCommandName() + " apikey <key>" + EnumChatFormatting.RED + " to manually set your existing API key."); + throw new MooCommandException("[Cowlection] You haven't set your Hypixel API key yet or the API key is invalid. Use " + EnumChatFormatting.DARK_RED + "/" + this.getCommandName() + " apikey " + EnumChatFormatting.RED + "to manually set your existing API key."); } if (args.length != 2) { throw new WrongUsageException("/" + getCommandName() + " skyblockstalk <playerName>"); @@ -827,7 +827,7 @@ public class MooCommand extends CommandBase { } else if ((args.length == 2 && (args[1].equalsIgnoreCase("party") || args[1].equalsIgnoreCase("p"))) || args.length == 1 && args[0].equalsIgnoreCase("dp")) { if (!CredentialStorage.isMooValid) { - throw new MooCommandException("[Cowlection] You haven't set your Hypixel API key yet or the API key is invalid. Use " + EnumChatFormatting.DARK_RED + "/" + this.getCommandName() + " apikey <key>" + EnumChatFormatting.RED + " to manually set your existing API key."); + throw new MooCommandException("[Cowlection] You haven't set your Hypixel API key yet or the API key is invalid. Use " + EnumChatFormatting.DARK_RED + "/" + this.getCommandName() + " apikey " + EnumChatFormatting.RED + "to manually set your existing API key."); } else if (dungeonsPartyListener != null) { throw new MooCommandException("Please wait a few seconds before using this command again."); } @@ -872,19 +872,21 @@ public class MooCommand extends CommandBase { private void handleApiKey(String[] args) throws CommandException { if (args.length == 1) { - String firstSentence; + MooChatComponent msg; EnumChatFormatting color; EnumChatFormatting colorSecondary; if (CredentialStorage.isMooValid && StringUtils.isNotEmpty(CredentialStorage.moo)) { - firstSentence = EnumChatFormatting.GREEN + "[Cowlection] You already set your Hypixel API key."; + msg = new MooChatComponent("[Cowlection] You already set your Hypixel API key.").green(); color = EnumChatFormatting.GRAY; colorSecondary = EnumChatFormatting.YELLOW; } else { - firstSentence = "[Cowlection] You haven't set your Hypixel API key yet or the API key is invalid."; + msg = new MooChatComponent("[Cowlection] You haven't set your Hypixel API key yet or the API key is invalid.").red(); color = EnumChatFormatting.RED; colorSecondary = EnumChatFormatting.DARK_RED; } - main.getChatHelper().sendMessage(color, firstSentence + color + " Use " + colorSecondary + "/" + this.getCommandName() + " apikey <key>" + color + " to manually set your API key."); + main.getChatHelper().sendMessage(msg.appendSibling(new MooChatComponent(color + " Use " + colorSecondary + "/" + this.getCommandName() + " apikey <key>" + color + " to manually set your API key.")) + .appendFreshSibling(new MooChatComponent(" ❢ ").lightPurple().setUrl("https://github.com/cow-mc/Cowlection/blob/master/CHANGELOG.md#note-on-api-keys-") + .appendSibling(new MooChatComponent("[open 'Note on API keys']").darkAqua().underline()))); } else { String key = args[1]; if (Utils.isValidUuid(key)) { diff --git a/src/main/java/de/cowtipper/cowlection/command/exception/MooCommandException.java b/src/main/java/de/cowtipper/cowlection/command/exception/MooCommandException.java index f16e488..fcf2283 100644 --- a/src/main/java/de/cowtipper/cowlection/command/exception/MooCommandException.java +++ b/src/main/java/de/cowtipper/cowlection/command/exception/MooCommandException.java @@ -1,9 +1,18 @@ package de.cowtipper.cowlection.command.exception; import net.minecraft.command.CommandException; +import net.minecraft.util.EnumChatFormatting; public class MooCommandException extends CommandException { + private final String msg; + public MooCommandException(String msg) { super("cowlection.commands.generic.exception", msg); + this.msg = msg; + } + + @Override + public String getLocalizedMessage() { + return EnumChatFormatting.getTextWithoutFormattingCodes(this.msg); } } diff --git a/src/main/java/de/cowtipper/cowlection/error/ApiHttpErrorEvent.java b/src/main/java/de/cowtipper/cowlection/error/ApiHttpErrorEvent.java index 757a2bf..45bb07f 100644 --- a/src/main/java/de/cowtipper/cowlection/error/ApiHttpErrorEvent.java +++ b/src/main/java/de/cowtipper/cowlection/error/ApiHttpErrorEvent.java @@ -21,11 +21,6 @@ public class ApiHttpErrorEvent extends Event { return url; } - public String getBaseUrl() { - int queryParamStart = url.indexOf('?', 10); - return queryParamStart > 0 ? url.substring(0, queryParamStart) : url; - } - public boolean wasUsingApiKey() { return wasUsingApiKey; } diff --git a/src/main/java/de/cowtipper/cowlection/listener/PlayerListener.java b/src/main/java/de/cowtipper/cowlection/listener/PlayerListener.java index 4478f1b..713902a 100644 --- a/src/main/java/de/cowtipper/cowlection/listener/PlayerListener.java +++ b/src/main/java/de/cowtipper/cowlection/listener/PlayerListener.java @@ -124,13 +124,17 @@ public class PlayerListener { public void onApiHttpError(ApiHttpErrorEvent e) { if (nextApiErrorMessage < System.currentTimeMillis() && Minecraft.getMinecraft().thePlayer != null) { this.nextApiErrorMessage = System.currentTimeMillis() + 3000; - MooChatComponent hoverComponent = new MooChatComponent.KeyValueTooltipComponent("Click to visit", e.getBaseUrl()); + MooChatComponent hoverComponent = new MooChatComponent.KeyValueTooltipComponent("Click to visit", e.getUrl()); if (e.wasUsingApiKey()) { - String eyeCatcher = "" + EnumChatFormatting.LIGHT_PURPLE + EnumChatFormatting.OBFUSCATED + "#" + EnumChatFormatting.RESET + EnumChatFormatting.RED; - hoverComponent.appendFreshSibling(new MooChatComponent(eyeCatcher + " Request was using your API-Key.").red()); + hoverComponent.appendFreshSibling(new MooChatComponent(EnumChatFormatting.LIGHT_PURPLE + " ❢ " + EnumChatFormatting.RED + " Request was using your API-Key.").red()); } - main.getChatHelper().sendMessage(new MooChatComponent(e.getMessage()).red() - .setUrl(e.getUrl(), hoverComponent)); + MooChatComponent errorMsg = new MooChatComponent(e.getMessage()).red() + .setUrl(e.getUrl(), hoverComponent); + if (e.wasUsingApiKey()) { + errorMsg.appendFreshSibling(new MooChatComponent(" ❢ ").lightPurple().setUrl("https://github.com/cow-mc/Cowlection/blob/master/CHANGELOG.md#note-on-api-keys-") + .appendSibling(new MooChatComponent("[open 'Note on API keys']").darkAqua().underline())); + } + main.getChatHelper().sendMessage(errorMsg); } } @@ -223,7 +227,7 @@ public class PlayerListener { } }; - new TickDelay(checkScoreboard, 40); // 2 second delay + retrying for 20 seconds, making sure scoreboard got sent + new TickDelay(checkScoreboard, 40); // 2-second delay + retrying for 20 seconds, making sure scoreboard got sent } else if (MooConfig.getEnableSkyBlockOnlyFeatures() == MooConfig.Setting.DISABLED) { isOnSkyBlock = false; unregisterSkyBlockListeners(); diff --git a/src/main/java/de/cowtipper/cowlection/util/ApiUtils.java b/src/main/java/de/cowtipper/cowlection/util/ApiUtils.java index b8fa4a5..89c979b 100644 --- a/src/main/java/de/cowtipper/cowlection/util/ApiUtils.java +++ b/src/main/java/de/cowtipper/cowlection/util/ApiUtils.java @@ -164,7 +164,7 @@ public class ApiUtils { private static BufferedReader makeApiCall(String url, boolean sendApiKey, String key) throws IOException { if (sendApiKey && !CredentialStorage.isMooValid) { - throw new ApiHttpErrorException("Your current Hypixel API key is invalid. Use /moo apikey to manually set your existing API key.", url); + throw new ApiHttpErrorException("Your current Hypixel API key is invalid. Use §4/moo apikey §cto manually set your API key.", url, true); } try { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); @@ -182,10 +182,10 @@ public class ApiUtils { if (connection.getResponseCode() == HttpStatus.SC_NO_CONTENT) { // http status 204 return null; } else if (connection.getResponseCode() == HttpStatus.SC_BAD_GATEWAY && url.startsWith("https://api.hypixel.net/")) { // http status 502 (cloudflare) - throw new ApiHttpErrorException("Couldn't contact Hypixel API (502 Bad Gateway). API might be down, check https://status.hypixel.net for info.", "https://status.hypixel.net"); + throw new ApiHttpErrorException("Couldn't contact Hypixel API (502 Bad Gateway). API might be down, check https://status.hypixel.net for info.", "https://status.hypixel.net", sendApiKey); } else if (connection.getResponseCode() == HttpStatus.SC_FORBIDDEN && sendApiKey && url.startsWith("https://api.hypixel.net/")) { // http status 403 Forbidden Cowlection.getInstance().getMoo().setMooValidity(false); - throw new ApiHttpErrorException("Your current Hypixel API key seems to be invalid. Use /moo apikey to manually set your existing API key.", url); + throw new ApiHttpErrorException("Your current Hypixel API key seems to be invalid. Use §4/moo apikey §cto manually set your API key.", url, true); } else if (connection.getResponseCode() == HttpStatus.SC_SERVICE_UNAVAILABLE) { // http status 503 Service Unavailable throw new ApiHttpErrorException("Couldn't contact the API (503 Service unavailable). API might be down, or you might be blocked by Cloudflare, check if you can reach: " + url, url, sendApiKey); } else if (connection.getResponseCode() == HttpStatus.SC_BAD_GATEWAY && url.startsWith("https://moulberry.codes/")) { // http status 502 (cloudflare) |