From 497a4af1c5c644f4d6b51dbdacea93816f2ad88d Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Thu, 29 Sep 2022 12:39:40 +0200 Subject: [PATCH] Fixed #3664 --- CHANGELOG.md | 1 + .../slimefun4/api/player/PlayerBackpack.java | 17 +++++++++++++++++ .../multiblocks/AbstractCraftingTable.java | 6 +++++- .../listeners/BackpackListener.java | 18 +++++++++++++----- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 462ee5fa2..14b65caf7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,7 @@ * Fixed an issue related to "Bee Wings" * Fixed #3573 * Fixed "round-robin" mode for cargo networks being very unreliable +* Fixed #3664 ## Release Candidate 32 (26 Jun 2022) https://thebusybiscuit.github.io/builds/TheBusyBiscuit/Slimefun4/stable/#32 diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/player/PlayerBackpack.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/player/PlayerBackpack.java index 5d942114f..5af1f1fe7 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/player/PlayerBackpack.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/player/PlayerBackpack.java @@ -1,10 +1,13 @@ package io.github.thebusybiscuit.slimefun4.api.player; import java.io.File; +import java.util.ArrayList; +import java.util.Iterator; import javax.annotation.Nonnull; import org.bukkit.Bukkit; +import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; @@ -132,6 +135,20 @@ public class PlayerBackpack { }); } + /** + * This will close the {@link Inventory} of this backpack for every {@link Player} + * that has opened it. + */ + public void closeForAll() { + Slimefun.runSync(() -> { + Iterator iterator = new ArrayList<>(inventory.getViewers()).iterator(); + + while (iterator.hasNext()) { + iterator.next().closeInventory(); + } + }); + } + /** * This will change the current size of this Backpack to the specified size. * diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/AbstractCraftingTable.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/AbstractCraftingTable.java index c45a8c246..4adf57bc9 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/AbstractCraftingTable.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/multiblocks/AbstractCraftingTable.java @@ -123,7 +123,11 @@ abstract class AbstractCraftingTable extends MultiBlockMachine { PlayerProfile.fromUUID(UUID.fromString(idSplit[0]), profile -> { Optional optional = profile.getBackpack(Integer.parseInt(idSplit[1])); - optional.ifPresent(playerBackpack -> playerBackpack.setSize(size)); + optional.ifPresent(playerBackpack -> { + // Safety feature for Issue #3664 + playerBackpack.closeForAll(); + playerBackpack.setSize(size); + }); }); return Optional.of(id); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BackpackListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BackpackListener.java index 796f6917b..1650b0b42 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BackpackListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BackpackListener.java @@ -96,6 +96,7 @@ public class BackpackListener implements Listener { if (backpack instanceof SlimefunBackpack slimefunBackpack) { if (e.getClick() == ClickType.NUMBER_KEY) { + // Prevent disallowed items from being moved using number keys. if (e.getClickedInventory().getType() != InventoryType.PLAYER) { ItemStack hotbarItem = e.getWhoClicked().getInventory().getItem(e.getHotbarButton()); @@ -103,12 +104,19 @@ public class BackpackListener implements Listener { e.setCancelled(true); } } - } else if (e.getClick() == ClickType.SWAP_OFFHAND && e.getClickedInventory().getType() != InventoryType.PLAYER) { - // Fixes #3265 - ItemStack offHandItem = e.getWhoClicked().getInventory().getItemInOffHand(); + } else if (e.getClick() == ClickType.SWAP_OFFHAND) { + if (e.getClickedInventory().getType() != InventoryType.PLAYER) { + // Fixes #3265 - Don't move disallowed items using the off hand. + ItemStack offHandItem = e.getWhoClicked().getInventory().getItemInOffHand(); - if (!isAllowed(slimefunBackpack, offHandItem)) { - e.setCancelled(true); + if (!isAllowed(slimefunBackpack, offHandItem)) { + e.setCancelled(true); + } + } else { + // Fixes #3664 - Do not swap the backpack to your off hand. + if (e.getCurrentItem() != null && e.getCurrentItem().isSimilar(item)) { + e.setCancelled(true); + } } } else if (!isAllowed(slimefunBackpack, e.getCurrentItem())) { e.setCancelled(true);