From cedd42f41155dc282a737fa643afe8fbdf748c62 Mon Sep 17 00:00:00 2001 From: Tim S <48109997+JakeTS@users.noreply.github.com> Date: Fri, 17 Jul 2020 16:56:48 -0400 Subject: [PATCH] Fix how random teleport picks a location (#3480) --- .../earth2me/essentials/RandomTeleport.java | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/RandomTeleport.java b/Essentials/src/com/earth2me/essentials/RandomTeleport.java index 39e42c0fd..29b9b3d84 100644 --- a/Essentials/src/com/earth2me/essentials/RandomTeleport.java +++ b/Essentials/src/com/earth2me/essentials/RandomTeleport.java @@ -155,12 +155,29 @@ public class RandomTeleport implements IConf { // Calculates a random location asynchronously. private CompletableFuture calculateRandomLocation(Location center, double minRange, double maxRange) { CompletableFuture future = new CompletableFuture<>(); - final int dx = RANDOM.nextBoolean() ? 1 : -1, dz = RANDOM.nextBoolean() ? 1 : -1; + // Find an equally distributed offset by randomly rotating a point inside a rectangle about the origin + double rectX = RANDOM.nextDouble() * (maxRange - minRange) + minRange; + double rectZ = RANDOM.nextDouble() * (maxRange + minRange) - minRange; + double offsetX, offsetZ; + int transform = RANDOM.nextInt(4); + if (transform == 0) { + offsetX = rectX; + offsetZ = rectZ; + } else if (transform == 1) { + offsetX = -rectZ; + offsetZ = rectX; + } else if (transform == 2) { + offsetX = -rectX; + offsetZ = -rectZ; + } else { + offsetX = rectZ; + offsetZ = -rectX; + } Location location = new Location( center.getWorld(), - center.getX() + dx * (minRange + RANDOM.nextDouble() * (maxRange - minRange)), + center.getX() + offsetX, center.getWorld().getMaxHeight(), - center.getZ() + dz * (minRange + RANDOM.nextDouble() * (maxRange - minRange)), + center.getZ() + offsetZ, 360 * RANDOM.nextFloat() - 180, 0 );