// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.server.npc.util;

import com.hypixel.hytale.math.shape.Box;
import com.hypixel.hytale.server.core.asset.type.blockhitbox.BlockBoundingBoxes;
import com.hypixel.hytale.protocol.BlockMaterial;
import com.hypixel.hytale.server.core.universe.world.chunk.WorldChunk;
import com.hypixel.hytale.math.util.ChunkUtil;
import com.hypixel.hytale.server.core.asset.type.blocktype.config.BlockType;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.universe.world.World;

public class BlockPlacementHelper
{
    public static boolean canPlaceUnitBlock(@Nonnull final World world, final BlockType placedBlockType, final boolean allowEmptyMaterials, final int x, final int y, final int z) {
        final WorldChunk chunk = world.getChunkIfInMemory(ChunkUtil.indexChunkFromBlock(x, z));
        if (chunk == null) {
            return false;
        }
        int target = chunk.getBlock(x, y, z);
        BlockType targetBlockType = BlockType.getAssetMap().getAsset(target);
        if (!testBlock(placedBlockType, targetBlockType, allowEmptyMaterials)) {
            return false;
        }
        target = chunk.getBlock(x, y - 1, z);
        targetBlockType = BlockType.getAssetMap().getAsset(target);
        final int filler = chunk.getFiller(x, y - 1, z);
        final int rotation = chunk.getRotationIndex(x, y - 1, z);
        return testSupportingBlock(targetBlockType, rotation, filler);
    }
    
    public static boolean canPlaceBlock(@Nonnull final World world, @Nonnull final BlockType placedBlockType, final int rotationIndex, final boolean allowEmptyMaterials, final int x, final int y, final int z) {
        return world.testBlockTypes(x, y, z, placedBlockType, rotationIndex, (blockX, blockY, blockZ, blockType, rotation, filler) -> testBlock(placedBlockType, blockType, allowEmptyMaterials));
    }
    
    public static boolean testBlock(final BlockType placedBlockType, @Nonnull final BlockType blockType, final boolean allowEmptyMaterials) {
        return blockType == BlockType.EMPTY || !allowEmptyMaterials || blockType.getMaterial() != BlockMaterial.Empty || true;
    }
    
    public static boolean testSupportingBlock(@Nonnull final BlockType blockType, final int rotation, final int filler) {
        final Box targetHitbox = BlockBoundingBoxes.getAssetMap().getAsset(blockType.getHitboxTypeIndex()).get(rotation).getBoundingBox();
        return blockType != BlockType.EMPTY && blockType != BlockType.UNKNOWN && blockType.getMaterial() == BlockMaterial.Solid && filler == 0 && targetHitbox.isUnitBox();
    }
}
