Jump to content

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


Recommended Posts


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.


Link to comment
Share on other sites

That's not how you compare Item Stacks.


You should, in this case, be doing

hand.getItem() == Items.dirt

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

If the item is custom (yours):

Look at the ItemHoe#onItemUse();


You can literally copy that code and change stuff you need.


Note: you will use different methods for left or rightclick.


If item is vanilla:

1. Subscribe to PlayerInteractEvent, do needed checks (for nulls and item held) and do your stuff.


2. Subscribe to PlayerUseItemEvent and do same as above - note: there are sub-events there.


Or find other events, there are probably few more that would allow it.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

To actually replace the block use


world.setBlock(x, y, z) [1.7.X]

world.setBlock(pos) [1.8] (not sure if that's correct haven't updated to 1.8 yet)




In 1.8 you have to do it like this:


world.setBlockState(BlockPos, IBlockState);




IBlockState dirtBlock = Blocks.dirt.getDefaultState();

world.setBlockState(new BlockPos(x, y, z), dirtBlock);


or with metadata


IBlockState plankBlock = Blocks.planks.getStateFromMeta(3); //This will be a jungle plank

world.setBlockState(new BlockPos(x, y, z), plankBlock);


Hope this helps you when you decide to update to 1.8 :).


EDIT: Read what diesieben07 said.


Link to comment
Share on other sites

@Lewie: Don't use getStateFromMeta, it completely defeats the purpose of block states. The beauty is that with Block states you don't have a random number like 3, you use something like this:


IBlockState junglePlanks = Blocks.planks.getDefaultState().withProperty(BlockPlanks.VARIANT, BlockPlanks.EnumType.JUNGLE);


Oh wow that's a lot easier, thanks for that :D.

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.

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.

  • Create New...

Important Information

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