Skip to content

Commit

Permalink
Add time-format-display-seconds option, which allows you to enable/di…
Browse files Browse the repository at this point in the history
…sable showing the seconds in the time format
  • Loading branch information
ajgeiss0702 committed May 12, 2024
1 parent a856f13 commit 2c4de51
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
9 changes: 6 additions & 3 deletions src/main/java/us/ajg0702/leaderboards/TimeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ public static void setStrings(String w, String d, String h, String m, String s)
public static final long WEEK = DAY * 7L;

public static String formatTimeSeconds(long timeSeconds) {
return formatTimeMs(timeSeconds*1000);
return formatTimeMs(timeSeconds*1000, true);
}
public static String formatTimeMs(long timeMs) {
public static String formatTimeSeconds(long timeSeconds, boolean withSeconds) {
return formatTimeMs(timeSeconds*1000, withSeconds);
}
public static String formatTimeMs(long timeMs, boolean withSeconds) {
int weeks = (int) (timeMs / WEEK);
int days = (int) ((timeMs % WEEK) / DAY);
int hours = (int) ((timeMs % DAY) / HOUR);
Expand All @@ -43,7 +46,7 @@ public static String formatTimeMs(long timeMs) {
String dayss = days != 0 ? days+d : "";
String hourss = hours != 0 ? hours+h : "";
String minutess = minutes != 0 ? minutes+m : "";
String secondss = seconds+s;
String secondss = withSeconds ? seconds+s : "";

return weekss+dayss+hourss+minutess+secondss;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@

public class PlaceholderFormatter {
private final Format defaultFormat = new Default();
private final List<Format> formats = Arrays.asList(
new Time(),
new ColonTime(),

defaultFormat
);

private final LeaderboardPlugin plugin;
private final List<Format> formats;

public PlaceholderFormatter(LeaderboardPlugin plugin) {
this.plugin = plugin;
formats = Arrays.asList(
new Time(plugin),
new ColonTime(),

defaultFormat
);
}

Map<String, Format> formatCache = new ConcurrentHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package us.ajg0702.leaderboards.formatting.formats;

import org.jetbrains.annotations.Nullable;
import us.ajg0702.leaderboards.Debug;
import us.ajg0702.leaderboards.LeaderboardPlugin;
import us.ajg0702.leaderboards.TimeUtils;
import us.ajg0702.leaderboards.formatting.Format;

Expand Down Expand Up @@ -56,6 +58,12 @@ private boolean isKnownTimePlaceholder(String placeholder) {
return is;
}

public final LeaderboardPlugin plugin;

public Time(@Nullable LeaderboardPlugin plugin) {
this.plugin = plugin;
}

@Override
public boolean matches(String output, String placeholder) {
if(isKnownTimePlaceholder(placeholder.toLowerCase(Locale.ROOT))) {
Expand Down Expand Up @@ -104,11 +112,16 @@ private static int getSeconds(String output, int multiplier, int seconds, Patter

@Override
public String toFormat(double input) {
return TimeUtils.formatTimeSeconds(Math.round(input));
return TimeUtils.formatTimeSeconds(Math.round(input), withSeconds());
}

@Override
public String getName() {
return "Time";
}

private boolean withSeconds() {
if(plugin == null) return true;
return plugin.getAConfig().getBoolean("time-format-display-seconds");
}
}
5 changes: 5 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ dont-add-zero: []
# Default: true
fetch-prefix-suffix-from-vault: true

# Should we include seconds in the time format?
# This only effects things that display the value directly from ajLeaderboards
# Default: true
time-format-display-seconds: true

# Allows setting custom bytebin location and webviewer link
# You should only need to change these if you don't want /ajlb viewer to send the data to my servers
# (which is only kept for 90 days or less, and you can contact me to delete sooner if you'd like)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public void testMatches() throws Exception {
"9h 21m 44s",
"2s",
"1d 14h 40m",
"0d 14h 40m"
"0d 14h 40m",
"9h 21m 44s",
"2h 8m 37s"
);
List<String> shouldNotMatch = Arrays.asList(
"hello there",
Expand All @@ -28,7 +30,7 @@ public void testMatches() throws Exception {
""
);

Time timeFormat = new Time();
Time timeFormat = new Time(null);

for (String match : shouldMatch) {
boolean res = timeFormat.matches(match, "");
Expand Down

0 comments on commit 2c4de51

Please sign in to comment.