aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/eu/olli/cowmoonication/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/eu/olli/cowmoonication/util')
-rw-r--r--src/main/java/eu/olli/cowmoonication/util/ImageUtils.java76
-rw-r--r--src/main/java/eu/olli/cowmoonication/util/Utils.java127
2 files changed, 203 insertions, 0 deletions
diff --git a/src/main/java/eu/olli/cowmoonication/util/ImageUtils.java b/src/main/java/eu/olli/cowmoonication/util/ImageUtils.java
new file mode 100644
index 0000000..cd224e8
--- /dev/null
+++ b/src/main/java/eu/olli/cowmoonication/util/ImageUtils.java
@@ -0,0 +1,76 @@
+package eu.olli.cowmoonication.util;
+
+import com.mojang.authlib.minecraft.MinecraftProfileTexture;
+import eu.olli.cowmoonication.Cowmoonication;
+import net.minecraft.client.Minecraft;
+import net.minecraft.client.renderer.ThreadDownloadImageData;
+import net.minecraft.util.ResourceLocation;
+import net.minecraftforge.fml.relauncher.ReflectionHelper;
+
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+import java.io.InputStream;
+
+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 part of the minion tier badge (center 2x2 pixel)
+ BufferedImage minionSkinTierBadge = minionSkinImage.getSubimage(43, 3, 2, 2);
+
+ // reference image for tier badges: each tier is 2x2 pixel
+ ResourceLocation tierBadgesLocation = new ResourceLocation(Cowmoonication.MODID, "minion-tier-badges.png");
+
+ try (InputStream tierBadgesStream = Minecraft.getMinecraft().getResourceManager().getResource(tierBadgesLocation).getInputStream()) {
+ BufferedImage tierBadges = ImageIO.read(tierBadgesStream);
+
+ final int maxTier = 11;
+ for (int tier = 0; tier < maxTier; tier++) {
+ BufferedImage tierBadgeRaw = tierBadges.getSubimage(tier * 2, 0, 2, 2);
+ if (ImageUtils.areEquals(minionSkinTierBadge, tierBadgeRaw)) {
+ return tier + 1;
+ }
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ return -1;
+ }
+ return -5;
+ }
+
+ /**
+ * Compares two images pixel by pixel
+ *
+ * @param imageA the first image
+ * @param imageB the second image
+ * @return whether the images are both the same or not
+ * @see <a href="https://stackoverflow.com/a/29886786">Source</a>
+ */
+ private static boolean areEquals(BufferedImage imageA, BufferedImage imageB) {
+ // images must be the same size
+ if (imageA.getWidth() != imageB.getWidth() || imageA.getHeight() != imageB.getHeight()) {
+ return false;
+ }
+
+ int width = imageA.getWidth();
+ int height = imageB.getHeight();
+
+ // loop over every pixel
+ for (int y = 0; y < height; y++) {
+ for (int x = 0; x < width; x++) {
+ // compare the pixels for equality
+ if (imageA.getRGB(x, y) != imageB.getRGB(x, y)) {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+}
diff --git a/src/main/java/eu/olli/cowmoonication/util/Utils.java b/src/main/java/eu/olli/cowmoonication/util/Utils.java
index ec355b9..8eb08a2 100644
--- a/src/main/java/eu/olli/cowmoonication/util/Utils.java
+++ b/src/main/java/eu/olli/cowmoonication/util/Utils.java
@@ -1,6 +1,7 @@
package eu.olli.cowmoonication.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;
@@ -77,4 +78,130 @@ public final class Utils {
) + "" + 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) {
+ 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;
+ }
}