Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.8 kB
2
Indexable
Never
```java
public class ChestCheckListener implements Listener {


    private final HashMap<UUID,Inventory> lastClickedInventory = new HashMap<>();

    @EventHandler
    public void onClose(InventoryCloseEvent closeEvent) {
        lastClickedInventory.remove(closeEvent.getPlayer().getUniqueId());
    }

    @EventHandler
    public void onDrag(InventoryDragEvent event) {

        System.out.println(event.getType().name() + " DRAG");
        if(event.getInventory() == null) return;
        if(event.getInventory().getType() == InventoryType.CRAFTING) return;
        Inventory clickedInventory = lastClickedInventory.get(event.getWhoClicked().getUniqueId());

//        if(event.getWhoClicked().getInventory().equals(clickedInventory)) return;

        System.out.println("?");
        if(event.getType()==DragType.EVEN) {
            ItemStack cursor = event.getCursor();
            ItemStack oldCursor = event.getOldCursor();

            System.out.println(cursor);
            System.out.println(oldCursor);
            if (clickedInventory == event.getInventory()) {
                event.setCancelled(moveItemBetweenInventory(clickedInventory, event.getWhoClicked().getInventory(), oldCursor));

            } else {
                event.setCancelled(moveItemBetweenInventory(clickedInventory, event.getInventory(), oldCursor));

            }
        }
    }

    @EventHandler
    public void onClick(InventoryClickEvent event) {

        System.out.println(event.getAction().name() + " CLICK");

        if (event.getClickedInventory() == null || event.getInventory() == null) return;
        if(event.getInventory().getType() == InventoryType.CRAFTING) return;

        Inventory inventory = event.getInventory();
        Inventory clickedInventory = event.getClickedInventory();
        if(event.getAction()==InventoryAction.MOVE_TO_OTHER_INVENTORY) {
            if(inventory.equals(clickedInventory)) {
                event.setCancelled(moveItemBetweenInventory(clickedInventory, event.getWhoClicked().getInventory(),event.getCurrentItem()));
            } else {
                event.setCancelled(moveItemBetweenInventory(clickedInventory,inventory,  event.getCurrentItem()));
            }
            return;
        }
        if(event.getAction()==InventoryAction.PICKUP_ALL) {
            lastClickedInventory.put(event.getWhoClicked().getUniqueId(), clickedInventory);
        }

        if(inventory.getType()==InventoryType.ENDER_CHEST && clickedInventory.getType() == InventoryType.ENDER_CHEST) {
            Inventory clickedInventoryC = lastClickedInventory.get(event.getWhoClicked().getUniqueId());
            if(clickedInventoryC.getType() == InventoryType.PLAYER) {
                event.setCancelled(moveItemBetweenInventory(clickedInventoryC, clickedInventory,event.getCursor()));

                if(event.isCancelled()) {
                    event.getWhoClicked().getInventory().addItem(event.getCursor().clone());
                    event.setCursor(null);
                }
            }
            return;
        }

        if(inventory.getType()== InventoryType.ENDER_CHEST &&
                clickedInventory.getType()==InventoryType.PLAYER &&
                  event.getAction() == InventoryAction.PLACE_ALL) {

            Inventory clickedInventoryC = lastClickedInventory.get(event.getWhoClicked().getUniqueId());

            if(clickedInventoryC.getType() == InventoryType.PLAYER) return;
            event.setCancelled(moveItemBetweenInventory(inventory, clickedInventory,event.getCursor()));
        }
    }

    private boolean moveItemBetweenInventory(Inventory from, Inventory to, ItemStack item) {
        System.out.println("MOVE ITEM FROM " + from.getType() + " TO " + to.getType() + " | " + item.getType().name());
        return item.getType()==Material.WOOD_SWORD;
    }
}
```