package hmggirlfront.items;

import gvclib.item.ItemGunBase;
import gvclib.network.GVCLClientMessageKeyPressed;
import gvclib.network.GVCLPacketHandler;
import hmggirlfront.mod_GirlFront;
import hmggirlfront.entity.EntityDoll;
import hmggirlfront.entity.EntityDoll_HL;
import hmggirlfront.event.level.WorldDifficultyLevel_new;
import net.minecraft.block.BlockLiquid;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Enchantments;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.stats.StatList;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;

public class ItemDollGun_HL extends ItemDollGunBase {
	public static String ads;

	public ItemDollGun_HL() {
		super();
		this.maxStackSize = 1;
	}

	public void onUpdate(ItemStack itemstack, World world, Entity entity, int i, boolean flag) {
		EntityPlayer entityplayer = (EntityPlayer) entity;
		int s;
		int li = getMaxDamage() - itemstack.getItemDamage();
		boolean lflag = cycleBolt(itemstack);
		boolean var5 = entityplayer.capabilities.isCreativeMode
				|| EnchantmentHelper.getEnchantmentLevel(Enchantments.INFINITY, itemstack) > 0;
		Item item = itemstack.getItem();
		ItemStack item_m = entityplayer.getHeldItemMainhand();
		ItemStack item_o = entityplayer.getHeldItemOffhand();
		NBTTagCompound nbt = itemstack.getTagCompound();

		if(itemstack.hasTagCompound()) {
			this.gunbase_recoil(itemstack, world, entity, i, flag, 1);
			if(!entityplayer.isHandActive())this.gunbase_cocking(itemstack, world, entity, i, flag);
			this.gunbase_reload(itemstack, world, entity, i, flag);
			this.gunbase_lockon(itemstack, world, entity, i, flag);
		}

		boolean gethand = false;
		if(itemstack == entityplayer.getHeldItemOffhand()) {
			gethand = true;
		}else if(flag){
			gethand = true;
		}
		if (gethand) {
			entityplayer.motionX = entityplayer.motionX * this.motion;
			//entityplayer.motionY = entityplayer.motionY * 0.1;
			entityplayer.motionZ = entityplayer.motionZ * this.motion;
		}

		if (!itemstack.isEmpty() && entityplayer != null) {
			if (itemstack.getItem() instanceof ItemGunBase) {
				ItemGunBase gun = (ItemGunBase) itemstack.getItem();
				this.Attachment(gun, itemstack, world, entityplayer, i, flag);
			}
		}
		
		//this.Fire_SR(itemstack, world, entityplayer, li, flag);
		super.onUpdate(itemstack, world, entity, i, flag);
	}

	@Override
	public byte getCycleCountrecoilre(ItemStack pItemstack) {
		return 3;
	}

