use of net.minecraft.entity.ai.pathing.EntityNavigation in project Sprout by Toadstool-Studios.
the class FindPlantGoal method start.
@Override
public void start() {
super.start();
if (targetPosition != null && !elephant.getIfEating()) {
EntityNavigation nav = elephant.getNavigation();
nav.startMovingTo(targetPosition.getX() + 0.5, targetPosition.getY() + 0.75, targetPosition.getZ() + 0.5, 0.3);
}
}
use of net.minecraft.entity.ai.pathing.EntityNavigation in project lithium-fabric by CaffeineMC.
the class LongJumpTaskMixin method run.
/**
* @author 2No2Name
* @reason only evaluate 20+ instead of ~100 possible jumps without affecting behavior
* [VanillaCopy] the whole method, commented changes
*/
@Overwrite
public void run(ServerWorld serverWorld, MobEntity mobEntity, long l) {
this.potentialTargets.clear();
this.potentialWeights.clear();
int potentialTotalWeight = 0;
this.lastTarget = Optional.empty();
this.cooldown = MAX_COOLDOWN;
this.targets.clear();
this.lastPos = Optional.of(mobEntity.getPos());
BlockPos goatPos = mobEntity.getBlockPos();
int goatX = goatPos.getX();
int goatY = goatPos.getY();
int goatZ = goatPos.getZ();
Iterable<BlockPos> iterable = BlockPos.iterate(goatX - this.horizontalRange, goatY - this.verticalRange, goatZ - this.horizontalRange, goatX + this.horizontalRange, goatY + this.verticalRange, goatZ + this.horizontalRange);
EntityNavigation entityNavigation = mobEntity.getNavigation();
BlockPos.Mutable targetPosCopy = new BlockPos.Mutable();
for (BlockPos targetPos : iterable) {
if (goatX == targetPos.getX() && goatZ == targetPos.getZ()) {
continue;
}
double squaredDistance = targetPos.getSquaredDistance(goatPos);
// Optimization: Evaluate the flight path check later (after random selection, but before world can be modified)
if (entityNavigation.isValidPosition(targetPos) && mobEntity.getPathfindingPenalty(LandPathNodeMaker.getLandNodeType(mobEntity.world, targetPosCopy.set(targetPos))) == 0.0F) {
this.potentialTargets.add(targetPos.asLong());
int weight = MathHelper.ceil(squaredDistance);
this.potentialWeights.add((short) weight);
potentialTotalWeight += weight;
}
}
// up to MAX_COOLDOWN random targets can be selected in keepRunning, so only this number of targets needs to be generated
while (this.targets.size() < MAX_COOLDOWN) {
// the number of random calls will be different from vanilla, but this is not reasonably detectable (not affecting world generation)
if (potentialTotalWeight == 0) {
// collection is empty/fully consumed, no more possible targets available
return;
}
int chosenIndex = findIndex(this.potentialWeights, serverWorld.random.nextInt(potentialTotalWeight));
long chosenPos = this.potentialTargets.getLong(chosenIndex);
short chosenWeight = this.potentialWeights.set(chosenIndex, (short) 0);
potentialTotalWeight -= chosenWeight;
// Very expensive method call, it shifts bounding boxes around and checks for collisions with them
Optional<Vec3d> optional = this.getRammingVelocity(mobEntity, Vec3d.ofCenter(targetPosCopy.set(chosenPos)));
// noinspection OptionalIsPresent
if (optional.isPresent()) {
// the weight in Target should be unused, as the random selection already took place
this.targets.add(new LongJumpTask.Target(new BlockPos(targetPosCopy), optional.get(), chosenWeight));
}
}
}
use of net.minecraft.entity.ai.pathing.EntityNavigation in project lithium-fabric by CaffeineMC.
the class MobEntityMixin method updateNavigationRegistration.
@Override
public void updateNavigationRegistration() {
if (this.isRegisteredToWorld()) {
EntityNavigation navigation = this.getNavigation();
if (this.registeredNavigation != navigation) {
((ServerWorldExtended) this.world).setNavigationInactive((MobEntity) (Object) this);
this.registeredNavigation = navigation;
if (navigation.getCurrentPath() != null) {
((ServerWorldExtended) this.world).setNavigationActive((MobEntity) (Object) this);
}
}
}
}
use of net.minecraft.entity.ai.pathing.EntityNavigation in project lithium-fabric by CaffeineMC.
the class ServerEntityHandlerMixin method startListeningOnEntityLoad.
@Redirect(method = "startTracking(Lnet/minecraft/entity/Entity;)V", at = @At(value = "INVOKE", target = "Ljava/util/Set;add(Ljava/lang/Object;)Z"))
private boolean startListeningOnEntityLoad(Set<MobEntity> set, Object mobEntityObj) {
MobEntity mobEntity = (MobEntity) mobEntityObj;
EntityNavigation navigation = mobEntity.getNavigation();
((NavigatingEntity) mobEntity).setRegisteredToWorld(navigation);
if (navigation.getCurrentPath() != null) {
((ServerWorldExtended) this.outer).setNavigationActive(mobEntity);
}
return set.add(mobEntity);
}
use of net.minecraft.entity.ai.pathing.EntityNavigation in project lithium-fabric by CaffeineMC.
the class ServerEntityHandlerMixin method stopListeningOnEntityUnload.
@Redirect(method = "stopTracking(Lnet/minecraft/entity/Entity;)V", at = @At(value = "INVOKE", target = "Ljava/util/Set;remove(Ljava/lang/Object;)Z"))
private boolean stopListeningOnEntityUnload(Set<MobEntity> set, Object mobEntityObj) {
MobEntity mobEntity = (MobEntity) mobEntityObj;
NavigatingEntity navigatingEntity = (NavigatingEntity) mobEntity;
if (navigatingEntity.isRegisteredToWorld()) {
EntityNavigation registeredNavigation = navigatingEntity.getRegisteredNavigation();
if (registeredNavigation.getCurrentPath() != null) {
((ServerWorldExtended) this.outer).setNavigationInactive(mobEntity);
}
navigatingEntity.setRegisteredToWorld(null);
}
return set.remove(mobEntity);
}
Aggregations