2016-08-15 17:09:01 -04:00
|
|
|
package WayofTime.bloodmagic.entity.ai;
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
import WayofTime.bloodmagic.entity.mob.EntityDemonBase;
|
2016-08-15 17:09:01 -04:00
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.block.state.IBlockState;
|
|
|
|
import net.minecraft.entity.EntityLivingBase;
|
|
|
|
import net.minecraft.entity.ai.EntityAIBase;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.init.Blocks;
|
|
|
|
import net.minecraft.pathfinding.PathNavigate;
|
|
|
|
import net.minecraft.pathfinding.PathNavigateGround;
|
|
|
|
import net.minecraft.pathfinding.PathNodeType;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.util.math.MathHelper;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public class EntityAIFollowOwner extends EntityAIBase {
|
|
|
|
World theWorld;
|
|
|
|
float maxDist;
|
|
|
|
float minDist;
|
2016-08-29 18:56:21 -04:00
|
|
|
private EntityDemonBase thePet;
|
2016-08-15 17:09:01 -04:00
|
|
|
private EntityLivingBase theOwner;
|
|
|
|
private double followSpeed;
|
|
|
|
private PathNavigate petPathfinder;
|
|
|
|
private int timeToRecalcPath;
|
|
|
|
private float oldWaterCost;
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public EntityAIFollowOwner(EntityDemonBase thePetIn, double followSpeedIn, float minDistIn, float maxDistIn) {
|
2016-08-15 17:09:01 -04:00
|
|
|
this.thePet = thePetIn;
|
2016-12-12 19:56:36 -08:00
|
|
|
this.theWorld = thePetIn.getEntityWorld();
|
2016-08-15 17:09:01 -04:00
|
|
|
this.followSpeed = followSpeedIn;
|
|
|
|
this.petPathfinder = thePetIn.getNavigator();
|
|
|
|
this.minDist = minDistIn;
|
|
|
|
this.maxDist = maxDistIn;
|
|
|
|
this.setMutexBits(3);
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (!(thePetIn.getNavigator() instanceof PathNavigateGround)) {
|
2016-08-15 17:09:01 -04:00
|
|
|
throw new IllegalArgumentException("Unsupported mob type for FollowOwnerGoal");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the EntityAIBase should begin execution.
|
|
|
|
*/
|
2017-08-15 21:30:48 -07:00
|
|
|
public boolean shouldExecute() {
|
2016-08-15 17:09:01 -04:00
|
|
|
EntityLivingBase entitylivingbase = this.thePet.getOwner();
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (entitylivingbase == null) {
|
2016-08-15 17:09:01 -04:00
|
|
|
return false;
|
2017-08-15 21:30:48 -07:00
|
|
|
} else if (entitylivingbase instanceof EntityPlayer && ((EntityPlayer) entitylivingbase).isSpectator()) {
|
2016-08-15 17:09:01 -04:00
|
|
|
return false;
|
2017-08-15 21:30:48 -07:00
|
|
|
} else if (this.thePet.isStationary()) {
|
2016-08-15 17:09:01 -04:00
|
|
|
return false;
|
2017-08-15 21:30:48 -07:00
|
|
|
} else if (this.thePet.getDistanceSqToEntity(entitylivingbase) < (double) (this.minDist * this.minDist)) {
|
2016-08-15 17:09:01 -04:00
|
|
|
return false;
|
2017-08-15 21:30:48 -07:00
|
|
|
} else {
|
2016-08-15 17:09:01 -04:00
|
|
|
this.theOwner = entitylivingbase;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether an in-progress EntityAIBase should continue executing
|
|
|
|
*/
|
2017-08-15 21:30:48 -07:00
|
|
|
public boolean continueExecuting() {
|
2016-08-15 17:09:01 -04:00
|
|
|
return !this.petPathfinder.noPath() && this.thePet.getDistanceSqToEntity(this.theOwner) > (double) (this.maxDist * this.maxDist) && !this.thePet.isStationary();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute a one shot task or start executing a continuous task
|
|
|
|
*/
|
2017-08-15 21:30:48 -07:00
|
|
|
public void startExecuting() {
|
2016-08-15 17:09:01 -04:00
|
|
|
this.timeToRecalcPath = 0;
|
|
|
|
this.oldWaterCost = this.thePet.getPathPriority(PathNodeType.WATER);
|
|
|
|
this.thePet.setPathPriority(PathNodeType.WATER, 0.0F);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the task
|
|
|
|
*/
|
2017-08-15 21:30:48 -07:00
|
|
|
public void resetTask() {
|
2016-08-15 17:09:01 -04:00
|
|
|
this.theOwner = null;
|
|
|
|
this.petPathfinder.clearPathEntity();
|
|
|
|
this.thePet.setPathPriority(PathNodeType.WATER, this.oldWaterCost);
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
private boolean isEmptyBlock(BlockPos pos) {
|
2016-08-15 17:09:01 -04:00
|
|
|
IBlockState iblockstate = this.theWorld.getBlockState(pos);
|
|
|
|
Block block = iblockstate.getBlock();
|
2016-12-12 19:56:36 -08:00
|
|
|
return block == Blocks.AIR || !iblockstate.isFullCube();
|
2016-08-15 17:09:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the task
|
|
|
|
*/
|
2017-08-15 21:30:48 -07:00
|
|
|
public void updateTask() {
|
2016-08-15 17:09:01 -04:00
|
|
|
this.thePet.getLookHelper().setLookPositionWithEntity(this.theOwner, 10.0F, (float) this.thePet.getVerticalFaceSpeed());
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (!this.thePet.isStationary()) {
|
|
|
|
if (--this.timeToRecalcPath <= 0) {
|
2016-08-15 17:09:01 -04:00
|
|
|
this.timeToRecalcPath = 10;
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (!this.petPathfinder.tryMoveToEntityLiving(this.theOwner, this.followSpeed)) {
|
|
|
|
if (!this.thePet.getLeashed()) {
|
|
|
|
if (this.thePet.getDistanceSqToEntity(this.theOwner) >= 144.0D) {
|
2016-12-12 19:56:36 -08:00
|
|
|
int i = MathHelper.floor(this.theOwner.posX) - 2;
|
|
|
|
int j = MathHelper.floor(this.theOwner.posZ) - 2;
|
|
|
|
int k = MathHelper.floor(this.theOwner.getEntityBoundingBox().minY);
|
2016-08-15 17:09:01 -04:00
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
for (int l = 0; l <= 4; ++l) {
|
|
|
|
for (int i1 = 0; i1 <= 4; ++i1) {
|
|
|
|
if ((l < 1 || i1 < 1 || l > 3 || i1 > 3) && this.theWorld.getBlockState(new BlockPos(i + l, k - 1, j + i1)).isTopSolid() && this.isEmptyBlock(new BlockPos(i + l, k, j + i1)) && this.isEmptyBlock(new BlockPos(i + l, k + 1, j + i1))) {
|
2016-08-15 17:09:01 -04:00
|
|
|
this.thePet.setLocationAndAngles((double) ((float) (i + l) + 0.5F), (double) k, (double) ((float) (j + i1) + 0.5F), this.thePet.rotationYaw, this.thePet.rotationPitch);
|
|
|
|
this.petPathfinder.clearPathEntity();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|