-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
TeleportStuckAction.java
44 lines (38 loc) · 1.49 KB
/
TeleportStuckAction.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
package net.citizensnpcs.api.ai;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import net.citizensnpcs.api.astar.pathfinder.MinecraftBlockExaminer;
import net.citizensnpcs.api.npc.NPC;
public class TeleportStuckAction implements StuckAction {
private TeleportStuckAction() {
// singleton
}
private boolean canStand(Block block) {
return MinecraftBlockExaminer.canStandIn(block.getType())
&& MinecraftBlockExaminer.canStandIn(block.getRelative(BlockFace.UP).getType());
}
@Override
public boolean run(NPC npc, Navigator navigator) {
if (!npc.isSpawned())
return false;
Location base = navigator.getTargetAsLocation();
if (npc.getEntity().getLocation().distanceSquared(base) <= RANGE)
return true;
Block block = base.getBlock();
int iterations = 0;
while (!canStand(block)) {
if (iterations++ >= MAX_ITERATIONS) {
block = base.getBlock();
break;
}
block = block.getRelative(BlockFace.UP);
}
npc.teleport(block.getLocation(), TeleportCause.PLUGIN);
return false;
}
public static TeleportStuckAction INSTANCE = new TeleportStuckAction();
private static final int MAX_ITERATIONS = 10;
private static final double RANGE = 10;
}