package com.loganmagnan.eventcore.utils;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Player;

public class BlockUtils {
    public static void breakBlockGroup(Player player, final Block block, final boolean useParticles) {
        final Material target = block.getType();
        final Block up = block.getRelative(BlockFace.SOUTH);
        final Block down = block.getRelative(BlockFace.NORTH);
        final Block left = block.getRelative(BlockFace.EAST);
        final Block right = block.getRelative(BlockFace.WEST);

        BlockData blockData = Material.AIR.createBlockData();

        player.sendBlockChange(block.getLocation(), blockData);

        if (useParticles) {
            block.breakNaturally();
        } else {
            block.setType(Material.AIR);
        }

        if (up.getType() == target) {
            breakBlockGroup(player, up, useParticles);
        }

        if (down.getType() == target) {
            breakBlockGroup(player, down, useParticles);
        }

        if (left.getType() == target) {
            breakBlockGroup(player, left, useParticles);
        }

        if (right.getType() == target) {
            breakBlockGroup(player, right, useParticles);
        }
    }

    public static void breakBlockGroup(Player player, final Block block) {
        breakBlockGroup(player, block, true);
    }
}