package gvclib.item;

import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

import com.mojang.blaze3d.vertex.VertexConsumer;
import gvclib.client.event.RenderTypeGun;
import gvclib.entity.bullet.EntityB_Bullet;
import gvclib.event.GVCSoundEvent;
import gvclib.item.gun.GunAttachment;
import gvclib.item.gun.GunBase_LoadStatus;
import gvclib.mod_GVCLib;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.core.Registry;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.stats.Stats;
import net.minecraft.util.Mth;
import net.minecraft.world.Container;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.AbstractArrow;
import net.minecraft.world.entity.projectile.Snowball;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.*;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.Level;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.extensions.IForgeItem;
import net.minecraftforge.fml.loading.FMLLoader;
import objmodel.AdvancedModelLoader;
import objmodel.IModelCustom;

import javax.annotation.Nullable;

public class ItemGunBase extends BowItem implements Vanishable, IForgeItem {

    public Item.Properties pro;
    /***
     * 1.19より追加
     * これで読み込ま専る
     */
    boolean load = false;//1.19より追加

    public static final int MAX_DRAW_DURATION = 20;
    public static final int DEFAULT_RANGE = 15;

    public ItemGunBase(Item.Properties p_40660_) {
        super(p_40660_);
    }

    public static final String Tag_Cycle		= "Cycle";
    public static final String Tag_CycleCount	= "CycleCount";

    public float f;
    public int j;

    public int isreload;
    public int retime;
    public int reloadtime;

    public static int maxdamege;

    public static boolean canreload;

    public int firetime;
    public int firegrenadetime;
    public int retimegrenade;


    public int powor;
    public float speed;
    public float bure;
    public double recoil;
    public float gra = 0.0299F;
    public float exlevel = 0;
    public float scopezoom = 1.0F;
    public  String adstexture = "gvclib:textures/misc/scope.png";

    public int cycle = 1;

    public float movespeed = 1.0F;

    public int pellet = 1;

    public float fire_posx = 0.3F;
    public float fire_posy = 1.5F;
    public float fire_posz = 1.0F;

    public float bureads;
    public double recoilads;

    public static int bulletcount;

    public static String ads;

    public int aaa;
    public static boolean grenadekey;

    public Item magazine = Items.APPLE;//null
    public String gun_mod_id = mod_GVCLib.MOD_ID;
    public String fire_sound = "fire_rifle";
    public String reload_sound = "reload_mag";
    public SoundEvent fire_sounds = GVCSoundEvent.getSound(this.fire_sound);
    //public SoundEvent reload_sound = CGSoundEvent.reload_mag;



    ////////////////////////////////////////////////
    public boolean obj_true = true;
    public IModelCustom obj_model = null;//AdvancedModelLoader.loadModel(new ResourceLocation("gvclib:textures/model/ar.obj"));
    public String obj_model_resource = "generalguns:textures/model/ak74.mqo";//AdvancedModelLoader.loadModel(new ResourceLocation("gvclib:textures/model/ar.obj"));
    public String obj_tex = "generalguns:textures/model/ak74.png";

    public float model_x = 1.8F;
    public float model_y = -2F;
    public float model_z = -3.5F;
    public float model_x_ads = 0F;
    public float model_y_ads = -1.7F;
    public float model_z_ads = -1.5F;

    public float Sprintrotationx = 20F;
    public float Sprintrotationy = 60F;
    public float Sprintrotationz = 0F;
    public float Sprintoffsetx = 0.5F;
    public float Sprintoffsety = 0.0F;
    public float Sprintoffsetz = 0.5F;

    public float rotationx = 0F;
    public float rotationy = 180F;
    public float rotationz = 0F;

    public float model_muzz_jump_x = 0;
    public float model_cock_z = -0.4F;

    public float mat22_x = 0.0F;
    public float mat22_y = 0.0F;
    public float mat22_z = 0.0F;

    public boolean zoomrender = true;




