| 1 | package thunder.hack.utility.world;
|
| 2 |
|
| 3 | import org.jetbrains.annotations.NotNull;
|
| 4 | import org.jetbrains.annotations.Nullable;
|
| 5 |
|
| 6 | import java.util.ArrayList;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Objects;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.core.Vec3i;
|
| 11 | import net.minecraft.world.level.block.Block;
|
| 12 | import net.minecraft.world.level.block.Blocks;
|
| 13 | import net.minecraft.world.phys.Vec3;
|
| 14 |
|
| 15 | import static thunder.hack.features.modules.Module.mc;
|
| 16 |
|
| 17 | public final class HoleUtility {
|
| 18 | public static final Vec3i[] VECTOR_PATTERN = {
|
| 19 | new Vec3i(0, 0, 1),
|
| 20 | new Vec3i(0, 0, -1),
|
| 21 | new Vec3i(1, 0, 0),
|
| 22 | new Vec3i(-1, 0, 0)
|
| 23 | };
|
| 24 |
|
| 25 | public static @NotNull List<BlockPos> getHolePoses(@NotNull Vec3 from) {
|
| 26 | List<BlockPos> positions = new ArrayList<>();
|
| 27 |
|
| 28 | double decimalX = from.x() - Math.floor(from.x());
|
| 29 | double decimalZ = from.z() - Math.floor(from.z());
|
| 30 | int offX = calcOffset(decimalX);
|
| 31 | int offZ = calcOffset(decimalZ);
|
| 32 | positions.add(getPos(from));
|
| 33 | for (int x = 0; x <= Math.abs(offX); ++x) {
|
| 34 | for (int z = 0; z <= Math.abs(offZ); ++z) {
|
| 35 | int properX = x * offX;
|
| 36 | int properZ = z * offZ;
|
| 37 | positions.add(Objects.requireNonNull(getPos(from)).offset(properX, 0, properZ));
|
| 38 | }
|
| 39 | }
|
| 40 |
|
| 41 | return positions;
|
| 42 | }
|
| 43 |
|
| 44 | public static @NotNull List<BlockPos> getSurroundPoses(@NotNull Vec3 from) {
|
| 45 | final BlockPos fromPos = BlockPos.containing(from);
|
| 46 | final ArrayList<BlockPos> tempOffsets = new ArrayList<>();
|
| 47 |
|
| 48 | final double decimalX = Math.abs(from.x()) - Math.floor(Math.abs(from.x()));
|
| 49 | final double decimalZ = Math.abs(from.z()) - Math.floor(Math.abs(from.z()));
|
| 50 | final int lengthXPos = calcLength(decimalX, false);
|
| 51 | final int lengthXNeg = calcLength(decimalX, true);
|
| 52 | final int lengthZPos = calcLength(decimalZ, false);
|
| 53 | final int lengthZNeg = calcLength(decimalZ, true);
|
| 54 |
|
| 55 | for (int x = 1; x < lengthXPos + 1; ++x) {
|
| 56 | tempOffsets.add(addToPlayer(fromPos, x, 0.0, 1 + lengthZPos));
|
| 57 | tempOffsets.add(addToPlayer(fromPos, x, 0.0, -(1 + lengthZNeg)));
|
| 58 | }
|
| 59 | for (int x = 0; x <= lengthXNeg; ++x) {
|
| 60 | tempOffsets.add(addToPlayer(fromPos, -x, 0.0, 1 + lengthZPos));
|
| 61 | tempOffsets.add(addToPlayer(fromPos, -x, 0.0, -(1 + lengthZNeg)));
|
| 62 | }
|
| 63 | for (int z = 1; z < lengthZPos + 1; ++z) {
|
| 64 | tempOffsets.add(addToPlayer(fromPos, 1 + lengthXPos, 0.0, z));
|
| 65 | tempOffsets.add(addToPlayer(fromPos, -(1 + lengthXNeg), 0.0, z));
|
| 66 | }
|
| 67 | for (int z = 0; z <= lengthZNeg; ++z) {
|
| 68 | tempOffsets.add(addToPlayer(fromPos, 1 + lengthXPos, 0.0, -z));
|
| 69 | tempOffsets.add(addToPlayer(fromPos, -(1 + lengthXNeg), 0.0, -z));
|
| 70 | }
|
| 71 |
|
| 72 | return tempOffsets;
|
| 73 | }
|
| 74 |
|
| 75 | private static @NotNull BlockPos getPos(@NotNull Vec3 from) {
|
| 76 | return BlockPos.containing(from.x(), from.y() - Math.floor(from.y()) > 0.8 ? Math.floor(from.y()) + 1.0 : Math.floor(from.y()), from.z());
|
| 77 | }
|
| 78 |
|
| 79 | public static int calcOffset(double dec) {
|
| 80 | return dec >= 0.7 ? 1 : (dec <= 0.3 ? -1 : 0);
|
| 81 | }
|
| 82 |
|
| 83 | public static int calcLength(double decimal, boolean negative) {
|
| 84 | if (negative) return decimal <= 0.3 ? 1 : 0;
|
| 85 | return decimal >= 0.7 ? 1 : 0;
|
| 86 | }
|
| 87 |
|
| 88 | public static BlockPos addToPlayer(@NotNull BlockPos playerPos, double x, double y, double z) {
|
| 89 | if (playerPos.getX() < 0) x = -x;
|
| 90 | if (playerPos.getY() < 0) y = -y;
|
| 91 | if (playerPos.getZ() < 0) z = -z;
|
| 92 | return playerPos.offset(BlockPos.containing(x, y, z));
|
| 93 | }
|
| 94 |
|
| 95 | public static boolean isHole(BlockPos pos) {
|
| 96 | return isSingleHole(pos)
|
| 97 | || validTwoBlockIndestructible(pos) || validTwoBlockBedrock(pos)
|
| 98 | || validQuadIndestructible(pos) || validQuadBedrock(pos);
|
| 99 | }
|
| 100 |
|
| 101 | public static boolean isSingleHole(BlockPos pos) {
|
| 102 | return validIndestructible(pos) || validBedrock(pos);
|
| 103 | }
|
| 104 |
|
| 105 | public static boolean validIndestructible(@NotNull BlockPos pos) {
|
| 106 | return !validBedrock(pos)
|
| 107 | && (isIndestructible(pos.offset(0, -1, 0)) || isBedrock(pos.offset(0, -1, 0)))
|
| 108 | && (isIndestructible(pos.offset(1, 0, 0)) || isBedrock(pos.offset(1, 0, 0)))
|
| 109 | && (isIndestructible(pos.offset(-1, 0, 0)) || isBedrock(pos.offset(-1, 0, 0)))
|
| 110 | && (isIndestructible(pos.offset(0, 0, 1)) || isBedrock(pos.offset(0, 0, 1)))
|
| 111 | && (isIndestructible(pos.offset(0, 0, -1)) || isBedrock(pos.offset(0, 0, -1)))
|
| 112 | && isReplaceable(pos)
|
| 113 | && isReplaceable(pos.offset(0, 1, 0))
|
| 114 | && isReplaceable(pos.offset(0, 2, 0));
|
| 115 | }
|
| 116 |
|
| 117 | public static boolean validBedrock(@NotNull BlockPos pos) {
|
| 118 | return isBedrock(pos.offset(0, -1, 0))
|
| 119 | && isBedrock(pos.offset(1, 0, 0))
|
| 120 | && isBedrock(pos.offset(-1, 0, 0))
|
| 121 | && isBedrock(pos.offset(0, 0, 1))
|
| 122 | && isBedrock(pos.offset(0, 0, -1))
|
| 123 | && isReplaceable(pos)
|
| 124 | && isReplaceable(pos.offset(0, 1, 0))
|
| 125 | && isReplaceable(pos.offset(0, 2, 0));
|
| 126 | }
|
| 127 |
|
| 128 | public static boolean validTwoBlockBedrock(@NotNull BlockPos pos) {
|
| 129 | if (!isReplaceable(pos)) return false;
|
| 130 | Vec3i addVec = getTwoBlocksDirection(pos);
|
| 131 |
|
| 132 | // If addVec not found -> hole incorrect
|
| 133 | if (addVec == null)
|
| 134 | return false;
|
| 135 |
|
| 136 | BlockPos[] checkPoses = new BlockPos[]{pos, pos.offset(addVec)};
|
| 137 | // Check surround poses of checkPoses
|
| 138 | for (BlockPos checkPos : checkPoses) {
|
| 139 |
|
| 140 | if(!isReplaceable(checkPos.offset(0, 1, 0)) || !isReplaceable(checkPos.offset(0, 2, 0)))
|
| 141 | return false;
|
| 142 |
|
| 143 | BlockPos downPos = checkPos.below();
|
| 144 | if (!isBedrock(downPos))
|
| 145 | return false;
|
| 146 |
|
| 147 | for (Vec3i vec : VECTOR_PATTERN) {
|
| 148 | BlockPos reducedPos = checkPos.offset(vec);
|
| 149 | if (!isBedrock(reducedPos) && !reducedPos.equals(pos) && !reducedPos.equals(pos.offset(addVec)))
|
| 150 | return false;
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | return true;
|
| 155 | }
|
| 156 |
|
| 157 | public static boolean validTwoBlockIndestructible(@NotNull BlockPos pos) {
|
| 158 | if (!isReplaceable(pos)) return false;
|
| 159 | Vec3i addVec = getTwoBlocksDirection(pos);
|
| 160 |
|
| 161 | // If addVec not found -> hole incorrect
|
| 162 | if (addVec == null)
|
| 163 | return false;
|
| 164 |
|
| 165 | BlockPos[] checkPoses = new BlockPos[]{pos, pos.offset(addVec)};
|
| 166 | // Check surround poses of checkPoses
|
| 167 | boolean wasIndestrictible = false;
|
| 168 | for (BlockPos checkPos : checkPoses) {
|
| 169 | BlockPos downPos = checkPos.below();
|
| 170 |
|
| 171 | if (isIndestructible(downPos))
|
| 172 | wasIndestrictible = true;
|
| 173 | else if (!isBedrock(downPos))
|
| 174 | return false;
|
| 175 |
|
| 176 | if(!isReplaceable(checkPos.offset(0, 1, 0)) || !isReplaceable(checkPos.offset(0, 2, 0)))
|
| 177 | return false;
|
| 178 |
|
| 179 | for (Vec3i vec : VECTOR_PATTERN) {
|
| 180 | BlockPos reducedPos = checkPos.offset(vec);
|
| 181 |
|
| 182 | if (isIndestructible(reducedPos)) {
|
| 183 | wasIndestrictible = true;
|
| 184 | continue;
|
| 185 | }
|
| 186 | if (!isBedrock(reducedPos) && !reducedPos.equals(pos) && !reducedPos.equals(pos.offset(addVec)))
|
| 187 | return false;
|
| 188 | }
|
| 189 | }
|
| 190 |
|
| 191 | return wasIndestrictible;
|
| 192 | }
|
| 193 |
|
| 194 | private static @Nullable Vec3i getTwoBlocksDirection(BlockPos pos) {
|
| 195 | // Try to get direction
|
| 196 | for (Vec3i vec : VECTOR_PATTERN) {
|
| 197 | if (isReplaceable(pos.offset(vec)))
|
| 198 | return vec;
|
| 199 | }
|
| 200 |
|
| 201 | return null;
|
| 202 | }
|
| 203 |
|
| 204 | public static boolean validQuadIndestructible(@NotNull BlockPos pos) {
|
| 205 | List<BlockPos> checkPoses = getQuadDirection(pos);
|
| 206 | // If checkPoses not found -> hole incorrect
|
| 207 | if (checkPoses == null)
|
| 208 | return false;
|
| 209 |
|
| 210 | boolean wasIndestrictible = false;
|
| 211 | for (BlockPos checkPos : checkPoses) {
|
| 212 | BlockPos downPos = checkPos.below();
|
| 213 | if (isIndestructible(downPos)) {
|
| 214 | wasIndestrictible = true;
|
| 215 | } else if (!isBedrock(downPos)) {
|
| 216 | return false;
|
| 217 | }
|
| 218 |
|
| 219 | if(!isReplaceable(checkPos.offset(0, 1, 0)) || !isReplaceable(checkPos.offset(0, 2, 0)))
|
| 220 | return false;
|
| 221 |
|
| 222 | for (Vec3i vec : VECTOR_PATTERN) {
|
| 223 | BlockPos reducedPos = checkPos.offset(vec);
|
| 224 |
|
| 225 | if (isIndestructible(reducedPos)) {
|
| 226 | wasIndestrictible = true;
|
| 227 | continue;
|
| 228 | }
|
| 229 | if (!isBedrock(reducedPos) && !checkPoses.contains(reducedPos)) {
|
| 230 | return false;
|
| 231 | }
|
| 232 | }
|
| 233 | }
|
| 234 |
|
| 235 | return wasIndestrictible;
|
| 236 | }
|
| 237 |
|
| 238 | public static boolean validQuadBedrock(@NotNull BlockPos pos) {
|
| 239 | List<BlockPos> checkPoses = getQuadDirection(pos);
|
| 240 | // If checkPoses not found -> hole incorrect
|
| 241 | if (checkPoses == null)
|
| 242 | return false;
|
| 243 |
|
| 244 | for (BlockPos checkPos : checkPoses) {
|
| 245 | BlockPos downPos = checkPos.below();
|
| 246 | if (!isBedrock(downPos))
|
| 247 | return false;
|
| 248 |
|
| 249 | if(!isReplaceable(checkPos.offset(0, 1, 0)) || !isReplaceable(checkPos.offset(0, 2, 0)))
|
| 250 | return false;
|
| 251 |
|
| 252 | for (Vec3i vec : VECTOR_PATTERN) {
|
| 253 | BlockPos reducedPos = checkPos.offset(vec);
|
| 254 | if (!isBedrock(reducedPos) && !checkPoses.contains(reducedPos)) {
|
| 255 | return false;
|
| 256 | }
|
| 257 | }
|
| 258 | }
|
| 259 |
|
| 260 | return true;
|
| 261 | }
|
| 262 |
|
| 263 | private static @Nullable List<BlockPos> getQuadDirection(@NotNull BlockPos pos) {
|
| 264 | // Try to get direction
|
| 265 | List<BlockPos> dirList = new ArrayList<>();
|
| 266 | dirList.add(pos);
|
| 267 |
|
| 268 | if (!isReplaceable(pos))
|
| 269 | return null;
|
| 270 |
|
| 271 | if (isReplaceable(pos.offset(1, 0, 0)) && isReplaceable(pos.offset(0, 0, 1)) && isReplaceable(pos.offset(1, 0, 1))) {
|
| 272 | dirList.add(pos.offset(1, 0, 0));
|
| 273 | dirList.add(pos.offset(0, 0, 1));
|
| 274 | dirList.add(pos.offset(1, 0, 1));
|
| 275 | }
|
| 276 | if (isReplaceable(pos.offset(-1, 0, 0)) && isReplaceable(pos.offset(0, 0, -1)) && isReplaceable(pos.offset(-1, 0, -1))) {
|
| 277 | dirList.add(pos.offset(-1, 0, 0));
|
| 278 | dirList.add(pos.offset(0, 0, -1));
|
| 279 | dirList.add(pos.offset(-1, 0, -1));
|
| 280 | }
|
| 281 | if (isReplaceable(pos.offset(1, 0, 0)) && isReplaceable(pos.offset(0, 0, -1)) && isReplaceable(pos.offset(1, 0, -1))) {
|
| 282 | dirList.add(pos.offset(1, 0, 0));
|
| 283 | dirList.add(pos.offset(0, 0, -1));
|
| 284 | dirList.add(pos.offset(1, 0, -1));
|
| 285 | }
|
| 286 | if (isReplaceable(pos.offset(-1, 0, 0)) && isReplaceable(pos.offset(0, 0, 1)) && isReplaceable(pos.offset(-1, 0, 1))) {
|
| 287 | dirList.add(pos.offset(-1, 0, 0));
|
| 288 | dirList.add(pos.offset(0, 0, 1));
|
| 289 | dirList.add(pos.offset(-1, 0, 1));
|
| 290 | }
|
| 291 |
|
| 292 | if (dirList.size() != 4)
|
| 293 | return null;
|
| 294 |
|
| 295 | return dirList;
|
| 296 | }
|
| 297 |
|
| 298 | private static boolean isIndestructible(BlockPos bp) {
|
| 299 | if (mc.level == null) return false;
|
| 300 |
|
| 301 | Block block = mc.level.getBlockState(bp).getBlock();
|
| 302 | return block == Blocks.OBSIDIAN || block == Blocks.NETHERITE_BLOCK
|
| 303 | || block == Blocks.CRYING_OBSIDIAN || block == Blocks.RESPAWN_ANCHOR;
|
| 304 | }
|
| 305 |
|
| 306 | private static boolean isBedrock(BlockPos bp) {
|
| 307 | if (mc.level == null) return false;
|
| 308 |
|
| 309 | return mc.level.getBlockState(bp).getBlock() == Blocks.BEDROCK;
|
| 310 | }
|
| 311 |
|
| 312 | private static boolean isReplaceable(BlockPos bp) {
|
| 313 | if (mc.level == null) return false;
|
| 314 |
|
| 315 | return mc.level.getBlockState(bp).canBeReplaced();
|
| 316 | }
|
| 317 | }
|
| 318 |
|