Skip to content

Commit

Permalink
Fixed NPE for uncached players
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
strumswell committed Oct 20, 2018
1 parent cfe2e34 commit beed50f
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 12 deletions.
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: ServerlistMOTD
author: Strumswell
version: X-FIRESTORM
version: X-2018-10-20
description: Change your Serverlist Motd!
load: POSTWORLD
depend: [ProtocolLib]
Expand Down
4 changes: 1 addition & 3 deletions src/cloud/bolte/serverlistmotd/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.UUID;

import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler;
Expand All @@ -17,7 +16,6 @@
import cloud.bolte.serverlistmotd.events.Ping;
import cloud.bolte.serverlistmotd.events.ProtocolLibImplementation;
import cloud.bolte.serverlistmotd.events.RestrictedModeJoin;
import cloud.bolte.serverlistmotd.motd.MotdState;
import cloud.bolte.serverlistmotd.util.IO;
import cloud.bolte.serverlistmotd.util.VaultIntegration;

Expand All @@ -34,7 +32,7 @@

public class Main extends JavaPlugin implements Listener {
public static Map<InetAddress, UUID> IP_UUID = new HashMap<InetAddress, UUID>();

@Override
public void onDisable() {
//Prepare HashMap and save it to disk
Expand Down
4 changes: 4 additions & 0 deletions src/cloud/bolte/serverlistmotd/motd/BanManagerMotd.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public String formatMotd(String motd, InetAddress ip) {
.replace("%time%", TimeVariable.getTime());

String playerName = Bukkit.getOfflinePlayer(Main.IP_UUID.get(ip)).getName();

if(playerName.equals(null)) {
playerName = "Strumswell";
}

BanInterface ban = new BanManager();
Date timestampconv = new Date((long) ban.expires(playerName) * 1000);
Expand Down
6 changes: 4 additions & 2 deletions src/cloud/bolte/serverlistmotd/motd/BanMotd.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ public String formatMotd(String motd, InetAddress ip) {
public void setBanMotd(ServerListPingEvent e, InetAddress ip) {
if (Main.IP_UUID.containsKey(ip)) {
OfflinePlayer p = Bukkit.getOfflinePlayer(Main.IP_UUID.get(ip));
if (p.isBanned()) {
e.setMotd(formatMotd(getMOTD(ip), ip));
if (p.hasPlayedBefore()) {
if (p.isBanned()) {
e.setMotd(formatMotd(getMOTD(ip), ip));
}
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/cloud/bolte/serverlistmotd/motd/MaxBansMotd.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.event.server.ServerListPingEvent;
import org.maxgamer.maxbans.MaxBans;
import org.maxgamer.maxbans.banmanager.TempBan;
Expand Down Expand Up @@ -78,9 +79,11 @@ public String formatMotd(String motd, InetAddress ip) {
*/
public void setBanMotd(ServerListPingEvent e, InetAddress ip) {
//Check if player is known and set motd
if (Main.IP_UUID.containsKey(ip) && MaxBans.instance.getBanManager()
.getBan(Bukkit.getOfflinePlayer(Main.IP_UUID.get(ip)).getName()) != null) {
OfflinePlayer p = Bukkit.getOfflinePlayer(Main.IP_UUID.get(ip));
if (p.hasPlayedBefore()) {
if (Main.IP_UUID.containsKey(ip) && MaxBans.instance.getBanManager().getBan(p.getName()) != null) {
e.setMotd(formatMotd(getMOTD(ip), ip));
}
}
}
}
11 changes: 9 additions & 2 deletions src/cloud/bolte/serverlistmotd/variables/MoneyVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.net.InetAddress;

import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;

import cloud.bolte.serverlistmotd.Main;
import cloud.bolte.serverlistmotd.util.VaultIntegration;
Expand All @@ -25,8 +26,14 @@ public class MoneyVariable {
*/
public static Double getMoney(InetAddress ip) {
try {
return VaultIntegration.getEcononomy().getBalance
(Bukkit.getOfflinePlayer(Main.IP_UUID.get(ip)));
OfflinePlayer p = Bukkit.getOfflinePlayer(Main.IP_UUID.get(ip));
if (p.hasPlayedBefore()) {
return VaultIntegration.getEcononomy().getBalance(p);
}else {
//User is uncached by Spigot
//p is null
return 0d;
}
} catch (NullPointerException npe) {
return -1d;
} catch (NoClassDefFoundError nc) {
Expand Down
8 changes: 7 additions & 1 deletion src/cloud/bolte/serverlistmotd/variables/PlayerVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ public static boolean isKnownPlayer(InetAddress ip) {
*/
public static String getNameFromIP(InetAddress ip) {
OfflinePlayer p = Bukkit.getOfflinePlayer(Main.IP_UUID.get(ip));
return p.getName();
if (p.hasPlayedBefore()) {
return p.getName();
}else {
//User is uncached by Spigot
//p is null
return "<unknown>";
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.UUID;

import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

import cloud.bolte.serverlistmotd.Main;
Expand Down Expand Up @@ -47,9 +48,18 @@ private static String getRandomOfflinePlayer() {
if (SpigotConfig.randomPlayerVariableUseTextEnabled()) {
return SpigotConfig.getRandomPlayerVariableText();
} else {
return Bukkit.getOfflinePlayer(values.get(index)).getName();
OfflinePlayer p = Bukkit.getOfflinePlayer(values.get(index));
if (p.hasPlayedBefore()) {
return p.getName();
}else {
//User is uncached by Spigot
//p is null
return "<unknown>";
}
}
} else {
//No player joined before
//Just returning my name ;-)
return "Strumswell";
}
}
Expand Down

0 comments on commit beed50f

Please sign in to comment.