package com.yolp900.itsjustacharm.common.blocks;
import com.yolp900.itsjustacharm.ItsJustaCharm;
import com.yolp900.itsjustacharm.api.IJCConstants;
import com.yolp900.itsjustacharm.common.blocks.base.ModBlock;
import com.yolp900.itsjustacharm.common.integration.waila.IWailaBodyProvider;
import com.yolp900.itsjustacharm.common.tileEntities.ModTileEntityIInventory;
import com.yolp900.itsjustacharm.common.tileEntities.TileEntityConstructionTable;
import com.yolp900.itsjustacharm.common.tooltips.ITooltipHolder;
import com.yolp900.itsjustacharm.reference.LibGuis;
import com.yolp900.itsjustacharm.reference.LibModIntegrations;
import com.yolp900.itsjustacharm.reference.LibTexts;
import mcp.mobius.waila.api.IWailaConfigHandler;
import mcp.mobius.waila.api.IWailaDataAccessor;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.Optional;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Random;
@Optional.Interface(modid = LibModIntegrations.Waila.MOD_ID, iface = LibModIntegrations.Waila.INTERFACE_BODY)
public class BlockConstructionTable extends ModBlock implements ITileEntityProvider, ITooltipHolder, IWailaBodyProvider {
public BlockConstructionTable() {
super(IJCConstants.Blocks.ConstructionTable, Material.ROCK);
this.setHardness(1.1F);
this.setResistance(9.5F);
}
@Override
@Nonnull
public TileEntity createNewTileEntity(@Nonnull World worldIn, int meta) {
return new TileEntityConstructionTable();
}
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, @Nullable ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {
if (!player.isSneaking()) {
player.openGui(ItsJustaCharm.instance, LibGuis.ConstructionTable.getID(), world, pos.getX(), pos.getY(), pos.getZ());
return true;
}
return false;
}
@Override
public boolean isNormalCube(IBlockState state, IBlockAccess world, BlockPos pos) {
return false;
}
@Override
public boolean isOpaqueCube(IBlockState state) {
return false;
}
@Override
public boolean isVisuallyOpaque() {
return false;
}
@Override
public void breakBlock(@Nonnull World world, @Nonnull BlockPos pos, @Nonnull IBlockState state) {
dropInventory(world, pos.getX(), pos.getY(), pos.getZ());
super.breakBlock(world, pos, state);
}
private void dropInventory(World world, int x, int y, int z) {
TileEntity tileEntity = world.getTileEntity(new BlockPos(x, y, z));
if (!(tileEntity instanceof ModTileEntityIInventory)) {
return;
}
IInventory inventory = (ModTileEntityIInventory)tileEntity;
for (int i = 0; i < inventory.getSizeInventory(); i++) {
if (i != 0) {
ItemStack itemStack = inventory.getStackInSlot(i);
if (itemStack != null && itemStack.stackSize > 0) {
Random rand = new Random();
float dX = rand.nextFloat() * 0.8F + 0.1F;
float dY = rand.nextFloat() * 0.8F + 0.1F;
float dZ = rand.nextFloat() * 0.8F + 0.1F;
EntityItem entityItem = new EntityItem(world, x + dX, y + dY, z + dZ, itemStack.copy());
if (itemStack.hasTagCompound() && itemStack.getTagCompound() != null) {
entityItem.getEntityItem().setTagCompound(itemStack.getTagCompound().copy());
}
float factor = 0.05F;
entityItem.motionX = rand.nextGaussian() * factor;
entityItem.motionY = rand.nextGaussian() * factor + 0.2F;
entityItem.motionZ = rand.nextGaussian() * factor;
world.spawnEntityInWorld(entityItem);
itemStack.stackSize = 0;
}
}
}
}
@Override
@Optional.Method(modid = LibModIntegrations.Waila.MOD_ID)
public LibModIntegrations.Waila.Tooltips getWailaBody(ItemStack stack, List<String> currenttip, IWailaDataAccessor accessor, IWailaConfigHandler config) {
return LibModIntegrations.Waila.Tooltips.ConstructionTable;
}
@Override
public LibTexts.Tooltips getTooltip(ItemStack stack, EntityPlayer player) {
return LibTexts.Tooltips.ConstructionTable;
}
}