package eu.mhsl.minecraft.pixelblocks.utils;

import eu.mhsl.minecraft.pixelblocks.Main;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlockWorld;
import org.bukkit.World;
import org.bukkit.event.Cancellable;
import org.jetbrains.annotations.Nullable;

import java.util.function.Function;

public class EventCanceling {
    public static void shouldCancelInPixelBlock(Cancellable event, World world, Function<PixelBlock, Boolean> callback) {
        if(!PixelBlockWorld.isPixelWorld(world)) return;
        @Nullable PixelBlock pixelBlock = PixelBlock.getPixelBlockFromBlockWorld(world);
        if(pixelBlock == null) {
            Main.logger().warning("Cancelling %s event because PixelBlock '%s' could not be found!".formatted(event.getClass().getSimpleName(), world.getName()));
            event.setCancelled(true);
            return;
        }
        if(callback.apply(pixelBlock)) event.setCancelled(true);
    }

    public static void cancelIfInPixelWorld(Cancellable event, World world) {
        if(PixelBlockWorld.isPixelWorld(world)) {
            event.setCancelled(true);
        }
    }
}
