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.
This commit is contained in:
triagonal 2021-06-27 06:11:03 +10:00 committed by GitHub
parent 64eb39a417
commit 3f36a52685
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 9 deletions

View File

@ -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<String> powertools = user.getPowertool(itemStack);
final List<String> 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));
}