Skip to content

Commit

Permalink
Add config for 24h time
Browse files Browse the repository at this point in the history
Closes #2
  • Loading branch information
Nincodedo committed Jan 21, 2024
1 parent 3fd4495 commit d6b4094
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/main/java/dev/nincodedo/mcserverdescption/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ private static void writeDefault() {
configWriter.write("thundering=Thundering\n");
configWriter.write("raining=Raining\n");
configWriter.write("clear=Clear\n");
configWriter.write("# Set use24HourClock to false to use a 12 hour clock\n");
configWriter.write("use24HourClock=false\n");
} catch (IOException e) {
LOGGER.error("Failed to write default config file.", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public void onInitialize() {
properties = Config.getConfigProperties();
String descriptionTemplate = properties.getProperty("description");
String descriptionTemplateStart = descriptionTemplate.substring(0, descriptionTemplate.indexOf("{"));
boolean use24HourClock = Boolean.parseBoolean(properties.getProperty("use24HourClock", "false"));
ServerTickEvents.END_SERVER_TICK.register(server -> {
if (server.isRemote()) {
String motd = server.getServerMotd();
Expand All @@ -26,7 +27,7 @@ public void onInitialize() {
World overworld = server.getOverworld();
if (overworld != null) {
long dayCount = overworld.getTimeOfDay() / 24000;
String timeString = getTimeString(overworld.getTimeOfDay());
String timeString = getTimeString(overworld.getTimeOfDay(), use24HourClock);
String weatherStatus = getWeatherStatus(overworld.isRaining(), overworld.isThundering());

String stringBuilder = descriptionTemplate;
Expand All @@ -42,18 +43,25 @@ public void onInitialize() {
}

@NotNull
private String getTimeString(long serverTimeOfDay) {
private String getTimeString(long serverTimeOfDay, boolean use24HourClock) {
long currentTime = serverTimeOfDay % 24000;
int hour = (int) (currentTime / 1000 + 6);
int minute = (int) (currentTime % 1000 * 60 / 1000);
String ampm = hour > 12 && hour <= 23 ? "PM" : "AM";
if (hour == 0 || hour == 24) {
hour = 12;
}
if (hour != 12) {
hour = hour % 12;
if (use24HourClock) {
if (hour == 24) {
hour = 0;
}
return String.format("%02d", hour) + ":" + String.format("%02d", minute);
} else {
String ampm = hour > 12 && hour <= 23 ? "PM" : "AM";
if (hour == 0 || hour == 24) {
hour = 12;
}
if (hour != 12) {
hour = hour % 12;
}
return hour + ":" + String.format("%02d", minute) + " " + ampm;
}
return hour + ":" + String.format("%02d", minute) + " " + ampm;
}

private String getWeatherStatus(boolean isRaining, boolean isThundering) {
Expand Down

0 comments on commit d6b4094

Please sign in to comment.