package gvclib.client.event;

import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.platform.Window;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.math.Vector3f;
import gvclib.item.ItemAttachment;
import gvclib.item.ItemGunBase;
import gvclib.mod_GVCLib;
import net.minecraft.client.CameraType;
import net.minecraft.client.Minecraft;
import net.minecraft.client.model.HumanoidModel;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.entity.LivingEntityRenderer;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.RenderHandEvent;
import net.minecraftforge.client.event.RenderLivingEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import objmodel.AdvancedModelLoader;
import org.lwjgl.glfw.GLFWErrorCallback;
//import org.lwjgl.opengl.GL11;
//import org.lwjgl.opengl.GL12;
//import org.lwjgl.opengl.GL;

import org.lwjgl.*;
import org.lwjgl.glfw.*;
//import org.lwjgl.opengl.*;
import org.lwjgl.system.*;

import java.nio.*;

import static org.lwjgl.glfw.Callbacks.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.*;

public class RenderGunEvent {
    @OnlyIn(Dist.CLIENT)
    @SubscribeEvent
    public void rendergun(RenderHandEvent event)
    {
        if (Minecraft.getInstance().options.bobView().get())
        {
            this.applyBobbing(event.getPartialTick());
        }
        Minecraft minecraft = Minecraft.getInstance();
        Player entityplayer = minecraft.player;
        ItemStack itemstack = ((Player) (entityplayer)).getMainHandItem();
        ItemStack itemstackl = ((Player) (entityplayer)).getOffhandItem();
        if (!itemstack.isEmpty() && itemstack.getItem() instanceof ItemGunBase) {//item
            {
                glinit();
                this.rendermain(entityplayer, itemstack, true, event.getPartialTick(), event.getPoseStack(), event.getMultiBufferSource());
            }
        }//item
        if (!itemstackl.isEmpty()  && itemstackl.getItem() instanceof ItemGunBase && itemstackl.hasTag()) {//item
            {
                this.rendermain(entityplayer, itemstackl, false, event.getPartialTick(), event.getPoseStack(), event.getMultiBufferSource());
            }
        }//item

    }

    @OnlyIn(Dist.CLIENT)
    @SubscribeEvent
    public void rendergunthird(RenderLivingEvent.Pre event)
    {
        //20/01/18試作
        Entity entity = event.getEntity();
        if(entity != null) {
            String name = entity.getName().getString();
            LivingEntityRenderer render = event.getRenderer();
            boolean flag = false;
            if ((render instanceof LivingEntityRenderer))
            {
                LivingEntityRenderer renderbase = (LivingEntityRenderer) render;
                if ((renderbase.getModel() instanceof HumanoidModel)) {
                    for(int ii = 0; ii < 512; ++ii) {
                        if(mod_GVCLib.gunlayer_mob_name[ii] != null && mod_GVCLib.gunlayer_mob_name[ii].equals(name)) {
                            flag = true;
                            break;
                        }
                    }
                    if(!flag) {
                        renderbase.addLayer(new LayerGunBase(renderbase));
                        ++mod_GVCLib.gunlayer_mob;
                        mod_GVCLib.gunlayer_mob_name[mod_GVCLib.gunlayer_mob] = name;
                        System.out.println(String.format("road"));
                    }
                }
                /*if(entity instanceof EntityLivingBase) {
                    EntityLivingBase living = (EntityLivingBase)entity;
                    ItemStack itemstack = ((EntityLivingBase) (living)).getHeldItemMainhand();
                    ItemStack itemstackl = ((EntityLivingBase) (living)).getHeldItemOffhand();
                    if (!itemstack.isEmpty() && itemstack.getItem() instanceof ItemGunBase) {//item
                        ItemGunBase gun = (ItemGunBase) itemstack.getItem();
                        gun.ModelLoad();
                    }
                    if (!itemstackl.isEmpty()  && itemstackl.getItem() instanceof ItemGunBase) {//item
                        ItemGunBase gun = (ItemGunBase) itemstackl.getItem();
                        gun.ModelLoad();
                    }
                }*/


            }
        }
    }

