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 1/6] 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 From edc867f5b030689699da129ef1a4ca011fdee157 Mon Sep 17 00:00:00 2001 From: Alessio Colombo <37039432+Sfiguz7@users.noreply.github.com> Date: Sun, 24 Mar 2024 00:44:13 +0100 Subject: [PATCH 2/6] Update src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/TeleportationManager.java --- .../thebusybiscuit/slimefun4/api/gps/TeleportationManager.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 c7bc40f140..182216c0a4 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 @@ -162,7 +162,8 @@ public int getTeleportationTime(int complexity, @Nonnull Location source, @Nonnu } long speed = 50_000L + (long)complexity * complexity; - int distance = 4 * distanceSquared(source, destination), time = 1; + int distance = 4 * distanceSquared(source, destination) + int time = 1; // If speed is greater than distance, ultimate time cost must be 1 tick. // Otherwise, speed WON'T overflow the range of int. From 701f5bc0a94b53ca781a9225fe070c41f451c61d Mon Sep 17 00:00:00 2001 From: Alessio Colombo <37039432+Sfiguz7@users.noreply.github.com> Date: Sun, 24 Mar 2024 00:52:49 +0100 Subject: [PATCH 3/6] Forgot a semicolon --- .../thebusybiscuit/slimefun4/api/gps/TeleportationManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 182216c0a4..7331b70ddf 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 @@ -162,7 +162,7 @@ public int getTeleportationTime(int complexity, @Nonnull Location source, @Nonnu } long speed = 50_000L + (long)complexity * complexity; - int distance = 4 * distanceSquared(source, destination) + int distance = 4 * distanceSquared(source, destination); int time = 1; // If speed is greater than distance, ultimate time cost must be 1 tick. From f2aae99f9063e77d1c74b10eaa2678ac6740d5bc Mon Sep 17 00:00:00 2001 From: Violet-Nonbloosom <86547296+Violet-Nonbloosom@users.noreply.github.com> Date: Thu, 28 Mar 2024 00:04:30 +0800 Subject: [PATCH 4/6] Update src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/TeleportationManager.java Co-authored-by: JustAHuman-xD <65748158+JustAHuman-xD@users.noreply.github.com> --- .../slimefun4/api/gps/TeleportationManager.java | 8 ++++---- 1 file changed, 4 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 7331b70ddf..530966b0a3 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 @@ -167,10 +167,10 @@ public int getTeleportationTime(int complexity, @Nonnull Location source, @Nonnu // 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; + if (speed <= distance) { + return Math.min(distance / (int) speed, 40); + } + return 1; } @ParametersAreNonnullByDefault From 66cc640cf090bcb468c046b52b805576ea868e8d Mon Sep 17 00:00:00 2001 From: Violet-Nonbloosom <86547296+Violet-Nonbloosom@users.noreply.github.com> Date: Thu, 28 Mar 2024 00:04:42 +0800 Subject: [PATCH 5/6] Update src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/TeleportationManager.java Co-authored-by: JustAHuman-xD <65748158+JustAHuman-xD@users.noreply.github.com> --- .../thebusybiscuit/slimefun4/api/gps/TeleportationManager.java | 1 - 1 file changed, 1 deletion(-) 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 530966b0a3..a761383745 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 @@ -163,7 +163,6 @@ public int getTeleportationTime(int complexity, @Nonnull Location source, @Nonnu long speed = 50_000L + (long)complexity * complexity; int distance = 4 * distanceSquared(source, destination); - int time = 1; // If speed is greater than distance, ultimate time cost must be 1 tick. // Otherwise, speed WON'T overflow the range of int. From 3956acc434139e32341224c5315dd88c765b7c7a Mon Sep 17 00:00:00 2001 From: Violet-Nonbloosom <86547296+Violet-Nonbloosom@users.noreply.github.com> Date: Wed, 3 Apr 2024 12:18:19 +0800 Subject: [PATCH 6/6] Fix: Rewrite overflow solution. --- .../slimefun4/api/gps/TeleportationManager.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 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 a761383745..4fbc9a864f 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 @@ -160,16 +160,13 @@ public int getTeleportationTime(int complexity, @Nonnull Location source, @Nonnu if (complexity < 100) { return 100; } + else if (complexity >= 12246) { // 50_000 + c^2 >= 150_000_000 => c >= 12246 + return 1; + } - long speed = 50_000L + (long)complexity * complexity; + int speed = 50_000 + complexity * complexity; int distance = 4 * distanceSquared(source, destination); - - // 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) { - return Math.min(distance / (int) speed, 40); - } - return 1; + return Math.min(distance / speed, 40); } @ParametersAreNonnullByDefault