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 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 = new Vector(pp.x, pp.y, pp.z).toLocation(player.getWorld()).getBlock();
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 PlayerNavigation method supertrimPath.
protected void supertrimPath() {
if (this.path == null)
return;
for (int var0 = 0; var0 < this.path.getNodeCount(); var0++) {
Node var1 = this.path.getNode(var0);
Node var2 = (var0 + 1 < this.path.getNodeCount()) ? this.path.getNode(var0 + 1) : null;
BlockState var3 = this.level.getBlockState(new BlockPos(var1.x, var1.y, var1.z));
if (var3.is(BlockTags.CAULDRONS)) {
this.path.replaceNode(var0, var1.cloneAndMove(var1.x, var1.y + 1, var1.z));
if (var2 != null && var1.y >= var2.y)
this.path.replaceNode(var0 + 1, var1.cloneAndMove(var2.x, var1.y + 1, var2.z));
}
}
}
use of net.minecraft.world.level.pathfinder.Node in project Citizens2 by CitizensDev.
the class PlayerNavigation method recomputePath.
@Override
public void recomputePath(BlockPos var0) {
if (this.path == null || this.path.isDone() || this.path.getNodeCount() == 0)
return;
Node var1 = this.path.getEndNode();
Vec3 var2 = new Vec3((var1.x + this.mob.getX()) / 2.0D, (var1.y + this.mob.getY()) / 2.0D, (var1.z + this.mob.getZ()) / 2.0D);
if (var0.closerThan(var2, (this.path.getNodeCount() - this.path.getNextNodeIndex())))
recomputePath();
}
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;
}
Aggregations