Jump to content

[Solved][1.10] Get Meta from Block for ItemStack


Seriak

Recommended Posts

I'm trying to update a mod from 1.7.10, I have an item that when you right click on a block it checks the players inventory for more of that block and adds them in a line.

 

Its working fine for stone, white wool, etc, anything with a meta value of 0/default state.

 

How can I create an item stack of colored wool, stained glass, etc from a block in the world, onItemUse? or how can I get the actual meta int?

Link to comment
Share on other sites

This is the code I had before

public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side,
	float xOff, float yOff, float zOff)
{
	int  = 0, yD = 0, zD = 0;
	switch (side){
	case 0: yD = -1; break; case 2: zD = -1; break; case 4:  = -1; break;
	case 1: yD =  1; break; case 3: zD =  1; break; case 5:  =  1; break;}

	int len = (player.isSneaking()) ? sneakLength : length;
	Block block = world.getBlock(x, y, z);
	int meta = world.getBlockMetadata(x, y, z);

	for (int i = 1; i <= len; i++)
	{
		Block b = world.getBlock(x + i * , y + i * yD, z + i * zD);
		if ( ( ( (b == Blocks.air) || (b == Blocks.water) || (b == Blocks.flowing_water) ||
			(b == Blocks.lava) || (b == Blocks.flowing_lava) ) ) &&
			( (player.capabilities.isCreativeMode) ||
			(player.inventory.consumeInventoryItem(Item.getItemFromBlock(block))) ) )
		{
			world.setBlock(x + i * , y + i * yD, z + i * zD, block);
			if (block instanceof SecurityWall) ((SecurityWall)block).onBlockPlacedBy(
				world, x + i * , y + i * yD, z + i * zD, player, stack);
			if (meta != 0) world.setBlockMetadataWithNotify(x + i * , y + i * yD, z + i * zD, meta, 0);
			stack.damageItem(1, player);
		}
		else
		{
			return true;
		}
	}

	if ((side == 1) && (player.posX > x) && (player.posX < x+1) && (player.posZ > z) && (player.posZ < z+1) &&
		(player.posY > y) && (player.posX < y+1)) player.posY += (player.isSneaking()) ? sneakLength : length;

	return true;
}	
}

and this is the code now

public EnumActionResult onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumHand hand,
	EnumFacing facing, float hitX, float hitY, float hitZ)
    {		
	int  = 0, yD = 0, zD = 0;
	switch (facing) 
	{   case WEST:  = -1; break;	case DOWN: yD = -1; break;	case NORTH: zD = -1; break;
		case EAST:  = 1; break;	case UP: yD = 1; break;		case SOUTH: zD = 1; break;}

	for (int i = 1; i <= (player.isSneaking() ? length / 2 : length); i++)
	{
		Block block = world.getBlockState(new BlockPos(pos.getX() + i * , pos.getY() + i * yD, pos.getZ() + i * zD)).getBlock();

		if ((((block == Blocks.AIR) || (block == Blocks.WATER) || (block == Blocks.FLOWING_WATER) || (block == Blocks.LAVA) || (block == Blocks.FLOWING_LAVA))) &&
			((player.capabilities.isCreativeMode) || (RemoveItemFromInventory(player, world.getBlockState(pos)))))
		{
			stack.damageItem(1, player);
			world.setBlockState(new BlockPos(pos.getX() + i * , pos.getY()+ i * yD, pos.getZ() + i * zD), world.getBlockState(pos));
			//if (block instanceof SecurityWall) ((SecurityWall)block).onBlockPlacedBy(world, pos.getX() + i * , pos.getY() + i * yD, pos.getZ() + i * zD, player, stack);
		}
		else break;
	}

	if ((facing == EnumFacing.UP) && (player.posX > pos.getX()) && (player.posX < pos.getX()+1) && (player.posZ > pos.getZ()) && (player.posZ < pos.getZ()+1) &&
		(player.posY > pos.getY()) && (player.posX < pos.getY()+3)) player.posY += (player.isSneaking()) ? length / 2 : length;

	return EnumActionResult.SUCCESS;
}	
private boolean RemoveItemFromInventory(EntityPlayer player, IBlockState block)
{
	int slot = player.inventory.getSlotFor(new ItemStack(block.getBlock()));
	if (slot == -1) return false;

	ItemStack stack = player.inventory.getStackInSlot(slot);
	--stack.stackSize;
	if (stack.stackSize == 0) player.inventory.removeStackFromSlot(slot);
	return true;
}

Link to comment
Share on other sites

