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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Selamat datang di OLXTOTO, situs slot gacor terpanas yang sedang booming di industri perjudian online. Jika Anda mencari pengalaman bermain yang luar biasa, maka OLXTOTO adalah tempat yang tepat untuk Anda. Dapatkan sensasi tidak biasa dengan variasi slot online terlengkap dan peluang memenangkan jackpot slot maxwin yang sering. Di sini, Anda akan merasakan keseruan yang luar biasa dalam bermain judi slot. DAFTAR OLXTOTO DISINI LOGIN OLXTOTO DISINI AKUN PRO OLXTOTO DISINI   Slot Gacor untuk Sensasi Bermain Maksimal Olahraga cepat dan seru dengan slot gacor di OLXTOTO. Rasakan sensasi bermain maksimal dengan mesin slot yang memberikan kemenangan beruntun. Temukan keberuntungan Anda di antara berbagai pilihan slot gacor yang tersedia dan rasakan kegembiraan bermain judi slot yang tak terlupakan. Situs Slot Terpercaya dengan Pilihan Terlengkap OLXTOTO adalah situs slot terpercaya yang menawarkan pilihan terlengkap dalam perjudian online. Nikmati berbagai genre dan tema slot online yang menarik, dari slot klasik hingga slot video yang inovatif. Dipercaya oleh jutaan pemain, OLXTOTO memberikan pengalaman bermain yang aman dan terjamin.   Jackpot Slot Maxwin Sering Untuk Peluang Besar Di OLXTOTO, kami tidak hanya memberikan hadiah slot biasa, tapi juga memberikan kesempatan kepada pemain untuk memenangkan jackpot slot maxwin yang sering. Dengan demikian, Anda dapat meraih keberuntungan besar dan memenangkan ribuan rupiah sebagai hadiah jackpot slot maxwin kami. Jackpot slot maxwin merupakan peluang besar bagi para pemain judi slot untuk meraih keuntungan yang lebih besar. Dalam permainan kami, Anda tidak harus terpaku pada kemenangan biasa saja. Kami hadir dengan jackpot slot maxwin yang sering, sehingga Anda memiliki peluang yang lebih besar untuk meraih kemenangan besar dengan hadiah yang menggiurkan. Dalam permainan judi slot, pengalaman bermain bukan hanya tentang keseruan dan hiburan semata. Kami memahami bahwa para pemain juga menginginkan kesempatan untuk meraih keberuntungan besar. Oleh karena itu, OLXTOTO hadir dengan jackpot slot maxwin yang sering untuk memberikan peluang besar kepada para pemain kami. Peluang Besar Menang Jackpot Slot Maxwin Peluang menang jackpot slot maxwin di OLXTOTO sangatlah besar. Anda tidak perlu khawatir tentang batasan atau pembatasan dalam meraih jackpot tersebut. Kami ingin memberikan kesempatan kepada semua pemain kami untuk merasakan sensasi menang dalam jumlah yang luar biasa. Jackpot slot maxwin kami dibuka untuk semua pemain judi slot di OLXTOTO. Anda memiliki peluang yang sama dengan pemain lainnya untuk memenangkan hadiah jackpot yang besar. Kami percaya bahwa semua orang memiliki kesempatan untuk meraih keberuntungan besar, dan itulah mengapa kami menyediakan jackpot slot maxwin yang sering untuk memenuhi harapan dan keinginan Anda.  
    • LOGIN DAN DAFTAR DISINI SEKARANG !!!! Blacktogel adalah situs judi slot online yang menjadi pilihan banyak penggemar judi slot gacor di Indonesia. Dengan platform yang sangat user-friendly dan berbagai macam permainan slot yang tersedia, Blacktogel menjadi tempat yang tepat untuk penggemar judi slot online. Dalam artikel ini, kami akan membahas tentang Blacktogel dan keunggulan situs slot gacor online yang disediakan.  
    • Situs bandar slot online Gacor dengan bonus terbesar saat ini sedang menjadi sorotan para pemain judi online. Dengan persaingan yang semakin ketat dalam industri perjudian online, pemain mencari situs yang tidak hanya menawarkan permainan slot yang gacor (sering memberikan kemenangan), tetapi juga bonus terbesar yang bisa meningkatkan peluang menang. Daftar disini : https://gesit.io/googlegopek
    • Situs bandar slot online Gacor dengan bonus terbesar saat ini sedang menjadi sorotan para pemain judi online. Dengan persaingan yang semakin ketat dalam industri perjudian online, pemain mencari situs yang tidak hanya menawarkan permainan slot yang gacor (sering memberikan kemenangan), tetapi juga bonus terbesar yang bisa meningkatkan peluang menang. Daftar disini : https://gesit.io/googlegopek
    • Situs bandar slot online Gacor dengan bonus terbesar saat ini sedang menjadi sorotan para pemain judi online. Dengan persaingan yang semakin ketat dalam industri perjudian online, pemain mencari situs yang tidak hanya menawarkan permainan slot yang gacor (sering memberikan kemenangan), tetapi juga bonus terbesar yang bisa meningkatkan peluang menang. Daftar disini : https://gesit.io/googlegopek
  • Topics

×
×
  • Create New...

Important Information

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