From 7bd11526b79d7a33711d065131d3c6ec64a46ba7 Mon Sep 17 00:00:00 2001 From: WakelessSloth56 Date: Mon, 22 Aug 2022 01:32:54 +0800 Subject: [PATCH] feat(utils): RandomTeleporter findSafePosition method aliases --- .../utils/game/RandomTeleporter.java | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) 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 aec07ade..bcafe100 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,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 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 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()) @@ -19,22 +21,32 @@ public static Optional 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 findSafePosition(LivingEntity living, BlockPos center, int radius, boolean surface) { + return findSafePosition(living.getLevel(), center, radius, surface, living.getBoundingBox(), living.getRandom()); + } + public static Optional 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; }