From f31189d84aa1061281595dd25724a1c3715f9cd9 Mon Sep 17 00:00:00 2001 From: leegod Date: Mon, 25 Mar 2024 20:53:21 +0800 Subject: [PATCH 1/2] feat(gui): Added updateTitle() method chore: Bumped version to 0.7.1b2-SNAPSHOT --- gradle.properties | 2 +- .../java/io/fairyproject/bukkit/gui/Gui.java | 34 ++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/gradle.properties b/gradle.properties index d3eda8e7..1fe7390d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -23,7 +23,7 @@ # # Fairy -fairy.version = 0.7.1b1-SNAPSHOT +fairy.version = 0.7.1b2-SNAPSHOT gradle-plugin.version = 1.2.0b9 mongojack.version = 4.2.0 diff --git a/io.fairyproject.modules/platform.bukkit/bukkit-gui/src/main/java/io/fairyproject/bukkit/gui/Gui.java b/io.fairyproject.modules/platform.bukkit/bukkit-gui/src/main/java/io/fairyproject/bukkit/gui/Gui.java index 918957ee..cb9b6dc2 100644 --- a/io.fairyproject.modules/platform.bukkit/bukkit-gui/src/main/java/io/fairyproject/bukkit/gui/Gui.java +++ b/io.fairyproject.modules/platform.bukkit/bukkit-gui/src/main/java/io/fairyproject/bukkit/gui/Gui.java @@ -47,13 +47,14 @@ public class Gui { private final List> closeCallbacks; private final List panes; private final boolean[] usedSlots; - private final Component title; + private Component title; @Nullable private Inventory inventory; private GuiSlot[] guiSlots; @Getter private EventNode eventNode; + private boolean titleUpdating; private int maxSlots; public Gui(BukkitEventNode bukkitEventNode, Component title) { @@ -70,6 +71,33 @@ public Gui(BukkitEventNode bukkitEventNode, Component title) { this.maxSlots = -1; } + @SuppressWarnings("deprecation") + public void updateTitle(@NotNull Player player, @NotNull Component title) { + this.title = title; + + if (!this.isOpening() || this.titleUpdating) + return; + + this.titleUpdating = true; + + MCPlayer mcPlayer = MCPlayer.from(player); + player.closeInventory(); + + MCSchedulers.getEntityScheduler(player).schedule(() -> { + if (!this.isOpening()) + return; + + try { + this.inventory = Bukkit.createInventory(null, this.getRows() * 9, MCAdventure.asLegacyString(this.title, mcPlayer.getLocale())); + this.renderSlots(player); + + player.openInventory(this.inventory); + } finally { + this.titleUpdating = false; + } + }, 1L); + } + public void openOrUpdate(Player player) { if (this.isOpening()) { this.update(player); @@ -287,6 +315,10 @@ private void onPlayerQuit(@NotNull PlayerQuitEvent event) { private void onInventoryClose(@NotNull InventoryCloseEvent event) { Player player = (Player) event.getPlayer(); + // if the title is updating, the inventory is already closed + if (this.titleUpdating) + return; + if (!this.isInventory(event.getInventory())) return; From 0dcb80173ea53f1dcae83c249a8709653beb3096 Mon Sep 17 00:00:00 2001 From: leegod Date: Mon, 25 Mar 2024 23:00:51 +0800 Subject: [PATCH 2/2] chore(gui): Simplified updateTitle() --- .../java/io/fairyproject/bukkit/gui/Gui.java | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/io.fairyproject.modules/platform.bukkit/bukkit-gui/src/main/java/io/fairyproject/bukkit/gui/Gui.java b/io.fairyproject.modules/platform.bukkit/bukkit-gui/src/main/java/io/fairyproject/bukkit/gui/Gui.java index cb9b6dc2..be4d1a56 100644 --- a/io.fairyproject.modules/platform.bukkit/bukkit-gui/src/main/java/io/fairyproject/bukkit/gui/Gui.java +++ b/io.fairyproject.modules/platform.bukkit/bukkit-gui/src/main/java/io/fairyproject/bukkit/gui/Gui.java @@ -81,21 +81,13 @@ public void updateTitle(@NotNull Player player, @NotNull Component title) { this.titleUpdating = true; MCPlayer mcPlayer = MCPlayer.from(player); - player.closeInventory(); - - MCSchedulers.getEntityScheduler(player).schedule(() -> { - if (!this.isOpening()) - return; - - try { - this.inventory = Bukkit.createInventory(null, this.getRows() * 9, MCAdventure.asLegacyString(this.title, mcPlayer.getLocale())); - this.renderSlots(player); - - player.openInventory(this.inventory); - } finally { - this.titleUpdating = false; - } - }, 1L); + try { + this.inventory = Bukkit.createInventory(null, this.getRows() * 9, MCAdventure.asLegacyString(this.title, mcPlayer.getLocale())); + this.renderSlots(player); + player.openInventory(this.inventory); + } finally { + this.titleUpdating = false; + } } public void openOrUpdate(Player player) { @@ -315,7 +307,7 @@ private void onPlayerQuit(@NotNull PlayerQuitEvent event) { private void onInventoryClose(@NotNull InventoryCloseEvent event) { Player player = (Player) event.getPlayer(); - // if the title is updating, the inventory is already closed + // if the title is updating, shouldn't attempt to close the gui if (this.titleUpdating) return;