package info.u_team.u_team_core.gui.render;

import com.mojang.blaze3d.systems.RenderSystem;

import info.u_team.u_team_core.util.RGBA;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.*;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.inventory.container.PlayerContainer;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fluids.FluidStack;

public class FluidInventoryRender {
	
	private static final ResourceLocation ATLAS = PlayerContainer.LOCATION_BLOCKS_TEXTURE;
	
	public void drawFluid(int x, int y, FluidStack stack) {
		if (stack == null || stack.isEmpty()) {
			return;
		}
		
		final TextureAtlasSprite sprite = getFluidSprite(stack);
		Minecraft.getInstance().getTextureManager().bindTexture(ATLAS);
		
		RenderSystem.enableBlend();
		RenderSystem.enableAlphaTest();
		
		final RGBA rgba = RGBA.fromARGB(stack.getFluid().getAttributes().getColor(stack));
		RenderSystem.color4f(rgba.getRedComponent(), rgba.getGreenComponent(), rgba.getBlueComponent(), rgba.getAlphaComponent());
		
		drawTextureWithMasking(x, y, sprite, 0, 0, 100);
		
		RenderSystem.color4f(1, 1, 1, 1);
		
		RenderSystem.disableAlphaTest();
		RenderSystem.disableBlend();
	}
	
	private TextureAtlasSprite getFluidSprite(FluidStack stack) {
		return Minecraft.getInstance().getAtlasSpriteGetter(ATLAS).apply(stack.getFluid().getAttributes().getStillTexture(stack));
	}
	
	// From JEI by mezz (MIT)
	// https://github.com/mezz/JustEnoughItems/blob/e2eba79a2065436ad7625fdb3ab67cb275fe78b0/src/main/java/mezz/jei/plugins/vanilla/ingredients/fluid/FluidStackRenderer.java#L151-L167
	private void drawTextureWithMasking(double xCoord, double yCoord, TextureAtlasSprite textureSprite, int maskTop, int maskRight, double zLevel) {
		double uMin = textureSprite.getMinU();
		double uMax = textureSprite.getMaxU();
		double vMin = textureSprite.getMinV();
		double vMax = textureSprite.getMaxV();
		uMax = uMax - (maskRight / 16.0 * (uMax - uMin));
		vMax = vMax - (maskTop / 16.0 * (vMax - vMin));
		
		Tessellator tessellator = Tessellator.getInstance();
		BufferBuilder bufferBuilder = tessellator.getBuffer();
		bufferBuilder.begin(7, DefaultVertexFormats.POSITION_TEX);
		bufferBuilder.pos(xCoord, yCoord + 16, zLevel).tex((float) uMin, (float) vMax).endVertex();
		bufferBuilder.pos(xCoord + 16 - maskRight, yCoord + 16, zLevel).tex((float) uMax, (float) vMax).endVertex();
		bufferBuilder.pos(xCoord + 16 - maskRight, yCoord + maskTop, zLevel).tex((float) uMax, (float) vMin).endVertex();
		bufferBuilder.pos(xCoord, yCoord + maskTop, zLevel).tex((float) uMin, (float) vMin).endVertex();
		tessellator.draw();
	}
	
}

@@ -200,18 +200,23 @@
 ent.
-renderer.*
+gui.AbstractGui
 ;%0Aim
@@ -282,74 +282,8 @@
 te;%0A
-import net.minecraft.client.renderer.vertex.DefaultVertexFormats;%0A
 impo
@@ -460,16 +460,36 @@
 yRender 
+extends AbstractGui 
 %7B%0A%09%0A%09pri
@@ -1105,54 +1105,38 @@
 %09%0A%09%09
-drawTextureWithMasking(x, y, sprite, 0, 0, 100
+blit(x, y, 100, 16, 16, sprite
 );%0A%09
@@ -1439,1254 +1439,9 @@
 ));%0A
+
 %09%7D%0A
-%09%0A%09// From JEI by mezz (MIT)%0A%09// https://github.com/mezz/JustEnoughItems/blob/e2eba79a2065436ad7625fdb3ab67cb275fe78b0/src/main/java/mezz/jei/plugins/vanilla/ingredients/fluid/FluidStackRenderer.java#L151-L167%0A%09private void drawTextureWithMasking(double xCoord, double yCoord, TextureAtlasSprite textureSprite, int maskTop, int maskRight, double zLevel) %7B%0A%09%09double uMin = textureSprite.getMinU();%0A%09%09double uMax = textureSprite.getMaxU();%0A%09%09double vMin = textureSprite.getMinV();%0A%09%09double vMax = textureSprite.getMaxV();%0A%09%09uMax = uMax - (maskRight / 16.0 * (uMax - uMin));%0A%09%09vMax = vMax - (maskTop / 16.0 * (vMax - vMin));%0A%09%09%0A%09%09Tessellator tessellator = Tessellator.getInstance();%0A%09%09BufferBuilder bufferBuilder = tessellator.getBuffer();%0A%09%09bufferBuilder.begin(7, DefaultVertexFormats.POSITION_TEX);%0A%09%09bufferBuilder.pos(xCoord, yCoord + 16, zLevel).tex((float) uMin, (float) vMax).endVertex();%0A%09%09bufferBuilder.pos(xCoord + 16 - maskRight, yCoord + 16, zLevel).tex((float) uMax, (float) vMax).endVertex();%0A%09%09bufferBuilder.pos(xCoord + 16 - maskRight, yCoord + maskTop, zLevel).tex((float) uMax, (float) vMin).endVertex();%0A%09%09bufferBuilder.pos(xCoord, yCoord + maskTop, zLevel).tex((float) uMin, (float) vMin).endVertex();%0A%09%09tessellator.draw();%0A%09%7D%0A%09%0A
 %7D%0A

Remove method from jei as its already in a blit method