    public void appendHoverText(ItemStack p_42880_, @Nullable Level p_42881_, List<Component> tooltip, TooltipFlag p_42883_) {

        MutableComponent kugiri = (Component.translatable(" : ", new Object[0])).withStyle(ChatFormatting.WHITE);

        String zan = String.valueOf(p_42880_.getMaxDamage() -p_42880_.getDamageValue() - 1);
        MutableComponent rema = Component.translatable("classicguns.gun.remain", new Object[0]);
        tooltip.add(rema.withStyle(ChatFormatting.RED));

        MutableComponent zan_int = Component.translatable(zan, new Object[0]);
        tooltip.add(zan_int.withStyle(ChatFormatting.RED));

        MutableComponent maganeme = Component.translatable("classicguns.gun.magazine", new Object[0]);
        tooltip.add(maganeme.withStyle(ChatFormatting.YELLOW));

        MutableComponent maganeme_ = Component.translatable(this.magazine.getName(new ItemStack(this.magazine)).getString(), new Object[0]);
        tooltip.add(maganeme_.withStyle(ChatFormatting.YELLOW));

        String pow = String.valueOf(powor);
        MutableComponent pow_int = (Component.translatable(pow, new Object[0])).withStyle(ChatFormatting.WHITE);
        MutableComponent pow_name = (Component.translatable("classicguns.gun.power", new Object[0])).withStyle(ChatFormatting.WHITE);
        //List<MutableComponent> anotherList = Arrays.asList(pow_name, kugiri, pow_int);
        tooltip.addAll(Arrays.asList(pow_name, pow_int));

        String spe = String.valueOf(speed);
        MutableComponent spe_int = (Component.translatable(spe, new Object[0])).withStyle(ChatFormatting.WHITE);
        MutableComponent spe_name = (Component.translatable("classicguns.gun.speed", new Object[0])).withStyle(ChatFormatting.WHITE);
        tooltip.addAll(Arrays.asList(spe_name, spe_int));

        String bur = String.valueOf(bure);
        MutableComponent bur_int = (Component.translatable(bur, new Object[0])).withStyle(ChatFormatting.WHITE);
        MutableComponent bur_name = (Component.translatable("classicguns.gun.bure", new Object[0])).withStyle(ChatFormatting.WHITE);
        tooltip.addAll(Arrays.asList(bur_name, bur_int));

        String relo = String.valueOf(reloadtime / 20);
        MutableComponent relo_int = (Component.translatable(relo, new Object[0])).withStyle(ChatFormatting.WHITE);
        MutableComponent relo_name = (Component.translatable("classicguns.gun.reloadtime", new Object[0])).withStyle(ChatFormatting.WHITE);
        tooltip.addAll(Arrays.asList(relo_name, relo_int));


        tooltip.add((Component.translatable("classicguns.gun.fire", new Object[0])).withStyle(ChatFormatting.WHITE));
        tooltip.add((Component.translatable("classicguns.gun.reload", new Object[0])).withStyle(ChatFormatting.WHITE));
        tooltip.add((Component.translatable("classicguns.gun.treload", new Object[0])).withStyle(ChatFormatting.WHITE));
    }

    @OnlyIn(Dist.CLIENT)
    public void ModelLoad() {
        if(obj_model == null && obj_model_resource != null) {
            obj_model = AdvancedModelLoader.loadModel(new ResourceLocation(obj_model_resource));
            /*if(reload_motion_name != null) {
                ResourceLocation reloadmotion = new ResourceLocation(reload_motion_name);
                ReloadMotionReader.read(this, reloadmotion);
            }
            if(recoil_motion_name != null) {
                ResourceLocation recoilmotion = new ResourceLocation(recoil_motion_name);
                RecoilMotionReader.read(this, recoilmotion);
            }*/
        }
    }

    public void releaseUsing(ItemStack p_40667_, Level p_40668_, LivingEntity p_40669_, int p_40670_) {
        if (p_40669_ instanceof Player) {
            Player player = (Player)p_40669_;
        }
    }


    public int getUseDuration(ItemStack p_40680_) {
        return 72000;
    }

    public UseAnim getUseAnimation(ItemStack p_40678_) {
        return UseAnim.BOW;
    }

    /*public InteractionResultHolder<ItemStack> use(Level p_40672_, Player p_40673_, InteractionHand p_40674_) {
        ItemStack itemstack = p_40673_.getItemInHand(p_40674_);
        boolean flag = !p_40673_.getProjectile(itemstack).isEmpty();

        InteractionResultHolder<ItemStack> ret = net.minecraftforge.event.ForgeEventFactory.onArrowNock(itemstack, p_40672_, p_40673_, p_40674_, flag);
        if (ret != null) return ret;

        if (!p_40673_.getAbilities().instabuild && !flag) {
            return InteractionResultHolder.fail(itemstack);
        } else {
            p_40673_.startUsingItem(p_40674_);
            return InteractionResultHolder.consume(itemstack);
        }
    }*/

