Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix item swap working on backpacks #4275

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

Expand Down Expand Up @@ -50,7 +51,7 @@
*/
public class BackpackListener implements Listener {

private final Map<UUID, ItemStack> backpacks = new HashMap<>();
private final Map<UUID, ItemStack> backpacks = new HashMap<>(); // UUID of players with backpack open

public void register(@Nonnull Slimefun plugin) {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
Expand Down Expand Up @@ -88,41 +89,61 @@ public void onItemDrop(PlayerDropItemEvent e) {
}
}

@EventHandler
public void onItemSwap(PlayerSwapHandItemsEvent e) {
Player player = e.getPlayer();
if (!backpacks.containsKey(player.getUniqueId())) {
return;
}

ItemStack item = player.getInventory().getItemInOffHand();
if (item == null || item.getType().isAir()) {
return;
}

SlimefunItem backpack = SlimefunItem.getByItem(item);
if (backpack instanceof SlimefunBackpack) {
e.setCancelled(true);
}
}

@EventHandler(ignoreCancelled = true)
public void onClick(InventoryClickEvent e) {
ItemStack item = backpacks.get(e.getWhoClicked().getUniqueId());
if (item == null) {
return;
}

if (item != null) {
SlimefunItem backpack = SlimefunItem.getByItem(item);

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());

if (!isAllowed(slimefunBackpack, hotbarItem)) {
e.setCancelled(true);
}
}
} 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);
}
} 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())) {
SlimefunItem backpack = SlimefunItem.getByItem(item);
if (!(backpack instanceof SlimefunBackpack slimefunBackpack)) {
return;
}

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());

if (!isAllowed(slimefunBackpack, hotbarItem)) {
e.setCancelled(true);
}
}
} 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);
}
} 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);
}
}

Expand Down
Loading