Problem with RIGHT_CLICK_BLOCK

Discussion in 'Plugin Development' started by SamJoll, Apr 7, 2021.

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

    SamJoll

    Hello everyone.
    I want to add func when player uses certain item and after column of blocks appears with height equal 6, but RIGHT_CLICK_BLOCK called twice and set 7-height pillar and also removed 2 items instead of 1.
    I don't register event twice and I tried check main hand
    [​IMG]
    Code presents below

    Code:
    @EventHandler
        public void onItemUse(PlayerInteractEvent e) {
            if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                if(EquipmentSlot.HAND.equals(e.getHand()) && flag_basic.targetItem.isSimilar(e.getItem())) {
                    Location basicOrigin = e.getClickedBlock().getLocation();
                    e.getItem().setAmount(e.getItem().getAmount() - 1);
    
                    for (int iB = 0; iB < 6; iB++) {
                        basicOrigin.add(0, 1, 0);
    
                        Bukkit.getServer().getWorld(e.getPlayer().getWorld().getName()).getBlockAt(basicOrigin).setType(Material.OAK_LOG);
    
                    }
                }
            }
        }
    
     
  2. Offline

    CruzAPI

    Try this code, it worked for me.
    Tested in 1.7.10 version.

    Use "==" instead of .equals() to compare Enums.

    Code:
    @EventHandler
    public void onItemUse(PlayerInteractEvent e)
    {
        if(e.getItem().getType() == Material.STICK && e.getAction() == Action.RIGHT_CLICK_BLOCK)
        {
            Block b = e.getClickedBlock();
            e.getItem().setAmount(e.getItem().getAmount() - 1);
           
            for(int iB = 1; iB <= 6; iB++)
            {
                b.getRelative(BlockFace.UP, iB).setType(Material.LOG);
            }
        }
    }
    
     
  3. Offline

    Amster

    If it runs twice you might be registering the pluginmanager registerevents function twice. Check in your onEnable() that that isn't true.
     
  4. Offline

    timtower Administrator Administrator Moderator

    @SamJoll Check which hand it is, it is firing for the main and offhand.
     
Thread Status:
Not open for further replies.

Share This Page