    public Predicate<ItemStack> getAllSupportedProjectiles() {
        return ARROW_ONLY;
    }

    public AbstractArrow customArrow(AbstractArrow arrow) {
        return arrow;
    }

    public int getDefaultProjectileRange() {
        return 15;
    }


    public void FireBullet(ItemStack stack, Level worldIn, Player playerIn)
    {

        if (this.isReload(stack))return;

        SoundEvent sound = null;
        SoundEvent gun = Registry.SOUND_EVENT.get(new ResourceLocation(this.gun_mod_id, this.fire_sound));
        if(gun != null){
            worldIn.playSound(playerIn, playerIn.getX(), playerIn.getY(), playerIn.getZ(),
                    gun, SoundSource.NEUTRAL, 3.0F, 1.0F);
        }else{
            worldIn.playSound(playerIn, playerIn.getX(), playerIn.getY(), playerIn.getZ(),
                    GVCSoundEvent.getSound(this.fire_sound), SoundSource.NEUTRAL, 3.0F, 1.0F);
        }


        {
            for (int pe = 0; pe < this.pellet; ++pe) {
                EntityB_Bullet snowballentity = new EntityB_Bullet(worldIn, playerIn);
                int ep = EnchantmentHelper.getEnchantmentLevel(Enchantments.POWER_ARROWS, playerIn);
                if(powor == -1){
                    snowballentity.flare = true;
                }else{
                    snowballentity.powor = this.powor + ep;
                }
                //snowballentity.gra = this.gra;
                snowballentity.setGravity(this.gra);
                snowballentity.exlevel = this.exlevel;
                int fm = EnchantmentHelper.getEnchantmentLevel(Enchantments.FLAMING_ARROWS, playerIn);
                if(fm > 0) {
                    snowballentity.flame = true;
                }
                float bbure = this.bure;
                if(playerIn.isCrouching()) {
                    bbure = this.bureads;
                }
                //    snowballentity.setItem(stack);
                snowballentity.shootFromRotation(playerIn, playerIn.getXRot(), playerIn.getYRot(), 0.0F, this.speed, bbure);
                if (!worldIn.isClientSide) worldIn.addFreshEntity(snowballentity);
                //System.out.println(String.format("spawn_entity"));
            }
           /* if (!worldIn.isClientSide){
                Snowball snowball = new Snowball(worldIn, playerIn);
                snowball.setItem(stack);
                snowball.shootFromRotation(playerIn, playerIn.getXRot(), playerIn.getYRot(), 0.0F, 1.5F, 1.0F);
                worldIn.addFreshEntity(snowball);
            }*/
        }
        {
            double reco = recoil;
            if(playerIn.isCrouching()) {
                reco = recoilads;
            }
            double xro = playerIn.getXRot();
            xro += (worldIn.random.nextFloat() * -2F) * reco;
            playerIn.setXRot((float) xro);
        }
        {
            double xx11 = 0;
            double zz11 = 0;
            double yy11 = 0;
            float xz = 1.57F;
            if(!playerIn.getMainHandItem().isEmpty() && !playerIn.getOffhandItem().isEmpty()) {
                if(playerIn.getMainHandItem() == stack) {
                    xz = 1.57F;
                }else if(playerIn.getOffhandItem() == stack){
                    xz = -1.57F;
                }
            }else if (playerIn.isCrouching()) {
                xz = 0;
            }else if(playerIn.getMainHandItem() == stack) {
                xz = 1.57F;
            }else if(playerIn.getOffhandItem() == stack){
                xz = -1.57F;
            }
            double yy = fire_posy;
            if (playerIn.isCrouching()) {
                yy = fire_posy - 0.2F;
            }
            double zzz = fire_posz * Math.cos(Math.toRadians(-playerIn.getXRot()));
            xx11 -= Mth.sin(playerIn.yHeadRot * 0.01745329252F) * zzz;
            zz11 += Mth.cos(playerIn.yHeadRot * 0.01745329252F) * zzz;
            xx11 -= Mth.sin(playerIn.yHeadRot * 0.01745329252F + xz) * fire_posx;
            zz11 += Mth.cos(playerIn.yHeadRot * 0.01745329252F + xz) * fire_posx;
            yy11 = Mth.sqrt((float) (zzz* zzz)) * Math.tan(Math.toRadians(-playerIn.getXRot())) * 1D;
            worldIn.addParticle(ParticleTypes.SMOKE,
                    playerIn.getX() + xx11, playerIn.getY() + yy + yy11, playerIn.getZ() + zz11, 0.0D, 0.1D, 0.0D);
        }

        playerIn.awardStat(Stats.ITEM_USED.get(this));
        if (!playerIn.getAbilities().instabuild) {
            //itemstack.shrink(1);
            stack.hurtAndBreak(1, playerIn, (p_220009_1_) -> {
                p_220009_1_.broadcastBreakEvent(playerIn.getUsedItemHand());
            });
        }

    }

