2012-01-26 23:37:39 +01:00
|
|
|
package cpw.mods.ironchest;
|
|
|
|
|
2012-01-29 02:55:20 +01:00
|
|
|
import java.util.Random;
|
|
|
|
|
2012-01-26 23:37:39 +01:00
|
|
|
import net.minecraft.src.BlockContainer;
|
2012-01-29 02:55:20 +01:00
|
|
|
import net.minecraft.src.EntityItem;
|
2012-01-26 23:37:39 +01:00
|
|
|
import net.minecraft.src.EntityLiving;
|
2012-01-28 01:07:39 +01:00
|
|
|
import net.minecraft.src.EntityPlayer;
|
2012-01-26 23:37:39 +01:00
|
|
|
import net.minecraft.src.IBlockAccess;
|
2012-01-29 02:55:20 +01:00
|
|
|
import net.minecraft.src.ItemStack;
|
2012-01-26 23:37:39 +01:00
|
|
|
import net.minecraft.src.Material;
|
|
|
|
import net.minecraft.src.MathHelper;
|
2012-01-29 02:55:20 +01:00
|
|
|
import net.minecraft.src.NBTTagCompound;
|
2012-01-26 23:37:39 +01:00
|
|
|
import net.minecraft.src.TileEntity;
|
|
|
|
import net.minecraft.src.World;
|
2012-01-28 01:07:39 +01:00
|
|
|
import net.minecraft.src.mod_IronChest;
|
2012-01-26 23:37:39 +01:00
|
|
|
import net.minecraft.src.forge.ITextureProvider;
|
|
|
|
|
|
|
|
public class BlockIronChest extends BlockContainer implements ITextureProvider {
|
|
|
|
|
2012-01-29 02:55:20 +01:00
|
|
|
private Random random;
|
2012-01-26 23:37:39 +01:00
|
|
|
public BlockIronChest(int id) {
|
|
|
|
super(id, Material.iron);
|
2012-01-27 22:05:42 +01:00
|
|
|
setBlockName("IronChest");
|
2012-01-26 23:37:39 +01:00
|
|
|
setHardness(3.0F);
|
2012-01-29 02:55:20 +01:00
|
|
|
random=new Random();
|
2012-01-26 23:37:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public TileEntity getBlockEntity() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getTextureFile() {
|
2012-01-29 17:30:49 +01:00
|
|
|
return "cpw/mods/ironchest/sprites/block_textures.png";
|
2012-01-26 23:37:39 +01:00
|
|
|
}
|
|
|
|
|
2012-01-27 22:38:21 +01:00
|
|
|
@Override
|
|
|
|
public boolean isOpaqueCube() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean renderAsNormalBlock() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public int getRenderType() {
|
|
|
|
return 22;
|
|
|
|
}
|
2012-01-26 23:37:39 +01:00
|
|
|
@Override
|
|
|
|
public TileEntity getBlockEntity(int metadata) {
|
2012-01-27 05:50:52 +01:00
|
|
|
return IronChestType.makeEntity(metadata);
|
2012-01-26 23:37:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getBlockTexture(IBlockAccess worldAccess, int i, int j, int k, int l) {
|
|
|
|
TileEntity te = worldAccess.getBlockTileEntity(i, j, k);
|
|
|
|
if (te != null && te instanceof TileEntityIronChest) {
|
|
|
|
TileEntityIronChest icte=(TileEntityIronChest) te;
|
|
|
|
if (l==0 || l==1) { // Top and Bottom
|
|
|
|
return icte.getType().getTextureRow()*16+1;
|
2012-01-27 05:50:52 +01:00
|
|
|
} else if (l==icte.getFacing()) { // Front
|
2012-01-26 23:37:39 +01:00
|
|
|
return icte.getType().getTextureRow()*16+2;
|
2012-01-27 05:50:52 +01:00
|
|
|
} else { // Back and Sides
|
|
|
|
return icte.getType().getTextureRow()*16;
|
2012-01-26 23:37:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-27 05:50:52 +01:00
|
|
|
@Override
|
|
|
|
public int getBlockTextureFromSideAndMetadata(int i, int j) {
|
|
|
|
IronChestType typ=IronChestType.values()[j];
|
|
|
|
switch (i) {
|
2012-01-26 23:37:39 +01:00
|
|
|
case 0:
|
|
|
|
case 1:
|
2012-01-27 05:50:52 +01:00
|
|
|
return typ.getTextureRow()*16+1;
|
2012-01-27 22:05:42 +01:00
|
|
|
case 3:
|
2012-01-27 05:50:52 +01:00
|
|
|
return typ.getTextureRow()*16+2;
|
2012-01-26 23:37:39 +01:00
|
|
|
default:
|
2012-01-27 05:50:52 +01:00
|
|
|
return typ.getTextureRow()*16;
|
2012-01-26 23:37:39 +01:00
|
|
|
}
|
|
|
|
}
|
2012-01-28 01:07:39 +01:00
|
|
|
@Override
|
|
|
|
public boolean blockActivated(World world, int i, int j, int k, EntityPlayer player) {
|
|
|
|
TileEntity te = world.getBlockTileEntity(i, j, k);
|
|
|
|
|
|
|
|
if(te == null || !(te instanceof TileEntityIronChest))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(world.isBlockSolidOnSide(i, j + 1, k, 0))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (world.isRemote) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
mod_IronChest.openGUI(player, (TileEntityIronChest)te);
|
|
|
|
return true;
|
|
|
|
}
|
2012-01-27 05:50:52 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBlockAdded(World world, int i, int j, int k) {
|
|
|
|
super.onBlockAdded(world, i, j, k);
|
|
|
|
world.markBlockNeedsUpdate(i, j, k);
|
|
|
|
}
|
|
|
|
|
2012-01-26 23:37:39 +01:00
|
|
|
@Override
|
|
|
|
public void onBlockPlacedBy(World world, int i, int j, int k, EntityLiving entityliving) {
|
|
|
|
byte chestFacing = 0;
|
|
|
|
int facing = MathHelper.floor_double((double) ((entityliving.rotationYaw * 4F) / 360F) + 0.5D) & 3;
|
|
|
|
if (facing == 0) {
|
|
|
|
chestFacing = 2;
|
|
|
|
}
|
|
|
|
if (facing == 1) {
|
|
|
|
chestFacing = 5;
|
|
|
|
}
|
|
|
|
if (facing == 2) {
|
|
|
|
chestFacing = 3;
|
|
|
|
}
|
|
|
|
if (facing == 3) {
|
|
|
|
chestFacing = 4;
|
|
|
|
}
|
|
|
|
TileEntity te = world.getBlockTileEntity(i, j, k);
|
|
|
|
if (te != null && te instanceof TileEntityIronChest) {
|
|
|
|
((TileEntityIronChest) te).setFacing(chestFacing);
|
2012-01-27 05:50:52 +01:00
|
|
|
world.markBlockNeedsUpdate(i, j, k);
|
2012-01-26 23:37:39 +01:00
|
|
|
}
|
|
|
|
}
|
2012-01-27 22:05:42 +01:00
|
|
|
@Override
|
|
|
|
protected int damageDropped(int i) {
|
|
|
|
return i;
|
|
|
|
}
|
2012-01-29 02:55:20 +01:00
|
|
|
|
|
|
|
public void onBlockRemoval(World world, int i, int j, int k)
|
|
|
|
{
|
2012-01-29 17:30:49 +01:00
|
|
|
TileEntityIronChest tileentitychest = (TileEntityIronChest)world.getBlockTileEntity(i, j, k);
|
2012-01-29 02:55:20 +01:00
|
|
|
if (tileentitychest != null)
|
|
|
|
{
|
|
|
|
for (int l = 0; l < tileentitychest.getSizeInventory(); l++)
|
|
|
|
{
|
|
|
|
ItemStack itemstack = tileentitychest.getStackInSlot(l);
|
|
|
|
if (itemstack == null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
float f = random.nextFloat() * 0.8F + 0.1F;
|
|
|
|
float f1 = random.nextFloat() * 0.8F + 0.1F;
|
|
|
|
float f2 = random.nextFloat() * 0.8F + 0.1F;
|
|
|
|
while (itemstack.stackSize > 0)
|
|
|
|
{
|
|
|
|
int i1 = random.nextInt(21) + 10;
|
|
|
|
if (i1 > itemstack.stackSize)
|
|
|
|
{
|
|
|
|
i1 = itemstack.stackSize;
|
|
|
|
}
|
|
|
|
itemstack.stackSize -= i1;
|
|
|
|
EntityItem entityitem = new EntityItem(world, (float)i + f, (float)j + f1, (float)k + f2, new ItemStack(itemstack.itemID, i1, 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.item.setTagCompound((NBTTagCompound)itemstack.getTagCompound().cloneTag());
|
|
|
|
}
|
|
|
|
world.spawnEntityInWorld(entityitem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
super.onBlockRemoval(world, i, j, k);
|
|
|
|
}
|
2012-01-26 23:37:39 +01:00
|
|
|
}
|