diff --git a/src/main/java/org/auioc/mcmod/arnicalib/utils/game/PositionUtils.java b/src/main/java/org/auioc/mcmod/arnicalib/utils/game/PositionUtils.java index f0aa535d..291b6b0c 100644 --- a/src/main/java/org/auioc/mcmod/arnicalib/utils/game/PositionUtils.java +++ b/src/main/java/org/auioc/mcmod/arnicalib/utils/game/PositionUtils.java @@ -1,7 +1,7 @@ package org.auioc.mcmod.arnicalib.utils.game; +import java.util.Optional; import java.util.Random; -import org.auioc.mcmod.arnicalib.api.java.exception.HException; import org.auioc.mcmod.arnicalib.utils.java.RandomUtils; import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos.MutableBlockPos; @@ -27,23 +27,23 @@ static boolean canStand(BlockPos pos, Level level) { return canStandOn(pos.below(), level); } - static int findStandableY(Level level, int x, int z, int minY, int maxY) throws HException { + static Optional findStandableY(Level level, int x, int z, int minY, int maxY) { minY = Math.max(minY, level.getMinBuildHeight()); maxY = Math.min(maxY, level.getMaxBuildHeight() - 1); var pos = new MutableBlockPos(x, maxY, z); while (true) { if (PositionUtils.canStand(pos, level)) { - return pos.getY(); + return Optional.of(pos.getY()); } if (pos.getY() > minY) { pos.move(Direction.DOWN); } else { - throw new HException(); + Optional.empty(); } } } - static int findStandableY(Level level, int x, int z) throws HException { + static Optional findStandableY(Level level, int x, int z) { return findStandableY(level, x, z, level.getMinBuildHeight(), level.getMaxBuildHeight() - 1); } diff --git a/src/main/java/org/auioc/mcmod/arnicalib/utils/game/RandomTeleporter.java b/src/main/java/org/auioc/mcmod/arnicalib/utils/game/RandomTeleporter.java index 1fb0de96..aec07ade 100644 --- a/src/main/java/org/auioc/mcmod/arnicalib/utils/game/RandomTeleporter.java +++ b/src/main/java/org/auioc/mcmod/arnicalib/utils/game/RandomTeleporter.java @@ -1,37 +1,39 @@ package org.auioc.mcmod.arnicalib.utils.game; -import org.auioc.mcmod.arnicalib.api.java.exception.HException; +import java.util.Optional; import net.minecraft.core.BlockPos; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.phys.Vec3; public class RandomTeleporter { - public static BlockPos findRandomSafePos(LivingEntity living, BlockPos center, int radius, boolean surface) throws HException { + public static Optional findSafePosition(LivingEntity living, BlockPos center, int radius, boolean surface) { var level = living.getLevel(); var pos = PositionUtils.random(center, radius, living.getRandom()).mutable(); if (PositionUtils.isInWorldBounds(pos, level)) { - pos.setY( - surface - ? PositionUtils.findStandableY(level, pos.getX(), pos.getZ()) - : PositionUtils.findStandableY(level, pos.getX(), pos.getZ(), center.getY() - radius, pos.getY()) - ); // throws HException + var y = surface + ? PositionUtils.findStandableY(level, pos.getX(), pos.getZ()) + : PositionUtils.findStandableY(level, pos.getX(), pos.getZ(), center.getY() - radius, pos.getY()); + if (y.isPresent()) { + pos.setY(y.get()); + } else { + return Optional.empty(); + } if (AABBUtils.isEmpty(AABBUtils.moveTo(living.getBoundingBox(), pos), level)) { - return pos.immutable(); + return Optional.of(pos.immutable()); } } - throw new HException(); + return Optional.empty(); } public static boolean teleport(LivingEntity living, BlockPos center, int radius, boolean surface, int maxTries) { if (living.level.isClientSide) return false; for (int t = 0; t < maxTries; t++) { - try { - EntityUtils.teleportTo(living, findRandomSafePos(living, center, radius, surface)); + var pos = findSafePosition(living, center, radius, surface); + if (pos.isPresent()) { + EntityUtils.teleportTo(living, pos.get()); return true; - } catch (HException e) { - continue; } } return false;