    public void inventoryTick(ItemStack stack, Level worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
        if(isSelected) {
            entityIn.setDeltaMovement(entityIn.getDeltaMovement().x * movespeed, entityIn.getDeltaMovement().y, entityIn.getDeltaMovement().z * movespeed);
        }
        Player entityplayer = (Player)entityIn;
        if(stack.hasTag()) {
            this.Gun_Reload(stack, worldIn, entityplayer, itemSlot, isSelected);
            CompoundTag nbt = stack.getTag();
            int reloadset = nbt.getInt("reloadset");
            if(reloadset > 0){
                --reloadset;
                nbt.putInt("reloadset", reloadset);
            }

            /*{
                if (!nbt.contains("Items")) {
                    nbt.put("Items", new ListTag());
                } else {
                    if (entityplayer instanceof ServerPlayer) {
                        ListTag listtag = nbt.getList("Items", 1);
                    }
                }
            }*/


        }else {
            CompoundTag ltags = new CompoundTag();
            stack.setTag(ltags);
        }

        if(!load){
            //magazine = mod_ClassicGuns.item_bullet.get();
            //magazine = Items.BEEF;

        //    magazine = Registry.ITEM.get(new ResourceLocation("minecraft","bow"));

            load = true;
        }

        GunAttachment.load(stack, worldIn, entityIn, itemSlot);

    }
    public void Gun_Reload(ItemStack itemstack, Level world, Entity entity, int i, boolean flag) {
        CompoundTag nbt = itemstack.getTag();
        if (itemstack.hasTag() && this.magazine != null) {
            Player entityplayer = (Player)entity;
            ItemGunBase gun = (ItemGunBase) itemstack.getItem();
            //if (this.isReload(itemstack) && flag && entityplayer.inventory.hasItemStack(new ItemStack(this.magazine))) {
            if (this.isReload(itemstack) && flag && !this.findAmmo(entityplayer).isEmpty()) {
                if (entity != null && entity instanceof Player) {
                    if (itemstack == entityplayer.getMainHandItem()) {
                        int reloadti = nbt.getInt("RloadTime");
                        {
                            gun.retime = reloadti;
                            if(gun.retime == 0) {
                                //SoundEvent sound = new SoundEvent(new ResourceLocation(mod_ClassicGuns.MOD_ID, reload_sound));
                                /*world.playSound(entityplayer, entityplayer.getX(), entityplayer.getY(), entityplayer.getZ(),
                                        GVCSoundEvent.getSound(this.reload_sound), SoundSource.NEUTRAL, 1.0F, 1.0F);*/
                                SoundEvent sound = null;
                                SoundEvent egun = Registry.SOUND_EVENT.get(new ResourceLocation(this.gun_mod_id, this.reload_sound));
                                if(egun != null){
                                    world.playSound(entityplayer, entityplayer.getX(), entityplayer.getY(), entityplayer.getZ(),
                                            egun, SoundSource.NEUTRAL, 3.0F, 1.0F);
                                }else{
                                    world.playSound(entityplayer, entityplayer.getX(), entityplayer.getY(), entityplayer.getZ(),
                                            GVCSoundEvent.getSound(this.reload_sound), SoundSource.NEUTRAL, 3.0F, 1.0F);
                                }
                            }
                            ++reloadti;
                            if (reloadti == gun.reloadtime) {
                                gun.retime = reloadti = 0;
                                nbt.putInt("RloadTime", 0);
                                {
                                    getReload(itemstack, world, entityplayer);
                                }
                            }else{
                                nbt.putInt("RloadTime", reloadti);
                            }
                        }
                    }
                }
            }
        }
    }

