|
| 1 | +package com.dumbdogdiner.stickyapi.bukkit.player; |
| 2 | + |
| 3 | +import org.bukkit.Bukkit; |
| 4 | +import org.bukkit.OfflinePlayer; |
| 5 | + |
| 6 | +import java.util.*; |
| 7 | + |
| 8 | +import static com.dumbdogdiner.stickyapi.common.util.NumberUtil.intHelper; |
| 9 | + |
| 10 | +@SuppressWarnings("unused") |
| 11 | +public class PlayerUtils { |
| 12 | + public static class Names { |
| 13 | + public static List<String> getOnlinePlayers() { |
| 14 | + ArrayList<String> playerNames = new ArrayList<>(); |
| 15 | + |
| 16 | + Bukkit.getOnlinePlayers().forEach(player -> { |
| 17 | + playerNames.remove(player.getName()); |
| 18 | + }); |
| 19 | + |
| 20 | + return playerNames; |
| 21 | + } |
| 22 | + |
| 23 | + public static List<String> getOfflinePlayers() { |
| 24 | + List<String> playerNames = getAllPlayers(); |
| 25 | + |
| 26 | + Bukkit.getOnlinePlayers().forEach(player -> { |
| 27 | + playerNames.remove(player.getName()); |
| 28 | + }); |
| 29 | + return playerNames; |
| 30 | + } |
| 31 | + |
| 32 | + public static List<String> getAllPlayers() { |
| 33 | + ArrayList<String> allPlayerNames = new ArrayList<>(); |
| 34 | + OfflinePlayer [] offlinePlayers = Bukkit.getServer().getOfflinePlayers(); |
| 35 | + Arrays.sort(offlinePlayers, new OfflinePlayerSeenComparator()); |
| 36 | + |
| 37 | + for (OfflinePlayer player : offlinePlayers) { |
| 38 | + allPlayerNames.add(player.getName()); |
| 39 | + } |
| 40 | + return allPlayerNames; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public static class OfflinePlayerSeenComparator implements Comparator<OfflinePlayer> { |
| 45 | + |
| 46 | + /** |
| 47 | + * Bigger number means seen more recently, which means we want it earlier in the list, so return -1 |
| 48 | + * if o1 has a bigger number, we should return a negative number |
| 49 | + * if o2 has a bigger number, we should return a positive number |
| 50 | + * Check the following, in order, break on non-tie: |
| 51 | + * lastSeen, lastLogin, firstLogin, compare names as strings |
| 52 | + * if still a tie just return 0 |
| 53 | + * @param o1 The first {@link org.bukkit.OfflinePlayer} |
| 54 | + * @param o2 The second {@link org.bukkit.OfflinePlayer} |
| 55 | + * @return An integer representing if o1 is less than, equal to, or greater than o2 |
| 56 | + */ |
| 57 | + @Override |
| 58 | + public int compare(OfflinePlayer o1, OfflinePlayer o2) { |
| 59 | + |
| 60 | + |
| 61 | + long [] diffs = new long[] { |
| 62 | + o2.getLastSeen() - o1.getLastSeen(), |
| 63 | + o2.getLastLogin() - o1.getLastLogin(), |
| 64 | + o2.getFirstPlayed() - o1.getFirstPlayed() |
| 65 | + }; |
| 66 | + |
| 67 | + for(long diff : diffs) { |
| 68 | + if (diff != 0) { |
| 69 | + return intHelper(diff); |
| 70 | + } |
| 71 | + } |
| 72 | + return Objects.requireNonNull(o1.getName()).compareTo(Objects.requireNonNull(o2.getName())); |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + } |
| 77 | +} |
0 commit comments