equivalent-exchange-3/src/main/java/com/pahimar/ee3/block/BlockCalcinator.java

152 lines
4 KiB
Java
Raw Normal View History

2013-08-23 16:59:50 +02:00
package com.pahimar.ee3.block;
import com.pahimar.ee3.EquivalentExchange3;
import com.pahimar.ee3.lib.GuiIds;
import com.pahimar.ee3.lib.RenderIds;
import com.pahimar.ee3.lib.Strings;
import com.pahimar.ee3.tileentity.TileCalcinator;
2013-08-23 16:59:50 +02:00
import net.minecraft.block.material.Material;
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.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import java.util.Random;
2013-08-23 16:59:50 +02:00
/**
* Equivalent-Exchange-3
* <p/>
2013-08-23 16:59:50 +02:00
* BlockCalcinator
*
2013-08-23 16:59:50 +02:00
* @author pahimar
* @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
*/
public class BlockCalcinator extends BlockEE
{
2013-08-23 16:59:50 +02:00
/**
* Is the random generator used by calcinator to drop the inventory contents
* in random directions.
*/
private Random rand = new Random();
public BlockCalcinator(int id)
{
2013-08-23 16:59:50 +02:00
super(id, Material.rock);
this.setUnlocalizedName(Strings.CALCINATOR_NAME);
this.setCreativeTab(EquivalentExchange3.tabsEE3);
this.setHardness(5F);
this.setBlockBounds(0.1F, 0.0F, 0.1F, 0.9F, 1.0F, 0.9F);
}
2013-09-07 04:47:12 +02:00
@Override
public String getUnlocalizedName()
{
return String.format("tile.%s%s", Strings.RESOURCE_PREFIX, Strings.CALCINATOR_NAME);
}
2013-08-23 16:59:50 +02:00
@Override
public TileEntity createNewTileEntity(World world)
{
2013-08-23 16:59:50 +02:00
return new TileCalcinator();
}
@Override
public boolean renderAsNormalBlock()
{
2013-08-23 16:59:50 +02:00
return false;
}
@Override
public boolean isOpaqueCube()
{
2013-08-23 16:59:50 +02:00
return false;
}
@Override
public int getRenderType()
{
2013-08-23 16:59:50 +02:00
return RenderIds.calcinatorRender;
2013-08-23 16:59:50 +02:00
}
@Override
public void breakBlock(World world, int x, int y, int z, int id, int meta)
{
2013-08-23 16:59:50 +02:00
dropInventory(world, x, y, z);
super.breakBlock(world, x, y, z, id, meta);
}
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9)
{
2013-08-23 16:59:50 +02:00
if (player.isSneaking())
{
2013-08-23 16:59:50 +02:00
return false;
}
else
{
if (!world.isRemote)
{
2013-08-23 16:59:50 +02:00
TileCalcinator tileCalcinator = (TileCalcinator) world.getBlockTileEntity(x, y, z);
if (tileCalcinator != null)
{
2013-08-23 16:59:50 +02:00
player.openGui(EquivalentExchange3.instance, GuiIds.CALCINATOR, world, x, y, z);
}
}
return true;
}
}
private void dropInventory(World world, int x, int y, int z)
{
2013-08-23 16:59:50 +02:00
TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
if (!(tileEntity instanceof IInventory))
{
2013-08-23 16:59:50 +02:00
return;
}
2013-08-23 16:59:50 +02:00
IInventory inventory = (IInventory) tileEntity;
for (int i = 0; i < inventory.getSizeInventory(); i++)
{
2013-08-23 16:59:50 +02:00
ItemStack itemStack = inventory.getStackInSlot(i);
if (itemStack != null && itemStack.stackSize > 0)
{
2013-08-23 16:59:50 +02:00
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, new ItemStack(itemStack.itemID, itemStack.stackSize, itemStack.getItemDamage()));
if (itemStack.hasTagCompound())
{
2013-08-23 16:59:50 +02:00
entityItem.getEntityItem().setTagCompound((NBTTagCompound) 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;
}
}
}
}