Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.8] Storing nbt tag compound in block by using its tile entity


Recommended Posts

Well, currently I'm using that, but the player can still get the block when it is broke in creative. How can I check if it is creative or not?

 

I mean, is there any problem by using !Minecraft.getMinecraft().thePlayer.capabilities.isCreativeMode in the breakBlock(World worldIn, BlockPos pos, IBlockState state) method?

Link to post
Share on other sites

In order to get the tile entity (!= null), I'm using the breakBlock(), instead of dropBlockAsItemWithChance(), or harvestBlock(), both return a null tile entity.

 

So, I'm setting the tag in an itemstack and spawning it before the super.breakBlock(). When I do this, it will spawn an extra block, so I need to override the getItemDropped() to null. However, when I do that, it will always drop an item, even in creative.

Link to post
Share on other sites

Nice. But I still have a problem. When I destroy the tile entity in survival, it is not completely destroyed, I can still collide with it, and when I right click it, the block returns. Am I doing something wrong?

 

 

 

@Override

public Item getItemDropped(IBlockState state, Random rand, int fortune)

{

return null;

}

 

@Override

public boolean removedByPlayer(World world, BlockPos pos, EntityPlayer player, boolean willHarvest)

{

if (willHarvest)

return true;

return super.removedByPlayer(world, pos, player, willHarvest);

}

 

@Override

public void harvestBlock(World world, EntityPlayer player, BlockPos pos, IBlockState state, TileEntity te)

{

super.harvestBlock(world, player, pos, state, te);

world.setBlockToAir(pos);

}

 

@Override

public List<ItemStack> getDrops(IBlockAccess worldIn, BlockPos pos, IBlockState state, int fortune)

{

List<ItemStack> drops = super.getDrops(worldIn, pos, state, fortune);

 

TileEntity tileEntity = worldIn.getTileEntity(pos);

if (tileEntity instanceof MyTile)

{

MyTile myTile= (MyTile) tileEntity;

drops.add(this.getThisBlockWithTag(myTile, pos));

for (ItemStack itemStack : myTile.getInventory())

drops.add(itemStack);

}

return drops;

}

 

private ItemStack getThisBlockWithTag(MyTile myTile, BlockPos pos)

{

ItemStack stack = new ItemStack(BlockRegistry.myBlock);

if (myTile != null)

{

NBTTagCompound compound = new NBTTagCompound();

compound.setByte("var1", (byte) myTile.getVar1());

 

[...]

 

stack.setTagCompound(compound);

}

return stack;

}

 

 

 

 

Link to post
Share on other sites

First, don't handle the inventory via getDrops, you should do that the traditional way, otherwise the inventory is lost when something other than a player destroys your block.

 

As for the ghost block, no idea what causes that, please post your complete block class.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.