Allows partial usage of time units if preferable
All checks were successful
EpicKnarvik97/KnarLib/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2023-03-06 14:31:49 +01:00
parent e8d62930ca
commit 2618d42992
2 changed files with 34 additions and 11 deletions

View File

@ -81,7 +81,7 @@
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId> <artifactId>spigot-api</artifactId>
<version>1.19.2-R0.1-SNAPSHOT</version> <version>1.19.3-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -33,7 +33,7 @@ public final class TimeFormatter {
return translator.getTranslatedMessage(TranslatableTimeUnit.UNIT_NOW); return translator.getTranslatedMessage(TranslatableTimeUnit.UNIT_NOW);
} else { } else {
if (sortedUnits == null) { if (sortedUnits == null) {
initializeUnits(); initializeUnits(translator);
} }
for (double unit : sortedUnits) { for (double unit : sortedUnits) {
if (duration / unit >= 1) { if (duration / unit >= 1) {
@ -77,7 +77,7 @@ public final class TimeFormatter {
/** /**
* Initializes the mapping of available time units for formatting permission sign duration * Initializes the mapping of available time units for formatting permission sign duration
*/ */
private static void initializeUnits() { private static void initializeUnits(Translator translator) {
double minute = 60; double minute = 60;
double hour = 60 * minute; double hour = 60 * minute;
double day = 24 * hour; double day = 24 * hour;
@ -87,18 +87,41 @@ public final class TimeFormatter {
double decade = 10 * year; double decade = 10 * year;
timeUnits = new HashMap<>(); timeUnits = new HashMap<>();
timeUnits.put(decade, new TranslatableTimeUnit[]{TranslatableTimeUnit.UNIT_DECADE, TranslatableTimeUnit.UNIT_DECADES}); registerTimeUnit(translator, decade, TranslatableTimeUnit.UNIT_DECADE, TranslatableTimeUnit.UNIT_DECADES);
timeUnits.put(year, new TranslatableTimeUnit[]{TranslatableTimeUnit.UNIT_YEAR, TranslatableTimeUnit.UNIT_YEARS}); registerTimeUnit(translator, year, TranslatableTimeUnit.UNIT_YEAR, TranslatableTimeUnit.UNIT_YEARS);
timeUnits.put(month, new TranslatableTimeUnit[]{TranslatableTimeUnit.UNIT_MONTH, TranslatableTimeUnit.UNIT_MONTHS}); registerTimeUnit(translator, month, TranslatableTimeUnit.UNIT_MONTH, TranslatableTimeUnit.UNIT_MONTHS);
timeUnits.put(week, new TranslatableTimeUnit[]{TranslatableTimeUnit.UNIT_WEEK, TranslatableTimeUnit.UNIT_WEEKS}); registerTimeUnit(translator, week, TranslatableTimeUnit.UNIT_WEEK, TranslatableTimeUnit.UNIT_WEEKS);
timeUnits.put(day, new TranslatableTimeUnit[]{TranslatableTimeUnit.UNIT_DAY, TranslatableTimeUnit.UNIT_DAYS}); registerTimeUnit(translator, day, TranslatableTimeUnit.UNIT_DAY, TranslatableTimeUnit.UNIT_DAYS);
timeUnits.put(hour, new TranslatableTimeUnit[]{TranslatableTimeUnit.UNIT_HOUR, TranslatableTimeUnit.UNIT_HOURS}); registerTimeUnit(translator, hour, TranslatableTimeUnit.UNIT_HOUR, TranslatableTimeUnit.UNIT_HOURS);
timeUnits.put(minute, new TranslatableTimeUnit[]{TranslatableTimeUnit.UNIT_MINUTE, TranslatableTimeUnit.UNIT_MINUTES}); registerTimeUnit(translator, minute, TranslatableTimeUnit.UNIT_MINUTE, TranslatableTimeUnit.UNIT_MINUTES);
timeUnits.put(1D, new TranslatableTimeUnit[]{TranslatableTimeUnit.UNIT_SECOND, TranslatableTimeUnit.UNIT_SECONDS}); if (!registerTimeUnit(translator, 1D, TranslatableTimeUnit.UNIT_SECOND, TranslatableTimeUnit.UNIT_SECONDS)) {
throw new IllegalStateException("No translation has been registered for UNIT_SECOND and UNIT_SECONDS");
}
sortedUnits = new ArrayList<>(timeUnits.keySet()); sortedUnits = new ArrayList<>(timeUnits.keySet());
Collections.sort(sortedUnits); Collections.sort(sortedUnits);
Collections.reverse(sortedUnits); Collections.reverse(sortedUnits);
} }
/**
* Registers one time unit
*
* @param translator <p>The translator to use for checking whether necessary translations have been set</p>
* @param seconds <p>The amount of seconds the time unit corresponds to</p>
* @param singular <p>The translatable time unit to use for the singular form</p>
* @param plural <p>The translatable time unit to use for the plural form</p>
*/
private static boolean registerTimeUnit(Translator translator, double seconds, TranslatableTimeUnit singular,
TranslatableTimeUnit plural) {
String singularTranslation = translator.getTranslatedMessage(singular);
String pluralTranslation = translator.getTranslatedMessage(plural);
if (singularTranslation != null && !singularTranslation.isBlank() && pluralTranslation != null &&
!pluralTranslation.isBlank()) {
timeUnits.put(seconds, new TranslatableTimeUnit[]{singular, plural});
return true;
} else {
return false;
}
}
} }