From 8c04009c66a92f92894a3c9c920af1378e72ad5f Mon Sep 17 00:00:00 2001 From: ineanto Date: Sat, 7 Jun 2025 12:46:01 +0200 Subject: [PATCH] fix: prevent NPE from invalid state of favorites field --- .../xyz/ineanto/nicko/gui/FavoritesGUI.java | 22 ++++++++++++------- .../FavoriteAppearanceEntryItem.java | 2 -- .../ineanto/nicko/profile/NickoProfile.java | 2 +- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/main/java/xyz/ineanto/nicko/gui/FavoritesGUI.java b/src/main/java/xyz/ineanto/nicko/gui/FavoritesGUI.java index 368ffaf..3856de4 100644 --- a/src/main/java/xyz/ineanto/nicko/gui/FavoritesGUI.java +++ b/src/main/java/xyz/ineanto/nicko/gui/FavoritesGUI.java @@ -16,6 +16,7 @@ import xyz.xenondevs.invui.gui.structure.Markers; import xyz.xenondevs.invui.item.Item; import xyz.xenondevs.invui.window.Window; +import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @@ -28,18 +29,23 @@ public class FavoritesGUI { final PlayerLanguage playerLanguage = new PlayerLanguage(player); this.title = playerLanguage.translate(LanguageKey.GUI.Titles.FAVORITES, false); - final NickoProfile profile = Nicko.getInstance().getDataStore().getData(player.getUniqueId()).orElse(NickoProfile.EMPTY_PROFILE); - final List favorites = profile.getFavorites(); - - final List items = favorites.stream() - .map((appearance) -> new FavoriteAppearanceEntryItem(playerLanguage, appearance)) - .collect(Collectors.toList()); - final HomeGUI parent = new HomeGUI(player); final GoBackItem backItem = new GoBackItem(player); final ScrollUpItem scrollUpItem = new ScrollUpItem(playerLanguage); final ScrollDownItem scrollDownItem = new ScrollDownItem(playerLanguage); + final NickoProfile profile = Nicko.getInstance().getDataStore().getData(player.getUniqueId()).orElse(NickoProfile.EMPTY_PROFILE); + final List favorites = profile.getFavorites(); + List items; + + if (favorites == null || favorites.isEmpty()) { + items = Collections.emptyList(); + } else { + items = favorites.stream() + .map((appearance) -> new FavoriteAppearanceEntryItem(playerLanguage, appearance)) + .collect(Collectors.toList()); + } + gui = ScrollGui.items(guiItemBuilder -> { guiItemBuilder.setStructure( "x x x x x x x x U", @@ -47,7 +53,7 @@ public class FavoritesGUI { "x x x x x x x x #", "x x x x x x x x #", "x x x x x x x x D", - "B % % % % % % % %"); + "% % % A B R % % %"); guiItemBuilder.addIngredient('x', Markers.CONTENT_LIST_SLOT_HORIZONTAL); guiItemBuilder.addIngredient('U', scrollUpItem); guiItemBuilder.addIngredient('D', scrollDownItem); diff --git a/src/main/java/xyz/ineanto/nicko/gui/items/favorites/FavoriteAppearanceEntryItem.java b/src/main/java/xyz/ineanto/nicko/gui/items/favorites/FavoriteAppearanceEntryItem.java index e96e6e5..e92abcc 100644 --- a/src/main/java/xyz/ineanto/nicko/gui/items/favorites/FavoriteAppearanceEntryItem.java +++ b/src/main/java/xyz/ineanto/nicko/gui/items/favorites/FavoriteAppearanceEntryItem.java @@ -14,7 +14,6 @@ import xyz.ineanto.nicko.gui.items.ItemDefaults; import xyz.ineanto.nicko.gui.items.common.choice.ChoiceCallback; import xyz.ineanto.nicko.language.LanguageKey; import xyz.ineanto.nicko.language.PlayerLanguage; -import xyz.ineanto.nicko.mojang.MojangAPI; import xyz.xenondevs.invui.item.builder.ItemBuilder; import xyz.xenondevs.invui.item.builder.SkullBuilder; import xyz.xenondevs.invui.item.impl.AsyncItem; @@ -24,7 +23,6 @@ import xyz.xenondevs.invui.util.MojangApiUtils; import java.io.IOException; public class FavoriteAppearanceEntryItem extends AsyncItem { - private final MojangAPI mojangAPI = Nicko.getInstance().getMojangAPI(); private final PlayerLanguage playerLanguage; private final Appearance appearance; diff --git a/src/main/java/xyz/ineanto/nicko/profile/NickoProfile.java b/src/main/java/xyz/ineanto/nicko/profile/NickoProfile.java index 139cff9..78945cf 100644 --- a/src/main/java/xyz/ineanto/nicko/profile/NickoProfile.java +++ b/src/main/java/xyz/ineanto/nicko/profile/NickoProfile.java @@ -25,7 +25,7 @@ public class NickoProfile implements Cloneable { private Appearance appearance; private Language language; private boolean randomSkin; - private transient List favorites; + private List favorites; public NickoProfile(Appearance appearance, Language language, boolean randomSkin, List favorites) { this.appearance = appearance;