package me.fridtjof.minecarttrains.utils; import org.bukkit.Location; import org.bukkit.Tag; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.data.Rail; import org.bukkit.util.Vector; import java.util.HashSet; import java.util.Set; public final class Rails { public static final double HEIGHT = 0.0625D; private static final int MAX_WALK_BLOCKS = 96; private Rails() { } public static boolean isRail(Block block) { return Tag.RAILS.isTagged(block.getType()); } public static Block railBlock(Location location) { Block block = location.getBlock(); if (isRail(block)) { return block; } Block below = block.getRelative(BlockFace.DOWN); return isRail(below) ? below : block; } public static Vector tangent(Location location, Vector hint) { Pose snapped = snap(location, hint); if (snapped != null) { return snapped.tangent(); } return flattened(hint); } public static Vector tangent(Rail.Shape shape, Vector hint) { Vector[] options = axes(shape); Vector best = options[0]; double bestDot = -2; Vector hintCopy = hint.clone(); if (hintCopy.lengthSquared() < 1.0E-8) { return best.clone(); } hintCopy.normalize(); for (Vector option : options) { double dot = option.dot(hintCopy); if (dot > bestDot) { bestDot = dot; best = option; } } return best.clone(); } public static Pose snap(Location location, Vector heading) { Block block = railBlock(location); if (!(block.getBlockData() instanceof Rail rail)) { return null; } Vector dir = unitOrZero(heading); double[] local = localPoint(block, location); double[][] points = facing(pathPoints(rail.getShape()), dir, local); PathHit hit = project(points, local); if (hit == null) { return null; } return new Pose(worldPoint(block, hit.x, hit.y, hit.z), hit.tangent.clone()); } public static Pose walk(Location start, Vector heading, double distance) { if (distance <= 1.0E-8) { return snap(start, heading); } Pose snapped = snap(start, heading); if (snapped == null) { return null; } Vector dir = unitOrZero(heading); if (dir.lengthSquared() < 1.0E-8) { return snapped; } Location current = snapped.location(); Block block = railBlock(current); double remaining = distance; Set visited = new HashSet<>(); for (int guard = 0; guard < MAX_WALK_BLOCKS && remaining > 1.0E-8; guard++) { if (!(block.getBlockData() instanceof Rail rail)) { return new Pose(current, dir); } Step step = stepInBlock(block, rail.getShape(), current, dir, remaining); current = step.location; dir = step.tangent; remaining -= step.consumed; if (remaining <= 1.0E-8) { return new Pose(current, dir); } if (step.exit == null || step.exit == BlockFace.SELF) { return new Pose(current, dir); } Block next = nextRail(block, step.exit); if (next == null) { return new Pose(current, dir); } if (step.consumed < 1.0E-7 && !visited.add(packBlock(block))) { return new Pose(current, dir); } block = next; Pose entered = snap(current, dir); if (entered != null) { current = entered.location(); dir = entered.tangent(); } } return new Pose(current, dir); } public static float yaw(Vector tangent) { return (float) Math.toDegrees(Math.atan2(-tangent.getX(), tangent.getZ())); } private static Step stepInBlock(Block block, Rail.Shape shape, Location world, Vector dir, double remaining) { double[] local = localPoint(block, world); double[][] points = facing(pathPoints(shape), dir, local); PathHit here = project(points, local); double pathLen = pathLength(points); if (here == null) { return new Step(world.clone(), dir.clone(), remaining, null); } double target = here.along + remaining; if (target <= pathLen - 1.0E-7) { PathHit dest = atDistance(points, target); return new Step(worldPoint(block, dest.x, dest.y, dest.z), dest.tangent.clone(), remaining, null); } PathHit end = atDistance(points, pathLen); double consumed = Math.max(0, pathLen - here.along); return new Step(worldPoint(block, end.x, end.y, end.z), end.tangent.clone(), consumed, exitFace(end.x, end.z)); } private static Block nextRail(Block current, BlockFace exit) { Block adjacent = current.getRelative(exit); if (isRail(adjacent)) { return adjacent; } Block up = adjacent.getRelative(BlockFace.UP); if (isRail(up)) { return up; } Block down = adjacent.getRelative(BlockFace.DOWN); return isRail(down) ? down : null; } private static double[][] pathPoints(Rail.Shape shape) { return switch (shape) { case NORTH_SOUTH -> new double[][] {{0.5, 0, 0}, {0.5, 0, 1}}; case EAST_WEST -> new double[][] {{0, 0, 0.5}, {1, 0, 0.5}}; case ASCENDING_NORTH -> new double[][] {{0.5, 0, 1}, {0.5, 1, 0}}; case ASCENDING_SOUTH -> new double[][] {{0.5, 0, 0}, {0.5, 1, 1}}; case ASCENDING_EAST -> new double[][] {{0, 0, 0.5}, {1, 1, 0.5}}; case ASCENDING_WEST -> new double[][] {{1, 0, 0.5}, {0, 1, 0.5}}; case SOUTH_EAST -> new double[][] {{0.5, 0, 1}, {0.5, 0, 0.5}, {1, 0, 0.5}}; case SOUTH_WEST -> new double[][] {{0.5, 0, 1}, {0.5, 0, 0.5}, {0, 0, 0.5}}; case NORTH_WEST -> new double[][] {{0.5, 0, 0}, {0.5, 0, 0.5}, {0, 0, 0.5}}; case NORTH_EAST -> new double[][] {{0.5, 0, 0}, {0.5, 0, 0.5}, {1, 0, 0.5}}; }; } private static double[][] facing(double[][] points, Vector dir, double[] local) { PathHit hit = project(points, local); if (hit == null || hit.tangent.dot(dir) >= 0) { return points; } return reverse(points); } private static double[][] reverse(double[][] points) { double[][] reversed = new double[points.length][]; for (int i = 0; i < points.length; i++) { reversed[i] = points[points.length - 1 - i]; } return reversed; } private static PathHit project(double[][] points, double[] local) { PathHit best = null; double bestDist = Double.MAX_VALUE; double along = 0; for (int i = 0; i < points.length - 1; i++) { double[] a = points[i]; double[] b = points[i + 1]; double abx = b[0] - a[0]; double aby = b[1] - a[1]; double abz = b[2] - a[2]; double ab2 = abx * abx + aby * aby + abz * abz; double t = 0; if (ab2 > 1.0E-12) { t = ((local[0] - a[0]) * abx + (local[1] - a[1]) * aby + (local[2] - a[2]) * abz) / ab2; t = Math.max(0, Math.min(1, t)); } double px = a[0] + abx * t; double py = a[1] + aby * t; double pz = a[2] + abz * t; double dist = (local[0] - px) * (local[0] - px) + (local[1] - py) * (local[1] - py) + (local[2] - pz) * (local[2] - pz); double segLen = Math.sqrt(ab2); if (dist < bestDist) { bestDist = dist; Vector tangent = segLen > 1.0E-8 ? new Vector(abx / segLen, aby / segLen, abz / segLen) : new Vector(); best = new PathHit(along + segLen * t, px, py, pz, tangent); } along += segLen; } return best; } private static PathHit atDistance(double[][] points, double distance) { double along = 0; for (int i = 0; i < points.length - 1; i++) { double[] a = points[i]; double[] b = points[i + 1]; double seg = dist(a, b); boolean last = i == points.length - 2; if (distance <= along + seg || last) { double t = seg < 1.0E-8 ? 0 : (distance - along) / seg; t = Math.max(0, Math.min(1, t)); Vector tangent = seg > 1.0E-8 ? new Vector((b[0] - a[0]) / seg, (b[1] - a[1]) / seg, (b[2] - a[2]) / seg) : new Vector(); return new PathHit(along + seg * t, a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t, tangent); } along += seg; } double[] last = points[points.length - 1]; return new PathHit(along, last[0], last[1], last[2], new Vector()); } private static double pathLength(double[][] points) { double length = 0; for (int i = 0; i < points.length - 1; i++) { length += dist(points[i], points[i + 1]); } return length; } private static double dist(double[] a, double[] b) { double dx = b[0] - a[0]; double dy = b[1] - a[1]; double dz = b[2] - a[2]; return Math.sqrt(dx * dx + dy * dy + dz * dz); } private static BlockFace exitFace(double x, double z) { if (x >= 0.999) { return BlockFace.EAST; } if (x <= 0.001) { return BlockFace.WEST; } if (z >= 0.999) { return BlockFace.SOUTH; } if (z <= 0.001) { return BlockFace.NORTH; } return BlockFace.SELF; } private static double[] localPoint(Block block, Location location) { return new double[] { location.getX() - block.getX(), location.getY() - (block.getY() + HEIGHT), location.getZ() - block.getZ() }; } private static Location worldPoint(Block block, double x, double y, double z) { return new Location(block.getWorld(), block.getX() + x, block.getY() + HEIGHT + y, block.getZ() + z); } private static Vector[] axes(Rail.Shape shape) { return switch (shape) { case NORTH_SOUTH -> new Vector[] {vec(0, 0, 1), vec(0, 0, -1)}; case EAST_WEST -> new Vector[] {vec(1, 0, 0), vec(-1, 0, 0)}; case ASCENDING_NORTH -> new Vector[] {norm(0, 1, -1), norm(0, -1, 1)}; case ASCENDING_SOUTH -> new Vector[] {norm(0, 1, 1), norm(0, -1, -1)}; case ASCENDING_EAST -> new Vector[] {norm(1, 1, 0), norm(-1, -1, 0)}; case ASCENDING_WEST -> new Vector[] {norm(-1, 1, 0), norm(1, -1, 0)}; case SOUTH_EAST -> new Vector[] {vec(0, 0, 1), vec(1, 0, 0)}; case SOUTH_WEST -> new Vector[] {vec(0, 0, 1), vec(-1, 0, 0)}; case NORTH_WEST -> new Vector[] {vec(0, 0, -1), vec(-1, 0, 0)}; case NORTH_EAST -> new Vector[] {vec(0, 0, -1), vec(1, 0, 0)}; }; } private static Vector flattened(Vector hint) { Vector flat = hint.clone().setY(0); if (flat.lengthSquared() < 1.0E-8) { return new Vector(0, 0, 0); } return flat.normalize(); } private static Vector unitOrZero(Vector heading) { if (heading == null || heading.lengthSquared() < 1.0E-8) { return new Vector(); } return heading.clone().normalize(); } private static long packBlock(Block block) { return ((long) block.getX() * 73428767L) ^ ((long) block.getZ() * 19349663L) ^ block.getY(); } private static Vector vec(double x, double y, double z) { return new Vector(x, y, z); } private static Vector norm(double x, double y, double z) { return new Vector(x, y, z).normalize(); } public static final class Pose { private final Location location; private final Vector tangent; public Pose(Location location, Vector tangent) { this.location = location; this.tangent = tangent; } public Location location() { return location; } public Vector tangent() { return tangent; } } private static final class PathHit { private final double along; private final double x; private final double y; private final double z; private final Vector tangent; private PathHit(double along, double x, double y, double z, Vector tangent) { this.along = along; this.x = x; this.y = y; this.z = z; this.tangent = tangent; } } private static final class Step { private final Location location; private final Vector tangent; private final double consumed; private final BlockFace exit; private Step(Location location, Vector tangent, double consumed, BlockFace exit) { this.location = location; this.tangent = tangent; this.consumed = consumed; this.exit = exit; } } }