/
SwimmingExaminer.java
72 lines (61 loc) · 2.48 KB
/
SwimmingExaminer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package net.citizensnpcs.api.astar.pathfinder;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Squid;
import org.bukkit.entity.WaterMob;
import org.bukkit.util.Vector;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.SpigotUtil;
public class SwimmingExaminer implements BlockExaminer {
private boolean canSwimInLava;
private final NPC npc;
public SwimmingExaminer(NPC npc) {
this.npc = npc;
}
public boolean canSwimInLava() {
return canSwimInLava;
}
@Override
public float getCost(BlockSource source, PathPoint point) {
// penalise non water blocks for fish
if (isWaterMob(npc.getEntity())
&& !MinecraftBlockExaminer.isLiquidOrInLiquid(source.getBlockAt(point.getVector()))) {
return 1F;
}
return 0;
}
@Override
public PassableState isPassable(BlockSource source, PathPoint point) {
Vector vector = point.getVector();
if (!MinecraftBlockExaminer.isLiquidOrInLiquid(
source.getWorld().getBlockAt(vector.getBlockX(), vector.getBlockY(), vector.getBlockZ()))) {
return PassableState.IGNORE;
}
if (isWaterMob(npc.getEntity())) {
return PassableState.PASSABLE;
}
Block block = source.getBlockAt(vector.clone().add(UP));
return isSwimmableLiquid(block.getType()) || MinecraftBlockExaminer.canStandIn(block) ? PassableState.PASSABLE
: PassableState.UNPASSABLE;
}
private boolean isSwimmableLiquid(Material material) {
if (material == Material.LAVA
|| (!SpigotUtil.isUsing1_13API() && material == Material.valueOf("STATIONARY_LAVA")))
return canSwimInLava();
return material == Material.WATER
|| (!SpigotUtil.isUsing1_13API() && material == Material.valueOf("STATIONARY_WATER"));
}
public void setCanSwimInLava(boolean canSwimInLava) {
this.canSwimInLava = canSwimInLava;
}
public static boolean isWaterMob(Entity entity) {
if (entity == null)
return false;
if (!SpigotUtil.isUsing1_13API())
return entity instanceof Squid;
return entity instanceof WaterMob || entity.getType().name().equals("TURTLE")
|| entity.getType().name().equals("FROG") || entity.getType().name().equals("AXOLOTL");
}
private static final Vector UP = new Vector(0, 1, 0);
}