feat(wip): load favorites

This commit is contained in:
ineanto 2025-06-06 20:48:34 +02:00
parent 814d7adea7
commit 66c9313d0e
Signed by: ineanto
GPG key ID: E511F9CAA2F9CE84
3 changed files with 107 additions and 10 deletions

View file

@ -1,12 +1,24 @@
package xyz.ineanto.nicko.gui;
import org.bukkit.entity.Player;
import xyz.ineanto.nicko.Nicko;
import xyz.ineanto.nicko.appearance.Appearance;
import xyz.ineanto.nicko.gui.items.common.GoBackItem;
import xyz.ineanto.nicko.gui.items.common.ScrollDownItem;
import xyz.ineanto.nicko.gui.items.common.ScrollUpItem;
import xyz.ineanto.nicko.gui.items.favorites.FavoriteAppearanceEntryItem;
import xyz.ineanto.nicko.language.LanguageKey;
import xyz.ineanto.nicko.language.PlayerLanguage;
import xyz.ineanto.nicko.profile.NickoProfile;
import xyz.xenondevs.invui.gui.Gui;
import xyz.xenondevs.invui.gui.ScrollGui;
import xyz.xenondevs.invui.gui.structure.Markers;
import xyz.xenondevs.invui.item.Item;
import xyz.xenondevs.invui.window.Window;
import java.util.List;
import java.util.stream.Collectors;
public class FavoritesGUI {
private final Player player;
private final Gui gui;
@ -16,17 +28,32 @@ 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);
this.gui = Gui.normal()
.setStructure(
"# # # # # # # # #",
"# # # S C E # # #",
"B # # # # # # # #"
)
.addIngredient('B', backItem.get(parent.getGUI(), parent.getTitle()))
.build();
gui = ScrollGui.items(guiItemBuilder -> {
guiItemBuilder.setStructure(
"x x x x x x x x U",
"x x x x x x x x #",
"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 % % % % % % % %");
guiItemBuilder.addIngredient('x', Markers.CONTENT_LIST_SLOT_HORIZONTAL);
guiItemBuilder.addIngredient('U', scrollUpItem);
guiItemBuilder.addIngredient('D', scrollDownItem);
guiItemBuilder.addIngredient('B', backItem.get(parent.getGUI(), parent.getTitle()));
guiItemBuilder.setContent(items);
});
this.player = player;
}

View file

@ -27,12 +27,13 @@ public class CacheEntryItem extends AsyncItem {
private final String name;
private final String uuid;
private final MojangAPI mojangAPI = Nicko.getInstance().getMojangAPI();
private final PlayerLanguage playerLanguage;
public CacheEntryItem(PlayerLanguage playerLanguage, String uuid) {
super(new SuppliedItem(() -> {
final ItemBuilder builder = new ItemBuilder(Material.PAINTING);
return playerLanguage.translateItem(builder, LanguageKey.GUI.LOADING);
}, (click -> true)).getItemProvider(),
}, (_ -> true)).getItemProvider(),
() -> {
final String dashedUuid = uuid.replaceAll("(.{8})(.{4})(.{4})(.{4})(.+)", "$1-$2-$3-$4-$5");
final UUID uuidObject = UUID.fromString(dashedUuid);
@ -44,6 +45,7 @@ public class CacheEntryItem extends AsyncItem {
return ItemDefaults.getErrorSkullItem(playerLanguage, LanguageKey.GUI.Admin.Cache.ENTRY, Nicko.getInstance().getMojangAPI().getUUIDName(uuid));
}
});
this.playerLanguage = playerLanguage;
this.uuid = uuid;
this.name = mojangAPI.getUUIDName(uuid);
}
@ -55,7 +57,6 @@ public class CacheEntryItem extends AsyncItem {
new ChoiceGUI(player, new ChoiceCallback() {
@Override
public void onConfirm() {
final PlayerLanguage playerLanguage = new PlayerLanguage(player);
player.sendMessage(playerLanguage.translate(LanguageKey.Event.Admin.Cache.INVALIDATE_ENTRY, true, name));
player.playSound(player.getLocation(), Sound.BLOCK_WOODEN_BUTTON_CLICK_ON, 1, 1f);
mojangAPI.eraseFromCache(uuid);

View file

@ -0,0 +1,69 @@
package xyz.ineanto.nicko.gui.items.favorites;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.jetbrains.annotations.NotNull;
import xyz.ineanto.nicko.Nicko;
import xyz.ineanto.nicko.appearance.Appearance;
import xyz.ineanto.nicko.gui.ChoiceGUI;
import xyz.ineanto.nicko.gui.InvalidateSkinGUI;
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;
import xyz.xenondevs.invui.item.impl.SuppliedItem;
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;
public FavoriteAppearanceEntryItem(PlayerLanguage playerLanguage, Appearance appearance) {
super(new SuppliedItem(() -> {
final ItemBuilder builder = new ItemBuilder(Material.PAINTING);
return playerLanguage.translateItem(builder, LanguageKey.GUI.LOADING);
}, (_ -> true)).getItemProvider(),
() -> {
try {
final String name = (appearance.name() == null ? appearance.skin() : appearance.name());
final String skin = (appearance.skin() == null ? appearance.name() : appearance.skin());
final SkullBuilder skull = new SkullBuilder(skin);
return playerLanguage.translateItem(skull, LanguageKey.GUI.Admin.Cache.ENTRY, name);
} catch (MojangApiUtils.MojangApiException | IOException e) {
Nicko.getInstance().getLogger().warning("Unable to get Head texture for specified UUID (" + appearance.skin() + ")! (GUI/Favorites/Entry)");
return ItemDefaults.getErrorSkullItem(playerLanguage, LanguageKey.GUI.Admin.Cache.ENTRY, Nicko.getInstance().getMojangAPI().getUUIDName("Notch"));
}
});
this.playerLanguage = playerLanguage;
this.appearance = appearance;
}
@Override
public void handleClick(@NotNull ClickType click, @NotNull Player player, @NotNull InventoryClickEvent event) {
if (click.isLeftClick() || click.isRightClick()) {
event.getView().close();
new ChoiceGUI(player, new ChoiceCallback() {
@Override
public void onConfirm() {
player.sendMessage(playerLanguage.translate(LanguageKey.Event.Admin.Cache.INVALIDATE_ENTRY, true, appearance.name()));
player.playSound(player.getLocation(), Sound.BLOCK_WOODEN_BUTTON_CLICK_ON, 1, 1f);
}
@Override
public void onCancel() {
new InvalidateSkinGUI(player).open();
}
}).open();
}
}
}