From 44154aeefd24f1d86f06251cda096bd1dca4448e Mon Sep 17 00:00:00 2001 From: Fulminazzo Date: Fri, 12 Apr 2024 00:06:13 +0200 Subject: [PATCH] Added openGUI function --- .../it/angrybear/yagl/utils/GUIUtils.java | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/gui/bukkit/src/main/java/it/angrybear/yagl/utils/GUIUtils.java b/gui/bukkit/src/main/java/it/angrybear/yagl/utils/GUIUtils.java index 7c8d3871..5a396123 100644 --- a/gui/bukkit/src/main/java/it/angrybear/yagl/utils/GUIUtils.java +++ b/gui/bukkit/src/main/java/it/angrybear/yagl/utils/GUIUtils.java @@ -1,18 +1,60 @@ package it.angrybear.yagl.utils; +import it.angrybear.yagl.GUIManager; +import it.angrybear.yagl.contents.GUIContent; import it.angrybear.yagl.guis.GUI; import it.angrybear.yagl.guis.GUIType; import it.angrybear.yagl.guis.TypeGUI; +import it.angrybear.yagl.items.BukkitItem; +import it.angrybear.yagl.viewers.Viewer; +import it.fulminazzo.fulmicollection.objects.Refl; import org.bukkit.Bukkit; +import org.bukkit.entity.Player; import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; +import java.util.UUID; + /** - * The type Gui utils. + * A collection of utilities for handling with {@link GUI}s and Bukkit. */ public class GUIUtils { + /** + * Opens the given {@link GUI} for the specified {@link Viewer}. + * + * @param gui the gui + * @param viewer the viewer + */ + public static void openGUI(final @NotNull GUI gui, @NotNull Viewer viewer) { + final UUID uuid = viewer.getUniqueId(); + final Player player = Bukkit.getPlayer(uuid); + if (player == null) throw new IllegalArgumentException(String.format("Cannot open GUI for offline player '%s'", viewer.getName())); + final Refl reflViewer = new Refl<>(viewer); + // Add to GUIManager if not present + viewer = GUIManager.getViewer(player); + // Save previous GUI, if present + GUIManager.getOpenGUIViewer(uuid).ifPresent((v, g) -> { + reflViewer.setFieldObject("previousGUI", g).setFieldObject("openGUI", null); + g.changeGUIAction().ifPresent(a -> a.execute(v, g, gui)); + player.closeInventory(); + }); + // Set new GUI + reflViewer.setFieldObject("openGUI", gui); + // Open inventory + Inventory inventory = guiToInventory(gui); + for (int i = 0; i < gui.size(); i++) { + GUIContent content = gui.getContent(viewer, i); + if (content != null) { + ItemStack o = content.render().copy(BukkitItem.class).create(); + inventory.setItem(i, o); + } + } + player.openInventory(inventory); + } + /** * Converts the given {@link GUI} to a {@link Inventory}. *