package eu.mhsl.minecraft.pixelblocks.listeners;

import eu.mhsl.minecraft.pixelblocks.utils.EventCanceling;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryInteractEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.CraftingInventory;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.PlayerInventory;

public class PreventInventoriesListener implements Listener {
    @EventHandler
    public void onInventoryOpen(InventoryOpenEvent event) {
        EventCanceling.shouldCancelInPixelBlock(
            event,
            event.getPlayer().getWorld(),
            (pixelBlock) -> isDisallowedInventory(event.getInventory())
        );
    }

    @EventHandler
    public void onInventoryInteract(InventoryInteractEvent event) {
        EventCanceling.shouldCancelInPixelBlock(
            event,
            event.getWhoClicked().getWorld(),
            (pixelBlock) -> isDisallowedInventory(event.getInventory())
        );
    }

    private static boolean isDisallowedInventory(Inventory inventory) {
        return !(inventory instanceof PlayerInventory
            || inventory instanceof CraftingInventory
            || inventory.getType() == InventoryType.ENDER_CHEST);
    }
}
