package hmggirlfront.entity;

import java.util.Collection;
import java.util.List;

import gvclib.entity.living.EntityGVCLivingBase;
import gvclib.entity.living.EntityVehicleBase;
import gvclib.entity.living.ISoldier;
import gvclib.event.GVCPotion;
import gvclib.item.ItemMagazine;
import hmggirlfront.mod_GirlFront;
import hmggirlfront.items.ItemDollAttachment;
import hmggirlfront.items.ItemDollCN;
import hmggirlfront.items.ItemDollGunBase;
import hmggirlfront.network.GFClientMessageKeyPressed;
import hmggirlfront.network.GFPacketHandler;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityAIFleeSun;
import net.minecraft.entity.ai.EntityAIRestrictSun;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.item.EntityFireworkRocket;
import net.minecraft.entity.monster.IMob;
import net.minecraft.entity.passive.IAnimals;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Items;
import net.minecraft.init.MobEffects;
import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumHand;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.DifficultyInstance;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.fml.common.FMLCommonHandler;

public class EntityDoll extends EntityDollBase implements IAnimals, ISoldier {

	
	
	 
	
	public EntityDoll(World worldIn) {
		super(worldIn);
		this.setSize(0.5F, 1.8F);
		this.biped = false;
		
		this.squad_followrange = mod_GirlFront.cfg_doll_followrange;
	}

	protected void initEntityAI() {
		this.tasks.addTask(1, new EntityAISwimming(this));
		this.tasks.addTask(2, new EntityAIRestrictSun(this));
		this.tasks.addTask(3, new EntityAIFleeSun(this, 1.0D));

	}