    /**
     * Updates the bobbing render effect of the player.
     */
    private void applyBobbing(float partialTicks)
    {
        PoseStack matrixstack = new PoseStack();
        if (Minecraft.getInstance().getCameraEntity() instanceof Player)
        {
            Player playerentity = (Player)Minecraft.getInstance().getCameraEntity();
           /* float f = entityplayer.distanceWalkedModified - entityplayer.prevDistanceWalkedModified;
            float f1 = -(entityplayer.distanceWalkedModified + f * partialTicks);
            float f2 = entityplayer.prevCameraYaw + (entityplayer.cameraYaw - entityplayer.prevCameraYaw) * partialTicks;
            float f3 = entityplayer.prevCameraPitch + (entityplayer.cameraPitch - entityplayer.prevCameraPitch) * partialTicks;
            GlStateManager.translatef(MathHelper.sin(f1 * (float)Math.PI) * f2 * 0.5F, -Math.abs(MathHelper.cos(f1 * (float)Math.PI) * f2), 0.0F);
            GlStateManager.rotatef(MathHelper.sin(f1 * (float)Math.PI) * f2 * 3.0F, 0.0F, 0.0F, 1.0F);
            GlStateManager.rotatef(Math.abs(MathHelper.cos(f1 * (float)Math.PI - 0.2F) * f2) * 5.0F, 1.0F, 0.0F, 0.0F);
            GlStateManager.rotatef(f3, 1.0F, 0.0F, 0.0F);*/
            float f = playerentity.walkDist - playerentity.walkDistO;
            float f1 = -(playerentity.walkDist + f * partialTicks);
            float f2 = Mth.lerp(partialTicks, playerentity.oBob, playerentity.bob);
            matrixstack.translate((double)(Mth.sin(f1 * (float)Math.PI) * f2 * 0.5F), (double)(-Math.abs(Mth.cos(f1 * (float)Math.PI) * f2)), 0.0D);
            matrixstack.mulPose(Vector3f.ZP.rotationDegrees(Mth.sin(f1 * (float)Math.PI) * f2 * 3.0F));
            matrixstack.mulPose(Vector3f.XP.rotationDegrees(Math.abs(Mth.cos(f1 * (float)Math.PI - 0.2F) * f2) * 5.0F));
        }
    }

