From c16fbf40ab0ebc7146589f146c0742d039a5b24e Mon Sep 17 00:00:00 2001 From: William Date: Fri, 24 Feb 2023 17:28:11 +0000 Subject: [PATCH] Simplify string comparable weight logic to fix issues with low weight diffs --- .../net/william278/velocitab/player/Role.java | 23 ++++--------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/src/main/java/net/william278/velocitab/player/Role.java b/src/main/java/net/william278/velocitab/player/Role.java index 4ee731a..ef4950d 100644 --- a/src/main/java/net/william278/velocitab/player/Role.java +++ b/src/main/java/net/william278/velocitab/player/Role.java @@ -40,25 +40,10 @@ public class Role implements Comparable { return Optional.ofNullable(name); } + @NotNull public String getStringComparableWeight(int maximumPossibleWeight, int lowestPossibleWeight) { - // Calculate the weight range and the ratio of the input weight to the weight range - int weightRange = maximumPossibleWeight - lowestPossibleWeight; - double weightRatio = (double) (maximumPossibleWeight - weight) / weightRange; - - // Convert the weight ratio to a string with 3 decimal places and remove the decimal point - String weightString = String.format("%.3f", weightRatio).replace(".", ""); - - // Pad the weight string with leading zeros to a length of 6 characters - weightString = String.format("%6s", weightString).replace(' ', '0'); - - // Prepend a minus sign for negative weights - if (weight < 0) { - weightString = "-" + weightString.substring(1); - } else { - // Reverse the weight string for non-negative weights - weightString = new StringBuilder(weightString).reverse().toString(); - } - - return weightString; + return String.format("%0" + String.valueOf(maximumPossibleWeight).length() + "d", weight - lowestPossibleWeight) + .replace(" ", "0") + .replace("-", "0"); } }