Jump to content

Recommended Posts

Posted (edited)

How can i get metadata as int of IBlockState ?

For example I have Oak Log as IBlockState and I need to get ZERO, because metadata of Oak Log is 0.

Edited by Dipo
Posted

What are you trying to achieve?
The only way I can think of is to invoke Block#getMetaFromState. But you shouldn't be using metadata from blockstates directly as it is pretty much only used for serialization purposes.

Posted

I just need it to print a name of Block with some meta, So i need metadata to create ItemStack with custom metadata and get its localized name.

Posted

Ask yourself first why you need the integer metadata.  What are you planning to do with it?

 

The only use for the metadata is for saving to disk.  Anything you do with an IBlockState should be via properties: https://mcforge.readthedocs.io/en/latest/blocks/states/.

 

In the case of logs, it's a little awkward since vanilla adds two separate blocks for logs (4 bits of metadata is not enough to store both the variant and the rotation): BlockOldLog and BlockNewLog.  So you'll need to check the block type and then get the BlockOldLog.VARIANT or BlockNewLog.VARIANT property as appropriate.

Posted

As I said I need i to print a name of Block with some meta, So i need metadata to create ItemStack with custom metadata and get its localized name.

Posted

You can't directly transform the metadata from a blockstate to a metadata of an ItemStack. For example the metadata of a sideways spruce log would be 5. An itemstack of a log block with a metadata of 5 would be something completely invalid since log metadata for items only goes as high as 3 - as you can see this approach doesn't work since metadata of items and metadata of associated blocks can represent completely different things since as I've said metadata of blocks is only used for serialization. 

The most accurate solution to getting an ItemStack from a block I can think of is Block#getPickBlock. While it is not 100% accurate(depends on the implementation especially for modded blocks) it is the ItemStack that is associated with middle-clicking that block and in most cases it will be the ItemStack representation of that block with that particular state in the world.

  • Like 1
Posted

In addition to what V0idWal1k3r said, getting the block meta directly is guaranteed to fail in 1.13, while the getPickBlock() solution should continue to work fine.

Posted

:D I have done it using Block#getPickBlock

Like this

ItemStack stack = block.getBlock().getPickBlock(block, null, sender.getEntityWorld(), null, (EntityPlayer) sender);

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.