Allows partial usage of time units if preferable
All checks were successful
EpicKnarvik97/KnarLib/pipeline/head This commit looks good
All checks were successful
EpicKnarvik97/KnarLib/pipeline/head This commit looks good
This commit is contained in:
parent
e8d62930ca
commit
2618d42992
2
pom.xml
2
pom.xml
@ -81,7 +81,7 @@
|
||||
<dependency>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
<artifactId>spigot-api</artifactId>
|
||||
<version>1.19.2-R0.1-SNAPSHOT</version>
|
||||
<version>1.19.3-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -33,7 +33,7 @@ public final class TimeFormatter {
|
||||
return translator.getTranslatedMessage(TranslatableTimeUnit.UNIT_NOW);
|
||||
} else {
|
||||
if (sortedUnits == null) {
|
||||
initializeUnits();
|
||||
initializeUnits(translator);
|
||||
}
|
||||
for (double unit : sortedUnits) {
|
||||
if (duration / unit >= 1) {
|
||||
@ -77,7 +77,7 @@ public final class TimeFormatter {
|
||||
/**
|
||||
* 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 hour = 60 * minute;
|
||||
double day = 24 * hour;
|
||||
@ -87,18 +87,41 @@ public final class TimeFormatter {
|
||||
double decade = 10 * year;
|
||||
|
||||
timeUnits = new HashMap<>();
|
||||
timeUnits.put(decade, new TranslatableTimeUnit[]{TranslatableTimeUnit.UNIT_DECADE, TranslatableTimeUnit.UNIT_DECADES});
|
||||
timeUnits.put(year, new TranslatableTimeUnit[]{TranslatableTimeUnit.UNIT_YEAR, TranslatableTimeUnit.UNIT_YEARS});
|
||||
timeUnits.put(month, new TranslatableTimeUnit[]{TranslatableTimeUnit.UNIT_MONTH, TranslatableTimeUnit.UNIT_MONTHS});
|
||||
timeUnits.put(week, new TranslatableTimeUnit[]{TranslatableTimeUnit.UNIT_WEEK, TranslatableTimeUnit.UNIT_WEEKS});
|
||||
timeUnits.put(day, new TranslatableTimeUnit[]{TranslatableTimeUnit.UNIT_DAY, TranslatableTimeUnit.UNIT_DAYS});
|
||||
timeUnits.put(hour, new TranslatableTimeUnit[]{TranslatableTimeUnit.UNIT_HOUR, TranslatableTimeUnit.UNIT_HOURS});
|
||||
timeUnits.put(minute, new TranslatableTimeUnit[]{TranslatableTimeUnit.UNIT_MINUTE, TranslatableTimeUnit.UNIT_MINUTES});
|
||||
timeUnits.put(1D, new TranslatableTimeUnit[]{TranslatableTimeUnit.UNIT_SECOND, TranslatableTimeUnit.UNIT_SECONDS});
|
||||
registerTimeUnit(translator, decade, TranslatableTimeUnit.UNIT_DECADE, TranslatableTimeUnit.UNIT_DECADES);
|
||||
registerTimeUnit(translator, year, TranslatableTimeUnit.UNIT_YEAR, TranslatableTimeUnit.UNIT_YEARS);
|
||||
registerTimeUnit(translator, month, TranslatableTimeUnit.UNIT_MONTH, TranslatableTimeUnit.UNIT_MONTHS);
|
||||
registerTimeUnit(translator, week, TranslatableTimeUnit.UNIT_WEEK, TranslatableTimeUnit.UNIT_WEEKS);
|
||||
registerTimeUnit(translator, day, TranslatableTimeUnit.UNIT_DAY, TranslatableTimeUnit.UNIT_DAYS);
|
||||
registerTimeUnit(translator, hour, TranslatableTimeUnit.UNIT_HOUR, TranslatableTimeUnit.UNIT_HOURS);
|
||||
registerTimeUnit(translator, minute, TranslatableTimeUnit.UNIT_MINUTE, TranslatableTimeUnit.UNIT_MINUTES);
|
||||
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());
|
||||
Collections.sort(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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user