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've made an item and I've got the code for the item to open an iron door, but it only makes the sound when I right click on the iron door.

 

Here's my code

@Override
    public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
{
	if(!world.isRemote){
		if(world.getBlockState(pos).getBlock() instanceof BlockDoor) {
			String sound;
			if(world.getBlockState(pos).getValue(BlockDoor.HALF) == EnumDoorHalf.UPPER){
				world.setBlockState(pos.down(), world.getBlockState(pos.down()).withProperty(BlockDoor.OPEN, world.getBlockState(pos.down()).getValue(BlockDoor.OPEN)));
				sound = (Boolean) world.getBlockState(pos.down()).getValue(BlockDoor.OPEN) ? "random.door_close" : "random.door_open";
				world.playSoundAtEntity(player, sound, 1.0f, 1.0f);
				return true;
			} else {
				world.setBlockState(pos, world.getBlockState(pos).withProperty(BlockDoor.OPEN, world.getBlockState(pos).getValue(BlockDoor.OPEN)));
				sound = (Boolean) world.getBlockState(pos).getValue(BlockDoor.OPEN) ? "random.door_close" : "random.door_open";
				world.playSoundAtEntity(player, sound, 1.0f, 1.0f);
				return true;
			}
		}
	}
	return true;
}

  • Author

Also, if I remove the (boolean) before the world.getBlockstate stuff, it suggests to either add "!= null" after that line or make that line a "if/else".

Are you telling it to update the block?

 

Also, your else statement is checking for the exact same blockState as your if statement. You also never check to see if the blockState is set to .CLOSED in your if statement.

 

Your code is only ever checking for the .OPEN state. This may be why it's not updating.

 

The other glaringly obvious issue is you return true everywhere. Your main if(!world.isremote) should return false.

Your code is to complex maybe you may find the issue if you write it simpler, this is your code:

				world.setBlockState(pos.down(), world.getBlockState(pos.down()).withProperty(BlockDoor.OPEN, world.getBlockState(pos.down()).getValue(BlockDoor.OPEN)));

Simplified:

BlockPos down = pos.down();
IBlockState state = world.getBlockState(down);
boolean open = state.getValue(BlockDoor.OPEN);
state = state.withProperty(BlockDoor.OPEN, open);
world.setBlockState(down, state);

Now read that carefully, and No I did not make any typos when translating your code.

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Consider supporting the team on Patreon

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.