2014-01-13 03:23:34 +01:00
|
|
|
package mekanism.common.block;
|
|
|
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
import mekanism.common.Mekanism;
|
|
|
|
import mekanism.common.item.ItemBlockCardboardBox;
|
|
|
|
import mekanism.common.tile.TileEntityCardboardBox;
|
2014-01-14 03:43:36 +01:00
|
|
|
import net.minecraft.block.Block;
|
2014-01-13 03:23:34 +01:00
|
|
|
import net.minecraft.block.BlockContainer;
|
|
|
|
import net.minecraft.block.material.Material;
|
2014-04-20 05:34:19 +02:00
|
|
|
import net.minecraft.client.renderer.texture.IIconRegister;
|
2014-01-13 03:23:34 +01:00
|
|
|
import net.minecraft.entity.item.EntityItem;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2014-04-20 22:15:44 +02:00
|
|
|
import net.minecraft.item.Item;
|
2014-01-13 03:23:34 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2014-04-20 05:34:19 +02:00
|
|
|
import net.minecraft.util.IIcon;
|
2014-01-13 03:23:34 +01:00
|
|
|
import net.minecraft.util.MovingObjectPosition;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
|
|
|
|
|
|
|
public class BlockCardboardBox extends BlockContainer
|
|
|
|
{
|
2014-04-20 05:34:19 +02:00
|
|
|
public IIcon[] icons = new IIcon[6];
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-04-20 22:15:44 +02:00
|
|
|
public BlockCardboardBox()
|
2014-01-13 03:23:34 +01:00
|
|
|
{
|
2014-04-20 22:15:44 +02:00
|
|
|
super(Material.cloth);
|
2014-01-13 03:23:34 +01:00
|
|
|
setCreativeTab(Mekanism.tabMekanism);
|
|
|
|
setHardness(0.5F);
|
|
|
|
setResistance(1F);
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-13 03:23:34 +01:00
|
|
|
@Override
|
|
|
|
@SideOnly(Side.CLIENT)
|
2014-04-20 22:15:44 +02:00
|
|
|
public void registerBlockIcons(IIconRegister register)
|
2014-01-13 03:23:34 +01:00
|
|
|
{
|
|
|
|
icons[0] = register.registerIcon("mekanism:CardboardBoxTop");
|
|
|
|
icons[1] = register.registerIcon("mekanism:CardboardBoxSide");
|
|
|
|
icons[2] = register.registerIcon("mekanism:CardboardBoxSideStorage");
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-13 03:23:34 +01:00
|
|
|
@Override
|
|
|
|
@SideOnly(Side.CLIENT)
|
2014-04-20 05:34:19 +02:00
|
|
|
public IIcon getIcon(int side, int meta)
|
2014-01-13 03:23:34 +01:00
|
|
|
{
|
|
|
|
if(side == 0 || side == 1)
|
|
|
|
{
|
|
|
|
return icons[0];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return meta == 0 ? icons[1] : icons[2];
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer entityplayer, int facing, float hitX, float hitY, float hitZ)
|
|
|
|
{
|
|
|
|
if(!world.isRemote && entityplayer.isSneaking())
|
|
|
|
{
|
|
|
|
ItemStack itemStack = new ItemStack(Mekanism.CardboardBox);
|
2014-04-20 05:34:19 +02:00
|
|
|
TileEntityCardboardBox tileEntity = (TileEntityCardboardBox)world.getTileEntity(x, y, z);
|
2014-03-08 02:00:25 +01:00
|
|
|
|
|
|
|
if(tileEntity.storedData != null)
|
|
|
|
{
|
|
|
|
BlockData data = tileEntity.storedData;
|
2014-06-09 16:19:36 +02:00
|
|
|
|
|
|
|
if(!data.block.canPlaceBlockAt(world, x, y, z))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-04-20 22:15:44 +02:00
|
|
|
if(data.block != null)
|
2014-03-08 02:00:25 +01:00
|
|
|
{
|
2014-06-09 16:19:36 +02:00
|
|
|
data.meta = data.block.onBlockPlaced(world, x, y, z, facing, hitX, hitY, hitZ, data.meta);
|
2014-03-08 02:00:25 +01:00
|
|
|
}
|
|
|
|
|
2014-04-20 22:15:44 +02:00
|
|
|
world.setBlock(x, y, z, data.block, data.meta, 3);
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-04-20 05:34:19 +02:00
|
|
|
if(data.tileTag != null && world.getTileEntity(x, y, z) != null)
|
2014-03-08 02:00:25 +01:00
|
|
|
{
|
|
|
|
data.updateLocation(x, y, z);
|
2014-04-20 05:34:19 +02:00
|
|
|
world.getTileEntity(x, y, z).readFromNBT(data.tileTag);
|
2014-03-08 02:00:25 +01:00
|
|
|
}
|
|
|
|
|
2014-04-20 22:15:44 +02:00
|
|
|
if(data.block != null)
|
2014-03-08 02:00:25 +01:00
|
|
|
{
|
2014-04-20 22:15:44 +02:00
|
|
|
data.block.onBlockPlacedBy(world, x, y, z, entityplayer, new ItemStack(data.block, 1, data.meta));
|
|
|
|
data.block.onPostBlockPlaced(world, x, y, z, data.meta);
|
2014-03-08 02:00:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
float motion = 0.7F;
|
|
|
|
double motionX = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
|
|
|
|
double motionY = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
|
|
|
|
double motionZ = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
|
|
|
|
|
|
|
|
EntityItem entityItem = new EntityItem(world, x + motionX, y + motionY, z + motionZ, itemStack);
|
|
|
|
|
|
|
|
world.spawnEntityInWorld(entityItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-13 03:23:34 +01:00
|
|
|
@Override
|
2014-04-20 22:15:44 +02:00
|
|
|
public TileEntity createNewTileEntity(World world, int meta)
|
2014-01-13 03:23:34 +01:00
|
|
|
{
|
|
|
|
return new TileEntityCardboardBox();
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
|
|
|
public ItemStack dismantleBlock(World world, int x, int y, int z, boolean returnBlock)
|
2014-01-13 03:23:34 +01:00
|
|
|
{
|
2014-03-08 02:00:25 +01:00
|
|
|
ItemStack itemStack = getPickBlock(null, world, x, y, z);
|
|
|
|
|
|
|
|
world.setBlockToAir(x, y, z);
|
|
|
|
|
|
|
|
if(!returnBlock)
|
|
|
|
{
|
|
|
|
float motion = 0.7F;
|
|
|
|
double motionX = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
|
|
|
|
double motionY = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
|
|
|
|
double motionZ = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
|
|
|
|
|
|
|
|
EntityItem entityItem = new EntityItem(world, x + motionX, y + motionY, z + motionZ, itemStack);
|
|
|
|
|
|
|
|
world.spawnEntityInWorld(entityItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
return itemStack;
|
2014-01-13 03:23:34 +01:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-13 03:23:34 +01:00
|
|
|
@Override
|
|
|
|
public ItemStack getPickBlock(MovingObjectPosition target, World world, int x, int y, int z)
|
|
|
|
{
|
2014-04-20 05:34:19 +02:00
|
|
|
TileEntityCardboardBox tileEntity = (TileEntityCardboardBox)world.getTileEntity(x, y, z);
|
2014-03-08 02:00:25 +01:00
|
|
|
|
|
|
|
ItemStack itemStack = new ItemStack(Mekanism.CardboardBox, 1, world.getBlockMetadata(x, y, z));
|
|
|
|
|
2014-02-02 01:20:10 +01:00
|
|
|
if(itemStack.getItemDamage() == 1)
|
|
|
|
{
|
2014-03-08 02:00:25 +01:00
|
|
|
if(tileEntity.storedData != null)
|
|
|
|
{
|
|
|
|
((ItemBlockCardboardBox)itemStack.getItem()).setBlockData(itemStack, tileEntity.storedData);
|
|
|
|
}
|
2014-02-02 01:20:10 +01:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-13 03:23:34 +01:00
|
|
|
return itemStack;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
|
|
|
@Override
|
2014-04-20 05:34:19 +02:00
|
|
|
public boolean removedByPlayer(World world, EntityPlayer player, int x, int y, int z)
|
2014-03-08 02:00:25 +01:00
|
|
|
{
|
|
|
|
if(!player.capabilities.isCreativeMode && !world.isRemote && canHarvestBlock(player, world.getBlockMetadata(x, y, z)))
|
|
|
|
{
|
|
|
|
float motion = 0.7F;
|
|
|
|
double motionX = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
|
|
|
|
double motionY = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
|
|
|
|
double motionZ = (world.rand.nextFloat() * motion) + (1.0F - motion) * 0.5D;
|
|
|
|
|
|
|
|
EntityItem entityItem = new EntityItem(world, x + motionX, y + motionY, z + motionZ, getPickBlock(null, world, x, y, z));
|
|
|
|
|
|
|
|
world.spawnEntityInWorld(entityItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
return world.setBlockToAir(x, y, z);
|
|
|
|
}
|
|
|
|
|
2014-01-13 03:23:34 +01:00
|
|
|
@Override
|
2014-03-08 02:00:25 +01:00
|
|
|
public int quantityDropped(Random random)
|
2014-01-13 03:23:34 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-04-20 22:15:44 +02:00
|
|
|
public Item getItemDropped(int i, Random random, int j)
|
2014-01-13 03:23:34 +01:00
|
|
|
{
|
2014-04-20 22:15:44 +02:00
|
|
|
return null;
|
2014-01-13 03:23:34 +01:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-13 03:23:34 +01:00
|
|
|
public static class BlockData
|
|
|
|
{
|
2014-04-20 22:15:44 +02:00
|
|
|
public Block block;
|
2014-01-13 03:23:34 +01:00
|
|
|
public int meta;
|
|
|
|
public NBTTagCompound tileTag;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-04-20 22:15:44 +02:00
|
|
|
public BlockData(Block b, int j, NBTTagCompound nbtTags)
|
2014-01-13 03:23:34 +01:00
|
|
|
{
|
2014-04-20 22:15:44 +02:00
|
|
|
block = b;
|
2014-01-13 03:23:34 +01:00
|
|
|
meta = j;
|
|
|
|
tileTag = nbtTags;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-13 03:23:34 +01:00
|
|
|
public BlockData() {}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-13 03:23:34 +01:00
|
|
|
public void updateLocation(int x, int y, int z)
|
|
|
|
{
|
|
|
|
if(tileTag != null)
|
|
|
|
{
|
|
|
|
tileTag.setInteger("x", x);
|
|
|
|
tileTag.setInteger("y", y);
|
|
|
|
tileTag.setInteger("z", z);
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-13 03:23:34 +01:00
|
|
|
public NBTTagCompound write(NBTTagCompound nbtTags)
|
|
|
|
{
|
2014-05-06 09:14:44 +02:00
|
|
|
nbtTags.setInteger("id", Block.getIdFromBlock(block));
|
2014-01-13 03:23:34 +01:00
|
|
|
nbtTags.setInteger("meta", meta);
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-13 03:23:34 +01:00
|
|
|
if(tileTag != null)
|
|
|
|
{
|
2014-04-20 05:34:19 +02:00
|
|
|
nbtTags.setTag("tileTag", tileTag);
|
2014-01-13 03:23:34 +01:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-13 03:23:34 +01:00
|
|
|
return nbtTags;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-13 03:23:34 +01:00
|
|
|
public static BlockData read(NBTTagCompound nbtTags)
|
|
|
|
{
|
|
|
|
BlockData data = new BlockData();
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-05-06 09:14:44 +02:00
|
|
|
data.block = Block.getBlockById(nbtTags.getInteger("id"));
|
2014-01-13 03:23:34 +01:00
|
|
|
data.meta = nbtTags.getInteger("meta");
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-13 03:23:34 +01:00
|
|
|
if(nbtTags.hasKey("tileTag"))
|
|
|
|
{
|
|
|
|
data.tileTag = nbtTags.getCompoundTag("tileTag");
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2014-01-13 03:23:34 +01:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|