    private void rendermain(Player entityplayer, ItemStack itemstack, boolean side, float partialTicks, PoseStack poseStack, MultiBufferSource multiBufferSource){
        int xxx = 1;
        if(!side){
            xxx = -1;
        }
        ItemGunBase gun = (ItemGunBase) itemstack.getItem();
        Minecraft minecraft = Minecraft.getInstance();
		/*CompoundNBT nbt = itemstack.getTag();
		boolean cocking = nbt.getBoolean("Cocking");
		int cockingtime = nbt.getInt("CockingTime");
		boolean recoiled = nbt.getBoolean("Recoiled");
		int recoiledtime = nbt.getInt("RecoiledTime");*/
        boolean cocking = false;
        int cockingtime = 0;
        boolean recoiled = false;
        int recoiledtime = 0;

        if(gun.obj_model == null && gun.obj_true) {
            gun.obj_model = AdvancedModelLoader.loadModel(new ResourceLocation(gun.obj_model_resource));
            System.out.println(String.format("model_road"));
        }

        if(minecraft.options.getCameraType() == CameraType.FIRST_PERSON && gun.obj_true && gun.obj_model != null){

            glfwMakeContextCurrent(minecraft.getWindow().getWindow());
            // This line is critical for LWJGL's interoperation with GLFW's
            // OpenGL context, or any context that is managed externally.
            // LWJGL detects the context that is current in the current thread,
            // creates the GLCapabilities instance and makes the OpenGL
            // bindings available for use.
            //       GL.createCapabilities();

//			System.out.println(String.format("model_test"));


            poseStack.pushPose();//GL11.glPushMatrix();//guns
            //GlStateManager.enableLighting();
            poseStack.scale(0.5F, 0.5F, 0.5F);//GL11.glScalef(0.5F, 0.5F, 0.5F);
            poseStack.scale(0.5F, 0.5F, 0.5F);//GL11.glScalef(0.5F, 0.5F, 0.5F);
            //		GL11.glScalef(0.5F, 0.5F, 0.5F);
            //		GL11.glScalef(0.5F, 0.5F, 0.5F);
            //     GL11.glEnable(GL12.GL_RESCALE_NORMAL);
            if(!recoiled) {
                poseStack.translate(0.0F, 0.0F, -1.0F*0.1F);//GL11.glTranslatef(0.0F, 0.0F, -1.0F*0.1F);
            }

            if (gun.isReload(itemstack)) {//1s //reload
                poseStack.translate(gun.model_x * xxx ,gun.model_y, gun.model_z + 1);//GL11.glTranslatef(gun.model_x * xxx ,gun.model_y, gun.model_z + 1);//1.5,-2,-2.5
                poseStack.mulPose(Vector3f.YP.rotationDegrees(180.0F));//GL11.glRotatef(180F, 0.0F, 1.0F, 0.0F);
                poseStack.mulPose(Vector3f.XP.rotationDegrees(30.0F));//GL11.glRotatef(30F, 1.0F, 0.0F, 0.0F);
                poseStack.mulPose(Vector3f.YP.rotationDegrees(60.0F));//GL11.glRotatef(60F, 0.0F, 1.0F, 0.0F);
                poseStack.mulPose(Vector3f.ZP.rotationDegrees(20.0F));//GL11.glRotatef(20F, 0.0F, 0.0F, 1.0F);
            }
            else
            if(entityplayer.isCrouching()){//ADS
                if(side && !entityplayer.getOffhandItem().isEmpty()){
                    poseStack.translate(gun.model_x * xxx ,gun.model_y, gun.model_z + 1);//GL11.glTranslatef(gun.model_x * xxx ,gun.model_y, gun.model_z + 1);//1.5,-2,-2.5
                    poseStack.mulPose(Vector3f.YP.rotationDegrees(180.0F));//GL11.glRotatef(180F, 0.0F, 1.0F, 0.0F);
                    //GL11.glRotatef(00F, 0.0F, 1.0F, 0.0F);
                    //GL11.glRotatef(0F, 1.0F, 0.0F, 0.0F);
                }else if(!side && !entityplayer.getMainHandItem().isEmpty()){
                    poseStack.translate(gun.model_x * xxx ,gun.model_y, gun.model_z + 1);// GL11.glTranslatef(gun.model_x * xxx ,gun.model_y, gun.model_z + 1);//1.5,-2,-2.5
                    poseStack.mulPose(Vector3f.YP.rotationDegrees(180.0F));//GL11.glRotatef(180F, 0.0F, 1.0F, 0.0F);
                    //GL11.glRotatef(00F, 0.0F, 1.0F, 0.0F);
                    //GL11.glRotatef(0F, 1.0F, 0.0F, 0.0F);
                }else{
                    poseStack.translate(ADS_X(gun, itemstack) * xxx,ADS_Y(gun, itemstack), ADS_Z(gun, itemstack));//GL11.glTranslatef(ADS_X(gun) * xxx,ADS_Y(gun), ADS_Z(gun));//0,-1.7,-1.5
                    poseStack.mulPose(Vector3f.XP.rotationDegrees(gun.rotationx));//GL11.glRotatef(gun.rotationx, 1.0F, 0.0F, 0.0F);
                    poseStack.mulPose(Vector3f.YP.rotationDegrees(gun.rotationy * xxx));//GL11.glRotatef(gun.rotationy * xxx, 0.0F, 1.0F, 0.0F);
                    poseStack.mulPose(Vector3f.ZP.rotationDegrees(gun.rotationz * xxx));//GL11.glRotatef(gun.rotationz * xxx, 0.0F, 0.0F, 1.0F);
       //             poseStack.mulPose(Vector3f.XP.rotationDegrees(5F));//GL11.glRotatef(5F, 1.0F, 0.0F, 0.0F);
                }
            }else if(entityplayer.isSprinting()){
                poseStack.translate(gun.model_x * xxx -0.5F,gun.model_y, gun.model_z);//GL11.glTranslatef(gun.model_x * xxx -0.5F,gun.model_y, gun.model_z);
                poseStack.translate(gun.Sprintoffsetx * xxx, gun.Sprintoffsety, gun.Sprintoffsetz);//GL11.glTranslatef(gun.Sprintoffsetx * xxx, gun.Sprintoffsety, gun.Sprintoffsetz);
                poseStack.mulPose(Vector3f.XP.rotationDegrees(-gun.Sprintrotationx));//GL11.glRotatef(-gun.Sprintrotationx, 1.0F, 0.0F, 0.0F);
                poseStack.mulPose(Vector3f.YP.rotationDegrees(gun.Sprintrotationy * xxx+180));//GL11.glRotatef(gun.Sprintrotationy * xxx+180, 0.0F, 1.0F, 0.0F);
                poseStack.mulPose(Vector3f.ZP.rotationDegrees(gun.Sprintrotationz));//GL11.glRotatef(gun.Sprintrotationz, 0.0F, 0.0F, 1.0F);

            }
            else{

                poseStack.translate(gun.model_x * xxx ,gun.model_y, gun.model_z + 1);//GL11.glTranslatef(gun.model_x * xxx ,gun.model_y, gun.model_z + 1);//1.5,-2,-2.5

                poseStack.mulPose(Vector3f.YP.rotationDegrees(180.0F));//GL11.glRotatef(180F, 0.0F, 1.0F, 0.0F);
                //GL11.glRotatef(00F, 0.0F, 1.0F, 0.0F);
                //		GL11.glRotatef(-3F, 1.0F, 0.0F, 0.0F);
            }//1e

            if(!recoiled) {
                poseStack.mulPose(Vector3f.XP.rotationDegrees(gun.model_muzz_jump_x));//GL11.glRotatef(gun.model_muzz_jump_x, 1.0F, 0.0F, 0.0F);
            }

            {
                poseStack.pushPose();//GL11.glPushMatrix();//guns

                if(!side){
                    //GL11.glScalef(1F, 1F, -1F);
                    //GL11.glFrontFace(GL11.GL_CCW);
                    //GL11.glColor3d(-1, -1, -1);
                    //	GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
                    //	GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
                    //             GL11.glEnable(GL11.GL_DEPTH_TEST);
                }

                //minecraft.textureManager.bindTexture(new ResourceLocation(gun.obj_tex));
                {
                    //             GL11.glEnable(GL11.GL_LIGHTING);
                    //			GL11.glEnable(GL11.GL_LIGHT1);
                    //             GL11.glEnable(GL11.GL_COLOR_MATERIAL);
                    //             BlockPos blockpos = new BlockPos(entityplayer.getLightProbePosition(partialTicks));
                    //             float light_level = entityplayer.level.getBrightness(blockpos);
                    //             GL11.glColor4f(light_level, light_level, light_level, 1F);
                    //			 System.out.println(String.format("%1$.2f", light_level));
                }
                minecraft.getEntityRenderDispatcher().textureManager.getTexture(new ResourceLocation(gun.obj_tex));
                //   VertexConsumer vertexconsumer = multiBufferSource.getBuffer(RenderType.text(new ResourceLocation(gun.obj_tex)));//RenderType.lightning()
                //   VertexConsumer vertexconsumer = multiBufferSource.getBuffer(RenderType.lightning());//RenderType.lightning()
                VertexConsumer vertexconsumer = multiBufferSource.getBuffer(RenderTypeGun.gunrender(new ResourceLocation(gun.obj_tex)));
                if(entityplayer.isCrouching()){
                    boolean render;
                    if(gun.getAttachment(gun, itemstack, 101) != null){
                        ItemAttachment sight = (ItemAttachment) gun.getAttachment(gun, itemstack, 101);
                        render = sight.zoomrender;
                    }else{
                        render = gun.zoomrender;
                    }
                    if(render)render_mat(gun, itemstack, entityplayer, recoiled,poseStack, vertexconsumer,multiBufferSource);
                }else{
                    //poseStack.
                    render_mat(gun, itemstack, entityplayer, recoiled,poseStack, vertexconsumer,multiBufferSource);
                }

                {
                    //			GL11.glDisable(GL11.GL_LIGHT1);
                    //			GL11.glDisable(GL11.GL_LIGHTING);
                }
                poseStack.popPose();//GL11.glPopMatrix();//gune
            }


            //    GL11.glDisable(GL12.GL_RESCALE_NORMAL);
            //GlStateManager.disableLighting();
            poseStack.popPose();//GL11.glPopMatrix();//gune
        }
    }

