|
19 | 19 | package net.onelitefeather.bettergopaint.utils; |
20 | 20 |
|
21 | 21 | import org.bukkit.Location; |
| 22 | +import org.bukkit.block.Block; |
22 | 23 | import org.jetbrains.annotations.NotNull; |
23 | 24 |
|
24 | 25 | public class Height { |
25 | 26 |
|
26 | | - public static int getHeight(@NotNull Location location) { |
27 | | - return location.getWorld().getHighestBlockYAt(location); |
| 27 | + /** |
| 28 | + * Gets the height of the nearest non-empty block at a given location. |
| 29 | + * |
| 30 | + * @param location the location to check |
| 31 | + * @return the height of the nearest non-empty block at the location |
| 32 | + */ |
| 33 | + public static int getNearestNonEmptyBlock(@NotNull Location location) { |
| 34 | + if (location.getBlock().getType().isEmpty()) { |
| 35 | + for (int y = location.getBlockY(); y >= location.getWorld().getMinHeight(); y--) { |
| 36 | + Block block = location.getWorld().getBlockAt(location.getBlockX(), y, location.getBlockZ()); |
| 37 | + if (!block.isEmpty()) { |
| 38 | + return y + 1; |
| 39 | + } |
| 40 | + } |
| 41 | + return location.getWorld().getMinHeight(); |
| 42 | + } else { |
| 43 | + for (int y = location.getBlockY(); y <= location.getWorld().getMaxHeight(); y++) { |
| 44 | + Block block = location.getWorld().getBlockAt(location.getBlockX(), y, location.getBlockZ()); |
| 45 | + if (block.isEmpty()) { |
| 46 | + return y; |
| 47 | + } |
| 48 | + } |
| 49 | + return location.getWorld().getMaxHeight(); |
| 50 | + } |
28 | 51 | } |
29 | 52 |
|
| 53 | + /** |
| 54 | + * Calculates the average height difference of the surrounding blocks from a given location, within a specified distance. |
| 55 | + * |
| 56 | + * @param location the location to calculate the average height difference from |
| 57 | + * @param height the reference height of the nearest non-empty block |
| 58 | + * @param distance the distance at which to calculate the average height difference |
| 59 | + * @return the average height difference of the surrounding blocks within the specified distance |
| 60 | + */ |
30 | 61 | public static double getAverageHeightDiffFracture(@NotNull Location location, int height, int distance) { |
31 | 62 | double totalHeight = 0; |
32 | | - totalHeight += Math.abs(getHeight(location.clone().add(distance, 0, -distance))) - height; |
33 | | - totalHeight += Math.abs(getHeight(location.clone().add(distance, 0, distance))) - height; |
34 | | - totalHeight += Math.abs(getHeight(location.clone().add(-distance, 0, distance))) - height; |
35 | | - totalHeight += Math.abs(getHeight(location.clone().add(-distance, 0, -distance))) - height; |
36 | | - totalHeight += Math.abs(getHeight(location.clone().add(0, 0, -distance))) - height; |
37 | | - totalHeight += Math.abs(getHeight(location.clone().add(0, 0, distance))) - height; |
38 | | - totalHeight += Math.abs(getHeight(location.clone().add(-distance, 0, 0))) - height; |
39 | | - totalHeight += Math.abs(getHeight(location.clone().add(distance, 0, 0))) - height; |
| 63 | + totalHeight += Math.abs(getNearestNonEmptyBlock(location.clone().add(distance, 0, -distance))) - height; |
| 64 | + totalHeight += Math.abs(getNearestNonEmptyBlock(location.clone().add(distance, 0, distance))) - height; |
| 65 | + totalHeight += Math.abs(getNearestNonEmptyBlock(location.clone().add(-distance, 0, distance))) - height; |
| 66 | + totalHeight += Math.abs(getNearestNonEmptyBlock(location.clone().add(-distance, 0, -distance))) - height; |
| 67 | + totalHeight += Math.abs(getNearestNonEmptyBlock(location.clone().add(0, 0, -distance))) - height; |
| 68 | + totalHeight += Math.abs(getNearestNonEmptyBlock(location.clone().add(0, 0, distance))) - height; |
| 69 | + totalHeight += Math.abs(getNearestNonEmptyBlock(location.clone().add(-distance, 0, 0))) - height; |
| 70 | + totalHeight += Math.abs(getNearestNonEmptyBlock(location.clone().add(distance, 0, 0))) - height; |
40 | 71 | return (totalHeight / 8d) / distance; |
41 | 72 | } |
42 | 73 |
|
| 74 | + /** |
| 75 | + * Calculates the average height difference angle of the surrounding blocks from a given location within a specified distance. |
| 76 | + * |
| 77 | + * @param location the location to calculate the average height difference angle from |
| 78 | + * @param distance the distance at which to calculate the average height difference angle |
| 79 | + * @return the average height difference angle of the surrounding blocks within the specified distance |
| 80 | + */ |
43 | 81 | public static double getAverageHeightDiffAngle(@NotNull Location location, int distance) { |
44 | 82 | double maxHeightDiff = 0; |
45 | 83 | double maxHeightDiff2 = 0; |
46 | | - double diff = Math.abs(getHeight(location.clone().add(distance, 0, -distance)) - getHeight(location.clone() |
47 | | - .add(-distance, 0, distance))); |
| 84 | + double diff = Math.abs(getNearestNonEmptyBlock(location.clone().add(distance, 0, -distance)) |
| 85 | + - getNearestNonEmptyBlock(location.clone().add(-distance, 0, distance))); |
48 | 86 | if (diff >= maxHeightDiff) { |
49 | 87 | maxHeightDiff = diff; |
50 | 88 | maxHeightDiff2 = maxHeightDiff; |
51 | 89 | } |
52 | | - diff = Math.abs(getHeight(location.clone().add(distance, 0, distance)) - getHeight(location.clone() |
53 | | - .add(-distance, 0, -distance))); |
| 90 | + diff = Math.abs(getNearestNonEmptyBlock(location.clone().add(distance, 0, distance)) |
| 91 | + - getNearestNonEmptyBlock(location.clone().add(-distance, 0, -distance))); |
54 | 92 | if (diff > maxHeightDiff) { |
55 | 93 | maxHeightDiff = diff; |
56 | 94 | maxHeightDiff2 = maxHeightDiff; |
57 | 95 | } |
58 | | - diff = Math.abs(getHeight(location.clone().add(distance, 0, 0)) - getHeight(location.clone() |
59 | | - .add(-distance, 0, 0))); |
| 96 | + diff = Math.abs(getNearestNonEmptyBlock(location.clone().add(distance, 0, 0)) |
| 97 | + - getNearestNonEmptyBlock(location.clone().add(-distance, 0, 0))); |
60 | 98 | if (diff > maxHeightDiff) { |
61 | 99 | maxHeightDiff = diff; |
62 | 100 | maxHeightDiff2 = maxHeightDiff; |
63 | 101 | } |
64 | | - diff = Math.abs(getHeight(location.clone().add(0, 0, -distance)) - getHeight(location.clone() |
65 | | - .add(0, 0, distance))); |
| 102 | + diff = Math.abs(getNearestNonEmptyBlock(location.clone().add(0, 0, -distance)) |
| 103 | + - getNearestNonEmptyBlock(location.clone().add(0, 0, distance))); |
66 | 104 | if (diff > maxHeightDiff) { |
67 | 105 | maxHeightDiff = diff; |
68 | 106 | maxHeightDiff2 = maxHeightDiff; |
|
0 commit comments