public static IBlockState getStateFromItem( final ItemStack bitItemStack ) { if ( bitItemStack != null && bitItemStack.getItem() instanceof ItemBlock ) { final ItemBlock blkItem = (ItemBlock) bitItemStack.getItem(); return blkItem.getBlock().getStateFromMeta( blkItem.getMetadata( bitItemStack ) ); } return null; }
/** * Gets the state to ItemStack lookup map, mainly for state -> ItemStack rendering lookup. * * @return The lookup map. */ public static Map<String, ItemStack> getStateLookup() { if (lookupMap == null) { lookupMap = new LinkedHashMap<>(); NonNullList<ItemStack> stacks = NonNullList.create(); StreamSupport.stream(ForgeRegistries.ITEMS.spliterator(), false)// .filter(item -> item instanceof ItemBlock)// .sorted(Comparator.comparingInt(Item.REGISTRY::getIDForObject))// .forEachOrdered(item -> item.getSubItems(CreativeTabs.SEARCH, stacks)); for (ItemStack stack : stacks) { if (stack.getItem() instanceof ItemBlock) { ItemBlock item = (ItemBlock) stack.getItem(); Block block = item.getBlock(); int blockMeta = item.getMetadata(stack.getItemDamage()); if (isValid(block, blockMeta)) { lookupMap.put(block.getRegistryName() + " >> " + blockMeta, stack); } } } } return lookupMap; }
/** * Gets the block colors from an item stack * @param stack Stack to check * @param world World * @param pos Pos * @param index Tint index * @return color, or -1 for undefined */ @SuppressWarnings("deprecation") public static int getStackBlockColors(ItemStack stack, @Nullable IBlockAccess world, @Nullable BlockPos pos, int index) { if(stack.isEmpty() || !(stack.getItem() instanceof ItemBlock)) { return -1; } ItemBlock item = (ItemBlock) stack.getItem(); IBlockState iblockstate = item.getBlock().getStateFromMeta(item.getMetadata(stack)); return Minecraft.getMinecraft().getBlockColors().colorMultiplier(iblockstate, world, pos, index); }
public static ItemStack getItemFromBlock( @Nonnull final IBlockState state ) { final Block blk = state.getBlock(); final Item i = blk.getItemDropped( state, RAND, 0 ); final int meta = blk.getMetaFromState( state ); final int damage = blk.damageDropped( state ); final Item blockVarient = Item.getItemFromBlock( blk ); // darn conversions... if ( blk == Blocks.GRASS ) { return new ItemStack( Blocks.GRASS ); } if ( i == null || blockVarient == null || blockVarient != i ) { return ModUtil.getEmptyStack(); } if ( blockVarient instanceof ItemBlock ) { final ItemBlock ib = (ItemBlock) blockVarient; if ( meta != ib.getMetadata( damage ) ) { // this item dosn't drop itself... BAIL! return ModUtil.getEmptyStack(); } } return new ItemStack( i, 1, damage ); }
@SuppressWarnings( "deprecation" ) public static IBlockState getStateFromItem( final ItemStack is ) { try { if ( !ModUtil.isEmpty( is ) && is.getItem() instanceof ItemBlock ) { final ItemBlock iblk = (ItemBlock) is.getItem(); final IBlockState state = iblk.getBlock().getStateFromMeta( iblk.getMetadata( is.getItemDamage() ) ); final ItemStack out = ModUtil.getItemFromBlock( state ); if ( !ModUtil.isEmpty( out ) && out.getItem() == is.getItem() && is.getItemDamage() == out.getItemDamage() ) { return state; } } } catch ( final Throwable t ) { // : ( } return Blocks.AIR.getDefaultState(); }
@Override public ItemStack dispenseStack(IBlockSource par1IBlockSource, ItemStack par2ItemStack) { EnumFacing facing = par1IBlockSource.getBlockState().getValue(BlockDispenser.FACING); Axis axis = facing.getAxis(); BlockPos pos = par1IBlockSource.getBlockPos().offset(facing); World world = par1IBlockSource.getWorld(); if(world.isAirBlock(pos) && block.canPlaceBlockAt(world, pos)) { int meta = item.getMetadata(par2ItemStack.getItemDamage()); IBlockState state; if(!(block instanceof BlockPistonBase)) state = block.getStateFromMeta(meta); else state = block.getDefaultState(); LockDirectionHotkey.setBlockRotated(world, state, pos, facing); SoundType soundtype = block.getSoundType(); world.playSound(null, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F); par2ItemStack.shrink(1); return par2ItemStack; } return super.dispenseStack(par1IBlockSource, par2ItemStack); }
private EnumActionResult placeBlock(ItemStack itemstack, EntityPlayer player, BlockPos pos, EnumFacing facing, World worldIn, EnumHand hand, float hitX, float hitY, float hitZ) { IBlockState stateAt = worldIn.getBlockState(pos); if(!stateAt.getBlock().isReplaceable(worldIn, pos)) pos = pos.offset(facing); if(itemstack.getItem() instanceof ItemBlock) { ItemBlock item = (ItemBlock) itemstack.getItem(); Block block = item.getBlock(); if(player.canPlayerEdit(pos, facing, itemstack) && worldIn.mayPlace(block, pos, false, facing, null)) { int i = item.getMetadata(itemstack.getMetadata()); IBlockState state = block.getStateForPlacement(worldIn, pos, facing, hitX, hitY, hitZ, i, player, hand); if(item.placeBlockAt(itemstack, player, worldIn, pos, facing, hitX, hitY, hitZ, state)) { state = worldIn.getBlockState(pos); SoundType soundtype = state.getBlock().getSoundType(state, worldIn, pos, player); worldIn.playSound(player, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F); if(!player.capabilities.isCreativeMode) shrinkInventory(itemstack, player); } return EnumActionResult.SUCCESS; } } return EnumActionResult.FAIL; }