    // The window handle
    private long window;

    public void glinit(){
        // Setup an error callback. The default implementation
        // will print the error message in System.err.
        GLFWErrorCallback.createPrint(System.err).set();

        // Initialize GLFW. Most GLFW functions will not work before doing this.
        if ( !glfwInit() )
            throw new IllegalStateException("Unable to initialize GLFW");

        // Configure GLFW
        glfwDefaultWindowHints(); // optional, the current window hints are already the default
        glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden after creation
        glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable

    }


    public static void render_mat(ItemGunBase gun, ItemStack itemstack, Player entityplayer, boolean recoiled
            , PoseStack poseStack, VertexConsumer vertexconsumer, MultiBufferSource multiBufferSource) {
        gun.obj_model.renderPart(vertexconsumer,poseStack, "mat1");
        if (!gun.isReload(itemstack))gun.obj_model.renderPart(vertexconsumer,poseStack,"mat3");//reload
        if(gun.getAttachment(gun, itemstack, 101) == null){
            gun.obj_model.renderPart(vertexconsumer,poseStack,"mat20");
            {
                poseStack.pushPose();//GL11.glPushMatrix();//guns
                VertexConsumer vertexconsumer2_l = multiBufferSource.getBuffer(RenderTypeGun.gunrender_light(new ResourceLocation(gun.obj_tex)));
                gun.obj_model.renderPart(vertexconsumer2_l,poseStack,"mat20_dot");
                VertexConsumer vertexconsumer2_l2 = multiBufferSource.getBuffer(RenderTypeGun.gunrender(new ResourceLocation(gun.obj_tex)));
                poseStack.popPose();//GL11.glPopMatrix();//gune
            }
        }else{
            gun.obj_model.renderPart(vertexconsumer,poseStack,"mat41");
        }
        gun.obj_model.renderPart(vertexconsumer,poseStack,"mat31");
        gun.obj_model.renderPart(vertexconsumer,poseStack,"mat32");
        gun.obj_model.renderPart(vertexconsumer,poseStack,"mat25");



        if (!gun.isReload(itemstack)){
            gun.obj_model.renderPart(vertexconsumer,poseStack,"mat22");
        }else{
            poseStack.translate(gun.mat22_x,gun.mat22_y, gun.mat22_z);
            poseStack.mulPose(Vector3f.XP.rotationDegrees(30F));
            poseStack.mulPose(Vector3f.YP.rotationDegrees(0));
            poseStack.mulPose(Vector3f.ZP.rotationDegrees(0));
            poseStack.translate(-gun.mat22_x,-gun.mat22_y, -gun.mat22_z);
            gun.obj_model.renderPart(vertexconsumer,poseStack,"mat22");
        }

		/*if (!recoiled)
		{
			GL11.glTranslatef(0.0F, 0.0F, gun.model_cock_z);//0, 0, -0.4
			gun.obj_model.renderPart("mat2");
		}else*/
        {
            gun.obj_model.renderPart(vertexconsumer,poseStack,"mat2");
        }


        if(gun.getAttachment(gun, itemstack, 101) != null){
            poseStack.pushPose();//GL11.glPushMatrix();//guns
            ItemAttachment sight = (ItemAttachment) gun.getAttachment(gun, itemstack, 101);
            Minecraft minecraft = Minecraft.getInstance();
            if(sight.obj_model == null)sight.obj_model = AdvancedModelLoader.loadModel(new ResourceLocation(sight.obj_model_string));
            //minecraft.getEntityRenderDispatcher().textureManager.getTexture(new ResourceLocation(sight.obj_tex));
            if(sight.obj_model != null){
                VertexConsumer vertexconsumer2 = multiBufferSource.getBuffer(RenderTypeGun.gunrender(new ResourceLocation(sight.obj_tex)));

                poseStack.translate(gun.am_sight_x,gun.am_sight_y, gun.am_sight_z);
                sight.obj_model.renderPart(vertexconsumer2,poseStack,"sight");
                poseStack.translate(-gun.am_sight_x,-gun.am_sight_y, -gun.am_sight_z);

                poseStack.pushPose();//GL11.glPushMatrix();//guns
                poseStack.translate(gun.am_sight_x,gun.am_sight_y, gun.am_sight_z);
                VertexConsumer vertexconsumer2_l = multiBufferSource.getBuffer(RenderTypeGun.gunrender_light(new ResourceLocation(sight.obj_tex)));
                sight.obj_model.renderPart(vertexconsumer2_l,poseStack,"dot");
                poseStack.translate(-gun.am_sight_x,-gun.am_sight_y, -gun.am_sight_z);
                poseStack.popPose();//GL11.glPopMatrix();//gune
            }
            poseStack.popPose();//GL11.glPopMatrix();//gune
        }
        if(gun.getAttachment(gun, itemstack, 102) != null){
            poseStack.pushPose();//GL11.glPushMatrix();//guns
            ItemAttachment sight = (ItemAttachment) gun.getAttachment(gun, itemstack, 102);
            Minecraft minecraft = Minecraft.getInstance();
            if(sight.obj_model == null)sight.obj_model = AdvancedModelLoader.loadModel(new ResourceLocation(sight.obj_model_string));
            //minecraft.getEntityRenderDispatcher().textureManager.getTexture(new ResourceLocation(sight.obj_tex));
            if(sight.obj_model != null) {
                VertexConsumer vertexconsumer2 = multiBufferSource.getBuffer(RenderTypeGun.gunrender(new ResourceLocation(sight.obj_tex)));
                poseStack.translate((gun.am_light_x + sight.x), (gun.am_light_y + sight.y), (gun.am_light_z + sight.z));
                poseStack.mulPose(Vector3f.XP.rotationDegrees(gun.am_light_xr));
                poseStack.mulPose(Vector3f.YP.rotationDegrees(gun.am_light_yr));
                poseStack.mulPose(Vector3f.ZP.rotationDegrees(gun.am_light_zr));
                sight.obj_model.renderPart(vertexconsumer2, poseStack, "sight");
                poseStack.translate(-(gun.am_light_x + sight.x), -(gun.am_light_y + sight.y), -(gun.am_light_z + sight.z));
            }
            poseStack.popPose();//GL11.glPopMatrix();//gune
        }
        if(gun.getAttachment(gun, itemstack, 8) != null){
            poseStack.pushPose();//GL11.glPushMatrix();//guns
            ItemAttachment sight = (ItemAttachment) gun.getAttachment(gun, itemstack, 8);
            Minecraft minecraft = Minecraft.getInstance();
            if(sight.obj_model == null)sight.obj_model = AdvancedModelLoader.loadModel(new ResourceLocation(sight.obj_model_string));
            //minecraft.getEntityRenderDispatcher().textureManager.getTexture(new ResourceLocation(sight.obj_tex));
            if(sight.obj_model != null) {
                VertexConsumer vertexconsumer2 = multiBufferSource.getBuffer(RenderTypeGun.gunrender(new ResourceLocation(sight.obj_tex)));
                poseStack.translate((gun.am_supu_x + sight.x), (gun.am_supu_y + sight.y), (gun.am_supu_z + sight.z));
                poseStack.mulPose(Vector3f.XP.rotationDegrees(gun.am_supu_xr));
                poseStack.mulPose(Vector3f.YP.rotationDegrees(gun.am_supu_yr));
                poseStack.mulPose(Vector3f.ZP.rotationDegrees(gun.am_supu_zr));
                sight.obj_model.renderPart(vertexconsumer2, poseStack, "sight");
                poseStack.translate(-(gun.am_supu_x + sight.x), -(gun.am_supu_y + sight.y), -(gun.am_supu_z + sight.z));
            }
            poseStack.popPose();//GL11.glPopMatrix();//gune
        }
        if(gun.getAttachment(gun, itemstack, 9) != null){
            poseStack.pushPose();//GL11.glPushMatrix();//guns
            ItemAttachment sight = (ItemAttachment) gun.getAttachment(gun, itemstack, 9);
            Minecraft minecraft = Minecraft.getInstance();
            if(sight.obj_model == null)sight.obj_model = AdvancedModelLoader.loadModel(new ResourceLocation(sight.obj_model_string));
            //minecraft.getEntityRenderDispatcher().textureManager.getTexture(new ResourceLocation(sight.obj_tex));
            if(sight.obj_model != null) {
                VertexConsumer vertexconsumer2 = multiBufferSource.getBuffer(RenderTypeGun.gunrender(new ResourceLocation(sight.obj_tex)));
                poseStack.translate((gun.am_grip_x + sight.x), (gun.am_grip_y + sight.y), (gun.am_grip_z + sight.z));
                poseStack.mulPose(Vector3f.XP.rotationDegrees(gun.am_grip_xr));
                poseStack.mulPose(Vector3f.YP.rotationDegrees(gun.am_grip_yr));
                poseStack.mulPose(Vector3f.ZP.rotationDegrees(gun.am_grip_zr));
                sight.obj_model.renderPart(vertexconsumer2, poseStack, "sight");
                poseStack.translate(-(gun.am_grip_x + sight.x), -(gun.am_grip_y + sight.y), -(gun.am_grip_z + sight.z));
            }
            poseStack.popPose();//GL11.glPopMatrix();//gune
        }/**/
    }


