package eu.mhsl.minecraft.pixelblocks.listeners;

import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
import io.papermc.paper.event.player.PrePlayerAttackEntityEvent;
import org.bukkit.Location;
import org.bukkit.entity.Interaction;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

public class BreakPixelBlockListener implements Listener {
    @EventHandler
    public void destroyPixelBlock(PrePlayerAttackEntityEvent event) {
        if(!(event.getAttacked() instanceof Interaction)) return;

        Location blockLocation = event.getAttacked().getLocation().toBlockLocation();
        PixelBlock pixelBlock = PixelBlock.getPixelBlockFromPlacedLocation(blockLocation);
        if(pixelBlock == null) return;
        pixelBlock.destroy(event.getPlayer(), false);
    }
}
