Jump to content

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


Recommended Posts

Posted

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

Posted
  On 7/19/2012 at 6:03 PM, DarkGuardsman said:

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... :|

Posted

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)

Posted
  On 7/19/2012 at 8:55 PM, Cutter said:

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!

Posted
  On 7/19/2012 at 10:10 PM, Thor597 said:

  Quote

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...

Posted
  On 7/19/2012 at 10:11 PM, Thor597 said:

  Quote

  Quote

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.

Posted
  On 7/20/2012 at 12:17 AM, Thor597 said:

No, my problem wasnt changing 1 damage value up, it was to change 1 metadata block into one completely other one, EG from birch wood planks to magenta wool.

just use world.setBlock(x,y,z,blockID); then.

Posted
  On 7/20/2012 at 12:27 AM, Thor597 said:

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.

Posted
  On 7/20/2012 at 12:31 AM, Cutter said:

  Quote

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!

Posted
  On 7/20/2012 at 12:45 AM, Thor597 said:

  Quote

  Quote

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.

Posted

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!

Posted
  On 7/20/2012 at 1:35 AM, Thor597 said:

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.

Posted

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.

Posted
  On 7/20/2012 at 10:19 PM, Cutter said:

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!

Posted
  On 7/20/2012 at 11:00 PM, Thor597 said:

  Quote

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 ^_^

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

    • One fateful day, my life took an unexpected turn when I received a phone call that would change everything. The voice on the other end claimed to be from my bank, delivering alarming news: my account had been frozen due to suspicious activity. Panic surged through me as I listened, my heart racing at the thought of losing my hard-earned savings. At that moment, I had about 130,000 USD in my bank, equivalent to around 2 BTC. The caller spoke with such authority and urgency that I felt compelled to act immediately. They insisted that the only way to protect my funds was to transfer Bitcoin BTC to them for "safekeeping. In my fear and confusion, I believed I was making a wise decision to secure my finances. Without fully grasping the implications, I complied and transferred the equivalent of my savings in Bitcoin, convinced I was safeguarding my money. It wasn’t until later that the reality of my situation hit me like a ton of bricks. I had been duped, and the weight of my mistake was unbearable. Shame and disbelief washed over me as I realized how easily I had been manipulated. How could I have let this happen? The feeling of vulnerability was overwhelming, and I was left grappling with the consequences of my actions. I learned about a recovery expert named RAPID DIGITAL RECOVERY. Desperate to reclaim what I had lost, I reached out for help. RAPID DIGITAL RECOVERY was knowledgeable and reassuring, explaining that there was a chance to trace the Bitcoin I had sent. With their expertise, they tracked the stolen funds to a peer-to-peer (P2P) exchanger based in the United Kingdom. This revelation sparked a glimmer of hope within me, a sense that perhaps justice could be served. RAPID DIGITAL RECOVERY collaborated with Action Fraud, the UK's national reporting center for fraud and cybercrime, to take decisive action against the scammers. Knowing that law enforcement was involved provided me with a sense of relief. The thought that the culprits behind my suffering could be brought to justice was comforting. In an incredible turn of events, RAPID DIGITAL RECOVERY successfully recovered all my funds, restoring my faith in the possibility of justice and recovery.
    • My game crashed in 1.12.2 here is the crash log https://pastebin.com/6MYu4mGy
    • I created a Modpack Forge in 1.20.1 for my friend and I. There are 135 mods including "Essential". I was able to play an 8 hour session without problem but when I relaunch my world, I crashed when I opened the menu of the game "ESC" or after about 15 minutes of session. I can't find the source of the problem. Latest.log and Debug.log : https://paste.ee/p/B0npvlRw
    • Hello! Faced with the same problem. Can you please describe in more detail how you rewrote the toNetwork and fromNetwork methods?
    • Why not?   Please explain what you have tried, in detail. Step by step is installing the server, placing mod .jar files in the mods folder within the folder you installed the server, and running the run.bat file. If this is not working for you, please post the debug.log from the logs folder to a site like https://mclo.gs and post the link to it here.
  • Topics

×
×
  • Create New...

Important Information

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