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 have created a wrench item. When right-clicking a rotatable block, such as a piston, with said wrench, it rotates the block. This works fine but;
If I rotate the block during 'onItemUse' and the block has an inventory, the inventory is shown - is there a way I can stop that but still rotate the block?
If I rotate the block during 'onItemUseFirst' no gui is opened but the block resets after a block update - is there a way to make the rotation stick, or is this just the wrong place to do the rotation.

I have my own rotate code, but for testing purposes I am just using the default method and still getting the results stated above:

    @Override
    public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
    {
    	if (hand == EnumHand.OFF_HAND)
    	{
    		return EnumActionResult.FAIL;
    	}

    	Block block = world.getBlockState(pos).getBlock();
    	if (!player.canPlayerEdit(pos, facing, player.getHeldItem(hand)))
    	{
    		return EnumActionResult.PASS;
    	}
    	
    	if (block.getValidRotations(world, pos) != null)
    	{
            if (!player.capabilities.isCreativeMode)
            {
                player.getHeldItem(hand).damageItem(1, player);
            }
            block.rotateBlock(world, pos, facing);
    		SoundType soundtype = block.getSoundType(world.getBlockState(pos), world, pos, player);
            world.playSound(player, pos, soundtype.getHitSound(), SoundCategory.BLOCKS, soundtype.getVolume() * 0.5F, soundtype.getPitch() * 0.75F);
        	player.swingArm(hand);
        	return EnumActionResult.SUCCESS;
    	}
    	return EnumActionResult.PASS;
    }

 

Edited by PegBeard

  • Author

Not meaning to sound rude, but I had figured that much out and it doesn't really answer my question.
Are you saying that there is no good way to get the rotation to stick when using  onItemUseFirst and no good way to give my item preference with  onItemUse except when sneaking?

 

I don't mind sticking with  onItemUse and sneaking, I am just curious if there is a way to do what I am asking, for versatility reasons.

30 minutes ago, diesieben07 said:

If you want the item to always rotate the block, even if it has a GUI and even if the player is not sneaking (this is counter-intuitive!)

To a degree. 

I can see the rational both ways, but I'd have to play with it for a while before I could decide if the counter-intuitive method would cause me more frustrating than the must-crouch method. 

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.

  • Author

It was identical to the code shown above, but in the  onItemUseFirst method. So I didn't want to over complicate the original post.

 

    @Override
    public EnumActionResult onItemUseFirst(EntityPlayer player, World world, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, EnumHand hand)
    {
    	if (hand == EnumHand.OFF_HAND)
    	{
    		return EnumActionResult.FAIL;
    	}

    	Block block = world.getBlockState(pos).getBlock();
    	if (!player.canPlayerEdit(pos, facing, player.getHeldItem(hand)))
    	{
    		return EnumActionResult.PASS;
    	}
    	
    	if (block.getValidRotations(world, pos) != null)
    	{
            if (!player.capabilities.isCreativeMode)
            {
                player.getHeldItem(hand).damageItem(1, player);
            }
            block.rotateBlock(world, pos, facing);
    		SoundType soundtype = block.getSoundType(world.getBlockState(pos), world, pos, player);
            world.playSound(player, pos, soundtype.getHitSound(), SoundCategory.BLOCKS, soundtype.getVolume() * 0.5F, soundtype.getPitch() * 0.75F);
        	player.swingArm(hand);
        	return EnumActionResult.SUCCESS;
    	}
    	return EnumActionResult.PASS;
    }

When doing the rotation this way, it appears to rotate, but when the block is updated, it reverts the rotation.

Edited by PegBeard

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.