From 3f36a52685ec3456a31214cc759ae48c4d35f9fe Mon Sep 17 00:00:00 2001 From: triagonal <10545540+triagonal@users.noreply.github.com> Date: Sun, 27 Jun 2021 06:11:03 +1000 Subject: [PATCH] Prevent NPEs in /powertool (#4276) This PR fixes a long-standing bug where using the `a:` or `r:` modes in `/powertool` with no commands already set on the item would result in an NPE. To prevent this, the powertool command list is now immediately initialized if it doesn't exist upon retrieval. --- .../essentials/commands/Commandpowertool.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandpowertool.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandpowertool.java index 88b7a0c4f..d6ba3fa0e 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandpowertool.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandpowertool.java @@ -8,7 +8,6 @@ import org.bukkit.Material; import org.bukkit.Server; import org.bukkit.inventory.ItemStack; -import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Locale; @@ -52,10 +51,11 @@ public class Commandpowertool extends EssentialsCommand { } final String itemName = itemStack.getType().toString().toLowerCase(Locale.ENGLISH).replaceAll("_", " "); - List powertools = user.getPowertool(itemStack); + final List powertools = user.getPowertool(itemStack) != null ? user.getPowertool(itemStack) : Lists.newArrayList(); + if (command != null && !command.isEmpty()) { if (command.equalsIgnoreCase("l:")) { - if (powertools == null || powertools.isEmpty()) { + if (powertools.isEmpty()) { throw new Exception(tl("powerToolListEmpty", itemName)); } else { sender.sendMessage(tl("powerToolList", StringUtil.joinList(powertools), itemName)); @@ -79,20 +79,16 @@ public class Commandpowertool extends EssentialsCommand { if (powertools.contains(command)) { throw new Exception(tl("powerToolAlreadySet", command, itemName)); } - } else if (powertools != null && !powertools.isEmpty()) { + } else if (!powertools.isEmpty()) { // Replace all commands with this one powertools.clear(); - } else { - powertools = new ArrayList<>(); } powertools.add(command); sender.sendMessage(tl("powerToolAttach", StringUtil.joinList(powertools), itemName)); } } else { - if (powertools != null) { - powertools.clear(); - } + powertools.clear(); sender.sendMessage(tl("powerToolRemoveAll", itemName)); }