Jump to content

[1.12.2] Get IBlockState from ItemStack


MiKeY_

Recommended Posts

Before I start, yes, I know, IBlockState != ItemStack. I have an issue that I'm trying to figure out so it's either finding out how to get the blockstate from the itemstack or it's finding the correct way of doing things.

 

So, I want to have a list of selectable blocks which I have, unfortunately this list is itemstacks and I need blocks / blockstates.

Here is how I build my list

for ( Block block : ForgeRegistries.BLOCKS ) {
  NonNullList<ItemStack> subBlocks = NonNullList.create();
  block.getSubBlocks( block.getCreativeTabToDisplayOn(), subBlocks );
  if ( Blocks.AIR.equals( block ) )
    continue; // avoids troubles

  for( ItemStack subBlock : subBlocks ) {
    if( subBlock.getItem() == Items.AIR )
      continue;

    Instance.guiBlockList.add( subBlock.isEmpty() ? new ItemStack(block) : subBlock );
  }
}

 

From this I am then able to create a perfectly suitable gui list that displays all the blocks and their subblocks ( wool, wool:green, wool:yellow, etc ). I then need to add this to a List<IBlockState> where the actual state is important.

IBlockState blockState = Block.getBlockFromItem( this.selectBlock.getItem() ).getDefaultState();

 

I'm currently doing it like this but it's pretty clear that this won't work for what I need due to the defaultState. I've looked into using `getStateForPlacement`but to no avail because I have no world data to give it. I only have an itemstack to work with.

Any help is greatly appreciated :D

 

EDIT: I'm currently working on rewritting my mod to get it off of the old id:meta system so the states crap is somewhat new to me.

Edited by MiKeY_
Link to comment
Share on other sites

1 hour ago, MiKeY_ said:

  if ( Blocks.AIR.equals( block ) )
    continue; // avoids troubles

 

Not sure why you do this after getting a block's subblocks.

1 hour ago, MiKeY_ said:

.getDefaultState();

This will never result in the right IBlockState for a block with multiple states.

 

There is no standard way of doing what you want to do.

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

5 minutes ago, Draco18s said:

Not sure why you do this after getting a block's subblocks.

This will never result in the right IBlockState for a block with multiple states.

 

There is no standard way of doing what you want to do.

I Actually can't remember why I put that there, I'm sure at the time there was a reason :P

 

I some what thought that it wasn't going to be an easy thing to fix, I'm currently expermeneting with this

Collection<IBlockState> blocksList = new ArrayList<>();
Collection<Block> blocks = ForgeRegistries.BLOCKS.getValuesCollection();

blocks.forEach( block -> block.getBlockState().getValidStates().forEach(iBlockState -> {
  iBlockState.getProperties().forEach( (iProperty, comparable) -> {
    if( !iProperty.getName().equals("variant") || blocksList.contains(iBlockState) )
      return;

    blocksList.add( iBlockState );
  } );
} ));

 

But I only care about the variant of the block or the complete lack of the varient and this code doesn't even close to get what I need it to do. I'm trying to find a way to use withProperty to create a new blockstate that is purely either the variant of the block as default. Things like grass would work fine with their defaults, things like stone, no so much.

Edited by MiKeY_
Link to comment
Share on other sites

9 minutes ago, diesieben07 said:

You can check if the Item is an ItemBlock and if so call Block#getStateForPlacement (read the contract about what to pass in for the values, the ItemStack metadata for example must not be used directly here).

This is however a kludge and not going to work for all block items, let alone things like the repeater item.

The only way to really do this would be to create a fake world, fake player, etc., call ItemStack#onItemUse and check if it placed a block in your fake world.

I'm getting the point there I'm just gunna force the player to find the block and put it in an inventory slot haha.
 

All I need is a list of all the blocks in the game... I never thought it would be this annoying :P By any chance do you know how to get a block with no properties at all so I can use withProps on an empty blockState?

 

edit: don't know if this helps, I'm basically just making a list you can search through to select a filter. A bit like StevesFactoryManager worked when you'd want to select the block to use.

Edited by MiKeY_
Link to comment
Share on other sites

1 minute ago, diesieben07 said:

It's still going to be an ItemStack. Blocks only exist placed down,  in the world.

 

Blocks: ForgeRegistries.BLOCKS.getValuesCollection()

Block states: ForgeRegistries.BLOCKS.getValuesCollection().stream().map(Block::getBlockState).flatMap(BlockStateContainer::getValidStates).collect(Collectors.toList())

 

Block#getDefaultState

 

Start with the IBlockState then. Going from IBlockState to ItemStack is a lot simpler.

Thanks, I think I'll keep messing around. I don't want the different directions of the blocks and things and that seems to be what is causing the bigest issues when using getValidStates

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.

Announcements



×
×
  • Create New...

Important Information

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