|
2 | 2 |
|
3 | 3 | import org.bukkit.Location; |
4 | 4 | import org.bukkit.Material; |
5 | | -import org.bukkit.block.data.BlockData; |
6 | | -import org.bukkit.block.data.type.Campfire; |
7 | | -import org.bukkit.block.data.type.Fire; |
8 | 5 |
|
9 | 6 | import java.util.List; |
10 | 7 |
|
11 | 8 | public class SafetyCheck { |
12 | 9 |
|
13 | | - /** |
14 | | - * Checks if the block above this location is water |
15 | | - * @param location the location to check |
16 | | - * @return boolean |
17 | | - */ |
18 | | - public static boolean underWater(Location location) { |
19 | | - Location locationAbove = location.clone().add(0, 1, 0); |
20 | | - return locationAbove.getBlock().getType() == Material.WATER; |
21 | | - } |
22 | | - |
23 | | - /** |
24 | | - * Checks if this block or the one above it is lava |
25 | | - * @param location the location to check |
26 | | - * @return boolean |
27 | | - */ |
28 | | - public static boolean insideLava(Location location) { |
29 | | - Location locationAbove = location.clone().add(0, 1, 0); |
30 | | - if (location.getBlock().getType() == Material.LAVA) { |
31 | | - return true; |
| 10 | + public static int checkSafetyFlags(Location location, List<Material> blacklistedMaterials) { |
| 11 | + int flags = 0; |
| 12 | + Location blockAbove = location.clone().add(0, 1, 0); |
| 13 | + Location blockBelow = location.clone().add(0, -1, 0); |
| 14 | + // Fall check, is the player in the air? i.e. is the block below them empty/air? |
| 15 | + if (isEmpty(blockBelow) || blockBelow.getBlock().isPassable()) { |
| 16 | + flags |= SafetyFlags.FALLING.bitFlag; |
32 | 17 | } |
33 | | - return locationAbove.getBlock().getType() == Material.LAVA; |
34 | | - } |
35 | | - |
36 | | - /** |
37 | | - * Checks if the block below the provided location is air or is otherwise empty |
38 | | - * @param location the location to check |
39 | | - * @return boolean |
40 | | - */ |
41 | | - public static boolean willFall(Location location) { |
42 | | - Location locationBelow = location.clone().add(0, -1, 0); |
43 | | - if (locationBelow.getBlock().isEmpty()) return true; |
44 | | - if (locationBelow.getBlock().getType().isEmpty()) return true; |
45 | | - return locationBelow.getBlock().getType().equals(Material.AIR); |
46 | | - } |
47 | | - |
48 | | - /** |
49 | | - * Checks if the block located at this position is a fire, or campfire |
50 | | - * @param location the location to check |
51 | | - * @return boolean |
52 | | - */ |
53 | | - public static boolean insideFire(Location location) { |
54 | | - BlockData blockData = location.getBlock().getBlockData(); |
55 | | - if (blockData instanceof Fire) { |
56 | | - return true; |
| 18 | + // Is there lava? |
| 19 | + if (isMaterial(location, Material.LAVA) || isMaterial(blockAbove, Material.LAVA)) { |
| 20 | + flags |= SafetyFlags.LAVA.bitFlag; |
| 21 | + } |
| 22 | + // Is the home encased in blocks? |
| 23 | + if (blockAbove.getBlock().isSolid()) { |
| 24 | + flags |= SafetyFlags.SUFFOCATION.bitFlag; |
| 25 | + } |
| 26 | + // Is the home under water? |
| 27 | + if (isMaterial(blockAbove, Material.WATER)) { |
| 28 | + flags |= SafetyFlags.UNDERWATER.bitFlag; |
57 | 29 | } |
58 | | - if (blockData instanceof Campfire campfire) { |
59 | | - return campfire.isLit(); |
| 30 | + // Is the home on a blacklisted block? |
| 31 | + if (blacklistedMaterials.contains(location.getBlock().getType())) { |
| 32 | + flags |= SafetyFlags.DAMAGE_RISK.bitFlag; |
60 | 33 | } |
61 | | - return false; |
| 34 | + return flags; |
62 | 35 | } |
63 | 36 |
|
64 | | - /** |
65 | | - * Checks if the block located at this position is solid |
66 | | - * @param location the location to check |
67 | | - * @return boolean |
68 | | - */ |
69 | | - public static boolean insideSolidBlocks(Location location) { |
70 | | - Location locationAbove = location.clone().add(0, 1, 0); |
71 | | - if (locationAbove.getBlock().isSolid()) return true; |
72 | | - return false; |
| 37 | + private static boolean isMaterial(Location location, Material material) { |
| 38 | + return location.getBlock().getType() == material; |
73 | 39 | } |
74 | 40 |
|
75 | | - /** |
76 | | - * Checks if the block located at this position, or the block above, are blacklisted |
77 | | - * @param location the location to check |
78 | | - * @param materialList the list of blacklisted materials |
79 | | - * @return boolean |
80 | | - */ |
81 | | - |
82 | | - public static boolean insideBlacklistedBlocks(Location location, List<Material> materialList) { |
83 | | - Location locationAbove = location.clone().add(0, 1, 0); |
84 | | - if (materialList.contains(locationAbove.getBlock().getType())) return true; |
85 | | - return materialList.contains(location.getBlock().getType()); |
| 41 | + private static boolean isEmpty(Location location) { |
| 42 | + return location.getBlock().getType().isEmpty(); |
86 | 43 | } |
87 | 44 | } |
| 45 | + |
0 commit comments