From b9d6b75423ea24c4947b3a655f199c3b34aa167a Mon Sep 17 00:00:00 2001 From: Cow Date: Fri, 29 May 2020 12:30:31 +0200 Subject: Added /moo stalkskyblock - Now using CommandExceptions instead of just red chat color for command error messages - Simplified creation of chat message --- .../java/eu/olli/cowmoonication/util/ApiUtils.java | 27 ++- .../olli/cowmoonication/util/MooChatComponent.java | 186 +++++++++++++++++++++ .../java/eu/olli/cowmoonication/util/Utils.java | 25 +++ 3 files changed, 233 insertions(+), 5 deletions(-) create mode 100644 src/main/java/eu/olli/cowmoonication/util/MooChatComponent.java (limited to 'src/main/java/eu/olli/cowmoonication/util') diff --git a/src/main/java/eu/olli/cowmoonication/util/ApiUtils.java b/src/main/java/eu/olli/cowmoonication/util/ApiUtils.java index aca1819..8515593 100644 --- a/src/main/java/eu/olli/cowmoonication/util/ApiUtils.java +++ b/src/main/java/eu/olli/cowmoonication/util/ApiUtils.java @@ -4,8 +4,10 @@ import com.google.gson.JsonArray; import com.google.gson.JsonParser; import com.mojang.util.UUIDTypeAdapter; import eu.olli.cowmoonication.Cowmoonication; +import eu.olli.cowmoonication.command.exception.ThrowingConsumer; import eu.olli.cowmoonication.config.MooConfig; import eu.olli.cowmoonication.data.Friend; +import eu.olli.cowmoonication.data.HySkyBlockStats; import eu.olli.cowmoonication.data.HyStalkingData; import eu.olli.cowmoonication.data.SlothStalkingData; import org.apache.http.HttpStatus; @@ -18,20 +20,20 @@ import java.net.HttpURLConnection; import java.net.URL; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.function.Consumer; public class ApiUtils { public static final String UUID_NOT_FOUND = "UUID-NOT-FOUND"; private static final String NAME_TO_UUID_URL = "https://api.mojang.com/users/profiles/minecraft/"; private static final String UUID_TO_NAME_URL = "https://api.mojang.com/user/profiles/%s/names"; private static final String STALKING_URL_OFFICIAL = "https://api.hypixel.net/status?key=%s&uuid=%s"; + private static final String SKYBLOCK_STATS_URL_OFFICIAL = "https://api.hypixel.net/skyblock/profiles?key=%s&uuid=%s"; private static final String STALKING_URL_UNOFFICIAL = "https://api.slothpixel.me/api/players/%s"; private static ExecutorService pool = Executors.newCachedThreadPool(); private ApiUtils() { } - public static void fetchFriendData(String name, Consumer action) { + public static void fetchFriendData(String name, ThrowingConsumer action) { pool.execute(() -> action.accept(getFriend(name))); } @@ -48,7 +50,7 @@ public class ApiUtils { return null; } - public static void fetchCurrentName(Friend friend, Consumer action) { + public static void fetchCurrentName(Friend friend, ThrowingConsumer action) { pool.execute(() -> action.accept(getCurrentName(friend))); } @@ -68,7 +70,7 @@ public class ApiUtils { return null; } - public static void fetchPlayerStatus(Friend friend, Consumer action) { + public static void fetchPlayerStatus(Friend friend, ThrowingConsumer action) { pool.execute(() -> action.accept(stalkPlayer(friend))); } @@ -83,7 +85,22 @@ public class ApiUtils { return null; } - public static void fetchPlayerOfflineStatus(Friend stalkedPlayer, Consumer action) { + public static void fetchSkyBlockStats(Friend friend, ThrowingConsumer action) { + pool.execute(() -> action.accept(stalkSkyBlockStats(friend))); + } + + private static HySkyBlockStats stalkSkyBlockStats(Friend friend) { + try (BufferedReader reader = makeApiCall(String.format(SKYBLOCK_STATS_URL_OFFICIAL, MooConfig.moo, UUIDTypeAdapter.fromUUID(friend.getUuid())))) { + if (reader != null) { + return GsonUtils.fromJson(reader, HySkyBlockStats.class); + } + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + public static void fetchPlayerOfflineStatus(Friend stalkedPlayer, ThrowingConsumer action) { pool.execute(() -> action.accept(stalkOfflinePlayer(stalkedPlayer))); } diff --git a/src/main/java/eu/olli/cowmoonication/util/MooChatComponent.java b/src/main/java/eu/olli/cowmoonication/util/MooChatComponent.java new file mode 100644 index 0000000..a489391 --- /dev/null +++ b/src/main/java/eu/olli/cowmoonication/util/MooChatComponent.java @@ -0,0 +1,186 @@ +package eu.olli.cowmoonication.util; + +import net.minecraft.event.ClickEvent; +import net.minecraft.event.HoverEvent; +import net.minecraft.util.ChatComponentText; +import net.minecraft.util.EnumChatFormatting; +import net.minecraft.util.IChatComponent; + +public class MooChatComponent extends ChatComponentText { + public MooChatComponent(String msg) { + super(msg); + } + + public MooChatComponent black() { + setChatStyle(getChatStyle().setColor(EnumChatFormatting.BLACK)); + return this; + } + + public MooChatComponent darkBlue() { + setChatStyle(getChatStyle().setColor(EnumChatFormatting.DARK_BLUE)); + return this; + } + + public MooChatComponent darkGreen() { + setChatStyle(getChatStyle().setColor(EnumChatFormatting.DARK_GREEN)); + return this; + } + + public MooChatComponent darkAqua() { + setChatStyle(getChatStyle().setColor(EnumChatFormatting.DARK_AQUA)); + return this; + } + + public MooChatComponent darkRed() { + setChatStyle(getChatStyle().setColor(EnumChatFormatting.DARK_RED)); + return this; + } + + public MooChatComponent darkPurple() { + setChatStyle(getChatStyle().setColor(EnumChatFormatting.DARK_PURPLE)); + return this; + } + + public MooChatComponent gold() { + setChatStyle(getChatStyle().setColor(EnumChatFormatting.GOLD)); + return this; + } + + public MooChatComponent gray() { + setChatStyle(getChatStyle().setColor(EnumChatFormatting.GRAY)); + return this; + } + + public MooChatComponent darkGray() { + setChatStyle(getChatStyle().setColor(EnumChatFormatting.DARK_GRAY)); + return this; + } + + public MooChatComponent blue() { + setChatStyle(getChatStyle().setColor(EnumChatFormatting.BLUE)); + return this; + } + + public MooChatComponent green() { + setChatStyle(getChatStyle().setColor(EnumChatFormatting.GREEN)); + return this; + } + + public MooChatComponent aqua() { + setChatStyle(getChatStyle().setColor(EnumChatFormatting.AQUA)); + return this; + } + + public MooChatComponent red() { + setChatStyle(getChatStyle().setColor(EnumChatFormatting.RED)); + return this; + } + + public MooChatComponent lightPurple() { + setChatStyle(getChatStyle().setColor(EnumChatFormatting.LIGHT_PURPLE)); + return this; + } + + public MooChatComponent yellow() { + setChatStyle(getChatStyle().setColor(EnumChatFormatting.YELLOW)); + return this; + } + + public MooChatComponent white() { + setChatStyle(getChatStyle().setColor(EnumChatFormatting.WHITE)); + return this; + } + + public MooChatComponent obfuscated() { + setChatStyle(getChatStyle().setObfuscated(true)); + return this; + } + + public MooChatComponent bold() { + setChatStyle(getChatStyle().setBold(true)); + return this; + } + + public MooChatComponent strikethrough() { + setChatStyle(getChatStyle().setStrikethrough(true)); + return this; + } + + public MooChatComponent underline() { + setChatStyle(getChatStyle().setUnderlined(true)); + return this; + } + + public MooChatComponent italic() { + setChatStyle(getChatStyle().setItalic(true)); + return this; + } + + public MooChatComponent reset() { + setChatStyle(getChatStyle().setParentStyle(null).setBold(false).setItalic(false).setObfuscated(false).setUnderlined(false).setStrikethrough(false)); + return this; + } + + public MooChatComponent setHover(IChatComponent hover) { + setChatStyle(getChatStyle().setChatHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hover))); + return this; + } + + public MooChatComponent setUrl(String url) { + setUrl(url, new KeyValueTooltipComponent("Click to visit", url)); + return this; + } + + public MooChatComponent setUrl(String url, String hover) { + setUrl(url, new MooChatComponent(hover).yellow()); + return this; + } + + public MooChatComponent setUrl(String url, IChatComponent hover) { + setChatStyle(getChatStyle().setChatClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url))); + setHover(hover); + return this; + } + + public MooChatComponent setSuggestCommand(String command) { + setChatStyle(getChatStyle().setChatClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, command))); + setHover(new KeyValueChatComponent("Run", command, " ")); + return this; + } + + /** + * Appends the given component in a new line, without inheriting formatting of previous siblings. + * + * @see ChatComponentText#appendSibling appendSibling + */ + public MooChatComponent appendFreshSibling(IChatComponent sibling) { + this.siblings.add(new ChatComponentText("\n").appendSibling(sibling)); + return this; + } + + @Deprecated + public MooChatComponent appendKeyValue(String key, String value) { + appendSibling(new MooChatComponent("\n").appendFreshSibling(new KeyValueChatComponent(key, value))); + return this; + } + + public static class KeyValueChatComponent extends MooChatComponent { + public KeyValueChatComponent(String key, String value) { + this(key, value, ": "); + } + + public KeyValueChatComponent(String key, String value, String separator) { + super(key); + appendText(separator); + gold().appendSibling(new MooChatComponent(value).yellow()); + } + } + + public static class KeyValueTooltipComponent extends MooChatComponent { + public KeyValueTooltipComponent(String key, String value) { + super(key); + appendText(": "); + gray().appendSibling(new MooChatComponent(value).yellow()); + } + } +} diff --git a/src/main/java/eu/olli/cowmoonication/util/Utils.java b/src/main/java/eu/olli/cowmoonication/util/Utils.java index 0ff4010..ec355b9 100644 --- a/src/main/java/eu/olli/cowmoonication/util/Utils.java +++ b/src/main/java/eu/olli/cowmoonication/util/Utils.java @@ -11,6 +11,7 @@ import java.util.regex.Pattern; public final class Utils { public static final Pattern VALID_UUID_PATTERN = Pattern.compile("^(\\w{8})-(\\w{4})-(\\w{4})-(\\w{4})-(\\w{12})$"); private static final Pattern VALID_USERNAME = Pattern.compile("^[\\w]{1,16}$"); + private static final char[] LARGE_NUMBERS = new char[]{'k', 'm', 'b', 't'}; private Utils() { } @@ -52,4 +53,28 @@ public final class Utils { dateFormatted); } } + + /** + * Formats a large number with abbreviations for each factor of a thousand (k, m, ...) + * + * @param number the number to format + * @return a String representing the number n formatted in a cool looking way. + * @see Source + */ + public static String formatNumberWithAbbreviations(double number) { + return formatNumberWithAbbreviations(number, 0); + } + + private static String formatNumberWithAbbreviations(double number, int iteration) { + @SuppressWarnings("IntegerDivisionInFloatingPointContext") double d = ((long) number / 100) / 10.0; + boolean isRound = (d * 10) % 10 == 0; //true if the decimal part is equal to 0 (then it's trimmed anyway) + // this determines the class, i.e. 'k', 'm' etc + // this decides whether to trim the decimals + // (int) d * 10 / 10 drops the decimal + return d < 1000 ? // this determines the class, i.e. 'k', 'm' etc + (d > 99.9 || isRound || d > 9.99 ? // this decides whether to trim the decimals + (int) d * 10 / 10 : d + "" // (int) d * 10 / 10 drops the decimal + ) + "" + LARGE_NUMBERS[iteration] + : formatNumberWithAbbreviations(d, iteration + 1); + } } -- cgit