Jump to content

[1.15.2] Block onActivate triggering twice?


TechnoSam

Recommended Posts

I'm brand new to Forge and Minecraft modding, never done anything like this before.

 

As a test, I wanted to just print a message when the player clicks a dirt block. I created a simple block class:

 

public class BlockLogger extends Block {
    private static final Logger LOGGER = LogManager.getLogger();

    public BlockLogger(Properties properties) {
        super(properties);
    }

    @Override
    public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
        if (!worldIn.isRemote()) {
            LOGGER.warn("Clicked!");
            player.sendMessage(new StringTextComponent("Clicked!"));
        }
        return super.onBlockActivated(state, worldIn, pos, player, handIn, hit);
    }
}

 

I registered the block in the root class of my mod with the following:

 

Registry.register(Registry.BLOCK, "dirt", new BlockLogger(Block.Properties.create(Material.EARTH, MaterialColor.DIRT).hardnessAndResistance(0.5F).sound(SoundType.GROUND)));

 

Running this, if I right-click the block with an open hand or a tool, I get two "Clicked!" messages. If I right-click the block with another place-able block, I get only one message like I expect. This behavior persists regardless of whether or not I call the parent's onBlockActivated method, but that's not surprising, it doesn't seem that the parent does anything other than returning PASS.

 

What's going on here? I don't suspect a client / server issue since it does work with a place-able block. Have I registered it wrong? Is this just entirely the wrong way to do this? Or is this intended behavior?

 

Thanks.

 

 

Link to comment
Share on other sites

1 hour ago, TechnoSam said:

Hand handIn

The player has two hands.

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.

Link to comment
Share on other sites

Oh wow, I didn't even think about that. Thanks a bunch!

 

I added the hand to the chat message so I could investigate what was going on. It seems like it triggers once for each hand in almost all scenarios. It triggers with only the main hand when right clicking with a block in the main hand. With a block in the off hand, it still triggers once for each hand. I can't manage to get only off hand.

 

What's the logic there? If I place a block from my off hand, wouldn't I want to only get one event? It seems like that makes it impossible to build an action that happens only when clicking with the off hand. (Not that I want to do that, I'm just trying to understand how the system works.)

Link to comment
Share on other sites

The main hand is checked. If something happens with the item or block as a result of the main hand (ItemActionResult.SUCCESS), the off hand is not checked.

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.

Link to comment
Share on other sites

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.