Rightclick event on a far away player

Discussion in 'Plugin Development' started by Themiller, Apr 15, 2020.

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

    Themiller

    Hi ! I tried to find the answer for hours on many forums but I failed..

    I would like to make a vote system in a plugin by right clicking on others players BUT they can be far away (maybe maximum 20 blocks away).

    Basically, this is working :

    Code:
    @EventHandler
    public void RightClick(PlayerInteractEntityEvent e)
    {
        Player p = e.getPlayer();
        if(e.getRightClicked() instanceof Player) {
            // Add the player UUID to HashMap
        }
    }
    But ONLY if the player is near...

    What can I do ? I thought about shooting with right click, then cancelling the damage and get the uuid of the player hit?

    Thank you for your help ❤
     
  2. Offline

    KarimAKL

  3. Offline

    Themiller

    Alright Thank you ! I created Ray and AABB Classes and I did this :

    Code:
    public static Player getTargetPlayer(Player player, int max) {
        List<Player> possible = player.getNearbyEntities(max, max, max).stream().filter(entity -> entity instanceof Player).map(entity -> (Player) entity).filter(player::hasLineOfSight).collect(Collectors.toList());
        Ray ray = Ray.from(player);
        double d = -1;
        Player closest = null;
           for (Player player1 : possible) {
               double dis = AABB.from(player1).collidesD(ray, 0, max);
               if (dis != -1) {
                   if (dis < d || d == -1) {
                       d = dis;
                       closest = player1;
                   }
               }
          }
        return closest;
    }
    
    
    @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
    public void onPlayerInteract(PlayerInteractEvent e) {
          Player p = e.getPlayer();
          if ((e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) || (e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK)) {
                Bukkit.broadcastMessage("You voted for : " + getTargetPlayer(p, 2));
         }
       }
    But it always return a null value... What is wrong in my code ?

    Thank you again for your help :)
     
    Last edited: Apr 16, 2020
  4. Offline

    KarimAKL

    @Themiller Unfortunately i've never tried ray-tracing myself, so i can't help any further.

    Hopefully someone else can help you.
     
  5. Offline

    Themiller

    @KarimAKL Np thank you for your help, I managed to make it work ! :D

    But now I would like to display number of votes on a player above his head and his name. Do you have some ideas ? :)
     
  6. Offline

    alant7_

    Hello, maybe you can change player's display name, or create an invisible armor stand with custom name that's riding the player.
     
Thread Status:
Not open for further replies.

Share This Page