	public void onPlayerStoppedUsing(ItemStack par1ItemStack, World par2World, EntityLivingBase par3EntityPlayer,
			int par4) {
		
	}

	
	
	
	
	
	public void spawnCreature(World worldIn, EntityPlayer playerIn, double par4, double par5, double par6)
    {

    	{
    		++par5;
			int var12 = MathHelper.floor((double) (playerIn.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
			EntityDoll_HL ent = new EntityDoll_HL(worldIn);
			ent.setLocationAndAngles(par4 + 0.5, par5, par6 + 0.5, var12, 0.0F);
			{
				ent.setItemStackToSlot(EntityEquipmentSlot.HEAD, new ItemStack(this));
			}
			ent.setcanDespawn(1);
			//ent.setsquad(playerIn.getDisplayNameString());
			//ent.setTamedBy(playerIn);
			ent.setMoveT(0);
			//if(main != null && main instanceof ItemDollGunBase) 
			/*{
				ItemDollGunBase gun = (ItemDollGunBase) this;
				ent.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(20D + 1 + gun.doll_hp);
				ent.setHealth((float) (20D + 1 + gun.doll_hp));
			}*/
			
			worldIn.spawnEntity(ent);
			
    	}
    	
	}
    
    public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
    {
		ItemStack itemStackIn = player.getHeldItem(hand);
		int par4 = pos.getX();
		int par5 = pos.getY();
		int par6 = pos.getZ();
		
		if (worldIn.isRemote)
        {
			return EnumActionResult.PASS;
        }
        else
		{
        	if (!player.capabilities.isCreativeMode)
            {
        		itemStackIn.shrink(1);
            }
        	spawnCreature(worldIn, player, (double)pos.getX(), (double)pos.getY(), (double)pos.getZ());
        	return EnumActionResult.PASS;
        }
    }
	
	/**
     * Called when the equipped item is right clicked.
     */
    public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn)
    {
    	ItemStack itemStackIn = playerIn.getHeldItem(handIn);
        if (worldIn.isRemote)
        {
            return new ActionResult(EnumActionResult.PASS, itemStackIn);
        }
        else
        {
            RayTraceResult raytraceresult = this.rayTrace(worldIn, playerIn, true);

            if (raytraceresult != null && raytraceresult.typeOfHit == RayTraceResult.Type.BLOCK)
            {
                BlockPos blockpos = raytraceresult.getBlockPos();

                if (!(worldIn.getBlockState(blockpos).getBlock() instanceof BlockLiquid))
                {
                    return new ActionResult(EnumActionResult.PASS, itemStackIn);
                }
                else if (worldIn.isBlockModifiable(playerIn, blockpos) && playerIn.canPlayerEdit(blockpos, raytraceresult.sideHit, itemStackIn))
                {
                    spawnCreature(worldIn, playerIn, (double)blockpos.getX(), (double)blockpos.getY(), (double)blockpos.getZ());
                    {

                    	if (!playerIn.capabilities.isCreativeMode)
                        {
                    		itemStackIn.shrink(1);
                        }

                        playerIn.addStat(StatList.getObjectUseStats(this));
                        return new ActionResult(EnumActionResult.SUCCESS, itemStackIn);
                    }
                }
                else
                {
                    return new ActionResult(EnumActionResult.FAIL, itemStackIn);
                }
            }
            else
            {
                return new ActionResult(EnumActionResult.PASS, itemStackIn);
            }
        }
    }
    
    
    /**rotationがプレイヤーに追従*/
	public boolean ridding_roteplayer = true;
	public int riddng_maximum = 1;
	public double[] riddingx = new double[32];
	public double[] riddingy = new double[32];
	public double[] riddingz = new double[32];
	
	public double[] riddingx_roteplayer = new double[32];
	public double[] riddingy_roteplayer = new double[32];
	public double[] riddingz_roteplayer = new double[32];
	
	@Deprecated
	public boolean ridding_rote;
	
	public boolean zoom = true;
	public boolean ridding_invisible = false;
	public boolean ridding_f = false;
	public boolean ridding_t = false;
	public float ridding_view1_x = 0F;
	public float ridding_view1_y = 0F;
	public float ridding_view1_z = 0F;
	public float ridding_view_x = 0F;
	public float ridding_view_y = -1F;
	public float ridding_view_z = -2F;
	@Deprecated
	public float ridding_view_gunner_x = 0F;
	@Deprecated
	public float ridding_view_gunner_y = 2F;
	@Deprecated
	public float ridding_view_gunner_z = 1F;
	public boolean biped = false;
	public float ridding_zoom = 2.0F;
	public String hud_icon = "gvclib:textures/hud/cross.png";
	public boolean render_hud_icon_hori = false;
	public String hud_icon_hori = "gvclib:textures/hud/cross.png";
	public boolean render_hud_scope = false;
	public String hud_icon_scope = "gvclib:textures/misc/scope.png";
	public boolean render_hud_scope_zoom = false;
	public String hud_icon_scope_zoom = "gvclib:textures/misc/scope.png";
	public boolean renderhud = false;
	public boolean render_rader = true;
	public boolean riddng_opentop = true;
	public float ridding_damege = 0.25F;
	
	public boolean aarader = false;
	
	
	public int vehicletype = 0;
	public String movesound = null;
	public double maxhp = 200D;
	public float setwidth = 1F;
	public float setheight = 1F;
	public float antibullet_0 = 1;
	public float antibullet_1 = 1;
	public float antibullet_2 = 1;
	public float antibullet_3 = 1;
	public boolean can_turret = false;
    public float turret_height = 0;
    public float damage_front = 0;
    public float damage_side = 0;
    public float damage_rear = 0;
    public float damage_top = 0;
    public float damage_bottom = 0;
    
    public float damage_turret_front = 0;
    public float damage_turret_side = 0;
    public float damage_turret_rear = 0;
	public float sp = 0.025F;
	public float turnspeed = 1.5F;
	public float stepHeight = 1.5F;
	
	/**true時に移動時ブロックを破壊する*/
	public boolean roodbreak = false;
	
	public float thmax = 5F;
	public float thmin = -2F;
	public float thmaxa = 0.2F;
	public float thmina = -0.15F;
	
	public float rotationp_max = -90;
	public float rotationp_min = 90;
	
	public boolean ridding_rotation_lock = false;
	/**ridding_rotation_lockがtrueの時のみ*/
	public float rotation_max = 60;
	
	
	public float model_angle_base_x = 0;
	public float model_angle_base_y = 0;
	public float model_angle_base_z = 0;
	public float model_angle_offset_x = 0;
	public float model_angle_offset_y = 0;
	public float model_angle_offset_z = 0;
	public float model_angle_x = 0;
	public float model_angle_y = 0;
	public float model_angle_z = 0;
	
	public int mob_min_range = 20;
	public int mob_max_range = 60;
	public int mob_min_height = 10;
	
	public boolean weapon1true = true;
	public int weapon1key  = -1;
	public String w1name = "";
	public boolean w1missile_aam = true;
	public int ammo1 = 10;
	//public int magazine = 1;
	//public int reload_time1 = 80;
	public int reloadsoundset1;
	public String reloadsound1;
	public int w1cycle;
	public int w1barst;
	public int weapon1;//
	public boolean w1crossfire = false;
	public boolean w1can_cooldown = false;
	@Deprecated
	public float w1kakumax = 180;
	@Deprecated
	public float w1kakumin = -180;
	public int[] bullet_type1 = new int[256];
	public String[] bullet_model1 = new String[32];
	public String[] bullet_tex1 = new String[32];
	public String[] fire_model1 = new String[32];
	public String[] fire_tex1 = new String[32];
	public int[] fire_time1 = new int[32];
	public String[] sound_fire1 = new String[32];
	public float[] fire_pointx1 = new float[32];
	public float[] fire_pointy1 = new float[32];
	public float[] fire_pointz1 = new float[32];
	public float[] basis_pointx1 = new float[32];
	public float[] basis_pointy1 = new float[32];
	public float[] basis_pointz1 = new float[32];
	public double[] fire_yoffset1 = new double[32];
	public boolean[] rotationfollowing1 = new boolean[32];
	public boolean[] rotationfirepoint1 = new boolean[32];
	public boolean[] rotation_player1 = new boolean[32];
	/**回転軸中心をarm_x,arm_zに合わせるかどうか
	 * 回転軸が中心にない物に使用*/
	public boolean[] rotation_firepointbxbz1 = new boolean[32];
	/**ピッチ方向(rotationpitch)をthisに合わせる(航空機のpitchがサーバーに適応されないため、爆撃の処置)*/
	public boolean[] rotation_lock_pitch_vehicle1 = new boolean[32];
	public int[] bullet_damage1 = new int[32];
	public float[] bullet_speed1 = new float[32];
	public float[] bullet_bure1 = new float[32];
	public float[] bullet_expower1 = new float[32];
	public boolean[] bullet_ex1 = new boolean[32];
	public int[] bullet_kazu1 = new int[32];
	public double[] bullet_gravity1 = new double[32];
	public int[] bullet_livingtime1 = new int[32];
	
	
	
	
	public int mob_w1range = 50;
	public int mob_w1range_max_y = 20;
	public int mob_w1range_min_y = 3;
	
	
	
	
	
	
	public int turret;
	public boolean[] turretrote = new boolean[32];
	public float[] arm_x = new float[32];
	public float[] arm_y = new float[32];
	public float[] arm_z = new float[32];
    
	public float[] hand_x = new float[32];
	public float[] hand_y = new float[32];
	public float[] hand_z = new float[32];
	
	/**mat6*/
	public int[] finger_type = new int[32];
	
	public int pera;
	public float[] pera_x = new float[32];
	public float[] pera_y = new float[32];
	public float[] pera_z = new float[32];
	public float[] perarote_x = new float[32];
	public float[] perarote_y = new float[32];
	public float[] perarote_z = new float[32];
	
	public int cloud;
	public double[] cloud_x = new double[32];
	public double[] cloud_y = new double[32];
	public double[] cloud_z = new double[32];
	
	public int rader;
	public int[] raderturret = new int[32];
	public float[] rader_x = new float[32];
	public float[] rader_y = new float[32];
	public float[] rader_z = new float[32];
	public float[] raderrote_x = new float[32];
	public float[] raderrote_y = new float[32];
	public float[] raderrote_z = new float[32];
	
	
	
	public boolean bomber_sighting = false;
    public boolean spg_sighting = false;
    public int spg_min_range = 40;
    /**水陸両用*/
    public boolean amphibious = false;
	
    public int exhaust = 0;
    public float[] exhaust_x = new float[32];
    public float[] exhaust_y = new float[32];
    public float[] exhaust_z = new float[32];
    public boolean exhaust_ab = false;
    
    public int laser_sight = 0;
    public float[] laser_sight_x = new float[32];
    public float[] laser_sight_y = new float[32];
    public float[] laser_sight_z = new float[32];
    public int[] laser_sight_weapon = new int[32];
    public boolean[] rote_laser_sight = new boolean[32];
}
