package com.minelittlepony.bigpony.mod.mixin;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import com.minelittlepony.bigpony.mod.LiteModBigPony;

import net.minecraft.client.renderer.entity.Render;
import net.minecraft.entity.Entity;

@Mixin(Render.class)
public abstract class MixinRender<T extends Entity> {
  
  @Shadow
  protected float shadowSize;
  protected float rawShadowSize = -1;
  
  // Redirect can't capture locals (wah-wah-waaaah)
  @Inject(method = "renderShadow(Lnet/minecraft/entity/Entity;DDDFF)V",
          at = @At("HEAD"))
  private void onBeforeRenderShadow(Entity entity, double x, double y, double z, float scale, float ticks, CallbackInfo info) {
      rawShadowSize = shadowSize;
      shadowSize = LiteModBigPony.instance().getUpdatedShadowSize(rawShadowSize, entity);
  }
  
  @Inject(method = "renderShadow(Lnet/minecraft/entity/Entity;DDDFF)V",
          at = @At("RETURN"))
  private void onAfterRenderShadow(Entity entity, double x, double y, double z, float scale, float ticks, CallbackInfo info) {
      shadowSize = rawShadowSize;
  }
}

@@ -223,82 +223,16 @@
 ion.
-Inject;%0Aimport org.spongepowered.asm.mixin.injection.callback.CallbackInfo
+Redirect
 ;%0A%0Ai
@@ -285,16 +285,69 @@
 gPony;%0A%0A
+import net.minecraft.client.renderer.GlStateManager;%0A
 import n
@@ -551,105 +551,147 @@
 ;%0A  
-protected float rawShadowSize = -1;%0A  %0A  // Redirect can't capture locals (wah-wah-waaaah)%0A  @Inj
+%0A  @Shadow%0A  private void renderShadow(Entity entityIn, double x, double y, double z, float shadowAlpha, float partialTicks) %7B%7D%0A  %0A  @Redir
 ect(
@@ -692,33 +692,35 @@
 irect(method = %22
-r
+doR
 enderShadow(Lnet
@@ -706,32 +706,39 @@
  %22doRenderShadow
+AndFire
 (Lnet/minecraft/
@@ -763,32 +763,33 @@
 F)V%22,%0A          
+ 
 at = @At(%22HEAD%22)
@@ -785,13 +785,150 @@
 @At(
-%22HEAD
+value = %22INVOKE%22,%0A                    target = %22Lnet/minecraft/client/renderer/entity/Render;renderShadow(Lnet/minecraft/entity/Entity;DDDFF)V
 %22))%0A
@@ -946,16 +946,16 @@
 oid 
-onBefore
+redirect
 Rend
@@ -955,32 +955,52 @@
 ectRenderShadow(
+Render%3CT%3E instance, 
 Entity entity, d
@@ -1026,37 +1026,39 @@
 double z, float 
-scale
+opacity
 , float ticks, C
@@ -1058,36 +1058,222 @@
 icks
-, CallbackInfo info) %7B%0A     
+) %7B%0A      GlStateManager.pushMatrix();%0A      // The shadows render a tiny bit off the ground, so let's shift it down so as not to cut through people's legs.%0A      GlStateManager.translate(0, -0.01F, 0);%0A      float
  raw
@@ -1393,274 +1393,125 @@
 ;%0A  
-%7D%0A
   
-%0A
   
-@Inject(method = %22renderShadow(Lnet/minecraft/entity/Entity;DDDFF)V%22,%0A          at = @At(%22RETURN%22))%0A  private void onAfterRenderShadow(Entity entity, double x, double y, double z, float scale, float ticks, CallbackInfo info) %7B%0A      shadowSize = rawShadowSize
+renderShadow(entity, x, y, z, opacity, ticks);%0A      shadowSize = rawShadowSize;%0A      GlStateManager.popMatrix()
 ;%0A  

Combine before/after shadow and translate the shadow so it doesn't cut our knees