/**
* This class was created by <Vazkii>. It's distributed as
* part of the Botania Mod. Get the Source Code in github:
* https://github.com/Vazkii/Botania
*
* Botania is Open Source and distributed under the
* Botania License: http://botaniamod.net/license.php
*
* File Created @ [Jul 18, 2014, 7:58:08 PM (GMT)]
*/
package vazkii.botania.common.block.decor;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.registry.GameRegistry;
import vazkii.botania.api.lexicon.ILexiconable;
import vazkii.botania.api.lexicon.LexiconEntry;
import vazkii.botania.api.state.BotaniaStateProps;
import vazkii.botania.common.achievement.ModAchievements;
import vazkii.botania.common.block.BlockMod;
import vazkii.botania.common.block.tile.TileSimpleInventory;
import vazkii.botania.common.block.tile.TileTinyPotato;
import vazkii.botania.common.core.helper.InventoryHelper;
import vazkii.botania.common.item.block.ItemBlockTinyPotato;
import vazkii.botania.common.lexicon.LexiconData;
import vazkii.botania.common.lib.LibBlockNames;
public class BlockTinyPotato extends BlockMod implements ILexiconable {
private static final AxisAlignedBB AABB = new AxisAlignedBB(0.375, 0, 0.375, 0.625, 0.375, 0.625);
public BlockTinyPotato() {
super(Material.CLOTH, LibBlockNames.TINY_POTATO);
setHardness(0.25F);
}
@Nonnull
@Override
public BlockStateContainer createBlockState() {
return new BlockStateContainer(this, BotaniaStateProps.CARDINALS);
}
@Override
protected IBlockState pickDefaultState() {
return blockState.getBaseState()
.withProperty(BotaniaStateProps.CARDINALS, EnumFacing.SOUTH);
}
@Override
public int getMetaFromState(IBlockState state) {
switch (state.getValue(BotaniaStateProps.CARDINALS)) {
case NORTH: return 0;
case WEST: return 3;
case EAST: return 1;
case SOUTH:
default:
return 2; // wai vazkii senpai
}
}
@Nonnull
@Override
public IBlockState getStateFromMeta(int meta) {
EnumFacing side;
switch (meta) {
case 3: side = EnumFacing.WEST; break;
case 0: side = EnumFacing.NORTH; break;
case 1: side = EnumFacing.EAST; break;
case 2:
default:
side = EnumFacing.SOUTH; break;
}
return getDefaultState().withProperty(BotaniaStateProps.CARDINALS, side);
}
@Override
public void breakBlock(@Nonnull World world, @Nonnull BlockPos pos, @Nonnull IBlockState state) {
TileSimpleInventory inv = (TileSimpleInventory) world.getTileEntity(pos);
InventoryHelper.dropInventory(inv, world, state, pos);
super.breakBlock(world, pos, state);
}
@Nonnull
@Override
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess world, BlockPos pos) {
return AABB;
}
@Override
public void registerItemForm() {
GameRegistry.register(new ItemBlockTinyPotato(this), getRegistryName());
}
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing par6, float par7, float par8, float par9) {
TileEntity tile = world.getTileEntity(pos);
if(tile instanceof TileTinyPotato) {
((TileTinyPotato) tile).interact(player, hand, player.getHeldItem(hand), par6);
player.addStat(ModAchievements.tinyPotatoPet, 1);
world.spawnParticle(EnumParticleTypes.HEART, pos.getX() + AABB.minX + Math.random() * (AABB.maxX - AABB.minX), pos.getY() + AABB.maxY, pos.getZ() + AABB.minZ + Math.random() * (AABB.maxZ - AABB.minZ), 0, 0 ,0);
}
return true;
}
@Override
public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase par5EntityLiving, ItemStack par6ItemStack) {
world.setBlockState(pos, state.withProperty(BotaniaStateProps.CARDINALS, par5EntityLiving.getHorizontalFacing().getOpposite()));
if (par6ItemStack.hasDisplayName())
((TileTinyPotato) world.getTileEntity(pos)).name = par6ItemStack.getDisplayName();
}
@Override
public boolean removedByPlayer(@Nonnull IBlockState state, World world, @Nonnull BlockPos pos, @Nonnull EntityPlayer player, boolean willHarvest) {
if (willHarvest) {
// Copy of super.removedByPlayer but don't set to air yet
// This is so getDrops below will have a TE to work with
onBlockHarvested(world, pos, state, player);
return true;
} else {
return super.removedByPlayer(state, world, pos, player, willHarvest);
}
}
@Override
public void harvestBlock(@Nonnull World world, EntityPlayer player, @Nonnull BlockPos pos, @Nonnull IBlockState state, TileEntity te, ItemStack stack) {
super.harvestBlock(world, player, pos, state, te, stack);
// Now delete the block and TE
world.setBlockToAir(pos);
}
@Nonnull
@Override
public List<ItemStack> getDrops(IBlockAccess world, BlockPos pos, @Nonnull IBlockState state, int fortune) {
ArrayList<ItemStack> list = new ArrayList<>();
TileEntity tile = world.getTileEntity(pos);
if(tile != null) {
ItemStack stack = new ItemStack(this);
String name = ((TileTinyPotato) tile).name;
if(!name.isEmpty())
stack.setStackDisplayName(name);
list.add(stack);
}
return list;
}
@Override
public boolean isOpaqueCube(IBlockState state) {
return false;
}
@Override
public boolean isFullCube(IBlockState state) {
return false;
}
@Nonnull
@Override
public EnumBlockRenderType getRenderType(IBlockState state) {
return EnumBlockRenderType.ENTITYBLOCK_ANIMATED;
}
@Override
public boolean hasTileEntity(IBlockState state) {
return true;
}
@Nonnull
@Override
public TileEntity createTileEntity(@Nonnull World world, @Nonnull IBlockState state) {
return new TileTinyPotato();
}
@Override
public LexiconEntry getEntry(World world, BlockPos pos, EntityPlayer player, ItemStack lexicon) {
return LexiconData.tinyPotato;
}
}