Jump to content

Changing a metadata block to another metadata block when right clicking.


Thor597

Recommended Posts

Ok, so I have the code set up and I figured out one of the things which is how to make a block turn into a metadata block; But what I need is to make a metadata block turn into another metadata block

 


public boolean onItemUse( ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7)
    {
            if (!par2EntityPlayer.canPlayerEdit(par4, par5, par6))
            {
                    return false;
            }

            int i = par3World.getBlockId(par4, par5, par6);

            if (par5 != 0 && i == BlockThatGetsChangedIntoTheBlockBelowAndThatNeedsToHaveADamageValue.blockID)
            {
                    Block block = BlockThatGetsPutInWorldWhenYouRightClickBlockAboveThisOneAlreadyHasMetadata;

                    par3World.playSoundEffect((float)par4 + 0.5F, (float)par5 + 0.5F, (float)par6 + 0.5F, block.stepSound.getStepSound(), (block.stepSound.getVolume() + 1.0F) / 2.0F, block.stepSound.getPitch() * 0.8F);

                    if (par3World.isRemote)
                    {
                            return true;
                    }
                    else
                    {
                            par3World.setBlockAndMetadataWithNotify(par4, par5, par6, block.blockID, 5); <The int at the end of this is the damage value
                            par1ItemStack.damageItem(1, par2EntityPlayer);
                            return true;
                    }

 

So basically I need the block: BlockThatGetsChangedIntoTheBlockBelowAndThatNeedsToBeAMetadataBlock.blockID to be able to have a damage value!

 

Thanks for reading, please help! :D

Link to comment
Share on other sites

just get the blocks location and use code too place a new block with a different value in that spot.

par3World.setBlockWithNotify(x, y, z, BlockID)

there is a meta data version of the above but i don't have time to find it sorry.

 

I have already tried setBlockAndMetadataWithNotify it doesnt work, and I cant put it a damage value... :|

Link to comment
Share on other sites

ItemStack item = par5EntityPlayer.getCurrentEquippedItem();
    	int a = world.getBlockMetadata(x, y, z);
    	
    	if(item!=null){
    		int itemID = item.itemID;
    			if(itemID == Item.bucketWater.shiftedIndex){
        	
    				if(a < 4){
    					
    					world.setBlockAndMetadataWithNotify(x, y, z, mod_cutter.storageTank.blockID, a+1);
    					par5EntityPlayer.inventory.setInventorySlotContents(par5EntityPlayer.inventory.currentItem, new ItemStack(Item.bucketEmpty));
    					return true;
    					
    				}
        	
    			}else if(itemID == Item.bucketEmpty.shiftedIndex){
    	        	
    	    		if(a > 0){
    	    					
    	    			world.setBlockAndMetadataWithNotify(x, y, z, mod_cutter.storageTank.blockID, a-1);
    	    			par5EntityPlayer.inventory.setInventorySlotContents(par5EntityPlayer.inventory.currentItem, new ItemStack(Item.bucketWater));
    	    			return true;
    	    					
    	    		}
    	        	
    	    	}
    			
    	}
        return false;

 

works for me. i have been messing around with the code for the past few days, trying to recreate a storage tank similar to buildcrafts without looking at their code. having problems with filling it with actual water though (about to post a thread for help)

Link to comment
Share on other sites

ItemStack item = par5EntityPlayer.getCurrentEquippedItem();
    	int a = world.getBlockMetadata(x, y, z);
    	
    	if(item!=null){
    		int itemID = item.itemID;
    			if(itemID == Item.bucketWater.shiftedIndex){
        	
    				if(a < 4){
    					
    					world.setBlockAndMetadataWithNotify(x, y, z, mod_cutter.storageTank.blockID, a+1);
    					par5EntityPlayer.inventory.setInventorySlotContents(par5EntityPlayer.inventory.currentItem, new ItemStack(Item.bucketEmpty));
    					return true;
    					
    				}
        	
    			}else if(itemID == Item.bucketEmpty.shiftedIndex){
    	        	
    	    		if(a > 0){
    	    					
    	    			world.setBlockAndMetadataWithNotify(x, y, z, mod_cutter.storageTank.blockID, a-1);
    	    			par5EntityPlayer.inventory.setInventorySlotContents(par5EntityPlayer.inventory.currentItem, new ItemStack(Item.bucketWater));
    	    			return true;
    	    					
    	    		}
    	        	
    	    	}
    			
    	}
        return false;

 

works for me. i have been messing around with the code for the past few days, trying to recreate a storage tank similar to buildcrafts without looking at their code. having problems with filling it with actual water though (about to post a thread for help)

 

Im trying to make a metadata block into another metadata block by right clicking said block with an item.

I already got the block that it turns into working with metadata but not the one you click. I dont know how to add in a damage value to the block that you right click to make into the second block. SetBlockMetadataWithNotify only works on the block that it turns into not the actual block you click. Thank you!

Link to comment
Share on other sites

ItemStack item = par5EntityPlayer.getCurrentEquippedItem();
    	int a = world.getBlockMetadata(x, y, z);
    	
    	if(item!=null){
    		int itemID = item.itemID;
    			if(itemID == Item.bucketWater.shiftedIndex){
        	
    				if(a < 4){
    					
    					world.setBlockAndMetadataWithNotify(x, y, z, mod_cutter.storageTank.blockID, a+1);
    					par5EntityPlayer.inventory.setInventorySlotContents(par5EntityPlayer.inventory.currentItem, new ItemStack(Item.bucketEmpty));
    					return true;
    					
    				}
        	
    			}else if(itemID == Item.bucketEmpty.shiftedIndex){
    	        	
    	    		if(a > 0){
    	    					
    	    			world.setBlockAndMetadataWithNotify(x, y, z, mod_cutter.storageTank.blockID, a-1);
    	    			par5EntityPlayer.inventory.setInventorySlotContents(par5EntityPlayer.inventory.currentItem, new ItemStack(Item.bucketWater));
    	    			return true;
    	    					
    	    		}
    	        	
    	    	}
    			
    	}
        return false;

 

works for me. i have been messing around with the code for the past few days, trying to recreate a storage tank similar to buildcrafts without looking at their code. having problems with filling it with actual water though (about to post a thread for help)

 

Im trying to make a metadata block into another metadata block by right clicking said block with an item.

I already got the block that it turns into working with metadata but not the one you click. I dont know how to add in a damage value to the block that you right click to make into the second block. SetBlockMetadataWithNotify only works on the block that it turns into not the actual block you click. Thank you! BTW there should've been a getBlockIdAndMetadata...

Link to comment
Share on other sites

ItemStack item = par5EntityPlayer.getCurrentEquippedItem();
    	[b]int a = world.getBlockMetadata(x, y, z);[/b]
    	
    	if(item!=null){
    		int itemID = item.itemID;
    			if(itemID == Item.bucketWater.shiftedIndex){
        	
    				if(a < 4){
    					
    					[b]world.setBlockAndMetadataWithNotify(x, y, z, mod_cutter.storageTank.blockID, a+1);[/b]
    					par5EntityPlayer.inventory.setInventorySlotContents(par5EntityPlayer.inventory.currentItem, new ItemStack(Item.bucketEmpty));
    					return true;
    					
    				}
        	
    			}else if(itemID == Item.bucketEmpty.shiftedIndex){
    	        	
    	    		if(a > 0){
    	    					
    	    			world.setBlockAndMetadataWithNotify(x, y, z, mod_cutter.storageTank.blockID, a-1);
    	    			par5EntityPlayer.inventory.setInventorySlotContents(par5EntityPlayer.inventory.currentItem, new ItemStack(Item.bucketWater));
    	    			return true;
    	    					
    	    		}
    	        	
    	    	}
    			
    	}
        return false;

 

works for me. i have been messing around with the code for the past few days, trying to recreate a storage tank similar to buildcrafts without looking at their code. having problems with filling it with actual water though (about to post a thread for help)

 

Im trying to make a metadata block into another metadata block by right clicking said block with an item.

I already got the block that it turns into working with metadata but not the one you click. I dont know how to add in a damage value to the block that you right click to make into the second block. SetBlockMetadataWithNotify only works on the block that it turns into not the actual block you click. Thank you! BTW there should've been a getBlockIdAndMetadata...

"BTW there should've been a getBlockIdAndMetadata..." = int a = world.getBlockMetadata(x, y, z);

 

"Im trying to make a metadata block into another metadata block by right clicking said block with an item." = world.setBlockAndMetadataWithNotify(x, y, z, mod_cutter.storageTank.blockID, a+1);

 

the "a+1" part changes the current blocks metadata to 1 higher, which is exactly what your looking for. unless the problem is different to what you described.

Link to comment
Share on other sites

Taking my previous example, how would i then specify that the planks im right clicking are birch planks and not any of the other planks?

world.setBlockAndMetadata(x, y, z,blockID, metaData);

 

say planks blockID is 50, and the metaData for the birch is 3 and you want to change the block at (0,0,0)

world.setBlockAndMetadata(0, 0, 0, 50, 3)

 

if you dont understand that method, then i think you should go back over metadata and relearn it.

Link to comment
Share on other sites

Taking my previous example, how would i then specify that the planks im right clicking are birch planks and not any of the other planks?

world.setBlockAndMetadata(x, y, z,blockID, metaData);

 

say planks blockID is 50, and the metaData for the birch is 3 and you want to change the block at (0,0,0)

world.setBlockAndMetadata(0, 0, 0, 50, 3)

 

if you dont understand that method, then i think you should go back over metadata and relearn it.

 

Read the first post and my first code.

 

I cant 'set' it because its not the block that it sets its the block that is already there; thats why I have GETBlockId there at the first block however on the second one(the one that i set when right clicked) logically enough has SET because it sets the block there. The birch planks, taking my example are not the block that gets set when the first one gets clicked but its actually the block getting clicked. What I have problems with is to make it so that birch planks in this case and not any of the other planks gets turned into magenta wool;and as said before, I already have the damage value for the block It TURNS INTO. So before you start being a dick, get your facts straight.

 

 

EDIT: If i am just misunderstanding what you've said, I deeply apologize and I can only ask of you to maybe tell me where to put the setBlockAndMetadata method and in what context, also; can you then make your code example like how we discussed it, with the birch plank/magenta wool thing? Its easier to understand that way.  Thanks!

Link to comment
Share on other sites

Taking my previous example, how would i then specify that the planks im right clicking are birch planks and not any of the other planks?

world.setBlockAndMetadata(x, y, z,blockID, metaData);

 

say planks blockID is 50, and the metaData for the birch is 3 and you want to change the block at (0,0,0)

world.setBlockAndMetadata(0, 0, 0, 50, 3)

 

if you dont understand that method, then i think you should go back over metadata and relearn it.

 

Read the first post and my first code.

 

I cant 'set' it because its not the block that it sets its the block that is already there; thats why I have GETBlockId there at the first block however on the second one(the one that i set when right clicked) logically enough has SET because it sets the block there. The birch planks, taking my example are not the block that gets set when the first one gets clicked but its actually the block getting clicked. What I have problems with is to make it so that birch planks in this case and not any of the other planks gets turned into magenta wool;and as said before, I already have the damage value for the block It TURNS INTO. So before you start being a dick, get your facts straight.

 

 

EDIT: If i am just misunderstanding what you've said, I deeply apologize and I can only ask of you to maybe tell me where to put the setBlockMetadata method and in what context, also; can you then make your code example like how we discussed it, with the birch plank/magenta wool thing? Thanks!

 

no, i was being an idiot. but from the sounds of it, all your looking for is an if condition checking to make sure that the metadata is of a certain value, yes?

 

in which case int birchPlankMetaData = world.getBlockMetadata(x, y, z); will get the metadata of the block being clicked on because you get the location of the block from the parameters of blockActivated.

 

then use a simple if statement to check whether birchPlankMetaData is equal to the metadata value you need.

 

public boolean blockActivated(World world, int x, int y, int z, EntityPlayer par5EntityPlayer){

    	int birchPlankMetaData = world.getBlockMetadata(x, y, z);
    	
    	if(birchPlankMetaData==0){
    		
    		world.setBlockAndMetadata(x, y, z, Block.cloth.blockID, 3);
    		return true;
    		
    	}
    	return false;
    }

as simple as that i think. worked for changing a meta data item i had into lightblue wool.

Link to comment
Share on other sites

Yes, this seems nice for that purpose, but -not to be ungrateful or anything- you see, the reason I was using an onItemUse method is because of these reasons:

 

1: If i were to use an onBlockActivated method I would have to edit base code which you know, no one would like to do

 

2: I want the tool im using onItemUse in to be the only tool able to do this.

 

3: I want to make the tool take damage when doing it

 

I think maybe 2 and 3 could be solved in a way with onBlockActivated, but I really really do not want to edit base code. Thanks anyway!

Link to comment
Share on other sites

Yes, this seems nice for that purpose, but -not to be ungrateful or anything- you see, the reason I was using an onItemUse method is because of these reasons:

 

1: If i were to use an onBlockActivated method I would have to edit base code which you know, no one would like to do

 

2: I want the tool im using onItemUse in to be the only tool able to do this.

 

3: I want to make the tool take damage when doing it

 

I think maybe 2 and 3 could be solved in a way with onBlockActivated, but I really really do not want to edit base code. Thanks anyway!

 

oh i didnt realise =.= its really late for me now so im going to bed, but if you dont find a fix for it in the next 12 hours or so, then i will look and post one with the 'onItemUse' method.

Link to comment
Share on other sites

public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World world, int x, int y, int z, int par7)
    {
    	int metaDataOfTargetBlock = world.getBlockMetadata(x, y, z);
    	
    	if(metaDataOfTargetBlock==1){
    		
    		world.setBlockAndMetadata(x, y, z, Block.cloth.blockID, 3);
    		return true;
    		
    	}
    	return false;
    }

 

this code works if you just add it to an item as an overide. BECAREFUL though, if you set the metaDataOfTargetBlock == 0, all blocks in the world will be changed as most have a metadata of 0. although, im pretty sure if you just add in the target blocks blockID into the if statement aswell, you can prevent that. i think par7 is metaData being provided from the tool itself, but i dont know.

Link to comment
Share on other sites

public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World world, int x, int y, int z, int par7)
    {
    	int metaDataOfTargetBlock = world.getBlockMetadata(x, y, z);
    	
    	if(metaDataOfTargetBlock==1){
    		
    		world.setBlockAndMetadata(x, y, z, Block.cloth.blockID, 3);
    		return true;
    		
    	}
    	return false;
    }

 

