package com.buuz135.industrial.tile.world;

import com.buuz135.industrial.tile.CustomColoredItemHandler;
import com.buuz135.industrial.tile.WorkingAreaElectricMachine;
import com.buuz135.industrial.utils.BlockUtils;
import com.buuz135.industrial.utils.WorkUtils;
import net.minecraft.block.Block;
import net.minecraft.item.EnumDyeColor;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.items.ItemStackHandler;

import java.util.List;

public class BlockDestroyerTile extends WorkingAreaElectricMachine {


    private ItemStackHandler outItems;

    public BlockDestroyerTile() {
        super(BlockDestroyerTile.class.getName().hashCode(), 0, 0, true);
    }

    @Override
    protected void initializeInventories() {
        super.initializeInventories();
        outItems = new ItemStackHandler(3 * 6) {
            @Override
            protected void onContentsChanged(int slot) {
                BlockDestroyerTile.this.markDirty();
            }
        };
        this.addInventory(new CustomColoredItemHandler(outItems, EnumDyeColor.ORANGE, "Output items", 18 * 3, 25, 6, 3) {
            @Override
            public boolean canInsertItem(int slot, ItemStack stack) {
                return false;
            }

            @Override
            public boolean canExtractItem(int slot) {
                return true;
            }

        });
        this.addInventoryToStorage(outItems, "block_destroyer_out");
    }

    @Override
    public AxisAlignedBB getWorkingArea() {
        return new AxisAlignedBB(this.pos.getX(), this.pos.getY(), this.pos.getZ(), this.pos.getX() + 1, this.pos.getY() + 1, this.pos.getZ() + 1).offset(new BlockPos(0, 0, 0).offset(this.getFacing().getOpposite()));
    }

    @Override
    public float work() {
        if (WorkUtils.isDisabled(this.getBlockType())) return 0;
        List<BlockPos> blockPosList = BlockUtils.getBlockPosInAABB(getWorkingArea());
        for (BlockPos pos : blockPosList) {
            if (!this.world.isAirBlock(pos)) {
                Block block = this.world.getBlockState(pos).getBlock();
                if (block.getBlockHardness(this.world.getBlockState(pos), this.world, pos) < 0) continue;
                List<ItemStack> drops = block.getDrops(this.world, pos, this.world.getBlockState(pos), 0);
                boolean canInsert = true;
                for (ItemStack stack : drops) {
                    if (!ItemHandlerHelper.insertItem(outItems, stack, true).isEmpty()) {
                        canInsert = false;
                        break;
                    }
                }
                if (canInsert) {
                    for (ItemStack stack : drops) {
                        ItemHandlerHelper.insertItem(outItems, stack, false);
                    }
                    this.world.setBlockToAir(pos);
                    return 1;
                }
            }
        }
        return 0;
    }
}

@@ -316,61 +316,250 @@
 ft.i
-tem.EnumDyeColor;%0Aimport net.minecraft.item.ItemStack
+nventory.IInventory;%0Aimport net.minecraft.inventory.InventoryHelper;%0Aimport net.minecraft.item.EnumDyeColor;%0Aimport net.minecraft.item.ItemStack;%0Aimport net.minecraft.tileentity.TileEntity;%0Aimport net.minecraft.tileentity.TileEntityShulkerBox
 ;%0Aim
@@ -643,16 +643,59 @@
 ockPos;%0A
+import net.minecraft.world.IWorldNameable;%0A
 import n
@@ -2441,24 +2441,84 @@
 getBlock();%0A
+                TileEntity tile = world.getTileEntity(pos);%0A
             
@@ -2607,24 +2607,24 @@
 ) continue;%0A
-
             
@@ -2800,32 +2800,297 @@
 tack : drops) %7B%0A
+                    if(tile instanceof IWorldNameable) %7B%0A                        if(((IWorldNameable) tile).hasCustomName()) %7B%0A                            stack.setStackDisplayName(((IWorldNameable) tile).getName());%0A                        %7D%0A                    %7D%0A
                 
@@ -3354,32 +3354,32 @@
 tack : drops) %7B%0A
-
                 
@@ -3454,32 +3454,275 @@
               %7D%0A
+                    if(tile instanceof TileEntityShulkerBox) %7B%0A                        InventoryHelper.dropInventoryItems(this.world, pos, (IInventory)tile);%0A                        ((TileEntityShulkerBox) tile).clear();%0A                    %7D%0A
                 

fix dupe bug with shulkerbox and block not keeping their custom name