diff --git a/src/main/java/fr/openmc/api/menulib/Menu.java b/src/main/java/fr/openmc/api/menulib/Menu.java index d843d736a..57d9b4f8c 100644 --- a/src/main/java/fr/openmc/api/menulib/Menu.java +++ b/src/main/java/fr/openmc/api/menulib/Menu.java @@ -147,9 +147,8 @@ public final void open() { Inventory inventory = getInventory(); - getContent().forEach((slot, item) -> { - setItem(owner, inventory, slot, item); - }); + getContent().forEach((slot, item) -> + setItem(owner, inventory, slot, item)); Bukkit.getServer().getPluginManager().callEvent(new OpenMenuEvent(owner, this)); @@ -222,6 +221,19 @@ public final Map fill(ItemStack item) { } return map; } + + public final void update() { + if (owner == null) return; + + Inventory open = owner.getOpenInventory().getTopInventory(); + + if (!(open.getHolder() instanceof Menu menu) || menu != this) return; + + getContent().forEach((slot, item) -> + setItem(owner, open, slot, item)); + + owner.updateInventory(); + } /** * Checks if the given {@link ItemStack} is associated with the specified item ID. diff --git a/src/main/java/fr/openmc/api/menulib/MenuLib.java b/src/main/java/fr/openmc/api/menulib/MenuLib.java index 8b54d450f..39e347757 100644 --- a/src/main/java/fr/openmc/api/menulib/MenuLib.java +++ b/src/main/java/fr/openmc/api/menulib/MenuLib.java @@ -83,6 +83,11 @@ public static void setItemClickEvent(Menu menu, ItemBuilder itemBuilder, Consume menu.getItemClickEvents().put(itemBuilder, e); } + public static void updateMenu(Player player) { + if (!(player.getOpenInventory().getTopInventory().getHolder() instanceof Menu menu)) return; + menu.update(); + } + public static void clearHistory(Player player) { menuHistory.remove(player); } @@ -216,7 +221,7 @@ public void onInventoryDrag(InventoryDragEvent e) { */ @EventHandler public void onClose(InventoryCloseEvent e) { - if (!(e.getPlayer() instanceof Player player)) return; + if (!(e.getPlayer() instanceof Player player)) return; if (e.getInventory().getHolder(false) instanceof Menu menu) { menu.onClose(e); diff --git a/src/main/java/fr/openmc/core/features/city/menu/CityChestMenu.java b/src/main/java/fr/openmc/core/features/city/menu/CityChestMenu.java index 862c495b1..8be787667 100644 --- a/src/main/java/fr/openmc/core/features/city/menu/CityChestMenu.java +++ b/src/main/java/fr/openmc/core/features/city/menu/CityChestMenu.java @@ -1,10 +1,12 @@ package fr.openmc.core.features.city.menu; +import fr.openmc.api.menulib.MenuLib; import fr.openmc.api.menulib.PaginatedMenu; import fr.openmc.api.menulib.utils.InventorySize; import fr.openmc.api.menulib.utils.ItemBuilder; import fr.openmc.api.menulib.utils.MenuUtils; import fr.openmc.api.menulib.utils.StaticSlots; +import fr.openmc.core.OMCPlugin; import fr.openmc.core.commands.utils.Restart; import fr.openmc.core.features.city.City; import fr.openmc.core.features.city.CityManager; @@ -17,6 +19,7 @@ import lombok.Getter; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Player; @@ -205,17 +208,15 @@ public int getSizeOfItems() { public void onClose(InventoryCloseEvent event) { if (Restart.isRestarting) return; HumanEntity humanEntity = event.getPlayer(); - if (!(humanEntity instanceof Player player)) { - return; - } + if (!(humanEntity instanceof Player player)) return; City city = CityManager.getPlayerCity(player.getUniqueId()); - if (city == null) { - return; - } + if (city == null) return; Inventory inv = event.getInventory(); exit(city, inv); + // fixes #1007 + Bukkit.getScheduler().runTaskLater(OMCPlugin.getInstance(), ()-> MenuLib.updateMenu(player), 5L); } private void exit(City city, Inventory inv) {