From 8fe3084892caa09791b1c18ef94e495e5a989041 Mon Sep 17 00:00:00 2001 From: Violet-Nonbloosom <86547296+Violet-Nonbloosom@users.noreply.github.com> Date: Thu, 21 Mar 2024 18:01:07 +0800 Subject: [PATCH] Fix TP time overflow problem --- .../slimefun4/api/gps/TeleportationManager.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/TeleportationManager.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/TeleportationManager.java index 19646ed4c0..c7bc40f140 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/TeleportationManager.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/TeleportationManager.java @@ -161,11 +161,15 @@ public int getTeleportationTime(int complexity, @Nonnull Location source, @Nonnu return 100; } - int speed = 50_000 + complexity * complexity; - int unsafeTime = Math.min(4 * distanceSquared(source, destination) / speed, 40); + long speed = 50_000L + (long)complexity * complexity; + int distance = 4 * distanceSquared(source, destination), time = 1; - // Fixes #3573 - Using Math.max is a safer way to ensure values > 0 than relying on addition. - return Math.max(1, unsafeTime); + // If speed is greater than distance, ultimate time cost must be 1 tick. + // Otherwise, speed WON'T overflow the range of int. + if (speed <= distance) + time = Math.min(distance / (int)speed, 40); + + return time; } @ParametersAreNonnullByDefault