Jump to content

[1.12.2] Preventing block breaking on left click with item


finley243

Recommended Posts

Hello there, I am trying to replicate the functionality of sword items in that left clicks while holding the item don't destroy blocks in creative. I dug around a bit in the Item.Sword class but don't see anything that would cause this functionality. (My guess is it's handled somewhere else, but I don't know where.) If anyone knows how to implement this for a new item type (or where the code that does this for swords is located), please respond. Thank you!

Link to comment
Share on other sites

Another thing you can do is Subscribe to the BlockEvent.BreakEvent and check if the player is holding your item and cancel that event. It doesn't have quite the same effect as the sword in creative, but it won't allow the player to break that block if he is holding the item.

 

@SubscribeEvent
public static void onBlockBreak(BlockEvent.BreakEvent event){
    PlayerEntity player = event.getPlayer();
    if(player != null){
        Item item = player.getHeldItemMainhand().getItem();
        if(item instanceof *youritemhere*){
            event.setCanceled(true);
        }
    }
}
  • Like 1
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.

×
×
  • Create New...

Important Information

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