Edit this to your needs, I believe it works. Post back if it doesn't.

 

            IBlockState state = world.getBlockState(new BlockPos(pos.getX() + i * , pos.getY() + i * yD, pos.getZ() + i * zD));
            Block block = state.getBlock();
            int meta = block.getMetaFromState(state);

          world.setBlockState(new BlockPos(pos.getX() + i * xD, pos.getY()+ i * yD, pos.getZ() + i * zD), world.getBlockState(pos), meta);

 

The

World#setBlockState

has 2 methods, one that takes the BlockPos and the new state, and one that takes BlockPos, new state, and 'flags', which has yet to be localized to 'meta' I believe.

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

Link to comment
Share on other sites

and 'flags', which has yet to be localized to 'meta' I believe.

 

No.

 

Absolutely not.

 

"Flags" is documented quite well.

 

    /**
     * Sets the block state at a given location. Flag 1 will cause a block update. Flag 2 will send the change to
     * clients (you almost always want this). Flag 4 prevents the block from being re-rendered, if this is a client
     * world. Flags can be added together.
     */
    public boolean setBlockState(BlockPos pos, IBlockState newState, int flags)

  • Haha 1

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

Ah, didn't see that! Thanks for that..

 

Edit: So is there even a way to set meta to a block / state any more? I don't see any methods that actually change the meta of the block.

 

World#setBlockState

sets the

IBlockState

at the specified position, which includes the

Block

and any properties that are saved to metadata. You can read more on this here.

 

Use

Block#getStateForPlacement(World, BlockPos, EnumFacing, float, float, float, int, EntityLivingBase, EnumHand)

to get the

IBlockState

that would be placed by an entity's held item.

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.

Link to comment
Share on other sites

I think I'm all set now. Thanks for pointing me in the right direction. This is what I ended up with:

 

Excerpt

Block selectedBlock = world.getBlockState(pos).getBlock();
	int meta = selectedBlock.damageDropped(world.getBlockState(pos));
	ItemStack blockStack = new ItemStack(world.getBlockState(pos).getBlock(), 1, meta);

 

Full methods

@Override
public EnumActionResult onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumHand hand,
	EnumFacing facing, float hitX, float hitY, float hitZ)
    {		
	int  = 0, yD = 0, zD = 0;
	switch (facing) 
	{   case WEST:  = -1; break;	case DOWN: yD = -1; break;	case NORTH: zD = -1; break;
		case EAST:  = 1; break;	case UP: yD = 1; break;		case SOUTH: zD = 1; break;}

	for (int i = 1; i <= (player.isSneaking() ? length / 2 : length); i++)
	{
		Block block = world.getBlockState(new BlockPos(pos.getX() + i * , pos.getY() + i * yD, pos.getZ() + i * zD)).getBlock();

		if ((((block == Blocks.AIR) || (block == Blocks.WATER) || (block == Blocks.FLOWING_WATER) || (block == Blocks.LAVA) || (block == Blocks.FLOWING_LAVA))) &&
			((player.capabilities.isCreativeMode) || (RemoveItemFromInventory(player, world, pos))))
		{
			stack.damageItem(1, player);
			world.setBlockState(new BlockPos(pos.getX() + i * , pos.getY()+ i * yD, pos.getZ() + i * zD), world.getBlockState(pos));
			//if (block instanceof SecurityWall) ((SecurityWall)block).onBlockPlacedBy(world, pos.getX() + i * , pos.getY() + i * yD, pos.getZ() + i * zD, player, stack);
		}
		else break;
	}

	if ((facing == EnumFacing.UP) && (player.posX > pos.getX()) && (player.posX < pos.getX()+1) && (player.posZ > pos.getZ()) && (player.posZ < pos.getZ()+1) &&
		(player.posY > pos.getY()) && (player.posX < pos.getY()+3)) player.posY += (player.isSneaking()) ? length / 2 : length;

	return EnumActionResult.SUCCESS;
}	
private boolean RemoveItemFromInventory(EntityPlayer player, World world, BlockPos pos)
{
	Block selectedBlock = world.getBlockState(pos).getBlock();
	int meta = selectedBlock.damageDropped(world.getBlockState(pos));
	ItemStack blockStack = new ItemStack(world.getBlockState(pos).getBlock(), 1, meta);

	int slot = player.inventory.getSlotFor(blockStack);
	if (slot != -1)
	{
		ItemStack stack = player.inventory.getStackInSlot(slot);
		if (--stack.stackSize == 0) player.inventory.removeStackFromSlot(slot);
		return true;
	}
	else return false;
}

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.