From a049a03faae482edbba31b88c379574ff623a84e Mon Sep 17 00:00:00 2001 From: md678685 Date: Mon, 21 Aug 2017 10:40:23 +0100 Subject: [PATCH] Catch NullPointerExceptions in SLPE (#1463) * Catch NullPointerExceptions in SLPE * Use the caught NPE's stacktrace Makes thrown exception actually useful --- .../essentials/EssentialsServerListener.java | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/EssentialsServerListener.java b/Essentials/src/com/earth2me/essentials/EssentialsServerListener.java index 31e76fe32..b82586629 100644 --- a/Essentials/src/com/earth2me/essentials/EssentialsServerListener.java +++ b/Essentials/src/com/earth2me/essentials/EssentialsServerListener.java @@ -17,7 +17,8 @@ import java.util.logging.Level; public class EssentialsServerListener implements Listener { private final transient IEssentials ess; - private boolean errorLogged = false; + private boolean unsupportedLogged = false; + private boolean npeWarned = false; private boolean isPaperSample; private Method setSampleText; private Method getSampleText; @@ -36,7 +37,7 @@ public class EssentialsServerListener implements Listener { } @EventHandler(priority = EventPriority.LOWEST) - public void onServerListPing(final ServerListPingEvent event) { + public void onServerListPing(final ServerListPingEvent event) throws Exception { if (isPaperSample) { try { List playerNames = (List) getSampleText.invoke(event, null); @@ -49,9 +50,18 @@ public class EssentialsServerListener implements Listener { } setSampleText.invoke(event, playerNames); } catch (IllegalAccessException | InvocationTargetException | ClassCastException e) { - if (!errorLogged) { - ess.getLogger().log(Level.WARNING, "Unable to hide players from server list ping using Paper 1.12+ method!", e); - errorLogged = true; + if (!unsupportedLogged) { + ess.getLogger().log(Level.WARNING, "Unable to hide players from server list ping " + + "using Paper 1.12+ method!", e); + unsupportedLogged = true; + } + } catch (NullPointerException e) { + if (!npeWarned) { + npeWarned = true; + Exception ex = new Exception("A plugin has fired a ServerListPingEvent " + + "without implementing Paper's methods. Point the author to https://git.io/v7Xzl."); + ex.setStackTrace(e.getStackTrace()); + throw ex; } } } else { @@ -64,10 +74,10 @@ public class EssentialsServerListener implements Listener { } } } catch (UnsupportedOperationException e) { - if (!errorLogged) { + if (!unsupportedLogged) { ess.getLogger().warning("Current server implementation does not support " + "hiding players from server list ping. Update or contact the maintainers."); - errorLogged = true; + unsupportedLogged = true; } } }