Make a distinction between PlayerInteractEvent and BlockPlaceEvent

Discussion in 'Plugin Development' started by MCMastery, Feb 20, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    MCMastery

    I am writing a claims plugin and need a way to see if players are placing blocks or just interacting with blocks. Unfortunately, PlayerInteractEvent gets called either way. Anyone have any ideas on a clean way to only perform the PlayerInteractEvent action if they weren't placing a block? Thanks
     
  2. Offline

    CraftCreeper6

    @MCMastery
    Code:
    player#getTargetBlock((Set<Material>) null, 5);
    For distinction, the (Set<Material>) null is not deprecated as oppose, a singular null is.

    If the players target block is null, they are not trying to place a block.
     
  3. Offline

    MCMastery

    It appears to never return null? I tested right clicking buttons and placing blocks and it says not null both times:
    Code:
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onPlayerInteract(PlayerInteractEvent evt) {
            if (evt.hasBlock() && (evt.getAction() == Action.PHYSICAL || (evt.getAction() == Action.RIGHT_CLICK_BLOCK))) {
                // we aren't placing a block
                if (evt.getPlayer().getTargetBlock((Set<Material>) null, 5) == null) {
                    evt.getPlayer().sendMessage("null");
                    // ...
                } else {
                    evt.getPlayer().sendMessage("not null");
                }
            }
        }

    EDIT: I'll try out Material::isBlock on the held item and Material::isInteractable on the clicked block

    EDIT 2: This appears to work? Needs more testing:
    Code:
    if (evt.getAction() == Action.RIGHT_CLICK_BLOCK && evt.hasItem() && evt.hasBlock() && evt.getItem().getType().isBlock() && (!evt.getClickedBlock().getType().isInteractable() || evt.getPlayer().isSneaking())) {
        evt.getPlayer().sendMessage("You are placing a block!");
        return;
    }
    It does take into account people sneaking to right-click on interactable blocks to place blocks on them (like shift-clicking a furnace to place a block on the furnace)
     
    Last edited: Feb 21, 2020
Thread Status:
Not open for further replies.

Share This Page