Minecraftforge: ItemStack getBlock() Possibility

Created on 14 Jun 2018  路  7Comments  路  Source: MinecraftForge/MinecraftForge

Hi Forge,
i want to check block but i have only the possibility to check it only by ItemStack but itemstack not have getBlock() or getBlockState(),

can you implement it for future update ?

Most helpful comment

Blindly passing the stack metadata into the block metadata is wrong. Vanilla wool is an example.
But this is a discussion for the forums.

All 7 comments

use Block.getBlockFromItem and Block.getStateFromMeta instead.

yea but "net.minecraftforge.client.event.RenderTooltipEvent.Color color"
use ItemStack with color.getStack() and not Other
if i use my implement class,
it's not used for block but only item
i use already switch with stack.getStack().getItem().getRegistryName().getResourcePath() to get block name but i dont want to use this for my mods.

create a custom itemblock class for your block then.

Sorry, Block.getBlockFromItem() working,
but is not realy intuitive, will adding getBlock() is easy to found the funtions with ItemStack and not with import Block.
thanks :+1: for help

I suggest you to create a utility class in your mod to do this job instead.

public final class ItemStackUtils {
  private ItemStackUtils() {}

  public static IBlockState getStateFromStack(ItemStack stack) {
    Block block = Block.getBlockFromItem(stack.getItem());
    if (block == null) {
      return Blocks.AIR.getDefaultState();
    }
    return block.getStateFromMeta(stack.getMetadata());
  }
}

This code should be good enough. In code, you can call this method to retrieve the state you want. To get a more accurate result, you can use getStateForPlacement in place of getStateFromMeta.

i will use this after few test with armor,
thanks for help :+1:

Blindly passing the stack metadata into the block metadata is wrong. Vanilla wool is an example.
But this is a discussion for the forums.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MSandro picture MSandro  路  3Comments

blay09 picture blay09  路  3Comments

bs2609 picture bs2609  路  3Comments

williewillus picture williewillus  路  3Comments

ErikBigDev picture ErikBigDev  路  3Comments