I have created an ItemBlock to save NBT data to it then someone breaks its block.
Block :
public class ChairBlock extends BlockContainer{
int renderType ;
private int x,y,z;
public ChairBlock(int par1, Material par2Material ) {
super(par1, par2Material);
this.setResistance(120F);
this.setHardness(120F);
this.setBlockUnbreakable();
}
@Override
public boolean renderAsNormalBlock() {
return false;
}
public void registerIcons(IconRegister par1IconRegister)
{
this.blockIcon = par1IconRegister.registerIcon("rs:Peripheral");
}
public boolean hasTileEntity(){
return true;
}
public boolean isOpaqueCube(){
return false;
}
@Override
public boolean isBlockSolid(IBlockAccess par1IBlockAccess, int par2, int par3, int par4, int par5) {
return true;
}
@Override
public int getRenderType() {
return renderType;
}
public void setRenderType(int id){
this.renderType = id;
}
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ)
{
if(world.isRemote)
return true;
MovingObjectPosition hit = RayTracer.retraceBlock(world, player, x, y, z);
RussyPeripheral tile = (RussyPeripheral) world.getBlockTileEntity(x, y, z);
if(!player.isSneaking()){
if(hit == null)
return false;
if(Utils.isHoldingUsableWrench(player) && tile.Username.equals(player.username)){
if(tile.blockMetadata + 1 > 3){
world.setBlockMetadataWithNotify(x, y, z, 0, 2);
}else{
world.setBlockMetadataWithNotify(x, y, z, tile.blockMetadata+1, 2);
}
return true;
}
if(hit.subHit == 1 && tile.getInstalledUpgrades().contains(tile.RVALVE_UPGRADE)){
tile.setValve1Activated();
}
}else{
if(Utils.isHoldingUsableWrench(player) && tile.Username.equals(player.username) && !player.capabilities.isCreativeMode){
this.removeBlockByPlayer(world, player, x, y, z);
}
}
return true;
}
@Override
public ItemStack getPickBlock(MovingObjectPosition mop, World world, int x, int y, int z)
{
RussyPeripheral tile = (RussyPeripheral) world.getBlockTileEntity(x, y, z);
ItemStack stack = new ItemStack(this,1);
if(!stack.hasTagCompound())
stack.setTagCompound(new NBTTagCompound());
stack.getTagCompound().setString("owner", tile.Username);
stack.getTagCompound().setBoolean("canWaitForKey",tile.canWaitForKey);
if(tile.eniquepass != null) stack.getTagCompound().setString("pass", tile.eniquepass);
stack.getTagCompound().setInteger("valve1Active", tile.valve1state);
stack.getTagCompound().setBoolean(tile.RVALVE_UPGRADE, tile.rightValveUpgrade);
for(int i = 0;i<MyTickHandler.loadedRussyTiles.size();i++){
if(MyTickHandler.loadedRussyTiles.get(i).xCoord == tile.xCoord
&& MyTickHandler.loadedRussyTiles.get(i).yCoord == tile.yCoord
&& MyTickHandler.loadedRussyTiles.get(i).zCoord == tile.zCoord){
MyTickHandler.loadedRussyTiles.remove(i);
}
}
return stack;
}
@Override
public TileEntity createNewTileEntity(World world){
RussyPeripheral tile = new RussyPeripheral();
tile.setUsername("NONE");
MyTickHandler.loadedRussyTiles.add(tile);
return tile;
}
public TileEntity createTileEntity(World world, int metadata)
{
RussyPeripheral tile = new RussyPeripheral();
tile.setUsername("NONE");
MyTickHandler.loadedRussyTiles.add(tile);
return tile;
}
@Override
public boolean removeBlockByPlayer(World world, EntityPlayer player, int x, int y, int z)
{
if(!player.capabilities.isCreativeMode && !world.isRemote)
dropBlockAsItem(world, x, y, z, world.getBlockMetadata(x, y, z), 0);
world.setBlockToAir(x, y, z);
return true;
}
public void onBlockPlacedBy(World par1World, int x, int y, int z, EntityLivingBase par5EntityLiving, ItemStack stack){
}
public void addCollisionBoxesToList(World par1World, int par2, int par3, int par4, AxisAlignedBB par5AxisAlignedBB, List par6List, Entity par7Entity)
{
RussyPeripheral tile = (RussyPeripheral) par1World.getBlockTileEntity(par2, par3, par4);
int ro = 0;
if(tile.worldObj != null){
ro = tile.getBlockMetadata();
}
float xMin,yMin,zMin,xMax,yMax,zMax;
xMin = 0.12500001F;
yMin = 0.0F;
zMin = 0.10500001F;
xMax = 0.9049993F;
yMax = 0.8949993F;
zMax = 0.88999933F;
if(ro == 1){
this.setBlockBounds(0.12500001F, 0.0F,0.10500001F, 0.9049993F, 0.8949993F, 0.88999933F);
super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
if(tile.getInstalledUpgrades().contains(tile.RVALVE_UPGRADE)){
this.setBlockBounds(0.034999996F, 0.17499997F, 0.54999965F, 0.13500002F, 0.39999977F, 0.7949994F);
super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
}
}else if(ro == 2){
this.setBlockBounds(0.105000004F, 0.0F, 0.13000001F, 0.88999933F, 0.8949993F, 0.9149993F);
super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
if(tile.getInstalledUpgrades().contains(tile.RVALVE_UPGRADE)){
this.setBlockBounds(0.19999994F, 0.16499998F, 0.044999994F, 0.44999975F, 0.39999977F, 0.145000001F);
super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
}
}else if(ro == 3){
this.setBlockBounds(0.085F, 0.0F, 0.10500001F, 0.87499934F, 0.8949993F, 0.8949993F);
super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
if(tile.getInstalledUpgrades().contains(tile.RVALVE_UPGRADE)){
this.setBlockBounds(0.85499936F, 0.16499998F, 0.19999999F, 0.95499927F, 0.3949998F, 0.44999975F);
super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
}
}else if(ro == 0){
this.setBlockBounds(0.11000001F, 0.0F, 0.095F, 0.88999933F, 0.8949993F, 0.87999934F);
super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
if(tile.getInstalledUpgrades().contains(tile.RVALVE_UPGRADE)){
this.setBlockBounds(0.54999965F, 0.16499998F, 0.86499935F, 0.7999994F, 0.39999977F, 0.96499926F);
super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
}
}
this.setBlockBounds(0, 0, 0, 1, 1, 1);
}
public MovingObjectPosition collisionRayTrace(World world, int x, int y, int z, Vec3 start, Vec3 end)
{
RussyPeripheral theTile = (RussyPeripheral)world.getBlockTileEntity(x, y, z);
if (theTile == null) {
return null;
}
List cuboids = new LinkedList();
theTile.addTraceableCuboids(cuboids);
return RayTracer.instance().rayTraceCuboids(new Vector3(start), new Vector3(end), cuboids, new BlockCoord(x, y, z), this);
}
@SideOnly(Side.CLIENT)
@ForgeSubscribe
public void onBlockHighlight(DrawBlockHighlightEvent event) {
if ((event.target.typeOfHit == EnumMovingObjectType.TILE) && (event.player.worldObj.getBlockTileEntity(event.target.blockX, event.target.blockY, event.target.blockZ) instanceof RussyPeripheral))
{
RayTracer.retraceBlock(event.player.worldObj, event.player, event.target.blockX, event.target.blockY, event.target.blockZ);
}
}
public AxisAlignedBB getSelectedBoundingBoxFromPool(World par1World, int par2, int par3, int par4)
{
return super.getSelectedBoundingBoxFromPool(par1World, par2, par3, par4);
}
}
ItemBlock
public class RussyPeripheralAsItem extends ItemBlock{
public RussyPeripheralAsItem(int par1) {
super(par1);
this.setHasSubtypes(false);
this.setUnlocalizedName(StatCollector.translateToLocal("rs.item.RussyP"));
this.setFull3D();
}
@Override
public boolean placeBlockAt(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ, int metadata)
{
if(super.placeBlockAt(stack, player, world, x, y, z, side, hitX, hitY, hitZ, metadata))
{
int rotation = MathHelper.floor_double((double)((player.rotationYaw * 4F) / 360F) + 2.5D) & 3;
world.setBlock(x, y, z, this.itemID, rotation, 2);
RussyPeripheral tile = (RussyPeripheral) world.getBlockTileEntity(x, y, z);
if(player instanceof EntityPlayer){
tile.Username = player.username;
}
if(stack.stackTagCompound != null){
tile.readFromNBT(stack.stackTagCompound);
}
player.setCurrentItemOrArmor(0, null);
}
return false;
}
@SideOnly(Side.CLIENT)
@Override
public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean extended)
{
if(!StringHelper.isShiftKeyDown()){
list.add(StringHelper.shiftForInfo);
}else{
if(stack.hasTagCompound()){
String Username = EnumChatFormatting.YELLOW+stack.stackTagCompound.getString("owner");
String eniquepass = EnumChatFormatting.YELLOW+stack.stackTagCompound.getString("pass");
boolean rightValveUpgrade = stack.stackTagCompound.getBoolean(RussyPeripheral.RVALVE_UPGRADE);
list.add(EnumChatFormatting.AQUA+"Owner: "+Username);
list.add(EnumChatFormatting.AQUA+"Eniquepass: "+eniquepass);
list.add(EnumChatFormatting.AQUA+"Installed modules:");
if(rightValveUpgrade) list.add(EnumChatFormatting.RED+" - Right Valve");
}
}
}
}
But then I try to break my block, my itemBlock drops well, but its NBT is empty. (I check NBT ingame with NBTEditor)
What am I doing wrong ?