aether-legacy/src/main/java/com/legacy/aether/server/entities/ai/EntityAIAttackContinuously.java
2017-01-21 12:07:36 -05:00

116 lines
4.1 KiB
Java

package com.legacy.aether.server.entities.ai;
import net.minecraft.entity.EntityCreature;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.ai.EntityAIBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.pathfinding.Path;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
public class EntityAIAttackContinuously extends EntityAIBase
{
World worldObj;
protected EntityCreature attacker;
protected int attackTick;
double speedTowardsTarget;
boolean longMemory;
Path entityPathEntity;
private double targetX;
private double targetY;
private double targetZ;
protected final int attackInterval = 20;
public EntityAIAttackContinuously(EntityCreature creature, double speedIn, boolean useLongMemory)
{
this.attacker = creature;
this.worldObj = creature.worldObj;
this.speedTowardsTarget = speedIn;
this.longMemory = useLongMemory;
this.setMutexBits(3);
}
@Override
public boolean shouldExecute()
{
EntityLivingBase entitylivingbase = this.attacker.getAttackTarget();
if (entitylivingbase == null)
{
return false;
}
else if (!entitylivingbase.isEntityAlive())
{
return false;
}
else
{
this.entityPathEntity = this.attacker.getNavigator().getPathToEntityLiving(entitylivingbase);
return this.entityPathEntity != null;
}
}
@Override
public boolean continueExecuting()
{
EntityLivingBase entitylivingbase = this.attacker.getAttackTarget();
return entitylivingbase == null ? false : (!entitylivingbase.isEntityAlive() ? false : (!this.longMemory ? !this.attacker.getNavigator().noPath() : (!this.attacker.isWithinHomeDistanceFromPosition(new BlockPos(entitylivingbase)) ? false : !(entitylivingbase instanceof EntityPlayer) || !((EntityPlayer)entitylivingbase).isSpectator() && !((EntityPlayer)entitylivingbase).isCreative())));
}
@Override
public void startExecuting()
{
this.attacker.getNavigator().setPath(this.entityPathEntity, this.speedTowardsTarget);
}
@Override
public void resetTask()
{
EntityLivingBase entitylivingbase = this.attacker.getAttackTarget();
if (entitylivingbase instanceof EntityPlayer && (((EntityPlayer)entitylivingbase).isSpectator() || ((EntityPlayer)entitylivingbase).isCreative()))
{
this.attacker.setAttackTarget((EntityLivingBase)null);
}
this.attacker.getNavigator().clearPathEntity();
}
@Override
public void updateTask()
{
EntityLivingBase entitylivingbase = this.attacker.getAttackTarget();
this.attacker.getLookHelper().setLookPositionWithEntity(entitylivingbase, 30.0F, 30.0F);
double d0 = this.attacker.getDistanceSq(entitylivingbase.posX, entitylivingbase.getEntityBoundingBox().minY, entitylivingbase.posZ);
if ((this.longMemory || this.attacker.getEntitySenses().canSee(entitylivingbase)) && (this.targetX == 0.0D && this.targetY == 0.0D && this.targetZ == 0.0D || entitylivingbase.getDistanceSq(this.targetX, this.targetY, this.targetZ) >= 1.0D || this.attacker.getRNG().nextFloat() < 0.05F))
{
this.targetX = entitylivingbase.posX;
this.targetY = entitylivingbase.getEntityBoundingBox().minY;
this.targetZ = entitylivingbase.posZ;
}
this.attackTick = Math.max(this.attackTick - 1, 0);
this.checkAndPerformAttack(entitylivingbase, d0);
}
protected void checkAndPerformAttack(EntityLivingBase p_190102_1_, double p_190102_2_)
{
double d0 = this.getAttackReachSqr(p_190102_1_);
if (p_190102_2_ <= d0 && this.attackTick <= 0)
{
this.attackTick = 20;
this.attacker.swingArm(EnumHand.MAIN_HAND);
this.attacker.attackEntityAsMob(p_190102_1_);
}
}
protected double getAttackReachSqr(EntityLivingBase attackTarget)
{
return (double)(this.attacker.width * 2.0F * this.attacker.width * 2.0F + attackTarget.width);
}
}