Package vazkii.botania.common.block

Source Code of vazkii.botania.common.block.BlockSpreader

/**
* 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 a
* Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License
* (http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_GB)
*
* File Created @ [Jan 25, 2014, 9:38:23 PM (GMT)]
*/
package vazkii.botania.common.block;

import java.util.List;
import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.block.BlockPistonBase;
import net.minecraft.block.material.Material;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import vazkii.botania.api.lexicon.ILexiconable;
import vazkii.botania.api.lexicon.LexiconEntry;
import vazkii.botania.api.mana.ILens;
import vazkii.botania.api.wand.IWandHUD;
import vazkii.botania.api.wand.IWandable;
import vazkii.botania.api.wand.IWireframeAABBProvider;
import vazkii.botania.client.lib.LibRenderIDs;
import vazkii.botania.common.block.tile.TileSimpleInventory;
import vazkii.botania.common.block.tile.mana.TileSpreader;
import vazkii.botania.common.item.ModItems;
import vazkii.botania.common.item.block.ItemBlockWithMetadataAndName;
import vazkii.botania.common.lexicon.LexiconData;
import vazkii.botania.common.lib.LibBlockNames;
import cpw.mods.fml.common.registry.GameRegistry;

public class BlockSpreader extends BlockModContainer implements IWandable, IWandHUD, ILexiconable, IWireframeAABBProvider {

  Random random;

  protected BlockSpreader() {
    super(Material.wood);
    setHardness(2.0F);
    setStepSound(soundTypeWood);
    setBlockName(LibBlockNames.SPREADER);

    random = new Random();
  }

  @Override
  protected boolean shouldRegisterInNameSet() {
    return false;
  }

  @Override
  public Block setBlockName(String par1Str) {
    GameRegistry.registerBlock(this, ItemBlockWithMetadataAndName.class, par1Str);
    return super.setBlockName(par1Str);
  }

  @Override
  public void registerBlockIcons(IIconRegister par1IconRegister) {
    // NO-OP
  }

  @Override
  public void getSubBlocks(Item par1, CreativeTabs par2, List par3) {
    for(int i = 0; i < 4; i++)
      par3.add(new ItemStack(par1, 1, i));
  }

  @Override
  public void onBlockPlacedBy(World par1World, int par2, int par3, int par4, EntityLivingBase par5EntityLivingBase, ItemStack par6ItemStack) {
    int orientation = BlockPistonBase.determineOrientation(par1World, par2, par3, par4, par5EntityLivingBase);
    TileSpreader spreader = (TileSpreader) par1World.getTileEntity(par2, par3, par4);
    par1World.setBlockMetadataWithNotify(par2, par3, par4, par6ItemStack.getItemDamage(), 1 | 2);

    switch(orientation) {
    case 0:
      spreader.rotationY = -90F;
      break;
    case 1:
      spreader.rotationY = 90F;
      break;
    case 2:
      spreader.rotationX = 270F;
      break;
    case 3:
      spreader.rotationX = 90F;
      break;
    case 4:
      break;
    default:
      spreader.rotationX = 180F;
      break;
    }
  }

  @Override
  public int damageDropped(int par1) {
    return par1;
  }

  @Override
  public boolean isOpaqueCube() {
    return false;
  }

  @Override
  public boolean renderAsNormalBlock() {
    return false;
  }

  @Override
  public IIcon getIcon(int par1, int par2) {
    return par2 >= 2 ? ModBlocks.dreamwood.getIcon(par1, 0) : ModBlocks.livingwood.getIcon(par1, 0);
  }

  @Override
  public int getRenderType() {
    return LibRenderIDs.idSpreader;
  }

  @Override
  public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9) {
    TileEntity tile = par1World.getTileEntity(par2, par3, par4);
    if(!(tile instanceof TileSpreader))
      return false;

    TileSpreader spreader = (TileSpreader) tile;
    ItemStack lens = spreader.getStackInSlot(0);
    ItemStack heldItem = par5EntityPlayer.getCurrentEquippedItem();
    boolean isHeldItemLens = heldItem != null && heldItem.getItem() instanceof ILens;

    if(heldItem != null)
      if(heldItem.getItem() == ModItems.twigWand)
        return false;

    if(lens == null && isHeldItemLens) {
      par5EntityPlayer.inventory.setInventorySlotContents(par5EntityPlayer.inventory.currentItem, null);
      spreader.setInventorySlotContents(0, heldItem.copy());
      spreader.markDirty();
    } else if(lens != null) {
      ItemStack add = lens.copy();
      if(!par5EntityPlayer.inventory.addItemStackToInventory(add))
        par5EntityPlayer.dropPlayerItemWithRandomChoice(add, false);
      spreader.setInventorySlotContents(0, null);
      spreader.markDirty();
    }

    return true;
  }

  @Override
  public void breakBlock(World par1World, int par2, int par3, int par4, Block par5, int par6) {
    TileEntity tile = par1World.getTileEntity(par2, par3, par4);
    if(!(tile instanceof TileSimpleInventory))
      return;

    TileSimpleInventory inv = (TileSimpleInventory) tile;

    if (inv != null) {
      for (int j1 = 0; j1 < inv.getSizeInventory(); ++j1) {
        ItemStack itemstack = inv.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, par2 + f, par3 + f1, par4 + 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.func_147453_f(par2, par3, par4, par5);
    }

    super.breakBlock(par1World, par2, par3, par4, par5, par6);
  }


  @Override
  public boolean onUsedByWand(EntityPlayer player, ItemStack stack, World world, int x, int y, int z, int side) {
    ((TileSpreader) world.getTileEntity(x, y, z)).onWanded(player, stack);
    return true;
  }

  @Override
  public TileEntity createNewTileEntity(World world, int meta) {
    return new TileSpreader();
  }

  @Override
  public void renderHUD(Minecraft mc, ScaledResolution res, World world, int x, int y, int z) {
    ((TileSpreader) world.getTileEntity(x, y, z)).renderHUD(mc, res);
  }

  @Override
  public LexiconEntry getEntry(World world, int x, int y, int z, EntityPlayer player, ItemStack lexicon) {
    int meta = world.getBlockMetadata(x, y, z);
    return meta == 0 ? LexiconData.spreader : meta == 1 ? LexiconData.redstoneSpreader : LexiconData.dreamwoodSpreader;
  }

  @Override
  public AxisAlignedBB getWireframeAABB(World world, int x, int y, int z) {
    float f = 1F / 16F;
    return AxisAlignedBB.getBoundingBox(x + f, y + f, z + f, x + 1 - f, y + 1 - f, z + 1 - f);
  }

}
TOP

Related Classes of vazkii.botania.common.block.BlockSpreader

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.