this code works if you just add it to an item as an overide. BECAREFUL though, if you set the metaDataOfTargetBlock == 0, all blocks in the world will be changed as most have a metadata of 0. although, im pretty sure if you just add in the target blocks blockID into the if statement aswell, you can prevent that. i think par7 is metaData being provided from the tool itself, but i dont know.

 

Thank you SOOOOO much! This is my finished code that works:

public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World world, int x, int y, int z, int par7)
    {
    	
        int i = world.getBlockId(x, y, z);

        if (y != 0 && i == Block.planks.blockID)
        {   	
    	int metaDataOfTargetBlock = world.getBlockMetadata(x, y, z);
    	
    	if(metaDataOfTargetBlock == 1){
    		
    		Block block = Block.cloth;	   		
    		world.playSoundEffect((float)x + 0.5F, (float)y + 0.5F, (float)z + 0.5F, block.stepSound.getStepSound(), (block.stepSound.getVolume() + 1.0F) / 2.0F, block.stepSound.getPitch() * 0.8F);	    		
    		world.setBlockAndMetadataWithNotify(x, y, z, Block.cloth.blockID, 3);
    		return true;
    	}
    		
    }
    	return false;
    }	

 

 

The only wrong thing in your code there was the world.setBlockAndMetadata, it shouldve been world.setBlockAndMetadataWithNotify but i've fixed that up now!

