Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/main/java/fr/openmc/api/menulib/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -222,6 +221,19 @@ public final Map<Integer, ItemBuilder> 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.
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/fr/openmc/api/menulib/MenuLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Loading