diff --git a/src/main/java/io/github/thebusybiscuit/sensibletoolbox/api/gui/InventoryGUI.java b/src/main/java/io/github/thebusybiscuit/sensibletoolbox/api/gui/InventoryGUI.java index 3108881b..53e03fb6 100644 --- a/src/main/java/io/github/thebusybiscuit/sensibletoolbox/api/gui/InventoryGUI.java +++ b/src/main/java/io/github/thebusybiscuit/sensibletoolbox/api/gui/InventoryGUI.java @@ -156,6 +156,11 @@ public interface InventoryGUI { */ void show(Player player); + /** + * Hides this GUI from all players + */ + void hideForAll(); + /** * Hide this GUI from the given player (pop it down) * diff --git a/src/main/java/io/github/thebusybiscuit/sensibletoolbox/api/items/BaseSTBMachine.java b/src/main/java/io/github/thebusybiscuit/sensibletoolbox/api/items/BaseSTBMachine.java index 875474d4..92718f27 100644 --- a/src/main/java/io/github/thebusybiscuit/sensibletoolbox/api/items/BaseSTBMachine.java +++ b/src/main/java/io/github/thebusybiscuit/sensibletoolbox/api/items/BaseSTBMachine.java @@ -584,6 +584,10 @@ public void onBlockRegistered(Location location, boolean isPlacing) { @Override public void onBlockUnregistered(Location loc) { + // Machines broken while viewed allow items to be taken out. + // We need to close the inventories if opened. + getGUI().hideForAll(); + getGUI().ejectItems(getInputSlots()); getGUI().ejectItems(getOutputSlots()); getGUI().ejectItems(getUpgradeSlots()); diff --git a/src/main/java/io/github/thebusybiscuit/sensibletoolbox/core/gui/STBInventoryGUI.java b/src/main/java/io/github/thebusybiscuit/sensibletoolbox/core/gui/STBInventoryGUI.java index 9170e8da..95faa270 100644 --- a/src/main/java/io/github/thebusybiscuit/sensibletoolbox/core/gui/STBInventoryGUI.java +++ b/src/main/java/io/github/thebusybiscuit/sensibletoolbox/core/gui/STBInventoryGUI.java @@ -37,6 +37,8 @@ import me.desht.dhutils.Debugger; import me.desht.dhutils.text.LogUtils; +import javax.annotation.Nonnull; + public class STBInventoryGUI implements InventoryGUI { // some handy stock textures @@ -103,6 +105,10 @@ private static void setOpenGUI(Player player, InventoryGUI gui) { } } + private boolean hasOpenGUI(@Nonnull Player player) { + return getOpenGUI(player) == null; + } + @Override public void addGadget(ClickableGadget gadget) { int slot = gadget.getSlot(); @@ -197,10 +203,19 @@ public void show(Player player) { monitor.doRepaint(); } } - Debugger.getInstance().debug(player.getName() + " opened GUI for " + getOwningItem()); - setOpenGUI(player, this); - listener.onGUIOpened(player); - player.openInventory(inventory); + if (hasOpenGUI(player)) { + Debugger.getInstance().debug(player.getName() + " opened GUI for " + getOwningItem()); + setOpenGUI(player, this); + listener.onGUIOpened(player); + player.openInventory(inventory); + } + } + + @Override + public void hideForAll() { + for (HumanEntity player : new ArrayList<>(inventory.getViewers())) { + hide((Player) player); + } } @Override