package eu.mhsl.minecraft.pixelblocks.listeners;

import eu.mhsl.minecraft.pixelblocks.utils.EventCanceling;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.hanging.HangingPlaceEvent;

public class PreventIllegalBlocksListener implements Listener {
    @EventHandler
    public void onBlockExplode(BlockExplodeEvent event) {
        EventCanceling.cancelIfInPixelWorld(event, event.getBlock().getWorld());
    }

    @EventHandler
    public void onCreatureSpawn(CreatureSpawnEvent event) {
        EventCanceling.cancelIfInPixelWorld(event, event.getLocation().getWorld());
    }

    @EventHandler
    public void onItemFrame(HangingPlaceEvent event) {
        if(event.getItemStack() == null || event.getPlayer() == null) return;
        if(!(event.getItemStack().getType().equals(Material.ITEM_FRAME) || event.getItemStack().getType().equals(Material.GLOW_ITEM_FRAME))) return;
        EventCanceling.cancelIfInPixelWorld(event, event.getPlayer().getWorld());
    }

    @EventHandler
    public void onEntityDamage(EntityDamageEvent event) {
        EventCanceling.cancelIfInPixelWorld(event, event.getEntity().getWorld());
    }

    @EventHandler
    public void onEntityExplode(EntityExplodeEvent event) {
        EventCanceling.cancelIfInPixelWorld(event, event.getLocation().getWorld());
    }
}
