Skip to content

Commit

Permalink
feat(utils): safe teleport method always_on_surface switch
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Jun 29, 2022
1 parent d2391ec commit 15ec843
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
@@ -1,7 +1,6 @@
package org.auioc.mcmod.arnicalib.utils.game;

import java.util.Random;
import javax.annotation.Nullable;
import org.auioc.mcmod.arnicalib.api.java.exception.HException;
import org.auioc.mcmod.arnicalib.utils.java.RandomUtils;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -44,14 +43,10 @@ static int findStandableY(Level level, int x, int z, int minY, int maxY) throws
}
}

static int findStandableY(Level level, int x, int z) throws Exception {
static int findStandableY(Level level, int x, int z) throws HException {
return findStandableY(level, x, z, level.getMinBuildHeight(), level.getMaxBuildHeight() - 1);
}

static MutableBlockPos setToStandableY(MutableBlockPos pos, @Nullable Integer minY, Level level) throws HException {
return pos.setY(findStandableY(level, pos.getX(), pos.getZ(), minY, pos.getY()));
}

static Vec3i random(Vec3i center, int radius, Random random) {
radius += 1;
return new Vec3i(
Expand Down
Expand Up @@ -7,24 +7,28 @@

public class RandomTeleporter {

private static BlockPos findRandomSafePos(LivingEntity living, BlockPos center, int radius) throws HException {
public static BlockPos findRandomSafePos(LivingEntity living, BlockPos center, int radius, boolean surface) throws HException {
var level = living.getLevel();
var pos = PositionUtils.random(center, radius, living.getRandom()).mutable();
if (PositionUtils.isInWorldBounds(pos, level)) {
PositionUtils.setToStandableY(pos, center.getY() - radius, level); // throws HException
pos.setY(
surface
? PositionUtils.findStandableY(level, pos.getX(), pos.getZ())
: PositionUtils.findStandableY(level, pos.getX(), pos.getZ(), center.getY() - radius, pos.getY())
); // throws HException
if (AABBUtils.isEmpty(AABBUtils.moveTo(living.getBoundingBox(), pos), level)) {
return pos.immutable();
}
}
throw new HException();
}

public static boolean safeTeleport(LivingEntity living, BlockPos center, int radius, int maxTries) {
public static boolean safeTeleport(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));
EntityUtils.teleportTo(living, findRandomSafePos(living, center, radius, surface));
return true;
} catch (HException e) {
continue;
Expand All @@ -34,7 +38,7 @@ public static boolean safeTeleport(LivingEntity living, BlockPos center, int rad
}

public static boolean safeTeleport(LivingEntity living, int radius, int maxTries) {
return safeTeleport(living, living.blockPosition(), radius, maxTries);
return safeTeleport(living, living.blockPosition(), radius, false, maxTries);
}

public static void unsafeTeleport(LivingEntity living, Vec3 center, double radius) {
Expand Down

0 comments on commit 15ec843

Please sign in to comment.