diff options
| author | Cow <cow@volloeko.de> | 2020-04-26 03:43:30 +0200 |
|---|---|---|
| committer | Cow <cow@volloeko.de> | 2020-04-26 03:43:30 +0200 |
| commit | 35553dceb9c683000095ce1a62b4e094234304bc (patch) | |
| tree | 8ed09cd69ba877be4defe9583007333c8164f8f0 /src/main/java/eu/olli/cowmoonication/util | |
| parent | f0686a8e45528e3dd682a6f7a69dd904730f6fb1 (diff) | |
| download | Cowlection-35553dceb9c683000095ce1a62b4e094234304bc.tar.gz Cowlection-35553dceb9c683000095ce1a62b4e094234304bc.tar.bz2 Cowlection-35553dceb9c683000095ce1a62b4e094234304bc.zip | |
Improved output of player stalking feature
- offline players now include 'offline for <duration>'
- better handling of special cases (e.g. nicked players)
- also: simplified API requests and config handling
Diffstat (limited to 'src/main/java/eu/olli/cowmoonication/util')
3 files changed, 56 insertions, 9 deletions
diff --git a/src/main/java/eu/olli/cowmoonication/util/ApiUtils.java b/src/main/java/eu/olli/cowmoonication/util/ApiUtils.java index 2a62153..650c56d 100644 --- a/src/main/java/eu/olli/cowmoonication/util/ApiUtils.java +++ b/src/main/java/eu/olli/cowmoonication/util/ApiUtils.java @@ -37,8 +37,7 @@ public class ApiUtils { } private static Friend getFriend(String name) { - try { - BufferedReader reader = makeApiCall(NAME_TO_UUID_URL + name); + try (BufferedReader reader = makeApiCall(NAME_TO_UUID_URL + name)) { if (reader == null) { return Friend.FRIEND_NOT_FOUND; } else { @@ -55,8 +54,7 @@ public class ApiUtils { } private static String getCurrentName(Friend friend) { - try { - BufferedReader reader = makeApiCall(String.format(UUID_TO_NAME_URL, UUIDTypeAdapter.fromUUID(friend.getUuid()))); + try (BufferedReader reader = makeApiCall(String.format(UUID_TO_NAME_URL, UUIDTypeAdapter.fromUUID(friend.getUuid())))) { if (reader == null) { return UUID_NOT_FOUND; } else { @@ -76,8 +74,7 @@ public class ApiUtils { } private static HyStalking stalkPlayer(Friend friend) { - try { - BufferedReader reader = makeApiCall(String.format(STALKING_URL_OFFICIAL, MooConfig.moo, UUIDTypeAdapter.fromUUID(friend.getUuid()))); + try (BufferedReader reader = makeApiCall(String.format(STALKING_URL_OFFICIAL, MooConfig.moo, UUIDTypeAdapter.fromUUID(friend.getUuid())))) { if (reader != null) { return gson.fromJson(reader, HyStalking.class); } @@ -92,8 +89,7 @@ public class ApiUtils { } private static SlothStalking stalkOfflinePlayer(Friend stalkedPlayer) { - try { - BufferedReader reader = makeApiCall(String.format(STALKING_URL_UNOFFICIAL, UUIDTypeAdapter.fromUUID(stalkedPlayer.getUuid()))); + try (BufferedReader reader = makeApiCall(String.format(STALKING_URL_UNOFFICIAL, UUIDTypeAdapter.fromUUID(stalkedPlayer.getUuid())))) { if (reader != null) { return gson.fromJson(reader, SlothStalking.class); } @@ -105,6 +101,7 @@ public class ApiUtils { private static BufferedReader makeApiCall(String url) throws IOException { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); + connection.setConnectTimeout(5000); connection.setReadTimeout(5000); connection.addRequestProperty("User-Agent", "Forge Mod " + Cowmoonication.MODNAME + "/" + Cowmoonication.VERSION + " (https://github.com/cow-mc/Cowmoonication/)"); diff --git a/src/main/java/eu/olli/cowmoonication/util/SlothStalking.java b/src/main/java/eu/olli/cowmoonication/util/SlothStalking.java index bcd0e01..1cc7c22 100644 --- a/src/main/java/eu/olli/cowmoonication/util/SlothStalking.java +++ b/src/main/java/eu/olli/cowmoonication/util/SlothStalking.java @@ -2,9 +2,10 @@ package eu.olli.cowmoonication.util; public class SlothStalking { private String username; + private String rank; private String rank_formatted; // private boolean online; - // private long last_login; + private long last_login; private long last_logout; private String last_game; @@ -15,6 +16,10 @@ public class SlothStalking { return rank_formatted.replace('&', 'ยง') + " " + username; } + public long getLastLogin() { + return last_login; + } + public long getLastLogout() { return last_logout; } @@ -22,4 +27,19 @@ public class SlothStalking { public String getLastGame() { return last_game; } + + public boolean hasNeverJoinedHypixel() { + // example player that has never joined Hypixel (as of April 2020): Joe + return rank == null && last_login == 0; + } + + public boolean hasNeverLoggedOut() { + // example player that has no logout value (as of April 2020): Pig (in general accounts that haven't logged in for a few years) + return last_login != 0 && last_logout == 0; + } + + public boolean isHidingOnlineStatus() { + // example players: any higher ranked player (mods, admins, ...) + return last_login == 0 && last_logout == 0; + } } diff --git a/src/main/java/eu/olli/cowmoonication/util/Utils.java b/src/main/java/eu/olli/cowmoonication/util/Utils.java index dd68cc3..0ff4010 100644 --- a/src/main/java/eu/olli/cowmoonication/util/Utils.java +++ b/src/main/java/eu/olli/cowmoonication/util/Utils.java @@ -1,7 +1,11 @@ package eu.olli.cowmoonication.util; +import com.mojang.realmsclient.util.Pair; import org.apache.commons.lang3.text.WordUtils; +import org.apache.commons.lang3.time.DateFormatUtils; +import org.apache.commons.lang3.time.DurationFormatUtils; +import java.util.concurrent.TimeUnit; import java.util.regex.Pattern; public final class Utils { @@ -22,4 +26,30 @@ public final class Utils { public static String fancyCase(String string) { return WordUtils.capitalizeFully(string.replace('_', ' ')); } + + /** + * Turn timestamp into pretty-formatted duration and date details. + * + * @param timestamp last login/logout + * @return 1st: duration between timestamp and now in words; 2nd: formatted date if time differences is >24h, otherwise null + */ + public static Pair<String, String> getLastOnlineWords(long timestamp) { + long duration = System.currentTimeMillis() - timestamp; + long daysPast = TimeUnit.MILLISECONDS.toDays(duration); + + String dateFormatted = null; + if (daysPast > 1) { + dateFormatted = DateFormatUtils.format(timestamp, "dd-MMM-yyyy"); + } + + if (daysPast > 31) { + return Pair.of( + DurationFormatUtils.formatPeriod(timestamp, System.currentTimeMillis(), (daysPast > 365 ? "y 'years' " : "") + "M 'months' d 'days'"), + dateFormatted); + } else { + return Pair.of( + DurationFormatUtils.formatDurationWords(duration, true, true), + dateFormatted); + } + } } |