    public static float ADS_X(ItemGunBase gun, ItemStack itemstack){
        float ads = gun.model_x_ads;
        if(gun.getAttachment(gun, itemstack, 101) != null){
            ItemAttachment sight = (ItemAttachment) gun.getAttachment(gun, itemstack, 101);
            ads = gun.am_sight_x + sight.x;
        }
        return ads;
    }
    public static float ADS_Y(ItemGunBase gun, ItemStack itemstack){
        float ads = gun.model_y_ads;
        if(gun.getAttachment(gun, itemstack, 101) != null){
            ItemAttachment sight = (ItemAttachment) gun.getAttachment(gun, itemstack, 101);
            ads = -(gun.am_sight_y + sight.y);
        }
        return ads;
    }
    public static float ADS_Z(ItemGunBase gun, ItemStack itemstack){
        float ads = gun.model_z_ads;
        if(gun.getAttachment(gun, itemstack, 101) != null){
            ItemAttachment sight = (ItemAttachment) gun.getAttachment(gun, itemstack, 101);
            ads = sight.eye_relief + gun.am_sight_z;
        }
        return ads;
    }

   /* public static ItemAttachment getAttachment(ItemGunBase gun, ItemStack itemstack, int id){
        ItemAttachment flag = null;

        CompoundTag compoundtag = itemstack.getOrCreateTag();
        if(id == 101){
            if(gun.sight.isEmpty()){
                if (compoundtag.contains("Items_SIGHT")) {
                    ListTag listtag = compoundtag.getList("Items_SIGHT", 10);
                    //for(int i = 0; i < listtag.size(); ++i)
                    int i = 0;
                    {
                        CompoundTag compoundtag1 = listtag.getCompound(i);
                        ItemStack itemstack2 = ItemStack.of(compoundtag1);
                        if(!itemstack2.isEmpty() && itemstack2.getItem()  instanceof ItemAttachment){
                            ItemAttachment amsight = (ItemAttachment) itemstack2.getItem();
                            flag = amsight;
                            gun.sight = itemstack2;
                        }
                    }
                }
            }else{
                flag = (ItemAttachment)gun.sight.getItem();
            }
        }
        if(id == 102){
            if(gun.light.isEmpty()){
                if (compoundtag.contains("Items_LIGHT")) {
                    ListTag listtag = compoundtag.getList("Items_LIGHT", 10);
                    {
                        CompoundTag compoundtag1 = listtag.getCompound(0);
                        ItemStack itemstack2 = ItemStack.of(compoundtag1);
                        if(!itemstack2.isEmpty() && itemstack2.getItem()  instanceof ItemAttachment){
                            ItemAttachment amsight = (ItemAttachment) itemstack2.getItem();
                            flag = amsight;
                            gun.light = itemstack2;
                        }
                    }
                }
            }
            else{
                flag = (ItemAttachment)gun.light.getItem();
            }
        }
        if(id == 8){
            if(gun.supp.isEmpty()){
                if (compoundtag.contains("Items_SUPP")) {
                    ListTag listtag = compoundtag.getList("Items_SUPP", 10);
                    {
                        CompoundTag compoundtag1 = listtag.getCompound(0);
                        ItemStack itemstack2 = ItemStack.of(compoundtag1);
                        if(!itemstack2.isEmpty() && itemstack2.getItem()  instanceof ItemAttachment){
                            ItemAttachment amsight = (ItemAttachment) itemstack2.getItem();
                            flag = amsight;
                            gun.supp = itemstack2;
                        }
                    }
                }
            }
            else{
                flag = (ItemAttachment)gun.supp.getItem();
            }
        }
        if(id == 9){
            if(gun.grip.isEmpty()){
                if (compoundtag.contains("Items_GRIP")) {
                    ListTag listtag = compoundtag.getList("Items_GRIP", 10);
                    {
                        CompoundTag compoundtag1 = listtag.getCompound(0);
                        ItemStack itemstack2 = ItemStack.of(compoundtag1);
                        if(!itemstack2.isEmpty() && itemstack2.getItem()  instanceof ItemAttachment){
                            ItemAttachment amsight = (ItemAttachment) itemstack2.getItem();
                            flag = amsight;
                            gun.grip = itemstack2;
                        }
                    }
                }
            }
            else{
                flag = (ItemAttachment)gun.grip.getItem();
            }
        }


        return flag;
    }*/
}

