TinkersConstruct/mods/tinker/common/InventoryBlock.java

161 lines
4.5 KiB
Java
Raw Normal View History

2013-03-11 00:05:11 -07:00
package mods.tinker.common;
2013-01-18 23:43:53 -08:00
2013-03-24 16:52:53 -07:00
import java.util.Random;
2013-01-18 23:43:53 -08:00
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
2013-03-11 00:05:11 -07:00
import net.minecraft.client.renderer.texture.IconRegister;
2013-01-18 23:43:53 -08:00
import net.minecraft.entity.EntityLiving;
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;
2013-03-11 00:05:11 -07:00
import net.minecraft.util.Icon;
2013-01-18 23:43:53 -08:00
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
public abstract class InventoryBlock extends BlockContainer
{
2013-03-24 16:52:53 -07:00
protected Random rand = new Random();
2013-01-18 23:43:53 -08:00
protected InventoryBlock(int id, Material material)
{
super(id, material);
}
/* Logic backend */
2013-03-13 21:36:17 -07:00
public TileEntity createNewTileEntity (World var1)
{ return null; }
public abstract TileEntity createTileEntity(World world, int metadata);
2013-01-18 23:43:53 -08:00
public abstract Integer getGui(World world, int x, int y, int z, EntityPlayer entityplayer);
public abstract Object getModInstance();
2013-03-13 21:36:17 -07:00
/*public void onBlockAdded(World par1World, int x, int y, int z)
{
System.out.println("Added");
//super.onBlockAdded(par1World, x, y, z);
par1World.setBlockTileEntity(x, y, z, this.createTileEntity(par1World, par1World.getBlockMetadata(x, y, z)));
}*/
2013-01-18 23:43:53 -08:00
@Override
public boolean onBlockActivated (World world, int x, int y, int z, EntityPlayer player, int side, float clickX, float clickY, float clickZ)
{
if (player.isSneaking())
return false;
Integer integer = getGui(world, x, y, z, player);
if (integer == null || integer == -1)
{
return false;
}
else
{
player.openGui(getModInstance(), integer, world, x, y, z);
return true;
}
}
/* Inventory */
@Override
public void breakBlock (World par1World, int x, int y, int z, int par5, int par6)
{
2013-02-27 14:13:27 -08:00
TileEntity te = par1World.getBlockTileEntity(x, y, z);
2013-01-18 23:43:53 -08:00
2013-02-27 14:13:27 -08:00
if (te != null && te instanceof InventoryLogic)
2013-01-18 23:43:53 -08:00
{
2013-02-27 14:13:27 -08:00
InventoryLogic logic = (InventoryLogic) te;
2013-01-18 23:43:53 -08:00
for (int iter = 0; iter < logic.getSizeInventory(); ++iter)
{
ItemStack stack = logic.getStackInSlot(iter);
2013-01-24 23:54:11 -08:00
if (stack != null && logic.canDropInventorySlot(iter))
2013-01-18 23:43:53 -08:00
{
2013-03-24 16:52:53 -07:00
float jumpX = rand.nextFloat() * 0.8F + 0.1F;
float jumpY = rand.nextFloat() * 0.8F + 0.1F;
float jumpZ = rand.nextFloat() * 0.8F + 0.1F;
2013-01-18 23:43:53 -08:00
while (stack.stackSize > 0)
{
2013-03-24 16:52:53 -07:00
int itemSize = rand.nextInt(21) + 10;
2013-01-18 23:43:53 -08:00
if (itemSize > stack.stackSize)
{
itemSize = stack.stackSize;
}
stack.stackSize -= itemSize;
EntityItem entityitem = new EntityItem(par1World, (double) ((float) x + jumpX), (double) ((float) y + jumpY), (double) ((float) z + jumpZ),
new ItemStack(stack.itemID, itemSize, stack.getItemDamage()));
if (stack.hasTagCompound())
{
2013-03-11 00:05:11 -07:00
entityitem.getEntityItem().setTagCompound((NBTTagCompound) stack.getTagCompound().copy());
2013-01-18 23:43:53 -08:00
}
float offset = 0.05F;
2013-03-24 16:52:53 -07:00
entityitem.motionX = (double) ((float) rand.nextGaussian() * offset);
entityitem.motionY = (double) ((float) rand.nextGaussian() * offset + 0.2F);
entityitem.motionZ = (double) ((float) rand.nextGaussian() * offset);
2013-01-18 23:43:53 -08:00
par1World.spawnEntityInWorld(entityitem);
}
}
}
}
super.breakBlock(par1World, x, y, z, par5, par6);
}
2013-03-11 00:05:11 -07:00
/* Placement */
2013-01-18 23:43:53 -08:00
@Override
2013-03-11 00:05:11 -07:00
public void onBlockPlacedBy(World world, int x, int y, int z, EntityLiving entityliving, ItemStack stack)
2013-01-18 23:43:53 -08:00
{
TileEntity logic = world.getBlockTileEntity(x, y, z);
if (logic instanceof IFacingLogic)
{
IFacingLogic direction = (IFacingLogic) logic;
if (entityliving == null)
{
2013-02-27 14:13:27 -08:00
direction.setDirection(0F, 0F, null);
2013-01-18 23:43:53 -08:00
}
else
{
2013-02-27 14:13:27 -08:00
direction.setDirection(entityliving.rotationYaw * 4F, entityliving.rotationPitch, entityliving);
2013-01-18 23:43:53 -08:00
}
}
}
public static boolean isActive(IBlockAccess world, int x, int y, int z)
{
TileEntity logic = world.getBlockTileEntity(x, y, z);
if (logic instanceof IActiveLogic)
{
return ((IActiveLogic)logic).getActive();
}
return false;
}
2013-02-27 14:13:27 -08:00
public int damageDropped (int meta)
{
return meta;
}
2013-03-11 00:05:11 -07:00
/* Textures */
public Icon[] icons;
public abstract String[] getTextureNames();
2013-03-21 17:38:04 -07:00
@Override
public void registerIcons(IconRegister iconRegister)
2013-03-11 00:05:11 -07:00
{
String[] textureNames = getTextureNames();
this.icons = new Icon[textureNames.length];
for (int i = 0; i < this.icons.length; ++i)
{
2013-03-21 17:38:04 -07:00
this.icons[i] = iconRegister.registerIcon("tinker:"+textureNames[i]);
2013-03-11 00:05:11 -07:00
}
}
2013-01-18 23:43:53 -08:00
}