/
MinecraftBlockExaminer.java
293 lines (265 loc) · 11.8 KB
/
MinecraftBlockExaminer.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package net.citizensnpcs.api.astar.pathfinder;
import java.util.EnumSet;
import java.util.ListIterator;
import java.util.Random;
import java.util.Set;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Waterlogged;
import org.bukkit.util.Vector;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import net.citizensnpcs.api.ai.event.CancelReason;
import net.citizensnpcs.api.ai.event.NavigatorCallback;
import net.citizensnpcs.api.astar.pathfinder.PathPoint.PathCallback;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.SpigotUtil;
public class MinecraftBlockExaminer implements BlockExaminer {
@Override
public float getCost(BlockSource source, PathPoint point) {
Vector pos = point.getVector();
Material above = source.getMaterialAt(pos.getBlockX(), pos.getBlockY() + 1, pos.getBlockZ());
Material below = source.getMaterialAt(pos.getBlockX(), pos.getBlockY() - 1, pos.getBlockZ());
Material in = source.getMaterialAt(pos);
if (above == WEB || in == WEB)
return 1F;
if (below == Material.SOUL_SAND || below == Material.ICE)
return 1F;
if (isLiquidOrInLiquid(source.getWorld().getBlockAt(pos.getBlockX(), pos.getBlockY(), pos.getBlockZ()))) {
if (in == Material.LAVA)
return 2F;
return 1F;
}
return 0F; // TODO: add light level-specific costs?
}
private boolean isClimbable(Material mat) {
return CLIMBABLE.contains(mat);
}
@Override
public PassableState isPassable(BlockSource source, PathPoint point) {
Vector pos = point.getVector();
if (pos.getBlockY() <= 0 || pos.getBlockY() >= 255) {
return PassableState.UNPASSABLE;
}
Material above = source.getMaterialAt(pos.getBlockX(), pos.getBlockY() + 1, pos.getBlockZ());
Material below = source.getMaterialAt(pos.getBlockX(), pos.getBlockY() - 1, pos.getBlockZ());
Material in = source.getMaterialAt(pos);
boolean canStand = canStandOn(below) || isLiquid(in, below) || isClimbable(below);
if (!canStand) {
return PassableState.UNPASSABLE;
}
if (isClimbable(in) && (isClimbable(above) || isClimbable(below))) {
point.addCallback(new LadderClimber());
} else if (!canStandIn(above) || !canStandIn(in)) {
return PassableState.UNPASSABLE;
}
if (!canJumpOn(below)) {
if (point.getParentPoint() == null) {
return PassableState.UNPASSABLE;
}
Vector parentPos = point.getParentPoint().getVector();
if ((parentPos.getX() != pos.getX() || parentPos.getZ() != pos.getZ())
&& pos.clone().subtract(point.getParentPoint().getVector()).getY() == 1) {
return PassableState.UNPASSABLE;
}
}
return PassableState.PASSABLE;
}
private class LadderClimber implements PathCallback {
boolean added = false;
ListIterator<Block> current;
@Override
public void run(final NPC npc, Block point, ListIterator<Block> path) {
current = path;
if (added || npc.data().<Boolean> get("running-ladder", false)) {
added = true;
return;
}
npc.getNavigator().getLocalParameters().addRunCallback(new Runnable() {
Location dummy = new Location(null, 0, 0, 0);
boolean sneakingForScaffolding;
private boolean isScaffolding(Material type) {
return type.name().contains("SCAFFOLDING");
}
@Override
public void run() {
Material in = npc.getEntity().getLocation(dummy).getBlock().getType(),
next = current.next().getType();
current.previous();
if (isClimbable(in)) {
if (current.next().getY() > current.previous().getY()) {
npc.getEntity().setVelocity(npc.getEntity().getVelocity().setY(0.3));
if (sneakingForScaffolding) {
npc.data().set(NPC.SNEAKING_METADATA, sneakingForScaffolding = false);
}
} else if (isScaffolding(in) || isScaffolding(next)) {
npc.data().set(NPC.SNEAKING_METADATA, sneakingForScaffolding = true);
}
} else if (sneakingForScaffolding) {
npc.data().set(NPC.SNEAKING_METADATA, sneakingForScaffolding = false);
}
}
});
npc.getNavigator().getLocalParameters().addSingleUseCallback(new NavigatorCallback() {
@Override
public void onCompletion(CancelReason cancelReason) {
npc.data().set("running-ladder", false);
npc.data().set(NPC.SNEAKING_METADATA, false);
}
});
added = true;
}
}
private static boolean canJumpOn(Material mat) {
return !NOT_JUMPABLE.contains(mat);
}
public static boolean canStandIn(Material... mat) {
boolean passable = true;
for (Material m : mat) {
passable &= !m.isSolid();
}
return passable;
}
public static boolean canStandOn(Block block) {
Block up = block.getRelative(BlockFace.UP);
return canStandOn(block.getType()) && canStandIn(up.getType())
&& canStandIn(up.getRelative(BlockFace.UP).getType());
}
public static boolean canStandOn(Material mat) {
return !UNWALKABLE.contains(mat) && mat.isSolid();
}
public static Location findRandomValidLocation(Location base, int xrange, int yrange) {
return findRandomValidLocation(base, xrange, yrange, null, new Random());
}
public static Location findRandomValidLocation(Location base, int xrange, int yrange,
Function<Block, Boolean> filter) {
return findRandomValidLocation(base, xrange, yrange, filter, new Random());
}
public static Location findRandomValidLocation(Location base, int xrange, int yrange,
Function<Block, Boolean> filter, Random random) {
for (int i = 0; i < 10; i++) {
int x = base.getBlockX() + random.nextInt(2 * xrange + 1) - xrange;
int y = base.getBlockY() + random.nextInt(2 * yrange + 1) - yrange;
int z = base.getBlockZ() + random.nextInt(2 * xrange + 1) - xrange;
if (!base.getWorld().isChunkLoaded(x >> 4, z >> 4)) {
continue;
}
Block block = base.getWorld().getBlockAt(x, y, z);
if (MinecraftBlockExaminer.canStandOn(block)) {
if (filter != null && !filter.apply(block)) {
continue;
}
return block.getLocation().add(0, 1, 0);
}
}
return null;
}
public static Location findValidLocation(Location location, int radius) {
Block base = location.getBlock();
if (canStandOn(base.getRelative(BlockFace.DOWN)))
return location;
for (int y = 0; y <= radius; y++) {
for (int x = -radius; x <= radius; x++) {
for (int z = -radius; z <= radius; z++) {
if (!base.getWorld().isChunkLoaded(base.getX() + x >> 4, base.getZ() + z >> 4)) {
continue;
}
Block relative = base.getRelative(x, y, z);
if (canStandOn(relative.getRelative(BlockFace.DOWN))) {
return relative.getLocation();
}
}
}
}
return location;
}
public static Location findValidLocation(Location location, int radius, int yradius) {
Block base = location.getBlock();
if (canStandOn(base.getRelative(BlockFace.DOWN)))
return location;
for (int y = -yradius; y <= yradius; y++) {
for (int x = -radius; x <= radius; x++) {
for (int z = -radius; z <= radius; z++) {
if (!base.getWorld().isChunkLoaded(base.getX() + x >> 4, base.getZ() + z >> 4)) {
continue;
}
Block relative = base.getRelative(x, y, z);
if (canStandOn(relative.getRelative(BlockFace.DOWN))) {
return relative.getLocation();
}
}
}
}
return location;
}
public static Location findValidLocationAbove(Location location, int radius) {
Block base = location.getBlock();
if (canStandOn(base.getRelative(BlockFace.DOWN)))
return location;
for (int y = 0; y <= radius; y++) {
Block relative = base.getRelative(0, y, 0);
if (canStandOn(relative.getRelative(BlockFace.DOWN))) {
return relative.getLocation();
}
}
return location;
}
public static boolean isDoor(Material in) {
return in.name().contains("DOOR") && !in.name().contains("TRAPDOOR");
}
public static boolean isGate(Material in) {
return in.name().contains("GATE") && !in.name().contains("GATEWAY");
}
public static boolean isLiquid(Material... materials) {
for (Material mat : materials) {
if (LIQUIDS.contains(mat)) {
return true;
}
}
return false;
}
public static boolean isLiquidOrInLiquid(Block block) {
if (isLiquid(block.getType()))
return true;
try {
BlockData data = block.getBlockData();
return data instanceof Waterlogged && ((Waterlogged) data).isWaterlogged();
} catch (Throwable t) {
return false;
}
}
public static boolean validPosition(Block in) {
return canStandIn(in.getType()) && canStandIn(in.getRelative(BlockFace.UP).getType())
&& canStandOn(in.getRelative(BlockFace.DOWN).getType());
}
private static final Set<Material> CLIMBABLE = EnumSet.of(Material.LADDER, Material.VINE);
private static final Set<Material> LIQUIDS = EnumSet.of(Material.WATER, Material.LAVA);
private static final Set<Material> NOT_JUMPABLE = EnumSet.of(Material.SPRUCE_FENCE, Material.BIRCH_FENCE,
Material.JUNGLE_FENCE, Material.ACACIA_FENCE, Material.DARK_OAK_FENCE);
private static final Set<Material> UNWALKABLE = EnumSet.of(Material.AIR, Material.CACTUS);
private static Material WEB = SpigotUtil.isUsing1_13API() ? Material.COBWEB : Material.valueOf("WEB");
static {
if (!SpigotUtil.isUsing1_13API()) {
LIQUIDS.add(Material.valueOf("STATIONARY_LAVA"));
LIQUIDS.add(Material.valueOf("STATIONARY_WATER"));
UNWALKABLE.add(Material.valueOf("STATIONARY_LAVA"));
NOT_JUMPABLE.addAll(Lists.newArrayList(Material.valueOf("FENCE"), Material.valueOf("IRON_FENCE"),
Material.valueOf("NETHER_FENCE"), Material.valueOf("COBBLE_WALL")));
} else {
try {
UNWALKABLE.add(Material.valueOf("CAMPFIRE"));
} catch (IllegalArgumentException e) {
// 1.13
}
try {
CLIMBABLE.add(Material.valueOf("SCAFFOLDING"));
} catch (IllegalArgumentException e) {
}
NOT_JUMPABLE.addAll(Lists.newArrayList(Material.valueOf("OAK_FENCE"),
Material.valueOf("NETHER_BRICK_FENCE"), Material.valueOf("COBBLESTONE_WALL")));
}
}
}