Posted January 24, 20169 yr 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; }
January 24, 20169 yr 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".
January 24, 20169 yr 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.
January 25, 20169 yr 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.