Dipo Posted July 18, 2018 Posted July 18, 2018 (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 July 18, 2018 by Dipo Quote
V0idWa1k3r Posted July 18, 2018 Posted July 18, 2018 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. Quote
Dipo Posted July 18, 2018 Author Posted July 18, 2018 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. Quote
desht Posted July 18, 2018 Posted July 18, 2018 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. Quote
Dipo Posted July 18, 2018 Author Posted July 18, 2018 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. Quote
V0idWa1k3r Posted July 18, 2018 Posted July 18, 2018 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. 1 Quote
desht Posted July 18, 2018 Posted July 18, 2018 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. Quote
Dipo Posted July 18, 2018 Author Posted July 18, 2018 I have done it using Block#getPickBlock Like this ItemStack stack = block.getBlock().getPickBlock(block, null, sender.getEntityWorld(), null, (EntityPlayer) sender); Quote
Dipo Posted July 18, 2018 Author Posted July 18, 2018 I know, but its a mod that is just for me to simplify something, and it does not matter if it crashes once a time Quote
Dipo Posted July 18, 2018 Author Posted July 18, 2018 I said that i did it using Block#getPickBlock, so i did not use any metadata Quote
Dipo Posted July 18, 2018 Author Posted July 18, 2018 I just passed twice null to a method, because these two parameters are never used in vanilla blocks Quote
Recommended Posts
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.