package com.voxelmodpack.hdskins.resource;

import com.voxelmodpack.hdskins.DynamicTextureImage;
import com.voxelmodpack.hdskins.ImageBufferDownloadHD;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.TextureUtil;
import net.minecraft.util.ResourceLocation;
import org.apache.commons.io.IOUtils;

import javax.annotation.Nullable;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.function.Supplier;

public class ImageLoader implements Supplier<ResourceLocation> {

    private Minecraft mc = Minecraft.getMinecraft();

    private final ResourceLocation original;

    public ImageLoader(ResourceLocation loc) {
        this.original = loc;
    }

    @Override
    @Nullable
    public ResourceLocation get() {
        BufferedImage image = getImage(original);
        final BufferedImage updated = new ImageBufferDownloadHD().parseUserSkin(image);
        if (updated == null) {
            return null;
        }
        if (updated == image) {
            // don't load a new image
            return this.original;
        }
        return this.mc.addScheduledTask(() -> loadSkin(updated)).get();
    }

    @Nullable
    private static BufferedImage getImage(ResourceLocation res) {
        try {
            InputStream in = Minecraft.getMinecraft().getResourceManager().getResource(res).getInputStream();
            try {
                return TextureUtil.readBufferedImage(in);
            } finally {
                IOUtils.closeQuietly(in);
            }
        } catch (IOException e) {
            return null;
        }
    }

    @Nullable
    private ResourceLocation loadSkin(BufferedImage image) {

        ResourceLocation conv = new ResourceLocation(original.getResourceDomain() + "-converted", original.getResourcePath());
        boolean success= this.mc.getTextureManager().loadTexture(conv, new DynamicTextureImage(image));
        return success ? conv : null;
    }

}

@@ -289,46 +289,8 @@
 ion;
-%0Aimport org.apache.commons.io.IOUtils;
 %0A%0Aim
@@ -414,16 +414,102 @@
 Stream;%0A
+import java.util.concurrent.Callable;%0Aimport java.util.concurrent.ExecutionException;%0A
 import j
@@ -615,16 +615,23 @@
 private 
+static 
 Minecraf
@@ -1194,64 +1194,304 @@
 urn 
-this.mc.addScheduledTask(() -%3E loadSkin(updated)).get();
+addTaskAndGet(() -%3E loadSkin(updated));%0A    %7D%0A%0A    private static %3CV%3E V addTaskAndGet(Callable%3CV%3E callable) %7B%0A        try %7B%0A            return mc.addScheduledTask(callable).get();%0A        %7D catch (InterruptedException %7C ExecutionException e) %7B%0A            throw new RuntimeException(e);%0A        %7D
 %0A   
@@ -1570,24 +1570,25 @@
 tion res) %7B%0A
+%0A
         try 
@@ -1587,30 +1587,17 @@
     try 
-%7B%0A            
+(
 InputStr
@@ -1609,32 +1609,10 @@
 n = 
-Minecraft.getMinecraft()
+mc
 .get
@@ -1666,32 +1666,12 @@
 am()
-;%0A            try %7B%0A    
+) %7B%0A
     
@@ -1724,88 +1724,8 @@
 n);%0A
-            %7D finally %7B%0A                IOUtils.closeQuietly(in);%0A            %7D%0A
     
@@ -2026,15 +2026,11 @@
 cess
+ 
 = 
-this.
 mc.g

Fix build error