Improves unit conversion by removing some redundancy and adding the seemingly missing minute unit
This commit is contained in:
parent
d1a7735b51
commit
b89b993220
@ -7,7 +7,10 @@ import org.bukkit.ChatColor;
|
|||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class represents a placed and active permission sign
|
* This class represents a placed and active permission sign
|
||||||
@ -20,6 +23,9 @@ public class PermissionSign {
|
|||||||
private final int duration;
|
private final int duration;
|
||||||
private final double cost;
|
private final double cost;
|
||||||
|
|
||||||
|
private static Map<Double, TranslatableMessage[]> timeUnits;
|
||||||
|
private static List<Double> sortedUnits;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiates a new permission sign
|
* Instantiates a new permission sign
|
||||||
*
|
*
|
||||||
@ -152,30 +158,17 @@ public class PermissionSign {
|
|||||||
if (duration == 0) {
|
if (duration == 0) {
|
||||||
return Translator.getTranslatedMessage(TranslatableMessage.SIGN_PERMANENT);
|
return Translator.getTranslatedMessage(TranslatableMessage.SIGN_PERMANENT);
|
||||||
} else {
|
} else {
|
||||||
double minute = 60;
|
if (sortedUnits == null) {
|
||||||
double hour = minute * 60;
|
initializeUnits();
|
||||||
double day = hour * 24;
|
}
|
||||||
if (duration / day >= 1) {
|
for (Double unit : sortedUnits) {
|
||||||
double days = round(duration / day);
|
if (duration / unit >= 1) {
|
||||||
if (days == 1) {
|
double units = round(duration / unit);
|
||||||
return (int) days + " " + Translator.getTranslatedMessage(TranslatableMessage.UNIT_DAY);
|
return formatDurationString(units, timeUnits.get(unit)[units == 1 ? 0 : 1],
|
||||||
} else {
|
(units * 10) % 10 == 0);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return formatDurationString(duration, TranslatableMessage.UNIT_SECONDS, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -203,4 +196,39 @@ public class PermissionSign {
|
|||||||
return Math.round(number * 100.0) / 100.0;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user