Skip to content

Commit

Permalink
Fix the issue + refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Intybyte committed Dec 21, 2024
1 parent d12ae85 commit 2cecadc
Showing 1 changed file with 50 additions and 29 deletions.
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

0 comments on commit 2cecadc

Please sign in to comment.