package com.thebrokenrail.twine.entity; import com.thebrokenrail.twine.Twine; import net.minecraft.entity.ai.goal.Goal; import net.minecraft.entity.ai.pathing.Path; import net.minecraft.entity.mob.MobEntityWithAi; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; import net.minecraft.world.WorldView; import net.minecraft.world.chunk.Chunk; import net.minecraft.world.chunk.ChunkStatus; public class StandOnGlowingObsidianGoal extends Goal { private final MobEntityWithAi mob; private Vec3d targetPos; private Path path; public StandOnGlowingObsidianGoal(MobEntityWithAi mob) { super(); this.mob = mob; } private boolean findTargetPos() { BlockPos blockPos = mob.getBlockPos(); BlockPos.Mutable mutable = new BlockPos.Mutable(); for (int k = 0; k <= Twine.AI_MAX_Y_DIFFERENCE; k = k > 0 ? -k : 1 - k) { for (int l = 0; l < Twine.AI_RANGE; ++l) { for (int m = 0; m <= l; m = m > 0 ? -m : 1 - m) { for (int n = m < l && m > -l ? l : 0; n <= l; n = n > 0 ? -n : 1 - n) { mutable.set(blockPos, m, k - 1, n); if (mob.isInWalkTargetRange(mutable) && isTargetPos(mob.world, mutable)) { targetPos = new Vec3d(mutable.getX() + 0.5d, mutable.getY(), mutable.getZ() + 0.5d); return true; } } } } } return false; } @Override public boolean canStart() { if ((mob.getHealth() / mob.getMaxHealth()) <= 0.5f && findTargetPos()) { path = mob.getNavigation().findPathTo(targetPos.x, targetPos.y, targetPos.z, 0); return path != null; } else { return false; } } @Override public boolean shouldContinue() { return !mob.getNavigation().isIdle(); } @Override public void start() { mob.getNavigation().startMovingAlong(path, 1f); } @Override public void stop() { targetPos = null; } private boolean isTargetPos(WorldView world, BlockPos pos) { Chunk chunk = world.getChunk(pos.getX() >> 4, pos.getZ() >> 4, ChunkStatus.FULL, false); if (chunk == null) { return false; } else { return chunk.getBlockState(pos).isOf(Twine.GLOWING_OBSIDIAN) && chunk.getBlockState(pos.up()).isAir() && chunk.getBlockState(pos.up(2)).isAir(); } } }