Versepelles Posted April 3, 2017 Share Posted April 3, 2017 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. Quote Link to comment Share on other sites More sharing options...
TheMasterGabriel Posted April 3, 2017 Share Posted April 3, 2017 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? Quote Link to comment Share on other sites More sharing options...
Versepelles Posted April 3, 2017 Author Share Posted April 3, 2017 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. Quote Link to comment Share on other sites More sharing options...
TheMasterGabriel Posted April 3, 2017 Share Posted April 3, 2017 Oh, if you are doing something like that, then yes do what @diesieben07 suggested. Mind you should also check for invalid registry names and metadatas as well, if the file can be edited externally, just as an added measure. Quote Link to comment Share on other sites More sharing options...
Versepelles Posted April 3, 2017 Author Share Posted April 3, 2017 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. Quote Link to comment Share on other sites More sharing options...
Draco18s Posted April 3, 2017 Share Posted April 3, 2017 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. Quote 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 More sharing options...
Kinniken Posted February 17, 2018 Share Posted February 17, 2018 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. Quote http://www.millenaire.org/img/dynamicsig.png[/img] Link to comment Share on other sites More sharing options...
Choonster Posted February 17, 2018 Share Posted February 17, 2018 (edited) 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 February 17, 2018 by Choonster 1 Quote 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 More sharing options...
Kinniken Posted February 17, 2018 Share Posted February 17, 2018 Great, thanks! I've just had a quick look at the code used and it's simpler than I feared, no exceptions really, just iterates over the properties and get the actual value via an existing method. Shouldn't be hard to mimic this. Quote http://www.millenaire.org/img/dynamicsig.png[/img] Link to comment Share on other sites More sharing options...
Kinniken Posted February 17, 2018 Share Posted February 17, 2018 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! Quote http://www.millenaire.org/img/dynamicsig.png[/img] Link to comment Share on other sites More sharing options...
Kinniken Posted February 18, 2018 Share Posted February 18, 2018 Tried it, it works fine, thanks for the help. Quote http://www.millenaire.org/img/dynamicsig.png[/img] Link to comment Share on other sites More sharing options...
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.