From ee1ebf0f6d151d7ec59ef613f931ee469321168c Mon Sep 17 00:00:00 2001 From: StarWishsama Date: Sat, 19 Oct 2024 21:01:40 +0800 Subject: [PATCH] feat(menu): lock menu when moving android --- .../controller/BlockDataController.java | 9 ++++++- .../general/Inventory/ChestMenu.java | 17 +++++++++++++ .../api/inventory/DirtyChestMenu.java | 24 +++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/xzavier0722/mc/plugin/slimefun4/storage/controller/BlockDataController.java b/src/main/java/com/xzavier0722/mc/plugin/slimefun4/storage/controller/BlockDataController.java index 8a8be2c2ca..2842fabbee 100644 --- a/src/main/java/com/xzavier0722/mc/plugin/slimefun4/storage/controller/BlockDataController.java +++ b/src/main/java/com/xzavier0722/mc/plugin/slimefun4/storage/controller/BlockDataController.java @@ -285,6 +285,13 @@ public void setBlockDataLocation(SlimefunBlockData blockData, Location target) { hasTicker = true; } + BlockMenu menu = null; + + if (blockData.isDataLoaded() && blockData.getBlockMenu() != null) { + menu = blockData.getBlockMenu(); + menu.lock(); + } + var chunk = blockData.getLocation().getChunk(); var chunkData = getChunkDataCache(chunk, false); if (chunkData != null) { @@ -304,9 +311,9 @@ public void setBlockDataLocation(SlimefunBlockData blockData, Location target) { chunkData.addBlockCacheInternal(newBlockData, true); - var menu = blockData.getBlockMenu(); if (menu != null) { newBlockData.setBlockMenu(new BlockMenu(menu.getPreset(), target, menu.getInventory())); + menu.unlock(); } key.addField(FieldKey.LOCATION); diff --git a/src/main/java/me/mrCookieSlime/CSCoreLibPlugin/general/Inventory/ChestMenu.java b/src/main/java/me/mrCookieSlime/CSCoreLibPlugin/general/Inventory/ChestMenu.java index a913ed7c74..54dba98243 100644 --- a/src/main/java/me/mrCookieSlime/CSCoreLibPlugin/general/Inventory/ChestMenu.java +++ b/src/main/java/me/mrCookieSlime/CSCoreLibPlugin/general/Inventory/ChestMenu.java @@ -1,6 +1,7 @@ package me.mrCookieSlime.CSCoreLibPlugin.general.Inventory; import city.norain.slimefun4.holder.SlimefunInventoryHolder; +import city.norain.slimefun4.utils.InventoryUtil; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack; import java.util.ArrayList; import java.util.HashMap; @@ -9,6 +10,7 @@ import java.util.Set; import java.util.UUID; import java.util.concurrent.CopyOnWriteArraySet; +import java.util.concurrent.atomic.AtomicBoolean; import javax.annotation.Nonnull; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -39,7 +41,9 @@ public class ChestMenu extends SlimefunInventoryHolder { private MenuOpeningHandler open; private MenuCloseHandler close; private MenuClickHandler playerclick; + private final Set viewers = new CopyOnWriteArraySet<>(); + private final AtomicBoolean lock = new AtomicBoolean(false); /** * Creates a new ChestMenu with the specified @@ -383,6 +387,19 @@ public boolean isSizeAutomaticallyInferred() { return size == -1; } + public boolean locked() { + return lock.get(); + } + + public void lock() { + lock.getAndSet(true); + InventoryUtil.closeInventory(this.inventory); + } + + public void unlock() { + lock.getAndSet(false); + } + @FunctionalInterface public interface MenuClickHandler { diff --git a/src/main/java/me/mrCookieSlime/Slimefun/api/inventory/DirtyChestMenu.java b/src/main/java/me/mrCookieSlime/Slimefun/api/inventory/DirtyChestMenu.java index 5cf98e3c93..baa281af27 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/api/inventory/DirtyChestMenu.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/api/inventory/DirtyChestMenu.java @@ -67,6 +67,10 @@ public boolean canOpen(Block b, Player p) { @Override public void open(Player... players) { + if (locked()) { + return; + } + super.open(players); // The Inventory will likely be modified soon @@ -154,6 +158,10 @@ public boolean fits(@Nonnull ItemStack item, int... slots) { throw new IllegalArgumentException("Cannot push null or AIR"); } + if (locked()) { + throw new IllegalStateException("Cannot push item when menu is locked"); + } + ItemStackWrapper wrapper = null; int amount = item.getAmount(); @@ -206,20 +214,36 @@ public void consumeItem(int slot) { } public void consumeItem(int slot, int amount) { + if (locked()) { + throw new IllegalStateException("Cannot consume item when menu is locked"); + } + consumeItem(slot, amount, false); } public void consumeItem(int slot, int amount, boolean replaceConsumables) { + if (locked()) { + throw new IllegalStateException("Cannot consume item when menu is locked"); + } + ItemUtils.consumeItem(getItemInSlot(slot), amount, replaceConsumables); markDirty(); } @Override public void replaceExistingItem(int slot, ItemStack item) { + if (locked()) { + throw new IllegalStateException("Cannot consume item when menu is locked"); + } + replaceExistingItem(slot, item, true); } public void replaceExistingItem(int slot, ItemStack item, boolean event) { + if (locked()) { + throw new IllegalStateException("Cannot consume item when menu is locked"); + } + if (event) { ItemStack previous = getItemInSlot(slot); item = preset.onItemStackChange(this, slot, previous, item);