Jump to content

Creating blockstate from string and meta


Versepelles

Recommended Posts

I'm trying to create an IBlockState or similar from strings in order to compare with an IBlockState in the world. For example, I'd like to test for an azure bluet at BlockPos pos = (0, 0, 0). The IBlockState state is gotten from world.getBlockState(pos). However, I'm trying to load the azure bluet from a text file, and so have String blockName = "red_flower" and int meta = 3 which correspond to the item data. Given this information, what would be a good way to check if state is an azure bluet? The save format is up for change too, if that would be good.

Link to comment
Share on other sites

The easiest thing, in my opinion, to do is save the block state in a reasonable way, which would be serialized NBT data. Save the blockstates via NBTUtil#writeBlockState and write the NBT data to a file (I haven't looked into this whatsoever, but I can't image it would too difficult). Then when you want to compare them, for whatever reason you are doing, read the NBT data from file, deserialize it via NBTUtil.readBlockState, and compare them.

 

Why are you doing this, anyway? Are you sure there isn't a better way achieving the functionality you desire?

Link to comment
Share on other sites

I'm making a psuedo config file, and there may be a better way. However, the goal of the file is to keep the data reasonable to edit by the modder. So, the text entry currently looks like this, where the last five entries are just some custom data to be associated with the entry block.

 

<name> <block> <meta> <earthy> <delicate> <bulbous> <vibrant> <unusual>
dandelion yellow_flower 0 1 1 1 1 1
poppy red_flower 0 1 0 0 2 0
blue_orchid red_flower 1 0 2 0 0 3
allium red_flower 2 0 0 -1 0 0
azure_bluet red_flower 3 5 0 0 0 0

 

I'd like to keep something similar to this format- I think that the block name and meta should be all that's required to create some kind of data structure (not sure if IBlockState is the right choice) to compare to an actual IBlockState later. I'd be fine just storing the name and metadata (for azure_bluet it would be ("red_flower", 3)) if someone can show how to compare that with an IBlockState.

Link to comment
Share on other sites

18 minutes ago, diesieben07 said:

First, get the block from it's registry name using ForgeRegistries.BLOCKS.getValue. Then call getStateFromMeta on that block with the metadata value.

Thanks- this is what I originally had, but the IDE said that the method getStateFromMeta was depreciated, so was wondering if there was a different "proper" method of achieving this.

Link to comment
Share on other sites

It's....not something you should be calling in most circumstances. However, for this purpose, this is the only method available for what you're doing.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

  • 10 months later...

I'll do a bit of a necro on this since it is very close to what I'm wanting to do. Like the OP, I need to get a blockstate from a human-readable config file. My current one, inherited from MC 1.7, uses meta values, for example:

 

birch leaves;minecraft:leaves;2;false;44/119/15

 

(with only the second and third values used to get the block state, the rest is mod-specific)

 

I would however really like to switch to something value-based, something like:

 

birch leaves;minecraft:leaves;variant=birch;false;44/119/15

 

The kind of description you find in the JSON files etc.

 

It would be a lot more readable and easy to debug when something goes wrong. Is there anyway of doing this? I've been hunting through the MC code but if there's a clearly-defined way of doing it I haven't found it yet.

 

Thanks.

 

Link to comment
Share on other sites

Minecraft uses StateMapperBase#getPropertyString to build the property strings uses as variants in blockstates files, but this is client-only.

 

You can take a look at NBTUtil.readBlockState and NBTUtil.writeBlockState to see how Minecraft reads blockstates from and writes them to NBT; you could apply similar logic to generate/parse a state string in a config file.

 

Edit: There's also CommandBase.convertArgToBlockState, used by commands to parse blockstates from strings. This is probably exactly what you want.

Edited by Choonster
  • Like 1

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

8 minutes ago, Choonster said:

Edit: There's also CommandBase.convertArgToBlockState, used by commands to parse blockstates from strings. This is probably exactly what you want.

Arg, that uses the exception from parseInt to switch between treating it as a meta and as values :( But the method within getBlockStatePropertyValueMap() looks like I exactly what I need indeed.

 

Perfect!

Link to comment
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.
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.

×
×
  • Create New...

Important Information

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