halmi4
halmi4

Reputation: 334

Minecraft forge mod: Remove a block and place a new block

I am currently making a minecraft forge mod. Do anyone know how to first remove a block on specific coordinates, and then place a new block on the same place? Like replacing the old block with a new block.

I will have it in the middle of this code:

if(player.getCurrentEquippedItem() != null)
  {

     if(hand.getItem() == Items.dirt)
     {

     }

  }

the Item.dirt is just a test

So if the player hold a specific item(i use dirt at the moment) and right click on the block, something will happend. Btw i have more code over that code that makes that happend when the player right click on the block.

I've Googled for it and didn't find anything.

Upvotes: 0

Views: 6637

Answers (1)

Steve Shipway
Steve Shipway

Reputation: 4072

If the specific item is a custom one, then it is much simpler.

In your custom item, set the onItemUse (for right-click) or onItemClick (for left click) function:

public boolean onItemUse(ItemStack item, EntityPlayer player, World world,
    // which block was in the target when clicked
    int posx, int posy, int posz, 
    // where on the target block was clicked (0.0-1.0)
    int side, float blockx, float blocky, float blockz)
{
    if(world.isRemote) {
        // can't do anything here as we don't own the world (client side)
        return false;
    }
    // server side - here we can change the block

This function is passed the coordinates of the block being clicked on, and the world object. Then, you can test the block being clicked, and do anything with it (this example sets it to be dirt):

rv = world.setBlock(posx,posy,posz,Blocks.dirt);

Of course, if you place this in the custom item's own onItemUse function, then you don't need to test to make sure that the item is equipped, since it must be in order to call the function.

Upvotes: 2

Related Questions