Mekanism-tilera-Edition/src/main/java/mekanism/common/block/BlockCardboardBox.java

252 lines
6.5 KiB
Java
Raw Normal View History

2014-01-13 03:23:34 +01:00
package mekanism.common.block;
import java.util.Random;
import mekanism.common.Mekanism;
import mekanism.common.MekanismBlocks;
2014-01-13 03:23:34 +01:00
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;
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;
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;
import net.minecraft.util.IIcon;
2014-01-13 03:23:34 +01:00
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.IBlockAccess;
2014-01-13 03:23:34 +01:00
import net.minecraft.world.World;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class BlockCardboardBox extends BlockContainer
{
private static boolean testingPlace = false;
public IIcon[] icons = new IIcon[6];
public BlockCardboardBox()
2014-01-13 03:23:34 +01:00
{
super(Material.cloth);
2014-01-13 03:23:34 +01:00
setCreativeTab(Mekanism.tabMekanism);
setHardness(0.5F);
setResistance(1F);
}
2014-01-13 03:23:34 +01:00
@Override
@SideOnly(Side.CLIENT)
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-01-13 03:23:34 +01:00
@Override
@SideOnly(Side.CLIENT)
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];
}
}
@Override
public boolean isReplaceable(IBlockAccess world, int x, int y, int z)
{
return testingPlace;
}
@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(MekanismBlocks.CardboardBox);
TileEntityCardboardBox tileEntity = (TileEntityCardboardBox)world.getTileEntity(x, y, z);
if(tileEntity.storedData != null)
{
BlockData data = tileEntity.storedData;
2014-06-09 16:19:36 +02:00
testingPlace = true;
2014-06-09 16:19:36 +02:00
if(!data.block.canPlaceBlockAt(world, x, y, z))
{
testingPlace = false;
2014-06-09 16:19:36 +02:00
return true;
}
testingPlace = false;
if(data.block != null)
{
2014-06-09 16:19:36 +02:00
data.meta = data.block.onBlockPlaced(world, x, y, z, facing, hitX, hitY, hitZ, data.meta);
}
world.setBlock(x, y, z, data.block, data.meta, 3);
if(data.tileTag != null && world.getTileEntity(x, y, z) != null)
{
data.updateLocation(x, y, z);
world.getTileEntity(x, y, z).readFromNBT(data.tileTag);
}
if(data.block != null)
{
data.block.onBlockPlacedBy(world, x, y, z, entityplayer, new ItemStack(data.block, 1, data.meta));
data.block.onPostBlockPlaced(world, x, y, z, data.meta);
}
2022-09-11 19:35:08 +02: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;
2022-09-11 19:35:08 +02:00
// EntityItem entityItem = new EntityItem(world, x + motionX, y + motionY, z + motionZ, itemStack);
2022-09-11 19:35:08 +02:00
// world.spawnEntityInWorld(entityItem);
}
}
return false;
}
2014-01-13 03:23:34 +01:00
@Override
public TileEntity createNewTileEntity(World world, int meta)
2014-01-13 03:23:34 +01:00
{
return new TileEntityCardboardBox();
}
public ItemStack dismantleBlock(World world, int x, int y, int z, boolean returnBlock)
2014-01-13 03:23:34 +01:00
{
ItemStack itemStack = getPickBlock(null, world, x, y, z, null);
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-01-13 03:23:34 +01:00
@Override
public ItemStack getPickBlock(MovingObjectPosition target, World world, int x, int y, int z, EntityPlayer player)
2014-01-13 03:23:34 +01:00
{
TileEntityCardboardBox tileEntity = (TileEntityCardboardBox)world.getTileEntity(x, y, z);
ItemStack itemStack = new ItemStack(MekanismBlocks.CardboardBox, 1, world.getBlockMetadata(x, y, z));
2014-02-02 01:20:10 +01:00
if(itemStack.getItemDamage() == 1)
{
if(tileEntity.storedData != null)
{
((ItemBlockCardboardBox)itemStack.getItem()).setBlockData(itemStack, tileEntity.storedData);
}
2014-02-02 01:20:10 +01:00
}
2014-01-13 03:23:34 +01:00
return itemStack;
}
@Override
2014-08-08 20:21:42 +02:00
public boolean removedByPlayer(World world, EntityPlayer player, int x, int y, int z, boolean willHarvest)
{
if(!player.capabilities.isCreativeMode && !world.isRemote && willHarvest)
{
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, player));
world.spawnEntityInWorld(entityItem);
}
return world.setBlockToAir(x, y, z);
}
2014-01-13 03:23:34 +01:00
@Override
public int quantityDropped(Random random)
2014-01-13 03:23:34 +01:00
{
return 0;
}
@Override
public Item getItemDropped(int i, Random random, int j)
2014-01-13 03:23:34 +01:00
{
return null;
2014-01-13 03:23:34 +01:00
}
2014-01-13 03:23:34 +01:00
public static class BlockData
{
public Block block;
2014-01-13 03:23:34 +01:00
public int meta;
public NBTTagCompound tileTag;
public BlockData(Block b, int j, NBTTagCompound nbtTags)
2014-01-13 03:23:34 +01:00
{
block = b;
2014-01-13 03:23:34 +01:00
meta = j;
tileTag = nbtTags;
}
2014-01-13 03:23:34 +01:00
public BlockData() {}
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-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-01-13 03:23:34 +01:00
if(tileTag != null)
{
nbtTags.setTag("tileTag", tileTag);
2014-01-13 03:23:34 +01:00
}
2014-01-13 03:23:34 +01:00
return nbtTags;
}
2014-01-13 03:23:34 +01:00
public static BlockData read(NBTTagCompound nbtTags)
{
BlockData data = new BlockData();
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-01-13 03:23:34 +01:00
if(nbtTags.hasKey("tileTag"))
{
data.tileTag = nbtTags.getCompoundTag("tileTag");
}
2014-01-13 03:23:34 +01:00
return data;
}
}
}