package com.voxelmodpack.hdskins.mixin;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.At.Shift;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Surrogate;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import com.voxelmodpack.hdskins.HDSkinManager;

import net.minecraft.client.renderer.IImageBuffer;
import net.minecraft.client.renderer.ImageBufferDownload;

@Mixin(ImageBufferDownload.class)
public abstract class MixinImageBufferDownload implements IImageBuffer {

    @Inject(
            method = "parseUserSkin",
            locals = LocalCapture.CAPTURE_FAILSOFT,
            at = @At(
                    value = "INVOKE",
                    shift = Shift.BEFORE,
                    target = "Ljava/awt/Graphics;dispose()V",
                    remap = false))
    private void update(BufferedImage image, CallbackInfo ci, BufferedImage image2, Graphics graphics) {
        // convert skins from mojang server
        if (image.getHeight() == 32) {
            HDSkinManager.INSTANCE.convertSkin(image2, graphics);
        }
    }

    // for optifine
    @Surrogate
    private void update(BufferedImage image, CallbackInfo ci, int w, int h, int k, BufferedImage image2, Graphics graphics) {
        // convert skins from mojang server
        if (image.getHeight() == 32) {
            HDSkinManager.INSTANCE.convertSkin(image2, graphics);
        }
    }

}

@@ -804,16 +804,78 @@
 UserSkin
+(Ljava/awt/image/BufferedImage;)Ljava/awt/image/BufferedImage;
 %22,%0A     
@@ -919,12 +919,17 @@
 FAIL
-SOFT
+EXCEPTION
 ,%0A  
@@ -1208,35 +1208,16 @@
 e image2
-, Graphics graphics
 ) %7B%0A    
@@ -1256,36 +1256,51 @@
  server%0A        
-if (
+boolean isLegacy = 
 image.getHeight(
@@ -1298,32 +1298,368 @@
 etHeight() == 32
+;%0A        Graphics graphics = image2.getGraphics();%0A        if (isLegacy) %7B%0A            HDSkinManager.INSTANCE.convertSkin(image2, graphics);%0A        %7D%0A    %7D%0A%0A    // development%0A    @Surrogate%0A    private void update(BufferedImage image, CallbackInfo ci, BufferedImage image2, Graphics graphics, boolean isLegacy) %7B%0A        if (isLegacy
 ) %7B%0A            
@@ -1886,16 +1886,34 @@
 graphics
+, boolean isLegacy
 ) %7B%0A    
@@ -1965,38 +1965,23 @@
    if (i
-mage.getHeight() == 32
+sLegacy
 ) %7B%0A    

Fix a mixin. It worked in development. Maybe fix #11