package com.teamderpy.shouldersurfing.math;

import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import org.lwjgl.opengl.GL11;

import com.mojang.blaze3d.matrix.MatrixStack;

import net.minecraft.client.renderer.GLAllocation;
import net.minecraft.util.math.vector.Matrix4f;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public class VectorConverter
{
	public static Vec2f project2D(Vector3d position, MatrixStack matrixStackIn, Matrix4f projectionIn)
	{
		IntBuffer viewport = GLAllocation.createDirectByteBuffer(64).asIntBuffer();
		FloatBuffer modelview = GLAllocation.createDirectFloatBuffer(16);
		FloatBuffer projection = GLAllocation.createDirectFloatBuffer(16);
		
		modelview.clear();
		projection.clear();
		viewport.clear();
		
		matrixStackIn.getLast().getMatrix().write(modelview);
		projectionIn.write(projection);
		GL11.glGetIntegerv(GL11.GL_VIEWPORT, viewport);
		
		Vec2f screen = VectorConverter.uProject(position, modelview, projection, viewport);
		
		if(screen == null || Float.isInfinite(screen.getX()) || Float.isInfinite(screen.getY()))
		{
			return null;
		}
		
		return screen;
	}
	
	private static void multMatrixVecf(FloatBuffer m, float[] in, float[] out)
	{
		for(int i = 0; i < 4; i++)
		{
			out[i] = in[0] * m.get(m.position() + i) + in[1] * m.get(m.position() + 4 + i) + in[2] * m.get(m.position() + 8 + i) + in[3] * m.get(m.position() + 12 + i);
		}
	}
	
	private static Vec2f uProject(Vector3d position, FloatBuffer modelMatrix, FloatBuffer projMatrix, IntBuffer viewport)
	{
		float[] in = new float[4];
		float[] out = new float[4];
		
		in[0] = (float) position.getX();
		in[1] = (float) position.getY();
		in[2] = (float) position.getZ();
		in[3] = 1.0F;
		
		VectorConverter.multMatrixVecf(modelMatrix, in, out);
		VectorConverter.multMatrixVecf(projMatrix, out, in);
		
		if(in[3] == 0.0F)
		{
			return null;
		}
		
		in[3] = (1.0F / in[3]) * 0.5F;
		
		in[0] = in[0] * in[3] + 0.5F;
		in[1] = in[1] * in[3] + 0.5F;
		in[2] = in[2] * in[3] + 0.5F;
		
		float x = in[0] * viewport.get(viewport.position() + 2) + viewport.get(viewport.position());
		float y = in[1] * viewport.get(viewport.position() + 3) + viewport.get(viewport.position() + 1);
		
		return new Vec2f(x, y);
	}
}

@@ -38,16 +38,40 @@
 .math;%0A%0A
+import java.nio.Buffer;%0A
 import j
@@ -816,16 +816,26 @@
 );%0A%09%09%0A%09%09
+((Buffer) 
 modelvie
@@ -835,16 +835,17 @@
 odelview
+)
 .clear()
@@ -844,24 +844,34 @@
 .clear();%0A%09%09
+((Buffer) 
 projection.c
@@ -868,16 +868,17 @@
 ojection
+)
 .clear()
@@ -881,24 +881,34 @@
 ar();%0A%09%09
+((Buffer) 
 viewport
 .clear()
@@ -899,16 +899,17 @@
 viewport
+)
 .clear()
@@ -2199,32 +2199,42 @@
 iewport.get(
+((Buffer) 
 viewport
 .position() 
@@ -2213,32 +2213,33 @@
 Buffer) viewport
+)
 .position() + 2)
@@ -2250,32 +2250,42 @@
 iewport.get(
+((Buffer) 
 viewport
 .position())
@@ -2264,32 +2264,33 @@
 Buffer) viewport
+)
 .position());%0A%09%09
@@ -2316,32 +2316,42 @@
 iewport.get(
+((Buffer) 
 viewport
 .position() 
@@ -2330,32 +2330,33 @@
 Buffer) viewport
+)
 .position() + 3)
@@ -2371,24 +2371,34 @@
 ort.get(
+((Buffer) 
 viewport
 .positio
@@ -2389,16 +2389,17 @@
 viewport
+)
 .positio

Fix jdk 9 compilation