diff options
Diffstat (limited to 'src/main/java/eu/olli/cowlection/util')
-rw-r--r-- | src/main/java/eu/olli/cowlection/util/ApiUtils.java | 139 | ||||
-rw-r--r-- | src/main/java/eu/olli/cowlection/util/ChatHelper.java | 82 | ||||
-rw-r--r-- | src/main/java/eu/olli/cowlection/util/GsonUtils.java | 94 | ||||
-rw-r--r-- | src/main/java/eu/olli/cowlection/util/ImageUtils.java | 64 | ||||
-rw-r--r-- | src/main/java/eu/olli/cowlection/util/MooChatComponent.java | 186 | ||||
-rw-r--r-- | src/main/java/eu/olli/cowlection/util/TickDelay.java | 29 | ||||
-rw-r--r-- | src/main/java/eu/olli/cowlection/util/Utils.java | 253 | ||||
-rw-r--r-- | src/main/java/eu/olli/cowlection/util/VersionChecker.java | 146 |
8 files changed, 0 insertions, 993 deletions
diff --git a/src/main/java/eu/olli/cowlection/util/ApiUtils.java b/src/main/java/eu/olli/cowlection/util/ApiUtils.java deleted file mode 100644 index 04a67cd..0000000 --- a/src/main/java/eu/olli/cowlection/util/ApiUtils.java +++ /dev/null @@ -1,139 +0,0 @@ -package eu.olli.cowlection.util; - -import com.google.gson.JsonArray; -import com.google.gson.JsonParser; -import com.google.gson.JsonSyntaxException; -import com.mojang.util.UUIDTypeAdapter; -import eu.olli.cowlection.Cowlection; -import eu.olli.cowlection.command.exception.ThrowingConsumer; -import eu.olli.cowlection.config.MooConfig; -import eu.olli.cowlection.data.Friend; -import eu.olli.cowlection.data.HyPlayerData; -import eu.olli.cowlection.data.HySkyBlockStats; -import eu.olli.cowlection.data.HyStalkingData; -import org.apache.http.HttpStatus; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -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 ONLINE_STATUS_URL = "https://api.hypixel.net/status?key=%s&uuid=%s"; - private static final String SKYBLOCK_STATS_URL = "https://api.hypixel.net/skyblock/profiles?key=%s&uuid=%s"; - private static final String PLAYER_URL = "https://api.hypixel.net/player?key=%s&uuid=%s"; - private static final ExecutorService pool = Executors.newCachedThreadPool(); - - private ApiUtils() { - } - - public static void fetchFriendData(String name, ThrowingConsumer<Friend> action) { - pool.execute(() -> action.accept(getFriend(name))); - } - - private static Friend getFriend(String name) { - try (BufferedReader reader = makeApiCall(NAME_TO_UUID_URL + name)) { - if (reader == null) { - return Friend.FRIEND_NOT_FOUND; - } else { - return GsonUtils.fromJson(reader, Friend.class); - } - } catch (IOException | JsonSyntaxException e) { - e.printStackTrace(); - } - return null; - } - - public static void fetchCurrentName(Friend friend, ThrowingConsumer<String> action) { - pool.execute(() -> action.accept(getCurrentName(friend))); - } - - private static String getCurrentName(Friend friend) { - try (BufferedReader reader = makeApiCall(String.format(UUID_TO_NAME_URL, UUIDTypeAdapter.fromUUID(friend.getUuid())))) { - if (reader == null) { - return UUID_NOT_FOUND; - } else { - JsonArray nameHistoryData = new JsonParser().parse(reader).getAsJsonArray(); - if (nameHistoryData.size() > 0) { - return nameHistoryData.get(nameHistoryData.size() - 1).getAsJsonObject().get("name").getAsString(); - } - } - } catch (IOException | JsonSyntaxException e) { - e.printStackTrace(); - } - return null; - } - - public static void fetchPlayerStatus(Friend friend, ThrowingConsumer<HyStalkingData> action) { - pool.execute(() -> action.accept(stalkPlayer(friend))); - } - - private static HyStalkingData stalkPlayer(Friend friend) { - try (BufferedReader reader = makeApiCall(String.format(ONLINE_STATUS_URL, MooConfig.moo, UUIDTypeAdapter.fromUUID(friend.getUuid())))) { - if (reader != null) { - return GsonUtils.fromJson(reader, HyStalkingData.class); - } - } catch (IOException | JsonSyntaxException e) { - e.printStackTrace(); - } - return null; - } - - public static void fetchSkyBlockStats(Friend friend, ThrowingConsumer<HySkyBlockStats> action) { - pool.execute(() -> action.accept(stalkSkyBlockStats(friend))); - } - - private static HySkyBlockStats stalkSkyBlockStats(Friend friend) { - try (BufferedReader reader = makeApiCall(String.format(SKYBLOCK_STATS_URL, MooConfig.moo, UUIDTypeAdapter.fromUUID(friend.getUuid())))) { - if (reader != null) { - return GsonUtils.fromJson(reader, HySkyBlockStats.class); - } - } catch (IOException | JsonSyntaxException e) { - e.printStackTrace(); - } - return null; - } - - public static void fetchPlayerOfflineStatus(Friend stalkedPlayer, ThrowingConsumer<HyPlayerData> action) { - pool.execute(() -> action.accept(stalkOfflinePlayer(stalkedPlayer))); - } - - private static HyPlayerData stalkOfflinePlayer(Friend stalkedPlayer) { - try (BufferedReader reader = makeApiCall(String.format(PLAYER_URL, MooConfig.moo, UUIDTypeAdapter.fromUUID(stalkedPlayer.getUuid())))) { - if (reader != null) { - return GsonUtils.fromJson(reader, HyPlayerData.class); - } - } catch (IOException | JsonSyntaxException e) { - e.printStackTrace(); - } - return null; - } - - private static BufferedReader makeApiCall(String url) throws IOException { - HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); - connection.setConnectTimeout(5000); - connection.setReadTimeout(10000); - connection.addRequestProperty("User-Agent", "Forge Mod " + Cowlection.MODNAME + "/" + Cowlection.VERSION + " (" + Cowlection.GITURL + ")"); - - connection.getResponseCode(); - if (connection.getResponseCode() == HttpStatus.SC_NO_CONTENT) { // http status 204 - return null; - } else { - BufferedReader reader; - InputStream errorStream = connection.getErrorStream(); - if (errorStream != null) { - reader = new BufferedReader(new InputStreamReader(errorStream)); - } else { - reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); - } - return reader; - } - } -} diff --git a/src/main/java/eu/olli/cowlection/util/ChatHelper.java b/src/main/java/eu/olli/cowlection/util/ChatHelper.java deleted file mode 100644 index 54e5739..0000000 --- a/src/main/java/eu/olli/cowlection/util/ChatHelper.java +++ /dev/null @@ -1,82 +0,0 @@ -package eu.olli.cowlection.util; - -import net.minecraft.client.Minecraft; -import net.minecraft.util.ChatComponentText; -import net.minecraft.util.ChatStyle; -import net.minecraft.util.EnumChatFormatting; -import net.minecraft.util.IChatComponent; -import net.minecraftforge.client.event.ClientChatReceivedEvent; -import net.minecraftforge.common.MinecraftForge; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class ChatHelper { - private static final Pattern USELESS_JSON_CONTENT_PATTERN = Pattern.compile("\"[A-Za-z]+\":false,?"); - private static final int DISPLAY_DURATION = 5000; - private final List<IChatComponent> offlineMessages = new ArrayList<>(); - private String[] aboveChatMessage; - private long aboveChatMessageExpiration; - - public ChatHelper() { - } - - public void sendMessage(EnumChatFormatting color, String text) { - sendMessage(new ChatComponentText(text).setChatStyle(new ChatStyle().setColor(color))); - } - - public void sendMessage(IChatComponent chatComponent) { - ClientChatReceivedEvent event = new ClientChatReceivedEvent((byte) 1, chatComponent); - MinecraftForge.EVENT_BUS.post(event); - if (!event.isCanceled()) { - if (Minecraft.getMinecraft().thePlayer == null) { - offlineMessages.add(event.message); - } else { - Minecraft.getMinecraft().thePlayer.addChatMessage(event.message); - } - } - } - - public void sendOfflineMessages() { - if (Minecraft.getMinecraft().thePlayer != null) { - Iterator<IChatComponent> offlineMessages = this.offlineMessages.iterator(); - if (offlineMessages.hasNext()) { - Minecraft.getMinecraft().thePlayer.playSound("random.levelup", 0.4F, 0.8F); - } - while (offlineMessages.hasNext()) { - Minecraft.getMinecraft().thePlayer.addChatMessage(offlineMessages.next()); - offlineMessages.remove(); - } - } - } - - public void sendAboveChatMessage(String... text) { - aboveChatMessage = text; - aboveChatMessageExpiration = Minecraft.getSystemTime() + DISPLAY_DURATION; - } - - public String[] getAboveChatMessage() { - if (aboveChatMessageExpiration < Minecraft.getSystemTime()) { - // message expired - aboveChatMessage = null; - } - return aboveChatMessage; - } - - public String cleanChatComponent(IChatComponent chatComponent) { - String component = IChatComponent.Serializer.componentToJson(chatComponent); - Matcher jsonMatcher = USELESS_JSON_CONTENT_PATTERN.matcher(component); - return jsonMatcher.replaceAll(""); - } - - public void sendShrug(String... args) { - String chatMsg = "\u00AF\\_(\u30C4)_/\u00AF"; // ¯\\_(ツ)_/¯" - if (args.length > 0) { - chatMsg = String.join(" ", args) + " " + chatMsg; - } - Minecraft.getMinecraft().thePlayer.sendChatMessage(chatMsg); - } -} diff --git a/src/main/java/eu/olli/cowlection/util/GsonUtils.java b/src/main/java/eu/olli/cowlection/util/GsonUtils.java deleted file mode 100644 index 030f42e..0000000 --- a/src/main/java/eu/olli/cowlection/util/GsonUtils.java +++ /dev/null @@ -1,94 +0,0 @@ -package eu.olli.cowlection.util; - -import com.google.gson.*; -import com.mojang.util.UUIDTypeAdapter; -import eu.olli.cowlection.data.HyPlayerData; -import net.minecraft.nbt.NBTBase; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.nbt.NBTTagList; -import net.minecraft.nbt.NBTTagString; -import net.minecraftforge.common.util.Constants; - -import java.io.Reader; -import java.lang.reflect.Type; -import java.util.UUID; - -public final class GsonUtils { - private static final Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).registerTypeAdapter(HyPlayerData.class, new HyPlayerDataDeserializer()).create(); - private static final Gson gsonPrettyPrinter = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).setPrettyPrinting().create(); - - private GsonUtils() { - } - - public static <T> T fromJson(String json, Type clazz) { - return gson.fromJson(json, clazz); - } - - public static <T> T fromJson(Reader json, Class<T> clazz) { - return gson.fromJson(json, clazz); - } - - public static String toJson(Object object) { - if (object instanceof NBTBase) { - return gsonPrettyPrinter.toJson(nbtToJson((NBTBase) object)); - } else { - return gson.toJson(object); - } - } - - private static JsonElement nbtToJson(NBTBase nbtElement) { - if (nbtElement instanceof NBTBase.NBTPrimitive) { - NBTBase.NBTPrimitive nbtNumber = (NBTBase.NBTPrimitive) nbtElement; - switch (nbtNumber.getId()) { - case Constants.NBT.TAG_BYTE: - return new JsonPrimitive(nbtNumber.getByte()); - case Constants.NBT.TAG_SHORT: - return new JsonPrimitive(nbtNumber.getShort()); - case Constants.NBT.TAG_INT: - return new JsonPrimitive(nbtNumber.getInt()); - case Constants.NBT.TAG_LONG: - return new JsonPrimitive(nbtNumber.getLong()); - case Constants.NBT.TAG_FLOAT: - return new JsonPrimitive(nbtNumber.getFloat()); - case Constants.NBT.TAG_DOUBLE: - return new JsonPrimitive(nbtNumber.getDouble()); - default: - return new JsonObject(); - } - } else if (nbtElement instanceof NBTTagString) { - return new JsonPrimitive(((NBTTagString) nbtElement).getString()); - } else if (nbtElement instanceof NBTTagList) { - NBTTagList nbtList = (NBTTagList) nbtElement; - JsonArray jsonArray = new JsonArray(); - for (int tagId = 0; tagId < nbtList.tagCount(); tagId++) { - jsonArray.add(nbtToJson(nbtList.get(tagId))); - } - return jsonArray; - } else if (nbtElement instanceof NBTTagCompound) { - NBTTagCompound nbtCompound = (NBTTagCompound) nbtElement; - JsonObject jsonObject = new JsonObject(); - for (String nbtEntry : nbtCompound.getKeySet()) { - jsonObject.add(nbtEntry, nbtToJson(nbtCompound.getTag(nbtEntry))); - } - return jsonObject; - } - return new JsonObject(); - } - - public static class HyPlayerDataDeserializer implements JsonDeserializer<HyPlayerData> { - @Override - public HyPlayerData deserialize(JsonElement json, Type type, JsonDeserializationContext jdc) throws JsonParseException { - if (!json.getAsJsonObject().get("success").getAsBoolean()) { - // status: failed - return null; - } - JsonElement player = json.getAsJsonObject().get("player"); - HyPlayerData hyPlayerData = gsonPrettyPrinter.fromJson(player, HyPlayerData.class); - if (hyPlayerData == null) { - // player hasn't played Hypixel before - return new HyPlayerData(); - } - return hyPlayerData; - } - } -} diff --git a/src/main/java/eu/olli/cowlection/util/ImageUtils.java b/src/main/java/eu/olli/cowlection/util/ImageUtils.java deleted file mode 100644 index 0af68f4..0000000 --- a/src/main/java/eu/olli/cowlection/util/ImageUtils.java +++ /dev/null @@ -1,64 +0,0 @@ -package eu.olli.cowlection.util; - -import com.mojang.authlib.minecraft.MinecraftProfileTexture; -import net.minecraft.client.Minecraft; -import net.minecraft.client.renderer.ThreadDownloadImageData; -import net.minecraft.util.ResourceLocation; -import net.minecraftforge.fml.relauncher.ReflectionHelper; - -import java.awt.image.BufferedImage; - -public class ImageUtils { - public static int getTierFromTexture(String minionSkinId) { - String textureUrl = "http://textures.minecraft.net/texture/" + minionSkinId; - MinecraftProfileTexture minionSkinTextureDetails = new MinecraftProfileTexture(textureUrl, null); - - ResourceLocation minionSkinLocation = Minecraft.getMinecraft().getSkinManager().loadSkin(minionSkinTextureDetails, MinecraftProfileTexture.Type.SKIN); - - ThreadDownloadImageData minionSkinTexture = (ThreadDownloadImageData) Minecraft.getMinecraft().getTextureManager().getTexture(minionSkinLocation); - BufferedImage minionSkinImage = ReflectionHelper.getPrivateValue(ThreadDownloadImageData.class, minionSkinTexture, "bufferedImage", "field_110560_d"); - - // extract relevant part of the minion tier badge (center 2x1 pixel) - BufferedImage minionSkinTierBadge = minionSkinImage.getSubimage(43, 3, 2, 1); - - return MinionTier.getByColors(minionSkinTierBadge.getRGB(0, 0), minionSkinTierBadge.getRGB(1, 0)).getTier(); - } - - private enum MinionTier { - UNKNOWN(-1, -1), - I(0, 0), - II(-2949295, -10566655), - III(-1245259, -10566655), - IV(-8922850, -983608), - V(-8110849, -11790679), - VI(-4681729, -11790679), - VII(-9486653, -3033345), - VIII(-907953, -7208930), - IX(-31330, -7208930), - X(-5046235, -20031), - XI(-15426142, -1769477); - - private final int color1; - private final int color2; - - MinionTier(int color1, int color2) { - this.color1 = color1; - this.color2 = color2; - } - - private static MinionTier getByColors(int color1, int color2) { - MinionTier[] tiers = values(); - for (int i = 1; i < tiers.length; i++) { - MinionTier minionTier = tiers[i]; - if (minionTier.color1 == color1 && minionTier.color2 == color2) { - return minionTier; - } - } - return UNKNOWN; - } - - private int getTier() { - return ordinal(); - } - } -} diff --git a/src/main/java/eu/olli/cowlection/util/MooChatComponent.java b/src/main/java/eu/olli/cowlection/util/MooChatComponent.java deleted file mode 100644 index 0c6a141..0000000 --- a/src/main/java/eu/olli/cowlection/util/MooChatComponent.java +++ /dev/null @@ -1,186 +0,0 @@ -package eu.olli.cowlection.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/cowlection/util/TickDelay.java b/src/main/java/eu/olli/cowlection/util/TickDelay.java deleted file mode 100644 index 9692ce7..0000000 --- a/src/main/java/eu/olli/cowlection/util/TickDelay.java +++ /dev/null @@ -1,29 +0,0 @@ -package eu.olli.cowlection.util; - -import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; -import net.minecraftforge.fml.common.gameevent.TickEvent; - -public class TickDelay { - private Runnable task; - private int waitingTicks; - - public TickDelay(Runnable task, int ticks) { - this.task = task; - this.waitingTicks = ticks; - - MinecraftForge.EVENT_BUS.register(this); - } - - @SubscribeEvent - public void onTick(TickEvent.ClientTickEvent e) { - if (e.phase == TickEvent.Phase.START) { - if (waitingTicks < 1) { - // we're done waiting! Do stuff and exit. - task.run(); - MinecraftForge.EVENT_BUS.unregister(this); - } - waitingTicks--; - } - } -} diff --git a/src/main/java/eu/olli/cowlection/util/Utils.java b/src/main/java/eu/olli/cowlection/util/Utils.java deleted file mode 100644 index 629a3dd..0000000 --- a/src/main/java/eu/olli/cowlection/util/Utils.java +++ /dev/null @@ -1,253 +0,0 @@ -package eu.olli.cowlection.util; - -import com.mojang.realmsclient.util.Pair; -import net.minecraft.util.EnumChatFormatting; -import org.apache.commons.lang3.text.WordUtils; -import org.apache.commons.lang3.time.DateFormatUtils; -import org.apache.commons.lang3.time.DurationFormatUtils; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Path; -import java.text.DecimalFormat; -import java.util.concurrent.TimeUnit; -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() { - } - - public static boolean isValidUuid(String uuid) { - return VALID_UUID_PATTERN.matcher(uuid).matches(); - } - - public static boolean isValidMcName(String username) { - return VALID_USERNAME.matcher(username).matches(); - } - - public static String fancyCase(String string) { - return WordUtils.capitalizeFully(string.replace('_', ' ')); - } - - /** - * Turn timestamp into pretty-formatted duration and date details. - * - * @param timestamp last login/logout - * @return 1st: duration between timestamp and now in words; 2nd: formatted date if time differences is >24h, otherwise null - */ - public static Pair<String, String> getDurationAsWords(long timestamp) { - long duration = System.currentTimeMillis() - timestamp; - long daysPast = TimeUnit.MILLISECONDS.toDays(duration); - - String dateFormatted = null; - if (daysPast > 1) { - dateFormatted = DateFormatUtils.format(timestamp, "dd-MMM-yyyy"); - } - - if (daysPast > 31) { - return Pair.of( - DurationFormatUtils.formatPeriod(timestamp, System.currentTimeMillis(), (daysPast > 365 ? "y 'years' " : "") + "M 'months' d 'days'"), - dateFormatted); - } else { - return Pair.of( - DurationFormatUtils.formatDurationWords(duration, true, true), - dateFormatted); - } - } - - public static String getDurationAsWord(long timestamp) { - long duration = System.currentTimeMillis() - timestamp; - long secondsPast = TimeUnit.MILLISECONDS.toSeconds(duration); - if (secondsPast < 60) { - return secondsPast + " second" + (secondsPast > 1 ? "s" : ""); - } - long minutesPast = TimeUnit.SECONDS.toMinutes(secondsPast); - if (minutesPast < 60) { - return minutesPast + " minute" + (minutesPast > 1 ? "s" : ""); - } - long hoursPast = TimeUnit.MINUTES.toHours(minutesPast); - if (hoursPast < 24) { - return hoursPast + " hour" + (hoursPast > 1 ? "s" : ""); - } - long daysPast = TimeUnit.HOURS.toDays(hoursPast); - if (daysPast < 31) { - return daysPast + " day" + (daysPast > 1 ? "s" : ""); - } - double monthsPast = daysPast / 30.5d; - if (monthsPast < 12) { - return new DecimalFormat("0.#").format(monthsPast) + " month" + (monthsPast >= 2 ? "s" : ""); - } - double yearsPast = monthsPast / 12d; - return new DecimalFormat("0.#").format(yearsPast) + " year" + (yearsPast >= 2 ? "s" : ""); - } - - public static String toRealPath(Path path) { - try { - return path.toRealPath().toString(); - } catch (IOException e) { - e.printStackTrace(); - return "file not found"; - } - } - - public static String toRealPath(File path) { - return toRealPath(path.toPath()); - } - - /** - * 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 <a href="https://stackoverflow.com/a/4753866">Source</a> - */ - 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); - } - - /** - * Convert Roman numerals to their corresponding Arabic numeral - * - * @param roman Roman numeral - * @return Arabic numeral - * @see <a href="https://www.w3resource.com/javascript-exercises/javascript-math-exercise-22.php">Source</a> - */ - public static int convertRomanToArabic(String roman) { - if (roman == null) return -1; - int number = romanCharToArabic(roman.charAt(0)); - - for (int i = 1; i < roman.length(); i++) { - int current = romanCharToArabic(roman.charAt(i)); - int previous = romanCharToArabic(roman.charAt(i - 1)); - if (current <= previous) { - number += current; - } else { - number = number - previous * 2 + current; - } - } - return number; - } - - private static int romanCharToArabic(char c) { - switch (c) { - case 'I': - return 1; - case 'V': - return 5; - case 'X': - return 10; - case 'L': - return 50; - case 'C': - return 100; - case 'D': - return 500; - case 'M': - return 1000; - default: - return -1; - } - } - - /** - * Convert Arabic numerals to their corresponding Roman numerals - * - * @param number Arabic numerals - * @return Roman numerals - * @see <a href="https://stackoverflow.com/a/48357180">Source</a> - */ - public static String convertArabicToRoman(int number) { - if (number == 0) { - return "0"; - } - String romanOnes = arabicToRomanChars(number % 10, "I", "V", "X"); - number /= 10; - - String romanTens = arabicToRomanChars(number % 10, "X", "L", "C"); - number /= 10; - - String romanHundreds = arabicToRomanChars(number % 10, "C", "D", "M"); - number /= 10; - - String romanThousands = arabicToRomanChars(number % 10, "M", "", ""); - - return romanThousands + romanHundreds + romanTens + romanOnes; - } - - private static String arabicToRomanChars(int n, String one, String five, String ten) { - switch (n) { - case 1: - return one; - case 2: - return one + one; - case 3: - return one + one + one; - case 4: - return one + five; - case 5: - return five; - case 6: - return five + one; - case 7: - return five + one + one; - case 8: - return five + one + one + one; - case 9: - return one + ten; - } - return ""; - } - - /** - * Get the minion tier's color for chat formatting - * - * @param tier minion tier - * @return color code corresponding to the tier - */ - public static EnumChatFormatting getMinionTierColor(int tier) { - EnumChatFormatting tierColor; - switch (tier) { - case 1: - tierColor = EnumChatFormatting.WHITE; - break; - case 2: - case 3: - case 4: - tierColor = EnumChatFormatting.GREEN; - break; - case 5: - case 6: - case 7: - tierColor = EnumChatFormatting.DARK_PURPLE; - break; - case 8: - case 9: - case 10: - tierColor = EnumChatFormatting.RED; - break; - case 11: - tierColor = EnumChatFormatting.AQUA; - break; - default: - tierColor = EnumChatFormatting.OBFUSCATED; - } - return tierColor; - } -} diff --git a/src/main/java/eu/olli/cowlection/util/VersionChecker.java b/src/main/java/eu/olli/cowlection/util/VersionChecker.java deleted file mode 100644 index b1a9d48..0000000 --- a/src/main/java/eu/olli/cowlection/util/VersionChecker.java +++ /dev/null @@ -1,146 +0,0 @@ -package eu.olli.cowlection.util; - -import eu.olli.cowlection.Cowlection; -import eu.olli.cowlection.config.MooConfig; -import net.minecraft.client.Minecraft; -import net.minecraft.event.ClickEvent; -import net.minecraft.event.HoverEvent; -import net.minecraft.util.ChatComponentText; -import net.minecraft.util.ChatStyle; -import net.minecraft.util.EnumChatFormatting; -import net.minecraft.util.IChatComponent; -import net.minecraftforge.common.ForgeModContainer; -import net.minecraftforge.common.ForgeVersion; -import net.minecraftforge.fml.common.Loader; - -import java.util.concurrent.TimeUnit; - -/** - * @see ForgeVersion - */ -public class VersionChecker { - /** - * Cooldown between to update checks in minutes - */ - private static final int CHECK_COOLDOWN = 15; - private static final String CHANGELOG_URL = Cowlection.GITURL + "blob/master/CHANGELOG.md"; - private final Cowlection main; - private long lastCheck; - private String newVersion; - private String downloadUrl; - - public VersionChecker(Cowlection main) { - this.main = main; - this.lastCheck = Minecraft.getSystemTime(); - newVersion = "[newVersion]"; - downloadUrl = Cowlection.GITURL + "releases"; - } - - public boolean runUpdateCheck(boolean isCommandTriggered) { - if (isCommandTriggered || (!ForgeModContainer.disableVersionCheck && MooConfig.doUpdateCheck)) { - Runnable handleResults = () -> main.getVersionChecker().handleVersionStatus(isCommandTriggered); - - long now = Minecraft.getSystemTime(); - - // only re-run if last check was >CHECK_COOLDOWN minutes ago - if (getNextCheck() < 0) { // next allowed check is "in the past", so we're good to go - lastCheck = now; - ForgeVersion.startVersionCheck(); - - // check status after 5 seconds - hopefully that's enough to check - new TickDelay(handleResults, 5 * 20); - return true; - } else { - new TickDelay(handleResults, 1); - } - } - return false; - } - - public void handleVersionStatus(boolean isCommandTriggered) { - ForgeVersion.CheckResult versionResult = ForgeVersion.getResult(Loader.instance().activeModContainer()); - if (versionResult.target != null) { - newVersion = versionResult.target.toString(); - downloadUrl = Cowlection.GITURL + "releases/download/v" + newVersion + "/" + Cowlection.MODNAME.replace(" ", "") + "-" + newVersion + ".jar"; - } - - IChatComponent statusMsg = null; - - if (isCommandTriggered) { - if (versionResult.status == ForgeVersion.Status.UP_TO_DATE) { - // up to date - statusMsg = new ChatComponentText("\u2714 You're running the latest version (" + Cowlection.VERSION + ").").setChatStyle(new ChatStyle().setColor(EnumChatFormatting.GREEN)); - } else if (versionResult.status == ForgeVersion.Status.PENDING) { - // pending - statusMsg = new ChatComponentText("\u279C " + "Version check either failed or is still running.").setChatStyle(new ChatStyle().setColor(EnumChatFormatting.YELLOW)) - .appendSibling(new ChatComponentText("\n \u278A Check for results again in a few seconds with " + EnumChatFormatting.GOLD + "/moo version").setChatStyle(new ChatStyle() - .setColor(EnumChatFormatting.YELLOW) - .setChatClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/moo version")) - .setChatHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ChatComponentText(EnumChatFormatting.YELLOW + "Run " + EnumChatFormatting.GOLD + "/moo version"))))) - .appendSibling(new ChatComponentText("\n \u278B Re-run update check with " + EnumChatFormatting.GOLD + "/moo update").setChatStyle(new ChatStyle() - .setColor(EnumChatFormatting.YELLOW) - .setChatClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/moo update")) - .setChatHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ChatComponentText(EnumChatFormatting.YELLOW + "Run " + EnumChatFormatting.GOLD + "/moo update"))))); - } else if (versionResult.status == ForgeVersion.Status.FAILED) { - // check failed - statusMsg = new ChatComponentText("\u2716 Version check failed for an unknown reason. Check again in a few seconds with ").setChatStyle(new ChatStyle().setColor(EnumChatFormatting.RED)) - .appendSibling(new ChatComponentText("/moo update").setChatStyle(new ChatStyle() - .setColor(EnumChatFormatting.GOLD) - .setChatClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/moo update")) - .setChatHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ChatComponentText(EnumChatFormatting.YELLOW + "Run " + EnumChatFormatting.GOLD + "/moo update"))))); - } - } - if (versionResult.status == ForgeVersion.Status.OUTDATED || versionResult.status == ForgeVersion.Status.BETA_OUTDATED) { - // outdated - IChatComponent spacer = new ChatComponentText(" ").setChatStyle(new ChatStyle().setParentStyle(null)); - - IChatComponent text = new ChatComponentText("\u279C New version of " + EnumChatFormatting.DARK_GREEN + Cowlection.MODNAME + " " + EnumChatFormatting.GREEN + "available (" + Cowlection.VERSION + " \u27A1 " + newVersion + ")\n").setChatStyle(new ChatStyle().setColor(EnumChatFormatting.GREEN)); - - IChatComponent download = new ChatComponentText("[Download]").setChatStyle(new ChatStyle() - .setColor(EnumChatFormatting.DARK_GREEN).setBold(true) - .setChatClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, downloadUrl)) - .setChatHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ChatComponentText(EnumChatFormatting.YELLOW + "Download the latest version of " + Cowlection.MODNAME)))); - - IChatComponent changelog = new ChatComponentText("[Changelog]").setChatStyle(new ChatStyle() - .setColor(EnumChatFormatting.DARK_AQUA).setBold(true) - .setChatClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, CHANGELOG_URL)) - .setChatHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ChatComponentText(EnumChatFormatting.YELLOW + "View changelog")))); - - IChatComponent updateInstructions = new ChatComponentText("[Update instructions]").setChatStyle(new ChatStyle() - .setColor(EnumChatFormatting.GOLD).setBold(true) - .setChatClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/moo updateHelp")) - .setChatHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ChatComponentText(EnumChatFormatting.YELLOW + "Run " + EnumChatFormatting.GOLD + "/moo updateHelp")))); - - IChatComponent openModsDirectory = new ChatComponentText("\n[Open Mods directory]").setChatStyle(new ChatStyle() - .setColor(EnumChatFormatting.GREEN).setBold(true) - .setChatClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/moo directory")) - .setChatHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ChatComponentText(EnumChatFormatting.YELLOW + "Open mods directory with command " + EnumChatFormatting.GOLD + "/moo directory\n\u279C Click to open mods directory")))); - - statusMsg = text.appendSibling(download).appendSibling(spacer).appendSibling(changelog).appendSibling(spacer).appendSibling(updateInstructions).appendSibling(spacer).appendSibling(openModsDirectory); - } - - if (statusMsg != null) { - if (isCommandTriggered) { - main.getChatHelper().sendMessage(statusMsg); - } else { - IChatComponent finalStatusMsg = statusMsg; - new TickDelay(() -> main.getChatHelper().sendMessage(finalStatusMsg) - , 6 * 20); - } - } - } - - public long getNextCheck() { - long cooldown = TimeUnit.MINUTES.toMillis(CHECK_COOLDOWN); - long systemTime = Minecraft.getSystemTime(); - return cooldown - (systemTime - lastCheck); - } - - public String getNewVersion() { - return newVersion; - } - - public String getDownloadUrl() { - return downloadUrl; - } -} |