Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

 

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.

???

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.

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.

OR

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.

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)

 

FYI:

 

In 1.8 you have to do it like this:

 

world.setBlockState(BlockPos, IBlockState);

 

Example:

 

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.

 

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

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.