use of net.minecraft.world.level.pathfinder.Node in project Citizens2 by CitizensDev.
the class NMSImpl method getTargetNavigator.
private MCNavigator getTargetNavigator(final org.bukkit.entity.Entity entity, final NavigatorParameters params, final Function<PathNavigation, Boolean> function) {
net.minecraft.world.entity.Entity raw = getHandle(entity);
raw.setOnGround(true);
// not sure of a better way around this - if onGround is false, then
// navigation won't execute, and calling entity.move doesn't
// entirely fix the problem.
final PathNavigation navigation = NMSImpl.getNavigation(entity);
final float oldWater = raw instanceof ServerPlayer ? ((EntityHumanNPC) raw).getPathfindingMalus(BlockPathTypes.WATER) : ((Mob) raw).getPathfindingMalus(BlockPathTypes.WATER);
if (params.avoidWater() && oldWater >= 0) {
if (raw instanceof ServerPlayer) {
((EntityHumanNPC) raw).setPathfindingMalus(BlockPathTypes.WATER, oldWater + 1F);
} else {
((Mob) raw).setPathfindingMalus(BlockPathTypes.WATER, oldWater + 1F);
}
}
return new MCNavigator() {
float lastSpeed;
CancelReason reason;
@Override
public CancelReason getCancelReason() {
return reason;
}
@Override
public Iterable<Vector> getPath() {
return new NavigationIterable(navigation);
}
@Override
public void stop() {
Path path = getPathEntity(navigation);
if (params.debug() && path != null) {
for (Player player : Bukkit.getOnlinePlayers()) {
for (int i = 0; i < path.getNodeCount(); i++) {
Node pp = path.getNode(i);
org.bukkit.block.Block block = player.getWorld().getBlockAt(pp.x, pp.y, pp.z);
player.sendBlockChange(block.getLocation(), block.getBlockData());
}
}
}
if (oldWater >= 0) {
if (raw instanceof ServerPlayer) {
((EntityHumanNPC) raw).setPathfindingMalus(BlockPathTypes.WATER, oldWater);
} else {
((Mob) raw).setPathfindingMalus(BlockPathTypes.WATER, oldWater);
}
}
navigation.stop();
}
@Override
public boolean update() {
if (params.speed() != lastSpeed) {
if (Messaging.isDebugging() && lastSpeed > 0) {
Messaging.debug("Repathfinding " + ((NPCHolder) entity).getNPC().getId() + " due to speed change from", lastSpeed, "to", params.speed());
}
Entity handle = getHandle(entity);
EntityDimensions size = null;
try {
size = (EntityDimensions) SIZE_FIELD_GETTER.invoke(handle);
if (handle instanceof AbstractHorse) {
SIZE_FIELD_SETTER.invoke(handle, new EntityDimensions(Math.min(0.99F, size.width), size.height, false));
}
} catch (Throwable e) {
e.printStackTrace();
}
if (!function.apply(navigation)) {
reason = CancelReason.STUCK;
}
try {
SIZE_FIELD_SETTER.invoke(handle, size);
} catch (Throwable e) {
e.printStackTrace();
// minecraft requires that an entity fit onto both blocks if width >= 1f, but we'd prefer to
// make it just fit on 1 so hack around it a bit.
}
lastSpeed = params.speed();
}
if (params.debug() && !navigation.isDone()) {
BlockData data = Material.DANDELION.createBlockData();
Path path = getPathEntity(navigation);
for (Player player : Bukkit.getOnlinePlayers()) {
for (int i = 0; i < path.getNodeCount(); i++) {
Node pp = path.getNode(i);
player.sendBlockChange(new Vector(pp.x, pp.y, pp.z).toLocation(player.getWorld()), data);
}
}
}
navigation.setSpeedModifier(params.speed());
return navigation.isDone();
}
};
}
use of net.minecraft.world.level.pathfinder.Node in project Citizens2 by CitizensDev.
the class NMSImpl method getTargetNavigator.
@Override
public MCNavigator getTargetNavigator(org.bukkit.entity.Entity entity, Iterable<Vector> dest, final NavigatorParameters params) {
List<Node> list = Lists.<Node>newArrayList(Iterables.<Vector, Node>transform(dest, (input) -> {
return new Node(input.getBlockX(), input.getBlockY(), input.getBlockZ());
}));
Node last = list.size() > 0 ? list.get(list.size() - 1) : null;
final Path path = new Path(list, last != null ? new BlockPos(last.x, last.y, last.z) : null, true);
return getTargetNavigator(entity, params, (input) -> {
return input.moveTo(path, params.speed());
});
}
use of net.minecraft.world.level.pathfinder.Node in project Citizens2 by CitizensDev.
the class PlayerNodeEvaluator method findAcceptedNode.
protected Node findAcceptedNode(int var0, int var1, int var2, int var3, double var4, Direction var6, BlockPathTypes var7) {
Node var8 = null;
BlockPos.MutableBlockPos var9 = new BlockPos.MutableBlockPos();
double var10 = getFloorLevel(var9.set(var0, var1, var2));
if (var10 - var4 > 1.125D)
return null;
BlockPathTypes var12 = getCachedBlockType(this.mob, var0, var1, var2);
float var13 = this.mob.getPathfindingMalus(var12);
double var14 = this.mob.getBbWidth() / 2.0D;
if (var13 >= 0.0F) {
var8 = getNode(var0, var1, var2);
var8.type = var12;
var8.costMalus = Math.max(var8.costMalus, var13);
}
if (var7 == BlockPathTypes.FENCE && var8 != null && var8.costMalus >= 0.0F && !canReachWithoutCollision(var8))
var8 = null;
if (var12 == BlockPathTypes.WALKABLE || (isAmphibious() && var12 == BlockPathTypes.WATER))
return var8;
if ((var8 == null || var8.costMalus < 0.0F) && var3 > 0 && var12 != BlockPathTypes.FENCE && var12 != BlockPathTypes.UNPASSABLE_RAIL && var12 != BlockPathTypes.TRAPDOOR && var12 != BlockPathTypes.POWDER_SNOW) {
var8 = findAcceptedNode(var0, var1 + 1, var2, var3 - 1, var4, var6, var7);
if (var8 != null && (var8.type == BlockPathTypes.OPEN || var8.type == BlockPathTypes.WALKABLE) && this.mob.getBbWidth() < 1.0F) {
double var16 = (var0 - var6.getStepX()) + 0.5D;
double var18 = (var2 - var6.getStepZ()) + 0.5D;
AABB var20 = new AABB(var16 - var14, getFloorLevel(this.level, var9.set(var16, (var1 + 1), var18)) + 0.001D, var18 - var14, var16 + var14, this.mob.getBbHeight() + getFloorLevel(this.level, var9.set(var8.x, var8.y, var8.z)) - 0.002D, var18 + var14);
if (hasCollisions(var20))
var8 = null;
}
}
if (!isAmphibious() && var12 == BlockPathTypes.WATER && !canFloat()) {
if (getCachedBlockType(this.mob, var0, var1 - 1, var2) != BlockPathTypes.WATER)
return var8;
while (var1 > this.mob.level.getMinBuildHeight()) {
var1--;
var12 = getCachedBlockType(this.mob, var0, var1, var2);
if (var12 == BlockPathTypes.WATER) {
var8 = getNode(var0, var1, var2);
var8.type = var12;
var8.costMalus = Math.max(var8.costMalus, this.mob.getPathfindingMalus(var12));
continue;
}
return var8;
}
}
if (var12 == BlockPathTypes.OPEN) {
int var16 = 0;
int var17 = var1;
while (var12 == BlockPathTypes.OPEN) {
var1--;
if (var1 < this.mob.level.getMinBuildHeight()) {
Node var18 = getNode(var0, var17, var2);
var18.type = BlockPathTypes.BLOCKED;
var18.costMalus = -1.0F;
return var18;
}
if (var16++ >= Setting.MC_NAVIGATION_MAX_FALL_DISTANCE.asInt() || var16++ >= this.mob.getMaxFallDistance()) {
Node var18 = getNode(var0, var1, var2);
var18.type = BlockPathTypes.BLOCKED;
var18.costMalus = -1.0F;
return var18;
}
var12 = getCachedBlockType(this.mob, var0, var1, var2);
var13 = this.mob.getPathfindingMalus(var12);
if (var12 != BlockPathTypes.OPEN && var13 >= 0.0F) {
var8 = getNode(var0, var1, var2);
var8.type = var12;
var8.costMalus = Math.max(var8.costMalus, var13);
break;
}
if (var13 < 0.0F) {
Node var18 = getNode(var0, var1, var2);
var18.type = BlockPathTypes.BLOCKED;
var18.costMalus = -1.0F;
return var18;
}
}
}
if (var12 == BlockPathTypes.FENCE) {
var8 = getNode(var0, var1, var2);
var8.closed = true;
var8.type = var12;
var8.costMalus = var12.getMalus();
}
return var8;
}
use of net.minecraft.world.level.pathfinder.Node in project Citizens2 by CitizensDev.
the class PlayerNodeEvaluator method getNeighbors.
@Override
public int getNeighbors(Node[] var0, Node var1) {
int var2 = 0;
int var3 = 0;
BlockPathTypes var4 = getCachedBlockType(this.mob, var1.x, var1.y + 1, var1.z);
BlockPathTypes var5 = getCachedBlockType(this.mob, var1.x, var1.y, var1.z);
if (this.mob.getPathfindingMalus(var4) >= 0.0F && var5 != BlockPathTypes.STICKY_HONEY)
var3 = Mth.floor(Math.max(1.0F, this.mob.maxUpStep));
double var6 = getFloorLevel(new BlockPos(var1.x, var1.y, var1.z));
Node var8 = findAcceptedNode(var1.x, var1.y, var1.z + 1, var3, var6, Direction.SOUTH, var5);
if (isNeighborValid(var8, var1))
var0[var2++] = var8;
Node var9 = findAcceptedNode(var1.x - 1, var1.y, var1.z, var3, var6, Direction.WEST, var5);
if (isNeighborValid(var9, var1))
var0[var2++] = var9;
Node var10 = findAcceptedNode(var1.x + 1, var1.y, var1.z, var3, var6, Direction.EAST, var5);
if (isNeighborValid(var10, var1))
var0[var2++] = var10;
Node var11 = findAcceptedNode(var1.x, var1.y, var1.z - 1, var3, var6, Direction.NORTH, var5);
if (isNeighborValid(var11, var1))
var0[var2++] = var11;
Node var12 = findAcceptedNode(var1.x - 1, var1.y, var1.z - 1, var3, var6, Direction.NORTH, var5);
if (isDiagonalValid(var1, var9, var11, var12))
var0[var2++] = var12;
Node var13 = findAcceptedNode(var1.x + 1, var1.y, var1.z - 1, var3, var6, Direction.NORTH, var5);
if (isDiagonalValid(var1, var10, var11, var13))
var0[var2++] = var13;
Node var14 = findAcceptedNode(var1.x - 1, var1.y, var1.z + 1, var3, var6, Direction.SOUTH, var5);
if (isDiagonalValid(var1, var9, var8, var14))
var0[var2++] = var14;
Node var15 = findAcceptedNode(var1.x + 1, var1.y, var1.z + 1, var3, var6, Direction.SOUTH, var5);
if (isDiagonalValid(var1, var10, var8, var15))
var0[var2++] = var15;
return var2;
}
use of net.minecraft.world.level.pathfinder.Node in project Citizens2 by CitizensDev.
the class PlayerNodeEvaluator method getStart.
@Override
public Node getStart() {
BlockPos.MutableBlockPos var1 = new BlockPos.MutableBlockPos();
int var0 = this.mob.getBlockY();
BlockState var2 = this.level.getBlockState(var1.set(this.mob.getX(), var0, this.mob.getZ()));
if (this.mob.canStandOnFluid(var2.getFluidState())) {
while (this.mob.canStandOnFluid(var2.getFluidState())) {
var0++;
var2 = this.level.getBlockState(var1.set(this.mob.getX(), var0, this.mob.getZ()));
}
var0--;
} else if (canFloat() && this.mob.isInWater()) {
while (var2.is(Blocks.WATER) || var2.getFluidState() == Fluids.WATER.getSource(false)) {
var0++;
var2 = this.level.getBlockState(var1.set(this.mob.getX(), var0, this.mob.getZ()));
}
var0--;
} else if (this.mob.isOnGround()) {
var0 = Mth.floor(this.mob.getY() + 0.5D);
} else {
BlockPos blockPos = this.mob.blockPosition();
while ((this.level.getBlockState(blockPos).isAir() || this.level.getBlockState(blockPos).isPathfindable(this.level, blockPos, PathComputationType.LAND)) && blockPos.getY() > this.mob.level.getMinBuildHeight()) blockPos = blockPos.below();
var0 = blockPos.above().getY();
}
BlockPos var3 = this.mob.blockPosition();
BlockPathTypes var4 = getCachedBlockType(this.mob, var3.getX(), var0, var3.getZ());
if (this.mob.getPathfindingMalus(var4) < 0.0F) {
AABB aABB = this.mob.getBoundingBox();
if (hasPositiveMalus(var1.set(aABB.minX, var0, aABB.minZ)) || hasPositiveMalus(var1.set(aABB.minX, var0, aABB.maxZ)) || hasPositiveMalus(var1.set(aABB.maxX, var0, aABB.minZ)) || hasPositiveMalus(var1.set(aABB.maxX, var0, aABB.maxZ))) {
Node var6 = getNode(var1);
var6.type = getBlockPathType(this.mob, var6.asBlockPos());
var6.costMalus = this.mob.getPathfindingMalus(var6.type);
return var6;
}
}
Node var5 = getNode(var3.getX(), var0, var3.getZ());
var5.type = getBlockPathType(this.mob, var5.asBlockPos());
var5.costMalus = this.mob.getPathfindingMalus(var5.type);
return var5;
}
Aggregations