aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/eu/olli/cowmoonication/util/ImageUtils.java
blob: cd224e8313048c51cfb623949d0da4cd1fa3181a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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;
    }
}