flan/common/src/main/java/io/github/flemmli97/flan/player/TeleportUtils.java

79 lines
3.7 KiB
Java
Raw Normal View History

package io.github.flemmli97.flan.player;
2021-06-08 20:33:40 +00:00
import io.github.flemmli97.flan.claim.Claim;
import io.github.flemmli97.flan.claim.ClaimStorage;
2021-12-03 21:37:35 +00:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.Tuple;
import net.minecraft.world.level.chunk.ChunkStatus;
import net.minecraft.world.level.levelgen.Heightmap;
import net.minecraft.world.phys.Vec3;
2021-06-08 20:33:40 +00:00
import java.util.function.BiFunction;
public class TeleportUtils {
2021-12-03 21:37:35 +00:00
public static BlockPos roundedBlockPos(Vec3 pos) {
2023-05-06 13:58:42 +00:00
return BlockPos.containing(pos);
2021-06-08 20:33:40 +00:00
}
2021-12-03 21:37:35 +00:00
public static Vec3 getTeleportPos(ServerPlayer player, Vec3 playerPos, ClaimStorage storage, int[] dim, BlockPos.MutableBlockPos bPos, BiFunction<Claim, BlockPos, Boolean> check) {
return getTeleportPos(player, playerPos, storage, dim, false, bPos, check);
}
public static Vec3 getTeleportPos(ServerPlayer player, Vec3 playerPos, ClaimStorage storage, int[] dim, boolean checkSub, BlockPos.MutableBlockPos bPos, BiFunction<Claim, BlockPos, Boolean> check) {
2021-12-03 21:37:35 +00:00
Tuple<Direction, Vec3> pos = nearestOutside(dim, playerPos);
bPos.set(pos.getB().x(), pos.getB().y(), pos.getB().z());
2021-06-08 20:33:40 +00:00
Claim claim = storage.getClaimAt(bPos);
if (checkSub) {
Claim sub = claim != null ? claim.getSubClaim(bPos) : null;
if (sub != null)
claim = sub;
}
if (claim == null || check.apply(claim, bPos)) {
2021-12-03 21:37:35 +00:00
Vec3 ret = pos.getB();
BlockPos rounded = roundedBlockPos(ret);
2023-06-11 19:47:12 +00:00
int y = player.level().getChunk(rounded.getX() >> 4, rounded.getZ() >> 4, ChunkStatus.FULL)
2021-12-03 21:37:35 +00:00
.getHeight(Heightmap.Types.MOTION_BLOCKING, rounded.getX() & 15, rounded.getZ() & 15);
Vec3 dest = new Vec3(ret.x, y + 1, ret.z);
2023-06-11 19:47:12 +00:00
if (player.level().noCollision(player, player.getBoundingBox().move(dest.subtract(player.position()))))
2021-08-04 10:33:01 +00:00
return dest;
2021-12-03 21:37:35 +00:00
return new Vec3(rounded.getX() + 0.5, y + 1, rounded.getZ() + 0.5);
}
2021-06-08 20:33:40 +00:00
int[] newDim = claim.getDimensions();
2021-12-03 21:37:35 +00:00
switch (pos.getA()) {
case NORTH -> dim[2] = newDim[2];
case SOUTH -> dim[3] = newDim[3];
case EAST -> dim[1] = newDim[1];
default -> dim[0] = newDim[0];
2021-06-08 20:33:40 +00:00
}
return getTeleportPos(player, playerPos, storage, dim, checkSub, bPos, check);
2021-06-08 20:33:40 +00:00
}
2021-12-03 21:37:35 +00:00
private static Tuple<Direction, Vec3> nearestOutside(int[] dim, Vec3 from) {
double northDist = Math.abs(from.z() - dim[2]);
double southDist = Math.abs(dim[3] - from.z());
double westDist = Math.abs(from.x() - dim[0]);
double eastDist = Math.abs(dim[1] - from.x());
2021-06-08 20:33:40 +00:00
if (northDist > southDist) {
if (eastDist > westDist) {
if (southDist > westDist)
2021-12-03 21:37:35 +00:00
return new Tuple<>(Direction.WEST, new Vec3(dim[0] - 1.5, from.y(), from.z()));
return new Tuple<>(Direction.SOUTH, new Vec3(from.x(), from.y(), dim[3] + 1.5));
2021-06-08 20:33:40 +00:00
}
if (southDist > eastDist)
2021-12-03 21:37:35 +00:00
return new Tuple<>(Direction.EAST, new Vec3(dim[1] + 1.5, from.y(), from.z()));
return new Tuple<>(Direction.SOUTH, new Vec3(from.x(), from.y(), dim[3] + 1.5));
2021-06-08 20:33:40 +00:00
}
if (eastDist > westDist) {
if (northDist > westDist)
2021-12-03 21:37:35 +00:00
return new Tuple<>(Direction.WEST, new Vec3(dim[0] - 1.5, from.y(), from.z()));
return new Tuple<>(Direction.NORTH, new Vec3(from.x(), from.y(), dim[2] - 1.5));
2021-06-08 20:33:40 +00:00
}
if (northDist > eastDist)
2021-12-03 21:37:35 +00:00
return new Tuple<>(Direction.EAST, new Vec3(dim[1] + 1.5, from.y(), from.z()));
return new Tuple<>(Direction.NORTH, new Vec3(from.x(), from.y(), dim[2] - 1.5));
2021-06-08 20:33:40 +00:00
}
}