/** * This class was created by . 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 @ [Feb 2, 2014, 2:10:14 PM (GMT)] */ package vazkii.botania.common.block.mana; import net.minecraft.block.SoundType; 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.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import vazkii.botania.api.BotaniaAPI; import vazkii.botania.api.lexicon.ILexiconable; import vazkii.botania.api.lexicon.LexiconEntry; import vazkii.botania.api.wand.IWandable; import vazkii.botania.common.block.BlockMod; import vazkii.botania.common.block.tile.TileRuneAltar; import vazkii.botania.common.block.tile.TileSimpleInventory; import vazkii.botania.common.lexicon.LexiconData; import vazkii.botania.common.lib.LibBlockNames; import javax.annotation.Nonnull; import java.util.Random; public class BlockRuneAltar extends BlockMod implements IWandable, ILexiconable { private static final AxisAlignedBB AABB = new AxisAlignedBB(0, 0, 0, 1, 0.75, 1); private final Random random = new Random(); public BlockRuneAltar() { super(Material.ROCK, LibBlockNames.RUNE_ALTAR); setHardness(2.0F); setResistance(10.0F); setSoundType(SoundType.STONE); BotaniaAPI.blacklistBlockFromMagnet(this, Short.MAX_VALUE); } @Nonnull @Override public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess world, BlockPos pos) { return AABB; } @Override public boolean isOpaqueCube(IBlockState state) { return false; } @Override public boolean isFullCube(IBlockState state) { return false; } @Override public boolean onBlockActivated(World par1World, BlockPos pos, IBlockState state, EntityPlayer par5EntityPlayer, EnumHand hand, ItemStack stack, EnumFacing side, float par7, float par8, float par9) { TileRuneAltar altar = (TileRuneAltar) par1World.getTileEntity(pos); if(par5EntityPlayer.isSneaking()) { if(altar.manaToGet == 0) for(int i = altar.getSizeInventory() - 1; i >= 0; i--) { ItemStack stackAt = altar.getItemHandler().getStackInSlot(i); if(stackAt != null) { ItemStack copy = stackAt.copy(); if(!par5EntityPlayer.inventory.addItemStackToInventory(copy)) par5EntityPlayer.dropItem(copy, false); altar.getItemHandler().setStackInSlot(i, null); par1World.updateComparatorOutputLevel(pos, this); break; } } } else if(altar.isEmpty() && stack == null) altar.trySetLastRecipe(par5EntityPlayer); else if(stack != null) return altar.addItem(par5EntityPlayer, stack); return false; } @Override public void breakBlock(@Nonnull World par1World, @Nonnull BlockPos pos, @Nonnull IBlockState state) { TileSimpleInventory inv = (TileSimpleInventory) par1World.getTileEntity(pos); if (inv != null) { for (int j1 = 0; j1 < inv.getSizeInventory(); ++j1) { ItemStack itemstack = inv.getItemHandler().getStackInSlot(j1); if (itemstack != null) { float f = random.nextFloat() * 0.8F + 0.1F; float f1 = random.nextFloat() * 0.8F + 0.1F; EntityItem entityitem; for (float f2 = random.nextFloat() * 0.8F + 0.1F; itemstack.stackSize > 0; par1World.spawnEntityInWorld(entityitem)) { int k1 = random.nextInt(21) + 10; if (k1 > itemstack.stackSize) k1 = itemstack.stackSize; itemstack.stackSize -= k1; entityitem = new EntityItem(par1World, pos.getX() + f, pos.getY() + f1, pos.getZ() + f2, new ItemStack(itemstack.getItem(), k1, itemstack.getItemDamage())); float f3 = 0.05F; entityitem.motionX = (float)random.nextGaussian() * f3; entityitem.motionY = (float)random.nextGaussian() * f3 + 0.2F; entityitem.motionZ = (float)random.nextGaussian() * f3; if (itemstack.hasTagCompound()) entityitem.getEntityItem().setTagCompound((NBTTagCompound)itemstack.getTagCompound().copy()); } } } par1World.updateComparatorOutputLevel(pos, state.getBlock()); } super.breakBlock(par1World, pos, state); } @Override public boolean hasTileEntity(IBlockState state) { return true; } @Nonnull @Override public TileEntity createTileEntity(@Nonnull World world, @Nonnull IBlockState state) { return new TileRuneAltar(); } @Override public boolean hasComparatorInputOverride(IBlockState state) { return true; } @Override public int getComparatorInputOverride(IBlockState state, World par1World, BlockPos pos) { TileRuneAltar altar = (TileRuneAltar) par1World.getTileEntity(pos); return altar.signal; } @Override public boolean onUsedByWand(EntityPlayer player, ItemStack stack, World world, BlockPos pos, EnumFacing side) { ((TileRuneAltar) world.getTileEntity(pos)).onWanded(player, stack); return true; } @Override public LexiconEntry getEntry(World world, BlockPos pos, EntityPlayer player, ItemStack lexicon) { return LexiconData.runicAltar; } }