Skip to content

Commit

Permalink
feat(utils): RandomTeleporter findSafePosition method aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Aug 21, 2022
1 parent a53cf6a commit 7bd1152
Showing 1 changed file with 22 additions and 10 deletions.
@@ -1,15 +1,17 @@
package org.auioc.mcmod.arnicalib.utils.game;

import java.util.Optional;
import java.util.Random;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;

public class RandomTeleporter {

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();
public static Optional<BlockPos> findSafePosition(Level level, BlockPos center, int radius, boolean surface, AABB aabb, Random random) {
var pos = PositionUtils.random(center, radius, random).mutable();
if (PositionUtils.isInWorldBounds(pos, level)) {
var y = surface
? PositionUtils.findStandableY(level, pos.getX(), pos.getZ())
Expand All @@ -19,22 +21,32 @@ public static Optional<BlockPos> findSafePosition(LivingEntity living, BlockPos
} else {
return Optional.empty();
}
if (AABBUtils.isEmpty(AABBUtils.moveTo(living.getBoundingBox(), pos), level)) {
if (AABBUtils.isEmpty(AABBUtils.moveTo(aabb, pos), level)) {
return Optional.of(pos.immutable());
}
}
return Optional.empty();
}

public static boolean teleport(LivingEntity living, BlockPos center, int radius, boolean surface, int maxTries) {
if (living.level.isClientSide) return false;
public static Optional<BlockPos> findSafePosition(LivingEntity living, BlockPos center, int radius, boolean surface) {
return findSafePosition(living.getLevel(), center, radius, surface, living.getBoundingBox(), living.getRandom());
}

public static Optional<BlockPos> findSafePosition(LivingEntity living, BlockPos center, int radius, boolean surface, int maxTries) {
for (int t = 0; t < maxTries; t++) {
var pos = findSafePosition(living, center, radius, surface);
if (pos.isPresent()) {
EntityUtils.teleportTo(living, pos.get());
return true;
}
if (pos.isPresent()) return pos;
}
return Optional.empty();
}

public static boolean teleport(LivingEntity living, BlockPos center, int radius, boolean surface, int maxTries) {
if (living.level.isClientSide) return false;

var pos = findSafePosition(living, center, radius, surface, maxTries);
if (pos.isPresent()) {
EntityUtils.teleportTo(living, pos.get());
return true;
}
return false;
}
Expand Down

0 comments on commit 7bd1152

Please sign in to comment.