	protected void applyEntityAttributes() {
		super.applyEntityAttributes();
		this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(20D + this.getArmID_L() + this.getArmID_A());
		// this.getEntityAttribute(SharedMonsterAttributes.KNOCKBACK_RESISTANCE).setBaseValue(20D);
		this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.25D);
		this.getEntityAttribute(SharedMonsterAttributes.FOLLOW_RANGE).setBaseValue(50.0D);
	}

	
	
	protected void setEquipmentBasedOnDifficulty(DifficultyInstance difficulty) {
		super.setEquipmentBasedOnDifficulty(difficulty);
		
	}
	
	public double getYOffset()
    {
		//-0.35D;
        return 0.1D;
    }
	
	public boolean attackEntityFrom(DamageSource source, float par2)
    {
    	Entity entity;
    	entity = source.getTrueSource();
    	
		if(entity != null){
			if(entity instanceof EntityPlayer && !mod_GirlFront.cfg_friendlyfire_player_to_doll){
				if(FMLCommonHandler.instance().getMinecraftServerInstance() != null) {
					for (EntityPlayerMP playermp : FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayers())
						GFPacketHandler.INSTANCE2.sendTo(new GFClientMessageKeyPressed(801, this.getEntityId()), playermp);
				}
				return false;
			}
			if(entity instanceof EntityDollBase){
				/*for (int k = 0; k < 20; ++k)
	            {
	                double d2 = this.rand.nextGaussian() * 0.02D;
	                double d0 = this.rand.nextGaussian() * 0.02D;
	                double d1 = this.rand.nextGaussian() * 0.02D;
	                this.world.spawnParticle(EnumParticleTypes.EXPLOSION_NORMAL, this.posX + (double)(this.rand.nextFloat() * this.width * 2.0F) - (double)this.width, this.posY + (double)(this.rand.nextFloat() * this.height), this.posZ + (double)(this.rand.nextFloat() * this.width * 2.0F) - (double)this.width, d2, d0, d1);
	            }*/
				if(FMLCommonHandler.instance().getMinecraftServerInstance() != null) {
					for (EntityPlayerMP playermp : FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayers())
						GFPacketHandler.INSTANCE2.sendTo(new GFClientMessageKeyPressed(801, this.getEntityId()), playermp);
				}
				//GFPacketHandler.INSTANCE2.sendToAll(new GFClientMessageKeyPressed(801, this.getEntityId()));
	    		return false;
	        }else {
				int ran = this.world.rand.nextInt(100);
				if (ran > evasion) {
					if(doll_armor > 0) {
						if(par2 <= doll_armor) {
							par2 = 1;
						}else {
							par2 = par2 - doll_armor;
						}
						this.playSound(SoundEvents.ITEM_SHIELD_BLOCK, 1F, 1F);
						if(FMLCommonHandler.instance().getMinecraftServerInstance() != null) {
							for (EntityPlayerMP playermp : FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayers())
								GFPacketHandler.INSTANCE2.sendTo(new GFClientMessageKeyPressed(802, this.getEntityId()), playermp);
						}
						//GFPacketHandler.INSTANCE2.sendToAll(new GFClientMessageKeyPressed(802, this.getEntityId()));
					}
					return super.attackEntityFrom(source, par2);
				} else {
					if(FMLCommonHandler.instance().getMinecraftServerInstance() != null) {
						for (EntityPlayerMP playermp : FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayers())
							GFPacketHandler.INSTANCE2.sendTo(new GFClientMessageKeyPressed(801, this.getEntityId()), playermp);
					}
					//GFPacketHandler.INSTANCE2.sendToAll(new GFClientMessageKeyPressed(801, this.getEntityId()));
					/*for (int k = 0; k < 20; ++k) {
						double d2 = this.rand.nextGaussian() * 0.02D;
						double d0 = this.rand.nextGaussian() * 0.02D;
						double d1 = this.rand.nextGaussian() * 0.02D;
						this.world.spawnParticle(EnumParticleTypes.EXPLOSION_NORMAL,
								this.posX + (double) (this.rand.nextFloat() * this.width * 2.0F) - (double) this.width,
								this.posY + (double) (this.rand.nextFloat() * this.height),
								this.posZ + (double) (this.rand.nextFloat() * this.width * 2.0F) - (double) this.width,
								d2, d0, d1);
					}*/
					return false;
				}
	        }
		}else{
			return super.attackEntityFrom(source, par2);
		}
    }
	
	public boolean CanAttack(Entity entity){
    	if(entity instanceof IMob && ((EntityLivingBase) entity).getHealth() > 0.0F){
    		return true;
    	}else
    	{
    		return false;
    	}
    }
	
	/**
     * Determines if an entity can be despawned, used on idle far away entities
     */
    protected boolean canDespawn()
    {
    	return false;
    }
	
	protected void dropLoot(boolean wasRecentlyHit, int lootingModifier, DamageSource source)
    {
        //this.dropFewItems(wasRecentlyHit, lootingModifier);
        //this.dropEquipment(wasRecentlyHit, lootingModifier);
    }
	
	public void getbeacon() {
    }
	
	public boolean usehand = false;
	
	public boolean processInteract(EntityPlayer player, EnumHand hand)
    {
		/*{
    		this.rotationYaw = this.rotationYaw + 5;
    		this.rotationYawHead = this.rotationYawHead + 5;
    		this.prevRotationYaw = this.prevRotationYaw + 5;
    		this.renderYawOffset = this.renderYawOffset + 5;
    	}
    	return true;
    	*/
		
    	ItemStack itemstack = player.getHeldItem(hand);
        boolean flag = !itemstack.isEmpty();
        
        boolean can_shrink = false;
        
        if(flag) {
        	if(itemstack.getItem() == Items.IRON_AXE) {//銃落とす
				ItemStack gun = this.getHeldItemMainhand();
				if(!gun.isEmpty() && !this.world.isRemote) {
					this.entityDropItem(gun, 0.0F);
    				itemstack.shrink(1);
				}
        	}
        }
        
        if(this.isOwner(player)){
        	if(this.isRiding() && this.getRidingEntity() != null && player.isSneaking()) {
        		Entity vehicle = this.getRidingEntity();
        		this.dismountRidingEntity();
        	}else if (this.getHealth() <= 0.0F) {
        		if(player.isSneaking()) {
    				NBTTagCompound nbt = player.getEntityData();
    				if(nbt != null) {
    					nbt.setInteger("vi", this.getEntityId());
                		player.openGui(mod_GirlFront.INSTANCE, 0,
                				player.world, (int) player.posX, (int) player.posY, (int) player.posZ);
    				}
    			}else {
    				if(flag) {
    					if(itemstack.getItem() == mod_GirlFront.idoll_doll_repair) {
    	        			this.deathTime = 0;
    	        			this.setHealth(this.getMaxHealth());
    	    				itemstack.shrink(1);
    	    				this.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, 1.0F);
    	    			}
    	        		if(itemstack.getItem() == mod_GirlFront.idoll_doll_dismantling  &&  player == this.getOwner()) {
    	        			ItemStack gun = this.getHeldItemMainhand();
            				if(!gun.isEmpty() && !this.world.isRemote) {
            					if(gun.getItem() instanceof ItemDollGunBase) {
            						ItemDollGunBase gunbase = (ItemDollGunBase) gun.getItem();
            						if(gunbase.doll_d_core > 0) {
            							this.entityDropItem(new ItemStack(mod_GirlFront.idoll_core, gunbase.doll_d_core), 0.0F);
            						}
            					}
            					this.entityDropItem(gun, 0.0F);
        	    				//itemstack.shrink(1);
            				}
            				this.setOwnerId(null);
    	        			if (!this.world.isRemote) {
    	        				
    							this.setDead();
    						}
    	    			}
    	        		if(itemstack.getItem() == mod_GirlFront.idoll_parts) {
    	    				if(this.getHealth() < this.getMaxHealth()) {
    	    					this.setHealth(this.getHealth() + 1);
    	        				itemstack.shrink(1);
    	    				}else {
    	    					this.deathTime = 0;
    	    					itemstack.shrink(1);
    	    				}
    	    				this.playSound(SoundEvents.ITEM_SHIELD_BREAK, 1.0F, 1.0F);
    	    			}
    				}
    			}
        		
        	}else {
        		if(flag) {
        			if(itemstack.getItem() == mod_GirlFront.idoll_pledge && this.getArmID_L() >= 50) {//誓約
        				this.setPledge(1);
        				itemstack.shrink(1);
        				this.playSound(SoundEvents.UI_TOAST_CHALLENGE_COMPLETE, 1.0F, 1.0F);
        				{
        					for (int k = 0; k < 20; ++k) {
        						double d2 = this.rand.nextGaussian() * 0.02D;
        						double d0 = this.rand.nextGaussian() * 0.02D;
        						double d1 = this.rand.nextGaussian() * 0.02D;
        						this.world.spawnParticle(EnumParticleTypes.HEART,
        								this.posX + (double) (this.rand.nextFloat() * this.width * 2.0F) - (double) this.width,
        								this.posY + (double) (this.rand.nextFloat() * this.height),
        								this.posZ + (double) (this.rand.nextFloat() * this.width * 2.0F) - (double) this.width,
        								d2, d0, d1);
        					}
        				}
        				/* if (!this.world.isRemote)
        			        {
        			            ItemStack firework = new ItemStack(Items.FIREWORKS, 1);
        			            EntityFireworkRocket entityfireworkrocket = 
        			            		new EntityFireworkRocket(this.world, (double)((float)this.posX), (double)((float)this.posY+2), (double)((float)this.posZ), firework);
        			            this.world.spawnEntity(entityfireworkrocket);
        			        }*/
        				
        			}
        			else if(itemstack.getItem() == mod_GirlFront.idoll_food) {//食料
        				this.setRemain_A(this.getRemain_A() + 1);
        				itemstack.shrink(1);
        				this.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, 1.0F);
        			}
        			else if(itemstack.getItem() == bullet_type) {//弾薬
        				this.setRemain_S(this.getRemain_S() + 1);
        				itemstack.shrink(1);
        				this.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, 1.0F);
        			}
        			else if(itemstack.getItem() instanceof ItemMagazine) {//弾薬
        				ItemMagazine mag = (ItemMagazine) itemstack.getItem();
						if(mag.magazine_tab.equals(this.bullet_type_tab)) {
							this.setRemain_S(this.getRemain_S() + 1);
	        				itemstack.shrink(1);
	        				this.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, 1.0F);
						}
        			}
        			else if(itemstack.getItem() == mod_GirlFront.idoll_doll_repair) {//全回復
	        			this.setHealth(this.getMaxHealth());
	    				itemstack.shrink(1);
	    				this.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, 1.0F);
	    			}
        			else if(itemstack.getItem() == mod_GirlFront.idoll_doll_report) {//作戦報告書
        				itemstack.shrink(1);
        				this.setArmID_S(this.getArmID_S() + 20);
        				this.playSound(SoundEvents.ENTITY_PLAYER_LEVELUP, 1.0F, 1.0F);
	    			
	    			}else if(itemstack.getItem() instanceof ItemDollAttachment) {//アタッチメント
        				ItemDollAttachment am = (ItemDollAttachment) itemstack.getItem();
        				ItemStack itemstackcopy = itemstack.copy();
        				if(am.id == 101) {//追加サイト系
        					ItemStack head = this.getItemStackFromSlot(EntityEquipmentSlot.HEAD);
            				if(!head.isEmpty()) {
            					if(!this.world.isRemote) 
            					{
									this.entityDropItem(head, 0.0F);
									this.setItemStackToSlot(EntityEquipmentSlot.HEAD, itemstackcopy);
            					}
            				}else {
            					if(!this.world.isRemote) 
            					{
									this.setItemStackToSlot(EntityEquipmentSlot.HEAD, itemstackcopy);
            					}
            				}
            				can_shrink = true;
        				}else
        				if(am.id == 50) {//特殊弾
        					ItemStack chest = this.getItemStackFromSlot(EntityEquipmentSlot.CHEST);
        						if (!mainitem.isEmpty() && mainitem.getItem() instanceof ItemDollGunBase) 
        				    	{
        							ItemDollGunBase gun = (ItemDollGunBase)mainitem.getItem();
        							for(int ic = 0; ic < gun.canuse_bullet.length; ++ic) {
            							if(gun.canuse_bullet[ic] != null && gun.canuse_bullet[ic].equals(am.bullet_name)) {
            								if(!chest.isEmpty()) {
            	            					if(!this.world.isRemote) 
            	            					{
            										this.entityDropItem(chest, 0.0F);
            										this.setItemStackToSlot(EntityEquipmentSlot.CHEST, itemstackcopy);
            	            					}
            	            				}else {
            	            					if(!this.world.isRemote) 
            	            					{
            										this.setItemStackToSlot(EntityEquipmentSlot.CHEST, itemstackcopy);
            	            					}
            	            				}
            								can_shrink = true;
            							}
                					}
        				    	}
        				}else
        				if(am.id == 80) {//フレームとか
        					ItemStack legs = this.getItemStackFromSlot(EntityEquipmentSlot.LEGS);
        					if(gun_class != 5) {
        						if(!legs.isEmpty()) {
                					if(!this.world.isRemote)
                					{
    									this.entityDropItem(legs, 0.0F);
    									this.setItemStackToSlot(EntityEquipmentSlot.LEGS, itemstackcopy);
                					}
                				}else {
                					if(!this.world.isRemote) 
                					{
    									this.setItemStackToSlot(EntityEquipmentSlot.LEGS, itemstackcopy);
                					}
                				}
        						can_shrink = true;
        					}
        				}else
            				if(am.id == 81) {//SG専用
            					ItemStack legs = this.getItemStackFromSlot(EntityEquipmentSlot.LEGS);
            					if(gun_class == 5) {
            						if(!legs.isEmpty()) {
                    					if(!this.world.isRemote)
                    					{
        									this.entityDropItem(legs, 0.0F);
        									this.setItemStackToSlot(EntityEquipmentSlot.LEGS, itemstackcopy);
                    					}
                    				}else {
                    					if(!this.world.isRemote) 
                    					{
        									this.setItemStackToSlot(EntityEquipmentSlot.LEGS, itemstackcopy);
                    					}
                    				}
            						can_shrink = true;
            					}
        				}else
        				if(am.id == 200) {//クラス処理
        					ItemStack feet = this.getItemStackFromSlot(EntityEquipmentSlot.FEET);
            				if(!feet.isEmpty()) {
            					if(!this.world.isRemote) 
            					{
									this.entityDropItem(feet, 0.0F);
									this.setItemStackToSlot(EntityEquipmentSlot.FEET, itemstackcopy);
            					}
            				}else {
            					if(!this.world.isRemote) 
            					{
									this.setItemStackToSlot(EntityEquipmentSlot.FEET, itemstackcopy);
            					}
            				}
            				can_shrink = true;
        				}
						if (!player.capabilities.isCreativeMode && can_shrink) {
							itemstack.shrink(itemstack.getCount());
						}
        				this.playSound(SoundEvents.BLOCK_STONE_BUTTON_CLICK_ON, 1.0F, 1.0F);
        				//this.playSound(SoundEvents.ENTITY_PLAYER_LEVELUP, 1.0F, 1.0F);
	    			}//アタッチメント
	    			else if(itemstack.getItem() == Items.EMERALD) {
	    				ItemStack head = this.getItemStackFromSlot(EntityEquipmentSlot.HEAD);
        				if(!head.isEmpty()) {
        					if(!this.world.isRemote) {
        						this.entityDropItem(head, 0.0F);
        						this.setItemStackToSlot(EntityEquipmentSlot.HEAD, head.EMPTY);
        					}
        				}
        				ItemStack chest = this.getItemStackFromSlot(EntityEquipmentSlot.CHEST);
        				if(!chest.isEmpty()) {
        					if(!this.world.isRemote) {
        						this.entityDropItem(chest, 0.0F);
        						this.setItemStackToSlot(EntityEquipmentSlot.CHEST, chest.EMPTY);
        					}
        				}
        				ItemStack legs = this.getItemStackFromSlot(EntityEquipmentSlot.LEGS);
        				if(!legs.isEmpty()) {
        					if(!this.world.isRemote) {
        						this.entityDropItem(legs, 0.0F);
        						this.setItemStackToSlot(EntityEquipmentSlot.LEGS, legs.EMPTY);
        					}
        				}
        				ItemStack feet = this.getItemStackFromSlot(EntityEquipmentSlot.FEET);
        				if(!feet.isEmpty()) {
        					if(!this.world.isRemote) {
        						this.entityDropItem(feet, 0.0F);
        						this.setItemStackToSlot(EntityEquipmentSlot.FEET, feet.EMPTY);
        					}
        				}
        			}
        			else if(itemstack.getItem() == mod_GirlFront.idoll_doll_dismantling  &&  player == this.getOwner()) {//解体準備
        				this.setHealth(0);
        			}
        			else if(itemstack.getItem() == Items.FEATHER) {
        				if(this.getFTMode() == 1) {
        					this.setFTMode(0);
        					if(this.world.isRemote)
        					player.sendMessage(new TextComponentTranslation("Gun Show", new Object[0]));
        				}else {
        					this.setFTMode(1);
        					if(this.world.isRemote)
        					player.sendMessage(new TextComponentTranslation("Gun Hidden", new Object[0]));
        				}
        			}
        			else if(itemstack.getItem() == Items.PAPER) {
        				if(this.getBoost()) {
        					this.setBoost(false);
        					if(this.world.isRemote)
        					player.sendMessage(new TextComponentTranslation("Status Show", new Object[0]));
        				}else {
        					this.setBoost(true);
        					if(this.world.isRemote)
        					player.sendMessage(new TextComponentTranslation("Status Hidden", new Object[0]));
        				}
        			}
        			else if(itemstack.getItem() == Items.REDSTONE) {
        				if(this.getBoosttime() == 0) {
        					this.setBoosttime(1);
        					if(this.world.isRemote)
        					player.sendMessage(new TextComponentTranslation("Skill OFF", new Object[0]));
        				}else {
        					this.setBoosttime(0);
        					if(this.world.isRemote)
        					player.sendMessage(new TextComponentTranslation("Skill ON", new Object[0]));
        				}
        			}
        			else if(itemstack.getItem() == mod_GirlFront.idoll_core) {
        				// 重装部隊
        	//			itemstack.shrink(1);
        				if(!this.getItemStackFromSlot(EntityEquipmentSlot.MAINHAND).isEmpty() && this.getItemStackFromSlot(EntityEquipmentSlot.MAINHAND).getItem() instanceof ItemDollGunBase) {
        					ItemDollGunBase gun = (ItemDollGunBase) this.getItemStackFromSlot(EntityEquipmentSlot.MAINHAND).getItem();
        					if(gun.hoplites_kazu >= 1) {
        						int kazu = 0;
        						double k = this.posX;
        						double l = this.posY;
        						double i = this.posZ;
        						int han = 20;
        						int bit = 0;
        						AxisAlignedBB axisalignedbb = (new AxisAlignedBB((double) (k - han), (double) (l - han),
        								(double) (i - han), (double) (k + han), (double) (l + han), (double) (i + han)))
        										.grow(1);
        						List<Entity> llist = this.world.getEntitiesWithinAABBExcludingEntity(this, axisalignedbb);
        						if (llist != null) {
        							for (int lj = 0; lj < llist.size(); lj++) {
        								Entity entity1 = (Entity) llist.get(lj);
        								if (entity1.canBeCollidedWith()  && entity1 != null) {
        									if (entity1 instanceof EntityDoll_Dummy) {
        										EntityDoll_Dummy dummy = (EntityDoll_Dummy) entity1;
        										if(dummy.getDummyID() == bit && this.getCustomNameTag().equals(dummy.getCustomNameTag())) {
        											++bit;
        										}
        									}
        								}
        							}
        						}
        						if(bit < gun.hoplites_kazu) {
        							{
        					        			float yaw = this.rotationYaw * (2 * (float) Math.PI / 360);
        										double mox = 0;
        										double moy = this.motionY;
        										double moz = 0;
        										
        										mox -= MathHelper.sin(yaw - 1.57F) * 2;
        										moz += MathHelper.cos(yaw - 1.57F) * 2;
        										{
        											EntityDoll_Dummy entityskeleton = new EntityDoll_Dummy(this.world);
        								            entityskeleton.setLocationAndAngles(this.posX + mox, this.posY+2, this.posZ + moz, this.rotationYaw, 0.0F);
        								            entityskeleton.setOwnerId_dummy(this.entityUniqueID);
        								            entityskeleton.setCustomNameTag(this.getCustomNameTag());
        								            if(this.getOwner() != null)entityskeleton.setTamedBy((EntityPlayer) this.getOwner());
        								            entityskeleton.setDummyID(bit);
        								            entityskeleton.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, this.getItemStackFromSlot(EntityEquipmentSlot.MAINHAND));
        								            if(gun.hoplites_subarm[bit] != null)entityskeleton.setItemStackToSlot(EntityEquipmentSlot.HEAD, new ItemStack(gun.hoplites_subarm[bit]));
        								            if(!this.world.isRemote) {
        								            	this.world.spawnEntity(entityskeleton);
        								            }
        										}
        							}
        						}
        					}
        				}
        			}
        			else if(player.isSneaking()) {
						if (itemstack.getItem() == mod_GirlFront.idoll_squad_cn) {//分隊登録
							ItemDollCN cn = (ItemDollCN) itemstack.getItem();
							NBTTagCompound nbtcn = itemstack.getTagCompound();
							if (nbtcn != null) {
								int squadid = nbtcn.getInteger("squadid");
								this.setSquadID(squadid);
							}
						} else {
							NBTTagCompound nbt = player.getEntityData();
							if (nbt != null) {
								nbt.setInteger("vi", this.getEntityId());
								player.openGui(mod_GirlFront.INSTANCE, 0,
										player.world, (int) player.posX, (int) player.posY, (int) player.posZ);
							}
						}
        			}else {
        				if(!(this.getRidingEntity() != null && this.getRidingEntity() instanceof EntityVehicleBase)) {
        					if(this.getMoveT() == 1) {
            					this.setMoveT(0);
            					if(this.world.isRemote)
            					player.sendMessage(new TextComponentTranslation("hmggirlfront.order.follow.name", new Object[0]));
            				}
            				else if(this.getMoveT() == 0) {
            					this.setMoveT(3);
            					if(this.world.isRemote)
            					player.sendMessage(new TextComponentTranslation("hmggirlfront.order.stay.name", new Object[0]));
            				}
            				else if(this.getMoveT() == 3) {
            					this.setMoveT(1);
            					if(this.world.isRemote)
            					player.sendMessage(new TextComponentTranslation("hmggirlfront.order.free.name", new Object[0]));
            				}
        				}
        			}
        		}else {//NoFlag
        			if(player.isSneaking()) {
        				NBTTagCompound nbt = player.getEntityData();
        				if(nbt != null) {
        					nbt.setInteger("vi", this.getEntityId());
                    		player.openGui(mod_GirlFront.INSTANCE, 0,
                    				player.world, (int) player.posX, (int) player.posY, (int) player.posZ);
        				}
        			}else {
        				if(!(this.getRidingEntity() != null && this.getRidingEntity() instanceof EntityVehicleBase)) {
        					if(this.getMoveT() == 1) {
            					this.setMoveT(0);
            					if(this.world.isRemote)
            					player.sendMessage(new TextComponentTranslation("hmggirlfront.order.follow.name", new Object[0]));
            				}else
            				if(this.getMoveT() == 0) {
            					this.setMoveT(3);
            					if(this.world.isRemote)
            					player.sendMessage(new TextComponentTranslation("hmggirlfront.order.stay.name", new Object[0]));
            				}
            				else if(this.getMoveT() == 3) {
            					this.setMoveT(1);
            					if(this.world.isRemote)
            					player.sendMessage(new TextComponentTranslation("hmggirlfront.order.free.name", new Object[0]));
            				}
            				/*else if(this.getMoveT() == 3) {
            					this.setMoveT(0);
            					if(this.world.isRemote)
            					player.sendMessage(new TextComponentTranslation("hmggirlfront.order.follow.name", new Object[0]));
            				}////*///
        				}
        			}
        		}
        	}
        	if(!flag && this.getMoveT() == 3) {
        		usehand = true;
        	}
            return true;
        }else {//noOwner
        	/*if(player.isSneaking()) {
				NBTTagCompound nbt = player.getEntityData();
				if(nbt != null) {
					nbt.setInteger("vi", this.getEntityId());
            		player.openGui(mod_GirlFront.INSTANCE, 0,
            				player.world, (int) player.posX, (int) player.posY, (int) player.posZ);
				}
			}else///*///
        	if(flag) {
        		if(itemstack.getItem() == Items.DIAMOND) {
    				this.setsquad(player.getDisplayNameString());
    				this.setTamedBy(player);
    				itemstack.shrink(1);
    				if(this.world.isRemote)
    				player.sendMessage(new TextComponentTranslation("hmggirlfront.order.follow.name", new Object[0]));
    			}else
        		if (this.getHealth() <= 0.0F) {
        			if(itemstack.getItem() == mod_GirlFront.idoll_doll_dismantling  &&  player == this.getOwner()) {
	        			ItemStack gun = this.getHeldItemMainhand();
        				if(!gun.isEmpty() && !this.world.isRemote) {
        					if(gun.getItem() instanceof ItemDollGunBase) {
        						ItemDollGunBase gunbase = (ItemDollGunBase) gun.getItem();
        						if(gunbase.doll_d_core > 0) {
        							this.entityDropItem(new ItemStack(mod_GirlFront.idoll_core, gunbase.doll_d_core), 0.0F);
        						}
        					}
        					this.entityDropItem(gun, 0.0F);
    	    				//itemstack.shrink(1);
        				}
	        			if (!this.world.isRemote) {
							this.setDead();
						}
	    			}
        		}else {
        			if(itemstack.getItem() == mod_GirlFront.idoll_doll_dismantling  &&  player == this.getOwner()) {
        				this.setHealth(0);
        			}
        		}
        		
        	}
        	return true;
        }
       /**/
    }
	
	/*public boolean processInteract(EntityPlayer player, EnumHand hand)
    {
    	this.rotationYaw = this.rotationYawHead = this.rotation =  this.rotationYaw + 1;
		return true;
    }*/
	
	 protected void onDeathUpdate() {
		 ++this.deathTicks;
		 this.addPotionEffect(new PotionEffect(MobEffects.GLOWING, 10, 10));
		 if(this.deathTicks == 1) {
			 List llist = this.world.getEntitiesWithinAABBExcludingEntity(this, this.getEntityBoundingBox()
		        		.expand(this.motionX, this.motionY, this.motionZ).grow(60.0D));
				if (llist != null) {
					for (int lj = 0; lj < llist.size(); lj++) {
						Entity entity1 = (Entity) llist.get(lj);
						if (entity1.canBeCollidedWith() && entity1 != null) {
							if (entity1 instanceof EntityLivingBase && entity1 instanceof EntityPlayer)
	                        {
								if (this.isOwner((EntityLivingBase) entity1)) 
								{
									EntityPlayer player = (EntityPlayer) entity1;
									if(this.world.isRemote && this.isOwner(player) &&  player == this.getOwner()) {
					    				//player.sendMessage(new TextComponentTranslation(this.getCustomNameTag() + "hmggirlfront.order.dead.name", new Object[0]));
										TextComponentTranslation aaa = new TextComponentTranslation("hmggirlfront.order.dead.name", new Object[0]);
										//player.sendMessage(new TextComponentTranslation("hmggirlfront.order.dead.name", new Object[0]));
										//player.sendMessage(new TextComponentTranslation(this.getCustomNameTag() + "  is a dangerous situation!", new Object[0]));
										player.sendMessage(new TextComponentTranslation(this.getCustomNameTag() + "  is down!", new Object[0]));
									break;
									}
								}
	                        }
						}
					}
				}
		 }
		}
	 
	 
	 /**
	     * Will get destroyed next tick.
	     */
	    public void setDead()
	    {
	    	/*if (this.getHealth() <= 0.0F) {
	    		this.isDead = true;
	    	}else {
	    		//for(int i = 0; i < 100; ++i) 
	    		{
	    			System.out.println(this.getCustomNameTag() + " : WHY?");
	    		}
	    	}
	        */
	    	Chunk chunk = world.getChunkFromBlockCoords(this.getPosition());
	    	int i = MathHelper.floor(this.posX / 16.0D);
	        int j = MathHelper.floor(this.posZ / 16.0D);
	        if (i != chunk.x || j != chunk.z)
	        {
	        	
	        }else if (!this.isRiding()) {
	    		this.isDead = true;
	    		System.out.println(this.getCustomNameTag() + " : isDead");
	    	}else {
	    		//for(int i = 0; i < 100; ++i) 
	    		{
	    			System.out.println(this.getCustomNameTag() + " : WHY?");
	    		}
	    	}
	        dummy0 = null;
	        dummy1 = null;
	        dummy2 = null;
	    }
	 
	    /**
	     * sets the dead flag. Used when you fall off the bottom of the world.
	     */
	    protected void outOfWorld()
	    {
	        //this.setDead();
	    	//for(int i = 0; i < 100; ++i) 
	    	{
	    		BlockPos spawn = world.getSpawnPoint();
	    		this.setPositionAndUpdate(spawn.getX(), spawn.getY(), spawn.getZ());
    			System.out.println(this.getCustomNameTag() + " : WHERE?");
    		}
	    }
	 
	 
	 
	 //getArmID_R  武器の種類＝モブのID
	 //getArmID_L  モブのレベル
	 //getArmID_S  EXP
	 //getRemain_L 残弾
	 //getRemain_R スキル
	 //getRemain_S　弾薬
	 //getRemain_A　飯
	/**
	 * Called frequently so the entity can update its state every tick as
	 * required. For example, zombies and skeletons use this to react to
	 * sunlight and start to burn.
	 */
	// public String sound;
	 //public String modid;
	
	    public int moveT5 = 0;
	    public int search_dummy =0;
	    public int dummy_count;
	    public EntityDoll_Dummy dummy0 = null;
	    public EntityDoll_Dummy dummy1 = null;
	    public EntityDoll_Dummy dummy2 = null;
	    public EntityDoll_Dummy dummy3 = null;
	    
	public void onLivingUpdate() {
		super.onLivingUpdate();
		this.biped = false;
		this.updateArmSwingProgress();
		if(this.ontick10sec == 0 && usehand) {
			usehand = false;
		}
		if(usehand && this.ontick10sec % 20 == 0) {
			this.world.spawnParticle(EnumParticleTypes.HEART, this.posX, this.posY + 1.5D * this.doll_size, this.posZ, 0.0D, 0.0D, 0.0D, new int[0]);
		}
		if (this.getHealth() > 0.0F) {
			this.deathTicks = 0;
		}
		
		//何が何でも銃は手放さない
	//	if (this.getRidingEntity() == null) 
		{// 1
			if( this.getItemStackFromSlot(EntityEquipmentSlot.MAINHAND).isEmpty()) {
				if(!mainitem.isEmpty()) {
					this.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, mainitem);
				}
			}
		}
		
		if (this.getParachute() == 1) {
			this.motionY *= 0.8F;
			if(this.onGround && !this.isPotionActive(MobEffects.LEVITATION)) {
				this.setParachute(0);
			}
		}
		
		/*NBTTagCompound nbt = this.getEntityData();
		if(nbt != null && this.world.getWorldTime() %10 == 0) {
			ResourceLocation resourcelocation = new ResourceLocation(nbt.getString("id"));
			System.out.println(this.getCustomNameTag() + (Object)resourcelocation);
		}*/
		
		GFEntityID gfid = new GFEntityID(this);
		int id = gfid.getID();
		
		DollAI_Level.load(this);
		DollAI_Reload.load(this);
		DollAI_ItemCatch.load(this);
		DollAI_Class.load(this);
		DollAI_MasterArm.load(this);
		//DollAI_MasterArm_H.load(this);
		DollAI_Skill.load(this);
		if(this.getBoosttime() == 0)DollAI_AutoSkill.load(this);
		
		
		
		//　寄生虫対策に実装。ポーション効果をバニラ以外は無効化
		if(mod_GirlFront.cfg_doll_other_vanillapotion && !this.world.isRemote){
			 Collection<PotionEffect> collection = this.getActivePotionEffects();

		        if (!collection.isEmpty())
		        {
		        	for (PotionEffect potioneffect : collection)
		            {
						Potion potion = potioneffect.getPotion();
						if(!GVCPotion.getVaniraPotion(potion)) {
							this.clearActivePotions();
							break;
						}
		            }
		        }
			
		}
		
		
		
		if(drone_inv) {
			++drone_inv_count;
			if(drone_inv_count > 10) {
				drone_inv = false;
				drone_inv_count = 0;
			}
		}
		
		//　重装部隊用
		if(!this.getItemStackFromSlot(EntityEquipmentSlot.MAINHAND).isEmpty() && this.getItemStackFromSlot(EntityEquipmentSlot.MAINHAND).getItem() instanceof ItemDollGunBase) {
			ItemDollGunBase gun = (ItemDollGunBase) this.getItemStackFromSlot(EntityEquipmentSlot.MAINHAND).getItem();
			if(gun.hoplites_kazu >= 1) {
				
				for(int dum = 0; dum < gun.hoplites_kazu; ++dum) {
					int kazu = 0;
					double k = this.posX;
					double l = this.posY;
					double i = this.posZ;
					int han = 20;
					AxisAlignedBB axisalignedbb = (new AxisAlignedBB((double) (k - han), (double) (l - han),
							(double) (i - han), (double) (k + han), (double) (l + han), (double) (i + han)))
									.grow(10);
					List<Entity> llist = this.world.getEntitiesWithinAABBExcludingEntity(this, axisalignedbb);
					if (llist != null) {
						for (int lj = 0; lj < llist.size(); lj++) {
							Entity entity1 = (Entity) llist.get(lj);
							if (entity1.canBeCollidedWith()  && entity1 != null) {
								if (entity1 instanceof EntityDoll_Dummy) {
									EntityDoll_Dummy dummy = (EntityDoll_Dummy) entity1;
									if(dum == 0) {
										if(dummy0 == null) {
											if(dummy.getDummyID() == dum && this.getCustomNameTag().equals(dummy.getCustomNameTag())) {
											}else {
												dummy0 = spawn_dummy(gun, dum);
											}
										}
									}
									if(dum == 1) {
										if(dummy1 == null) {
											if(dummy.getDummyID() == dum && this.getCustomNameTag().equals(dummy.getCustomNameTag())) {
											}else {
												dummy1 = spawn_dummy(gun, dum);
											}
										}
									}
									if(dum == 2) {
										if(dummy2 == null) {
											if(dummy.getDummyID() == dum && this.getCustomNameTag().equals(dummy.getCustomNameTag())) {
											}else {
												dummy2 = spawn_dummy(gun, dum);
											}
										}
									}
								}
							}
						}
					}
					
				}
				
				/*
				int kazu = 0;
				double k = this.posX;
				double l = this.posY;
				double i = this.posZ;
				int han = 20;
				int bit = 0;
				int bitbase = 0;
				boolean flag = false;
				AxisAlignedBB axisalignedbb = (new AxisAlignedBB((double) (k - han), (double) (l - han),
						(double) (i - han), (double) (k + han), (double) (l + han), (double) (i + han)))
								.grow(10);
				List<Entity> llist = this.world.getEntitiesWithinAABBExcludingEntity(this, axisalignedbb);
				if (llist != null) {
					for (int lj = 0; lj < llist.size(); lj++) {
						Entity entity1 = (Entity) llist.get(lj);
						if (entity1.canBeCollidedWith()  && entity1 != null) {
							if (entity1 instanceof EntityDoll_Dummy) {
								EntityDoll_Dummy dummy = (EntityDoll_Dummy) entity1;
								if(dummy.getDummyID() == bit && this.getCustomNameTag().equals(dummy.getCustomNameTag())) {
									++bit;
								}
								++bitbase;
							}
						}
					}
				}
				if(bit < gun.hoplites_kazu) {
					++search_dummy;
					this.setMoveT(this.getMoveT());
					if(search_dummy > 20){
			        			float yaw = this.rotationYaw * (2 * (float) Math.PI / 360);
								double mox = 0;
								double moy = this.motionY;
								double moz = 0;
								
								mox -= MathHelper.sin(yaw - 1.57F) * 2;
								moz += MathHelper.cos(yaw - 1.57F) * 2;
								{
									EntityDoll_Dummy entityskeleton = new EntityDoll_Dummy(this.world);
						            entityskeleton.setLocationAndAngles(this.posX + mox, this.posY+2, this.posZ + moz, this.rotationYaw, 0.0F);
						            entityskeleton.setOwnerId_dummy(this.entityUniqueID);
						            entityskeleton.setCustomNameTag(this.getCustomNameTag());
						            if(this.getOwner() != null)entityskeleton.setTamedBy((EntityPlayer) this.getOwner());
						            entityskeleton.setDummyID(bit);
						            entityskeleton.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, this.getItemStackFromSlot(EntityEquipmentSlot.MAINHAND));
						            if(gun.hoplites_subarm[bit] != null)entityskeleton.setItemStackToSlot(EntityEquipmentSlot.HEAD, new ItemStack(gun.hoplites_subarm[bit]));
						            if(!this.world.isRemote) {
						            	this.world.spawnEntity(entityskeleton);
						            }
								}
					}
				}else {
					//this.cooltime = 0;
					search_dummy = 0;
				}*/
				
				if(this.getMoveT() == 5) {
					++moveT5;
					if(moveT5 > 400) {
						this.setMoveT(3);
					}
				}else {
					moveT5 = 0;
				}
				
				
			}
		}
	}
	
	public EntityDoll_Dummy spawn_dummy(ItemDollGunBase gun, int bit){
		float yaw = this.rotationYaw * (2 * (float) Math.PI / 360);
		double mox = 0;
		double moy = this.motionY;
		double moz = 0;
		
		mox -= MathHelper.sin(yaw - 1.57F) * 2;
		moz += MathHelper.cos(yaw - 1.57F) * 2;
		{
			EntityDoll_Dummy entityskeleton = new EntityDoll_Dummy(this.world);
            entityskeleton.setLocationAndAngles(this.posX + mox, this.posY+2, this.posZ + moz, this.rotationYaw, 0.0F);
            entityskeleton.setOwnerId_dummy(this.entityUniqueID);
            entityskeleton.setCustomNameTag(this.getCustomNameTag());
            if(this.getOwner() != null)entityskeleton.setTamedBy((EntityPlayer) this.getOwner());
            entityskeleton.setDummyID(bit);
            entityskeleton.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, this.getItemStackFromSlot(EntityEquipmentSlot.MAINHAND));
            if(gun.hoplites_subarm[bit] != null)entityskeleton.setItemStackToSlot(EntityEquipmentSlot.HEAD, new ItemStack(gun.hoplites_subarm[bit]));
            if(!this.world.isRemote) {
            	this.world.spawnEntity(entityskeleton);
            }
            return entityskeleton;
		}
		
	}
}