    public void getReload(ItemStack par1ItemStack, Level par2World, Player par3EntityPlayer)
    {
        boolean linfinity = EnchantmentHelper.getEnchantmentLevel(Enchantments.INFINITY_ARROWS, par3EntityPlayer) > 0;
        if(!par3EntityPlayer.getAbilities().instabuild){//2019/10/27
            for (int i = 0; i < par3EntityPlayer.getInventory().getContainerSize(); ++i) {
                ItemStack itemstack = par3EntityPlayer.getInventory().getItem(i);
                if (this.isAmmo(itemstack)) {
                    ItemStack item = itemstack;
                    if (!item.isEmpty()) {
                        int zan = getDamage(par1ItemStack) - 0;
                        int lii = item.getCount() - zan;
                        if (!linfinity) {
                            if (lii <= 0) {
                                setDamage(par1ItemStack, getDamage(par1ItemStack) - item.getCount());
                            } else {
                                setDamage(par1ItemStack, 0);
                            }
                        } else {
                            setDamage(par1ItemStack, 0);
                        }
                        if (!linfinity) {
                            item.shrink(zan);
                            if (item.isEmpty()) {
                                par3EntityPlayer.getInventory().removeItem(item);
                            }
                        }
                        if(this.getDamage(par1ItemStack) == 0) {
                            break;
                        }
                    }
                }
            }
        }

    }

    private ItemStack findAmmo(Player player)
    {
        if (this.isAmmo(player.getItemInHand(InteractionHand.OFF_HAND)))
        {
            return player.getItemInHand(InteractionHand.OFF_HAND);
        }
        else if (this.isAmmo(player.getItemInHand(InteractionHand.MAIN_HAND)))
        {
            return player.getItemInHand(InteractionHand.MAIN_HAND);
        }
        else
        {
            for (int i = 0; i < player.getInventory().getContainerSize(); ++i)
            {
                ItemStack itemstack = player.getInventory().getItem(i);

                if (this.isAmmo(itemstack))
                {
                    return itemstack;
                }
            }

            return ItemStack.EMPTY;
        }
    }


    public boolean isAmmo(ItemStack stack)
    {
        return !stack.isEmpty() && stack.getItem() == this.magazine;
    }

    /**?ｿｽe?ｿｽ?ｿｽ0?ｿｽ?ｿｽ?ｿｽ?ｿｽ?ｿｽm?ｿｽ?ｿｽ?ｿｽ?ｿｽ
     * */
    public boolean isReload(ItemStack par1ItemStack) {
        //if (this.getMaxDamage(par1ItemStack) - par1ItemStack.getDamage() > 0) {
        if (par1ItemStack.getDamageValue() < par1ItemStack.getMaxDamage() - 1) {
            return false;
        }else {
            return true;
        }
    }

    public boolean onEntitySwing(ItemStack stack, LivingEntity entity)
    {
        if(entity instanceof Player) {
            Player player = (Player) entity;
            if (stack.hasTag() && !entity.isUsingItem()) {
                CompoundTag nbt = stack.getTag();
                //int reloadset = nbt.getInt("reloadset");
                nbt.putInt("reloadset", 5);
            }
        }
        return false;
    }

    public boolean checkTags(ItemStack pitemstack) {
        if (pitemstack.hasTag()) {
            return true;
        }
        CompoundTag ltags = new CompoundTag();
        pitemstack.setTag(ltags);
        ltags.putInt("Reload", 0x0000);
        ltags.putByte("Bolt", (byte)0);
        CompoundTag lammo = new CompoundTag();
        for (int li = 0; li < getMaxDamage(); li++) {
            lammo.putLong(Integer.toString(li), 0L);
        }
        //ltags.setCompoundTag("Ammo", lammo);
        return false;
    }

    protected boolean cycleBolt(ItemStack pItemstack) {
        checkTags(pItemstack);
        CompoundTag lnbt = pItemstack.getTag();
        byte lb = lnbt.getByte("Bolt");
        if (lb <= 0) {
//				if (pReset) resetBolt(pItemstack);
            return true;
        } else {
            lnbt.putByte("Bolt", --lb);
            return false;
        }
    }

