Skip to content

Commit beed50f

Browse files
committed
Fixed NPE for uncached players
Fixed a bug where a recorded (by SMOTD) UUID isn't cached in usercache.json or the corresponding player hasn't joined before resulted in a NullPointerException.
1 parent cfe2e34 commit beed50f

8 files changed

Lines changed: 42 additions & 12 deletions

File tree

plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: ServerlistMOTD
22
author: Strumswell
3-
version: X-FIRESTORM
3+
version: X-2018-10-20
44
description: Change your Serverlist Motd!
55
load: POSTWORLD
66
depend: [ProtocolLib]

src/cloud/bolte/serverlistmotd/Main.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.util.UUID;
88

99
import org.bukkit.Bukkit;
10-
import org.bukkit.command.CommandSender;
1110
import org.bukkit.event.Listener;
1211
import org.bukkit.plugin.java.JavaPlugin;
1312
import org.bukkit.scheduler.BukkitScheduler;
@@ -17,7 +16,6 @@
1716
import cloud.bolte.serverlistmotd.events.Ping;
1817
import cloud.bolte.serverlistmotd.events.ProtocolLibImplementation;
1918
import cloud.bolte.serverlistmotd.events.RestrictedModeJoin;
20-
import cloud.bolte.serverlistmotd.motd.MotdState;
2119
import cloud.bolte.serverlistmotd.util.IO;
2220
import cloud.bolte.serverlistmotd.util.VaultIntegration;
2321

@@ -34,7 +32,7 @@
3432

3533
public class Main extends JavaPlugin implements Listener {
3634
public static Map<InetAddress, UUID> IP_UUID = new HashMap<InetAddress, UUID>();
37-
35+
3836
@Override
3937
public void onDisable() {
4038
//Prepare HashMap and save it to disk

src/cloud/bolte/serverlistmotd/motd/BanManagerMotd.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ public String formatMotd(String motd, InetAddress ip) {
4545
.replace("%time%", TimeVariable.getTime());
4646

4747
String playerName = Bukkit.getOfflinePlayer(Main.IP_UUID.get(ip)).getName();
48+
49+
if(playerName.equals(null)) {
50+
playerName = "Strumswell";
51+
}
4852

4953
BanInterface ban = new BanManager();
5054
Date timestampconv = new Date((long) ban.expires(playerName) * 1000);

src/cloud/bolte/serverlistmotd/motd/BanMotd.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ public String formatMotd(String motd, InetAddress ip) {
7777
public void setBanMotd(ServerListPingEvent e, InetAddress ip) {
7878
if (Main.IP_UUID.containsKey(ip)) {
7979
OfflinePlayer p = Bukkit.getOfflinePlayer(Main.IP_UUID.get(ip));
80-
if (p.isBanned()) {
81-
e.setMotd(formatMotd(getMOTD(ip), ip));
80+
if (p.hasPlayedBefore()) {
81+
if (p.isBanned()) {
82+
e.setMotd(formatMotd(getMOTD(ip), ip));
83+
}
8284
}
8385
}
8486
}

src/cloud/bolte/serverlistmotd/motd/MaxBansMotd.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import org.bukkit.Bukkit;
66
import org.bukkit.ChatColor;
7+
import org.bukkit.OfflinePlayer;
78
import org.bukkit.event.server.ServerListPingEvent;
89
import org.maxgamer.maxbans.MaxBans;
910
import org.maxgamer.maxbans.banmanager.TempBan;
@@ -78,9 +79,11 @@ public String formatMotd(String motd, InetAddress ip) {
7879
*/
7980
public void setBanMotd(ServerListPingEvent e, InetAddress ip) {
8081
//Check if player is known and set motd
81-
if (Main.IP_UUID.containsKey(ip) && MaxBans.instance.getBanManager()
82-
.getBan(Bukkit.getOfflinePlayer(Main.IP_UUID.get(ip)).getName()) != null) {
82+
OfflinePlayer p = Bukkit.getOfflinePlayer(Main.IP_UUID.get(ip));
83+
if (p.hasPlayedBefore()) {
84+
if (Main.IP_UUID.containsKey(ip) && MaxBans.instance.getBanManager().getBan(p.getName()) != null) {
8385
e.setMotd(formatMotd(getMOTD(ip), ip));
86+
}
8487
}
8588
}
8689
}

src/cloud/bolte/serverlistmotd/variables/MoneyVariable.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.net.InetAddress;
44

55
import org.bukkit.Bukkit;
6+
import org.bukkit.OfflinePlayer;
67

78
import cloud.bolte.serverlistmotd.Main;
89
import cloud.bolte.serverlistmotd.util.VaultIntegration;
@@ -25,8 +26,14 @@ public class MoneyVariable {
2526
*/
2627
public static Double getMoney(InetAddress ip) {
2728
try {
28-
return VaultIntegration.getEcononomy().getBalance
29-
(Bukkit.getOfflinePlayer(Main.IP_UUID.get(ip)));
29+
OfflinePlayer p = Bukkit.getOfflinePlayer(Main.IP_UUID.get(ip));
30+
if (p.hasPlayedBefore()) {
31+
return VaultIntegration.getEcononomy().getBalance(p);
32+
}else {
33+
//User is uncached by Spigot
34+
//p is null
35+
return 0d;
36+
}
3037
} catch (NullPointerException npe) {
3138
return -1d;
3239
} catch (NoClassDefFoundError nc) {

src/cloud/bolte/serverlistmotd/variables/PlayerVariable.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ public static boolean isKnownPlayer(InetAddress ip) {
3636
*/
3737
public static String getNameFromIP(InetAddress ip) {
3838
OfflinePlayer p = Bukkit.getOfflinePlayer(Main.IP_UUID.get(ip));
39-
return p.getName();
39+
if (p.hasPlayedBefore()) {
40+
return p.getName();
41+
}else {
42+
//User is uncached by Spigot
43+
//p is null
44+
return "<unknown>";
45+
}
4046

4147
}
4248
}

src/cloud/bolte/serverlistmotd/variables/RandomPlayerVariable.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.UUID;
66

77
import org.bukkit.Bukkit;
8+
import org.bukkit.OfflinePlayer;
89
import org.bukkit.entity.Player;
910

1011
import cloud.bolte.serverlistmotd.Main;
@@ -47,9 +48,18 @@ private static String getRandomOfflinePlayer() {
4748
if (SpigotConfig.randomPlayerVariableUseTextEnabled()) {
4849
return SpigotConfig.getRandomPlayerVariableText();
4950
} else {
50-
return Bukkit.getOfflinePlayer(values.get(index)).getName();
51+
OfflinePlayer p = Bukkit.getOfflinePlayer(values.get(index));
52+
if (p.hasPlayedBefore()) {
53+
return p.getName();
54+
}else {
55+
//User is uncached by Spigot
56+
//p is null
57+
return "<unknown>";
58+
}
5159
}
5260
} else {
61+
//No player joined before
62+
//Just returning my name ;-)
5363
return "Strumswell";
5464
}
5565
}

0 commit comments

Comments
 (0)