fix: prevent NPE from invalid state of favorites field

This commit is contained in:
ineanto 2025-06-07 12:46:01 +02:00
parent 0f4b239148
commit 8c04009c66
Signed by: ineanto
GPG key ID: E511F9CAA2F9CE84
3 changed files with 15 additions and 11 deletions

View file

@ -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<Appearance> favorites = profile.getFavorites();
final List<Item> 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<Appearance> favorites = profile.getFavorites();
List<Item> 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);

View file

@ -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;

View file

@ -25,7 +25,7 @@ public class NickoProfile implements Cloneable {
private Appearance appearance;
private Language language;
private boolean randomSkin;
private transient List<Appearance> favorites;
private List<Appearance> favorites;
public NickoProfile(Appearance appearance, Language language, boolean randomSkin, List<Appearance> favorites) {
this.appearance = appearance;