package eu.mhsl.minecraft.pixelblocks.listeners;

import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlockWorld;
import eu.mhsl.minecraft.pixelblocks.utils.EventCanceling;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPistonExtendEvent;
import org.bukkit.event.block.BlockRedstoneEvent;

public class PreventRedstoneListener implements Listener {
    @EventHandler
    public void preventRedstone(BlockRedstoneEvent event) {
        boolean isInPixelBlock = PixelBlockWorld.isPixelWorld(event.getBlock().getWorld());
        if(isInPixelBlock) event.setNewCurrent(0);
    }

    @EventHandler
    public void blockPistons(BlockPistonExtendEvent event) {
        EventCanceling.shouldCancelInPixelBlock(
            event,
            event.getBlock().getWorld(),
            (pixelBlock) -> event.getBlocks().stream()
                .anyMatch(block -> !pixelBlock.getPixelWorld()
                    .allowPlacements(block.getLocation().add(event.getDirection().getDirection())))
        );
    }
}
