Jump to content

Recommended Posts

Posted

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.

Posted

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?

Posted

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.

Posted
  On 4/3/2017 at 12:38 AM, 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.

Expand  

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.

Posted

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.

  • 10 months later...
Posted

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.

 

Posted (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 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.

Posted
  On 2/17/2018 at 5:31 PM, Choonster said:

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

Expand  

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!

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello all. I'm currently grappling with the updateShape method in a custom class extending Block.  My code currently looks like this: The conditionals in CheckState are there to switch blockstate properties, which is working fine, as it functions correctly every time in getStateForPlacement.  The problem I'm running into is that when I update a state, the blocks seem to call CheckState with the position of the block which was changed updated last.  If I build a wall I can see the same change propagate across. My question thus is this: is updateShape sending its return to the neighbouring block?  Is each block not independently executing the updateShape method, thus inserting its own current position?  The first statement appears to be true, and the second false (each block is not independently executing the method). I have tried to fix this by saving the block's own position to a variable myPos at inception, and then feeding this in as CheckState(myPos) but this causes a worse outcome, where all blocks take the update of the first modified block, rather than just their neighbour.  This raises more questions than it answers, obviously: how is a different instance's variable propagating here?  I also tried changing it so that CheckState did not take a BlockPos, but had myPos built into the body - same problem. I have previously looked at neighbourUpdate and onNeighbourUpdate, but could not find a way to get this to work at all.  One post on here about updatePostPlacement and other methods has proven itself long superceded.  All other sources on the net seem to be out of date. Many thanks in advance for any help you might offer me, it's been several days now of trying to get this work and several weeks of generally trying to get round this roadblock.  - Sandermall
    • sorry, I might be stupid, but how do I open it? because the only options I have are too X out, copy it, which doesn't work and send crash report, which doesn't show it to me, also, sorry for taking so long.
    • Can you reproduce this with version 55.0.21? A whole lot of plant placement issues were just fixed in this PR.
    • Necro'ing that thread to ask if you found a solution ? I'm encountering the same crash on loading the world. I created the world in Creative to test my MP, went into survival to test combat, died, crashed on respawn and since then crash on loading the world. Deactivating Oculus isn't fixing it either, and I don't have Optifine (Twilight forest is incompatible)
  • Topics

×
×
  • Create New...

Important Information

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