Skip to content

Commit

Permalink
feat(menu): lock menu when moving android
Browse files Browse the repository at this point in the history
  • Loading branch information
StarWishsama committed Oct 19, 2024
1 parent 4168d38 commit ee1ebf0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -39,7 +41,9 @@ public class ChestMenu extends SlimefunInventoryHolder {
private MenuOpeningHandler open;
private MenuCloseHandler close;
private MenuClickHandler playerclick;

private final Set<UUID> viewers = new CopyOnWriteArraySet<>();
private final AtomicBoolean lock = new AtomicBoolean(false);

/**
* Creates a new ChestMenu with the specified
Expand Down Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ee1ebf0

Please sign in to comment.