Try to avoid water more

This commit is contained in:
fullwall 2020-04-08 23:59:49 +08:00
parent ba13f6c389
commit 14ebf26935
9 changed files with 130 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import java.util.List;
import org.bukkit.Effect;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.util.Vector;
import com.google.common.collect.Lists;
@ -14,8 +15,12 @@ import net.citizensnpcs.api.ai.NavigatorParameters;
import net.citizensnpcs.api.ai.TargetType;
import net.citizensnpcs.api.ai.event.CancelReason;
import net.citizensnpcs.api.astar.AStarMachine;
import net.citizensnpcs.api.astar.pathfinder.BlockExaminer;
import net.citizensnpcs.api.astar.pathfinder.BlockSource;
import net.citizensnpcs.api.astar.pathfinder.ChunkBlockSource;
import net.citizensnpcs.api.astar.pathfinder.MinecraftBlockExaminer;
import net.citizensnpcs.api.astar.pathfinder.Path;
import net.citizensnpcs.api.astar.pathfinder.PathPoint;
import net.citizensnpcs.api.astar.pathfinder.VectorGoal;
import net.citizensnpcs.api.astar.pathfinder.VectorNode;
import net.citizensnpcs.api.npc.NPC;
@ -79,6 +84,20 @@ public class AStarNavigationStrategy extends AbstractPathStrategy {
@Override
public boolean update() {
if (!planned) {
params.examiner(new BlockExaminer() {
@Override
public float getCost(BlockSource source, PathPoint point) {
Vector pos = point.getVector();
Material in = source.getMaterialAt(pos);
Material above = source.getMaterialAt(pos.setY(pos.getY() + 1));
return params.avoidWater() && MinecraftBlockExaminer.isLiquid(in, above) ? 1F : 0F;
}
@Override
public PassableState isPassable(BlockSource source, PathPoint point) {
return PassableState.IGNORE;
}
});
Location location = npc.getEntity().getLocation();
VectorGoal goal = new VectorGoal(destination, (float) params.pathDistanceMargin());
setPlan(ASTAR.runFully(goal,

View File

@ -125,7 +125,7 @@ public class CitizensNavigator implements Navigator, Runnable {
defaultParams.updatePathRate(root.getInt("updatepathrate"));
}
defaultParams.speedModifier((float) root.getDouble("speedmodifier", 1F));
defaultParams.avoidWater(root.getBoolean("avoidwater"));
defaultParams.avoidWater(root.getBoolean("avoidwater"));
if (!root.getBoolean("usedefaultstuckaction") && defaultParams.stuckAction() == TeleportStuckAction.INSTANCE) {
defaultParams.stuckAction(null);
}

View File

@ -193,6 +193,7 @@ import net.minecraft.server.v1_10_R1.PacketPlayOutEntityTeleport;
import net.minecraft.server.v1_10_R1.PacketPlayOutPlayerInfo;
import net.minecraft.server.v1_10_R1.PathEntity;
import net.minecraft.server.v1_10_R1.PathPoint;
import net.minecraft.server.v1_10_R1.PathType;
import net.minecraft.server.v1_10_R1.PathfinderGoalSelector;
import net.minecraft.server.v1_10_R1.ReportedException;
import net.minecraft.server.v1_10_R1.ScoreboardTeam;
@ -461,6 +462,15 @@ public class NMSImpl implements NMSBridge {
// navigation won't execute, and calling entity.move doesn't
// entirely fix the problem.
final NavigationAbstract navigation = NMSImpl.getNavigation(entity);
final float oldWater = raw instanceof EntityPlayer ? ((EntityHumanNPC) raw).a(PathType.WATER)
: ((EntityInsentient) raw).a(PathType.WATER);
if (params.avoidWater() && oldWater >= 0) {
if (raw instanceof EntityPlayer) {
((EntityHumanNPC) raw).a(PathType.WATER, oldWater + 1F);
} else {
((EntityInsentient) raw).a(PathType.WATER, oldWater + 1F);
}
}
return new MCNavigator() {
float lastSpeed;
CancelReason reason;
@ -477,6 +487,13 @@ public class NMSImpl implements NMSBridge {
@Override
public void stop() {
if (oldWater >= 0) {
if (raw instanceof EntityPlayer) {
((EntityHumanNPC) raw).a(PathType.WATER, oldWater);
} else {
((EntityInsentient) raw).a(PathType.WATER, oldWater);
}
}
stopNavigation(navigation);
}

View File

@ -211,6 +211,7 @@ import net.minecraft.server.v1_11_R1.PacketPlayOutEntityTeleport;
import net.minecraft.server.v1_11_R1.PacketPlayOutPlayerInfo;
import net.minecraft.server.v1_11_R1.PathEntity;
import net.minecraft.server.v1_11_R1.PathPoint;
import net.minecraft.server.v1_11_R1.PathType;
import net.minecraft.server.v1_11_R1.PathfinderGoalSelector;
import net.minecraft.server.v1_11_R1.RegistryMaterials;
import net.minecraft.server.v1_11_R1.ReportedException;
@ -480,6 +481,15 @@ public class NMSImpl implements NMSBridge {
// navigation won't execute, and calling entity.move doesn't
// entirely fix the problem.
final NavigationAbstract navigation = NMSImpl.getNavigation(entity);
final float oldWater = raw instanceof EntityPlayer ? ((EntityHumanNPC) raw).a(PathType.WATER)
: ((EntityInsentient) raw).a(PathType.WATER);
if (params.avoidWater() && oldWater >= 0) {
if (raw instanceof EntityPlayer) {
((EntityHumanNPC) raw).a(PathType.WATER, oldWater + 1F);
} else {
((EntityInsentient) raw).a(PathType.WATER, oldWater + 1F);
}
}
return new MCNavigator() {
float lastSpeed;
CancelReason reason;
@ -506,6 +516,13 @@ public class NMSImpl implements NMSBridge {
}
}
}
if (oldWater >= 0) {
if (raw instanceof EntityPlayer) {
((EntityHumanNPC) raw).a(PathType.WATER, oldWater);
} else {
((EntityInsentient) raw).a(PathType.WATER, oldWater);
}
}
stopNavigation(navigation);
}

View File

@ -215,6 +215,7 @@ import net.minecraft.server.v1_12_R1.PacketPlayOutEntityTeleport;
import net.minecraft.server.v1_12_R1.PacketPlayOutPlayerInfo;
import net.minecraft.server.v1_12_R1.PathEntity;
import net.minecraft.server.v1_12_R1.PathPoint;
import net.minecraft.server.v1_12_R1.PathType;
import net.minecraft.server.v1_12_R1.PathfinderGoalSelector;
import net.minecraft.server.v1_12_R1.RegistryMaterials;
import net.minecraft.server.v1_12_R1.ReportedException;
@ -484,6 +485,15 @@ public class NMSImpl implements NMSBridge {
// navigation won't execute, and calling entity.move doesn't
// entirely fix the problem.
final NavigationAbstract navigation = NMSImpl.getNavigation(entity);
final float oldWater = raw instanceof EntityPlayer ? ((EntityHumanNPC) raw).a(PathType.WATER)
: ((EntityInsentient) raw).a(PathType.WATER);
if (params.avoidWater() && oldWater >= 0) {
if (raw instanceof EntityPlayer) {
((EntityHumanNPC) raw).a(PathType.WATER, oldWater + 1F);
} else {
((EntityInsentient) raw).a(PathType.WATER, oldWater + 1F);
}
}
return new MCNavigator() {
float lastSpeed;
CancelReason reason;
@ -510,6 +520,13 @@ public class NMSImpl implements NMSBridge {
}
}
}
if (oldWater >= 0) {
if (raw instanceof EntityPlayer) {
((EntityHumanNPC) raw).a(PathType.WATER, oldWater);
} else {
((EntityInsentient) raw).a(PathType.WATER, oldWater);
}
}
stopNavigation(navigation);
}

View File

@ -230,6 +230,7 @@ import net.minecraft.server.v1_13_R2.PacketPlayOutEntityTeleport;
import net.minecraft.server.v1_13_R2.PacketPlayOutPlayerInfo;
import net.minecraft.server.v1_13_R2.PathEntity;
import net.minecraft.server.v1_13_R2.PathPoint;
import net.minecraft.server.v1_13_R2.PathType;
import net.minecraft.server.v1_13_R2.PathfinderGoalSelector;
import net.minecraft.server.v1_13_R2.RegistryMaterials;
import net.minecraft.server.v1_13_R2.ReportedException;
@ -506,6 +507,15 @@ public class NMSImpl implements NMSBridge {
// navigation won't execute, and calling entity.move doesn't
// entirely fix the problem.
final NavigationAbstract navigation = NMSImpl.getNavigation(entity);
final float oldWater = raw instanceof EntityPlayer ? ((EntityHumanNPC) raw).a(PathType.WATER)
: ((EntityInsentient) raw).a(PathType.WATER);
if (params.avoidWater() && oldWater >= 0) {
if (raw instanceof EntityPlayer) {
((EntityHumanNPC) raw).a(PathType.WATER, oldWater + 1F);
} else {
((EntityInsentient) raw).a(PathType.WATER, oldWater + 1F);
}
}
return new MCNavigator() {
float lastSpeed;
CancelReason reason;
@ -532,6 +542,13 @@ public class NMSImpl implements NMSBridge {
}
}
}
if (oldWater >= 0) {
if (raw instanceof EntityPlayer) {
((EntityHumanNPC) raw).a(PathType.WATER, oldWater);
} else {
((EntityInsentient) raw).a(PathType.WATER, oldWater);
}
}
stopNavigation(navigation);
}

View File

@ -249,6 +249,7 @@ import net.minecraft.server.v1_14_R1.PacketPlayOutEntityTeleport;
import net.minecraft.server.v1_14_R1.PacketPlayOutPlayerInfo;
import net.minecraft.server.v1_14_R1.PathEntity;
import net.minecraft.server.v1_14_R1.PathPoint;
import net.minecraft.server.v1_14_R1.PathType;
import net.minecraft.server.v1_14_R1.PathfinderGoalSelector;
import net.minecraft.server.v1_14_R1.PlayerChunkMap;
import net.minecraft.server.v1_14_R1.PlayerChunkMap.EntityTracker;
@ -543,6 +544,15 @@ public class NMSImpl implements NMSBridge {
// navigation won't execute, and calling entity.move doesn't
// entirely fix the problem.
final NavigationAbstract navigation = NMSImpl.getNavigation(entity);
final float oldWater = raw instanceof EntityPlayer ? ((EntityHumanNPC) raw).a(PathType.WATER)
: ((EntityInsentient) raw).a(PathType.WATER);
if (params.avoidWater() && oldWater >= 0) {
if (raw instanceof EntityPlayer) {
((EntityHumanNPC) raw).a(PathType.WATER, oldWater + 1F);
} else {
((EntityInsentient) raw).a(PathType.WATER, oldWater + 1F);
}
}
return new MCNavigator() {
float lastSpeed;
CancelReason reason;
@ -569,6 +579,13 @@ public class NMSImpl implements NMSBridge {
}
}
}
if (oldWater >= 0) {
if (raw instanceof EntityPlayer) {
((EntityHumanNPC) raw).a(PathType.WATER, oldWater);
} else {
((EntityInsentient) raw).a(PathType.WATER, oldWater);
}
}
stopNavigation(navigation);
}

View File

@ -250,6 +250,7 @@ import net.minecraft.server.v1_15_R1.PacketPlayOutEntityTeleport;
import net.minecraft.server.v1_15_R1.PacketPlayOutPlayerInfo;
import net.minecraft.server.v1_15_R1.PathEntity;
import net.minecraft.server.v1_15_R1.PathPoint;
import net.minecraft.server.v1_15_R1.PathType;
import net.minecraft.server.v1_15_R1.PathfinderGoalSelector;
import net.minecraft.server.v1_15_R1.PlayerChunkMap;
import net.minecraft.server.v1_15_R1.PlayerChunkMap.EntityTracker;
@ -544,6 +545,15 @@ public class NMSImpl implements NMSBridge {
// navigation won't execute, and calling entity.move doesn't
// entirely fix the problem.
final NavigationAbstract navigation = NMSImpl.getNavigation(entity);
final float oldWater = raw instanceof EntityPlayer ? ((EntityHumanNPC) raw).a(PathType.WATER)
: ((EntityInsentient) raw).a(PathType.WATER);
if (params.avoidWater() && oldWater >= 0) {
if (raw instanceof EntityPlayer) {
((EntityHumanNPC) raw).a(PathType.WATER, oldWater + 1F);
} else {
((EntityInsentient) raw).a(PathType.WATER, oldWater + 1F);
}
}
return new MCNavigator() {
float lastSpeed;
CancelReason reason;
@ -570,6 +580,13 @@ public class NMSImpl implements NMSBridge {
}
}
}
if (oldWater >= 0) {
if (raw instanceof EntityPlayer) {
((EntityHumanNPC) raw).a(PathType.WATER, oldWater);
} else {
((EntityInsentient) raw).a(PathType.WATER, oldWater);
}
}
stopNavigation(navigation);
}

View File

@ -159,6 +159,7 @@ import net.minecraft.server.v1_8_R3.EntityTypes;
import net.minecraft.server.v1_8_R3.EntityWither;
import net.minecraft.server.v1_8_R3.GenericAttributes;
import net.minecraft.server.v1_8_R3.MathHelper;
import net.minecraft.server.v1_8_R3.Navigation;
import net.minecraft.server.v1_8_R3.NavigationAbstract;
import net.minecraft.server.v1_8_R3.NetworkManager;
import net.minecraft.server.v1_8_R3.Packet;
@ -412,6 +413,10 @@ public class NMSImpl implements NMSBridge {
// navigation won't execute, and calling entity.move doesn't
// entirely fix the problem.
final NavigationAbstract navigation = NMSImpl.getNavigation(entity);
boolean oldAvoidsWater = navigation instanceof Navigation ? ((Navigation) navigation).e() : false;
if (navigation instanceof Navigation) {
((Navigation) navigation).a(params.avoidWater());
}
return new MCNavigator() {
float lastSpeed;
CancelReason reason;
@ -428,6 +433,9 @@ public class NMSImpl implements NMSBridge {
@Override
public void stop() {
if (navigation instanceof Navigation) {
((Navigation) navigation).a(oldAvoidsWater);
}
stopNavigation(navigation);
}