Skip to content

Commit

Permalink
Merge pull request #145 from FairyProject/feat/gui/update-title
Browse files Browse the repository at this point in the history
feat(gui): Added updateTitle() method
  • Loading branch information
LeeGodSRC authored Mar 25, 2024
2 parents de3bc4a + 0dcb801 commit d482a13
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ public class Gui {
private final List<Consumer<Player>> closeCallbacks;
private final List<Pane> panes;
private final boolean[] usedSlots;
private final Component title;
private Component title;

@Nullable
private Inventory inventory;
private GuiSlot[] guiSlots;
@Getter
private EventNode<Event> eventNode;
private boolean titleUpdating;
private int maxSlots;

public Gui(BukkitEventNode bukkitEventNode, Component title) {
Expand All @@ -70,6 +71,25 @@ 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);
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) {
if (this.isOpening()) {
this.update(player);
Expand Down Expand Up @@ -287,6 +307,10 @@ private void onPlayerQuit(@NotNull PlayerQuitEvent event) {

private void onInventoryClose(@NotNull InventoryCloseEvent event) {
Player player = (Player) event.getPlayer();
// if the title is updating, shouldn't attempt to close the gui
if (this.titleUpdating)
return;

if (!this.isInventory(event.getInventory()))
return;

Expand Down

0 comments on commit d482a13

Please sign in to comment.