Jump to content

Recommended Posts

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;
}

Posted

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.

Posted

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

×   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



×
×
  • Create New...

Important Information

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