diff --git a/src/main/java/net/knarcraft/permissionsigns/container/PermissionSign.java b/src/main/java/net/knarcraft/permissionsigns/container/PermissionSign.java index 4314b49..62a0e6d 100644 --- a/src/main/java/net/knarcraft/permissionsigns/container/PermissionSign.java +++ b/src/main/java/net/knarcraft/permissionsigns/container/PermissionSign.java @@ -7,7 +7,10 @@ import org.bukkit.ChatColor; import org.bukkit.Location; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * This class represents a placed and active permission sign @@ -20,6 +23,9 @@ public class PermissionSign { private final int duration; private final double cost; + private static Map timeUnits; + private static List sortedUnits; + /** * Instantiates a new permission sign * @@ -152,30 +158,17 @@ public class PermissionSign { if (duration == 0) { return Translator.getTranslatedMessage(TranslatableMessage.SIGN_PERMANENT); } else { - double minute = 60; - double hour = minute * 60; - double day = hour * 24; - if (duration / day >= 1) { - double days = round(duration / day); - if (days == 1) { - return (int) days + " " + Translator.getTranslatedMessage(TranslatableMessage.UNIT_DAY); - } else { - return days + " " + Translator.getTranslatedMessage(TranslatableMessage.UNIT_DAYS); - } - } else if (duration / hour >= 1) { - double hours = round(duration / hour); - if (hours == 1) { - return (int) hours + " " + Translator.getTranslatedMessage(TranslatableMessage.UNIT_HOUR); - } else { - return hours + " " + Translator.getTranslatedMessage(TranslatableMessage.UNIT_HOURS); - } - } else { - if (duration == 1) { - return duration + " " + Translator.getTranslatedMessage(TranslatableMessage.UNIT_SECOND); - } else { - return duration + " " + Translator.getTranslatedMessage(TranslatableMessage.UNIT_SECONDS); + if (sortedUnits == null) { + initializeUnits(); + } + for (Double unit : sortedUnits) { + if (duration / unit >= 1) { + double units = round(duration / unit); + return formatDurationString(units, timeUnits.get(unit)[units == 1 ? 0 : 1], + (units * 10) % 10 == 0); } } + return formatDurationString(duration, TranslatableMessage.UNIT_SECONDS, false); } } @@ -203,4 +196,39 @@ public class PermissionSign { return Math.round(number * 100.0) / 100.0; } + /** + * Formats a duration string + * + * @param duration

The duration to display

+ * @param translatableMessage

The time unit to display

+ * @param castToInt

Whether to cast the duration to an int

+ * @return

The formatted duration string

+ */ + private String formatDurationString(double duration, TranslatableMessage translatableMessage, boolean castToInt) { + if (castToInt) { + return (int) duration + " " + Translator.getTranslatedMessage(translatableMessage); + } else { + return duration + " " + Translator.getTranslatedMessage(translatableMessage); + } + } + + /** + * Initializes the mapping of available time units for formatting permission sign duration + */ + private static void initializeUnits() { + double minute = 60; + double hour = minute * 60; + double day = hour * 24; + + timeUnits = new HashMap<>(); + timeUnits.put(day, new TranslatableMessage[]{TranslatableMessage.UNIT_DAY, TranslatableMessage.UNIT_DAYS}); + timeUnits.put(hour, new TranslatableMessage[]{TranslatableMessage.UNIT_HOUR, TranslatableMessage.UNIT_HOURS}); + timeUnits.put(minute, new TranslatableMessage[]{TranslatableMessage.UNIT_MINUTE, TranslatableMessage.UNIT_MINUTES}); + timeUnits.put(1D, new TranslatableMessage[]{TranslatableMessage.UNIT_SECOND, TranslatableMessage.UNIT_SECONDS}); + + sortedUnits = new ArrayList<>(timeUnits.keySet()); + Collections.sort(sortedUnits); + Collections.reverse(sortedUnits); + } + }