Improves unit conversion by removing some redundancy and adding the seemingly missing minute unit

This commit is contained in:
Kristian Knarvik 2022-01-22 18:02:03 +01:00
parent d1a7735b51
commit b89b993220

View File

@ -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<Double, TranslatableMessage[]> timeUnits;
private static List<Double> 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);
if (sortedUnits == null) {
initializeUnits();
}
} 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);
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 <p>The duration to display</p>
* @param translatableMessage <p>The time unit to display</p>
* @param castToInt <p>Whether to cast the duration to an int</p>
* @return <p>The formatted duration string</p>
*/
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);
}
}