Skip to content

Commit 6dc2404

Browse files
authored
Add player utils; minor fixups (#15)
* Add player utils; minor fixups * Make the requested changes
1 parent 3237aa0 commit 6dc2404

File tree

6 files changed

+112
-8
lines changed

6 files changed

+112
-8
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66
group = 'com.dumbdogdiner'
77

8-
version = '1.4.6'
8+
version = '1.5.0'
99

1010

1111
configurations {

src/main/java/com/dumbdogdiner/stickyapi/bukkit/command/AsyncCommand.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.bukkit.command.TabCompleter;
3333

3434
import com.dumbdogdiner.stickyapi.StickyAPI;
35+
import org.jetbrains.annotations.NotNull;
3536

3637
import javax.annotation.Nullable;
3738

@@ -187,9 +188,9 @@ public void setTabCompleter(TabCompleter completer) {
187188
* @throws IllegalArgumentException if sender, alias, or args is null
188189
*/
189190
@Override
190-
public java.util.List<String> tabComplete(CommandSender sender, String alias, String[] args)
191+
public java.util.@NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, String[] args)
191192
throws CommandException, IllegalArgumentException {
192-
if (sender == null || alias == null || args == null)
193+
if (args == null)
193194
throw new NullPointerException("arguments to tabComplete cannot be null");
194195

195196
List<String> completions = null;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
}

src/main/java/com/dumbdogdiner/stickyapi/bukkit/util/StartupUtil.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ public static boolean setupConfig(JavaPlugin plugin) {
3838
try {
3939
if (!plugin.getDataFolder().exists()) {
4040
plugin.getLogger().info("Error: No folder was found! Creating...");
41-
plugin.getDataFolder().mkdirs();
41+
if(!plugin.getDataFolder().mkdirs()){
42+
plugin.getLogger().info("Error: Couldn't create the folder somehow??");
43+
return false;
44+
}
4245
plugin.saveDefaultConfig();
4346
plugin.saveConfig();
4447
plugin.getLogger().info("The folder was created successfully!");
@@ -60,7 +63,7 @@ public static boolean setupConfig(JavaPlugin plugin) {
6063
public static LocaleProvider setupLocale(JavaPlugin plugin, LocaleProvider localeProvider) {
6164
localeProvider = new LocaleProvider(new File(plugin.getDataFolder(), "locale"));
6265
int loadedLocales = localeProvider.loadAllLocales();
63-
Boolean localeEnabled = localeProvider.setDefaultLocale("messages.en_us");
66+
boolean localeEnabled = localeProvider.setDefaultLocale("messages.en_us");
6467

6568
if (!localeEnabled) {
6669
plugin.getLogger()

src/main/java/com/dumbdogdiner/stickyapi/common/arguments/Arguments.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public Arguments optionalFlag(String name, String flag) {
8686
return this;
8787
}
8888

89-
debug.print("Found flag at position " + String.valueOf(index) + " - new args size = " + unparsedArgs.size());
89+
debug.print("Found flag at position " + index + " - new args size = " + unparsedArgs.size());
9090

9191
parsedArgs.put(name, unparsedArgs.get(index));
9292
unparsedArgs.remove(index);
@@ -215,8 +215,9 @@ public Arguments optionalSentence(String name, int length) {
215215
String concatenated = String.join(" ", Arrays.copyOfRange(unparsedArgs.toArray(new String[unparsedArgs.size()]), position, end));
216216
parsedArgs.put(name, concatenated);
217217

218-
for (int s = position; s != end; s++)
219-
unparsedArgs.remove(position);
218+
if (end > position) {
219+
unparsedArgs.subList(position, end).clear();
220+
}
220221

221222
debug.print("Found sentence of length " + String.valueOf(length) + " - new args size = " + String.valueOf(unparsedArgs.size()));
222223

src/main/java/com/dumbdogdiner/stickyapi/common/util/NumberUtil.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,26 @@ public static int getRandomNumber(int min, int max) {
8383
throw new IllegalArgumentException("Min may not be greater than max!");
8484
return (int) ((Math.random() * (max - min)) + min);
8585
}
86+
87+
/**
88+
* Try to return long as an int, capped at int max and int min
89+
* @param l The long to convert
90+
* @return Returns the long as a capped int
91+
*/
92+
public static int intHelper(long l){
93+
try {
94+
return Math.toIntExact(l);
95+
} catch (ArithmeticException ae){
96+
switch(Long.compare(l, 0)){
97+
case 1:
98+
return Integer.MAX_VALUE;
99+
case 0:
100+
return 0;
101+
case -1:
102+
return Integer.MIN_VALUE;
103+
default:
104+
throw new ArithmeticException(); // Somehow Long.compare is broken?? This should be impossible
105+
}
106+
}
107+
}
86108
}

0 commit comments

Comments
 (0)