Fixed NPE when opening the Settings Panel with some flags being hidden

The NPE could only occur if a non-op player was trying to open the Settings Panel or change tab or change mode, leading to the call of a null PanelItem.

I added some nullability annotations to make it 100% clear that Flag#toPanelItem(...) returns null if the player is not op and the flag is invisible.
This commit is contained in:
Florian CUNY 2019-08-13 15:04:31 +02:00
parent 7b78267027
commit 267c9c73d8
4 changed files with 10 additions and 3 deletions

View File

@ -359,6 +359,7 @@ public class Flag implements Comparable<Flag> {
* @param invisible - true if this flag is not visible to players * @param invisible - true if this flag is not visible to players
* @return - PanelItem for this flag or null if item is invisible to user * @return - PanelItem for this flag or null if item is invisible to user
*/ */
@Nullable
public PanelItem toPanelItem(BentoBox plugin, User user, @Nullable Island island, boolean invisible) { public PanelItem toPanelItem(BentoBox plugin, User user, @Nullable Island island, boolean invisible) {
// Invisibility // Invisibility
if (!user.isOp() && invisible) { if (!user.isOp() && invisible) {

View File

@ -1,5 +1,7 @@
package world.bentobox.bentobox.api.panels; package world.bentobox.bentobox.api.panels;
import org.eclipse.jdt.annotation.Nullable;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -25,7 +27,7 @@ public interface Tab {
* Return the panel items for this tab * Return the panel items for this tab
* @return a list of items in slot order * @return a list of items in slot order
*/ */
List<PanelItem> getPanelItems(); List<@Nullable PanelItem> getPanelItems();
/** /**
* @return the permission required to view this tab or empty if no permission required * @return the permission required to view this tab or empty if no permission required

View File

@ -3,6 +3,7 @@ package world.bentobox.bentobox.api.panels;
import java.security.InvalidParameterException; import java.security.InvalidParameterException;
import java.util.List; import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Objects;
import java.util.TreeMap; import java.util.TreeMap;
import org.bukkit.Material; import org.bukkit.Material;
@ -88,7 +89,7 @@ public class TabbedPanel extends Panel implements PanelListener {
// Show the active tab // Show the active tab
if (tpb.getTabs().containsKey(activeTab)) { if (tpb.getTabs().containsKey(activeTab)) {
List<PanelItem> panelItems = tab.getPanelItems(); List<PanelItem> panelItems = tab.getPanelItems();
panelItems.stream().skip(page * 43L).limit(page * 43L + 43L).forEach(i -> items.put(items.lastKey() + 1, i)); panelItems.stream().filter(Objects::nonNull).skip(page * 43L).limit(page * 43L + 43L).forEach(i -> items.put(items.lastKey() + 1, i));
// Add forward and backward icons // Add forward and backward icons
if (page > 0) { if (page > 0) {
// Previous page icon // Previous page icon

View File

@ -11,6 +11,8 @@ import org.bukkit.Sound;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.ClickType;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.flags.Flag; import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.flags.Flag.Type; import world.bentobox.bentobox.api.flags.Flag.Type;
@ -111,7 +113,8 @@ public class SettingsTab implements Tab, ClickHandler {
* @return list of all the panel items for this flag type * @return list of all the panel items for this flag type
*/ */
@Override @Override
public List<PanelItem> getPanelItems() { @NonNull
public List<@Nullable PanelItem> getPanelItems() {
List<Flag> flags = getFlags(); List<Flag> flags = getFlags();
int i = 0; int i = 0;
// Jump past empty tabs // Jump past empty tabs