    protected void resetBolt(ItemStack pItemstack) {
        checkTags(pItemstack);
        pItemstack.getTag().putByte("Bolt", getCycleCount(pItemstack));
    }

    public byte getCycleCount(ItemStack pItemstack) {
        return (byte) (this.cycle - 1);
    }

    public static void updateCheckinghSlot(Entity pEntity, ItemStack pItemstack) {
        if (pEntity instanceof Player) {
            Player lep = (Player)pEntity;
            Container lctr = lep.getInventory();
            for (int li = 0; li < lctr.getMaxStackSize(); li++) {
                ItemStack lis = (lctr.getItem(li));
                if (lis == pItemstack) {
                    lctr.setItem(li, pItemstack.copy());
                    break;
                }
            }
        }
    }
    //public static class Properties {

    public ItemGunBase LoadingEvent(BufferedReader br, String domain, String modid) {
        GunBase_LoadStatus.load(this, br, domain);
        this.gun_mod_id = modid;
        return this;
    }
    //}


    public ItemStack sight = new ItemStack((Item)null);
    public ItemStack light = new ItemStack((Item)null);
    public ItemStack supp = new ItemStack((Item)null);
    public ItemStack grip = new ItemStack((Item)null);

    public float am_sight_x = 0;
    public float am_sight_y = 1.6F;
    public float am_sight_z = 1.8F;
    public float am_sightbase_x = 0;
    public float am_sightbase_y = 1.6F;
    public float am_sightbase_z = 1.8F;
    public float am_sightbase_z1 = 1.8F;

    public boolean rightmode = false;
    public float rightposx = -0.295F;
    public float rightposy = 1.3F;
    public float rightposz = 3.2F;
    public float roterightx = 0F;
    public float roterighty = 0F;
    public float roterightz = 0F;
    public int lighttime = 0;
    public IModelCustom am_light_model = null;
    public String am_light_tex = "gvclib:textures/model/ar.png";
    public boolean am_light = false;
    public float am_light_x = -0.295F;
    public float am_light_y = 1.3F;
    public float am_light_z = 3.2F;
    public float am_light_xr= 0F;
    public float am_light_yr= 0F;
    public float am_light_zr= 0F;
    public float[] am_colorlevel = new float[1024];
    public int am_light_kazu = 1;
    public int now_lightid = 0;




    public float supuposx = 0.0F;
    public float supuposy = 1.2F;
    public float supuposz = 5.8F;
    public IModelCustom am_supu_model = null;
    public String am_supu_tex = "gvclib:textures/model/ar.png";
    public boolean am_supu = false;
    public float am_supu_x = 0.0F;
    public float am_supu_y = 1.2F;
    public float am_supu_z = 5.8F;
    public float am_supu_xr = 0.0F;
    public float am_supu_yr = 0.0F;
    public float am_supu_zr = 0.0F;

    public float gripposx = 0.0F;
    public float gripposy = 0.85F;
    public float gripposz = 3.5F;
    public IModelCustom am_grip_model = null;
    public String am_grip_tex = "gvclib:textures/model/ar.png";
    public boolean am_grip = false;
    public float am_grip_x = 0.0F;
    public float am_grip_y = 0.85F;
    public float am_grip_z = 3.5F;
    public float am_grip_xr = 0.0F;
    public float am_grip_yr = 0.0F;
    public float am_grip_zr = 0.0F;

    public double am_grip_recoil = 0.4F;
    public double recoilbase = 0.0F;
    public float am_grip_bure = 0.8F;
    public float burebase = 0.0F;

    public boolean am_bullet = false;
    public int p_id = 0;
    public int p_level;
    public int p_time;
    public int p_idbase = 0;
    public float exlevelbase;
    public int poworbase;
    public int pelletbase;

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

        CompoundTag compoundtag = itemstack.getOrCreateTag();
        if(id == 101){
            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;
                    }
                }
            }
            /*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 (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;
                    }
                }
            }
        }
        if(id == 8){
            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;
                    }
                }
            }
        }
        if(id == 9){
            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;
                    }
                }
            }
        }


        return flag;
    }



}