Link to comment
Share on other sites

public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World world, int x, int y, int z, int par7)
    {
    	int metaDataOfTargetBlock = world.getBlockMetadata(x, y, z);
    	
    	if(metaDataOfTargetBlock==1){
    		
    		world.setBlockAndMetadata(x, y, z, Block.cloth.blockID, 3);
    		return true;
    		
    	}
    	return false;
    }

 

this code works if you just add it to an item as an overide. BECAREFUL though, if you set the metaDataOfTargetBlock == 0, all blocks in the world will be changed as most have a metadata of 0. although, im pretty sure if you just add in the target blocks blockID into the if statement aswell, you can prevent that. i think par7 is metaData being provided from the tool itself, but i dont know.

 

Thank you SOOOOO much! This is my finished code that works:

public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World world, int x, int y, int z, int par7)
    {
    	
        int i = world.getBlockId(x, y, z);

        if (y != 0 && i == Block.planks.blockID)
        {   	
    	int metaDataOfTargetBlock = world.getBlockMetadata(x, y, z);
    	
    	if(metaDataOfTargetBlock == 1){
    		
    		Block block = Block.cloth;	   		
    		world.playSoundEffect((float)x + 0.5F, (float)y + 0.5F, (float)z + 0.5F, block.stepSound.getStepSound(), (block.stepSound.getVolume() + 1.0F) / 2.0F, block.stepSound.getPitch() * 0.8F);	    		
    		world.setBlockAndMetadataWithNotify(x, y, z, Block.cloth.blockID, 3);
    		return true;
    	}
    		
    }
    	return false;
    }	

 

 

The only wrong thing in your code there was the world.setBlockAndMetadata, it shouldve been world.setBlockAndMetadataWithNotify but i've fixed that up now!

 

ah, in my original code im using world.setBlockAndMetadataWithNotify, not sure how it changed. anyways, np, glad i could help ^_^

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.