Skip to content

Commit

Permalink
feat(utils): findStandableY, findSafePosition methods return optional…
Browse files Browse the repository at this point in the history
… value
  • Loading branch information
WakelessSloth56 committed Aug 21, 2022
1 parent 8face1f commit a53cf6a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
@@ -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;
Expand All @@ -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<Integer> 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<Integer> findStandableY(Level level, int x, int z) {
return findStandableY(level, x, z, level.getMinBuildHeight(), level.getMaxBuildHeight() - 1);
}

Expand Down
@@ -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<BlockPos> 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;
Expand Down

0 comments on commit a53cf6a

Please sign in to comment.