Player Right-CLick a Player

Discussion in 'Spigot Plugin Development' started by TheRedstoneGeek, Apr 21, 2017.

  1. I just would like to know an example of say a player right clciking another player with a certain item and how to get the player clicked, the player and the item
  2. Try that (The code is not tested :/):
    Code (Text):

        public void onPlayerInteractAtEntity(PlayerInteractAtEntityEvent event)

            if (event.getRightClicked().getType().equals(EntityType.PLAYER))
                //Do somethings
  3. ^ Don't try use that, along the right lines but almost all the names are wrong (guessing it was done outside of an ide, so no worries :).

    Use PlayerInteractEntityEvent, then check if e.getEntity is instanceof Player. I personally would avoid using entity types.
    • Agree Agree x 1
  4. I had that previously and it just doesn't work

    I ahve tried both methods and neither work
  5. Ok , thank you.
    I had done this code quickly so I did not care
  6. Have you registered your events?

    Add a broadcast to check the event is being called
  7. Code (Text):

        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
            hoeMeta.setDisplayName(ChatColor.BLUE + "Freeze");
            ArrayList<String> lores = new ArrayList<String>();
            lores.add(ChatColor.AQUA + "Right click a player to freeze them!");

        public void onInventory(PlayerInteractEntityEvent e) {
            Player p = e.getPlayer();
            if (e.getRightClicked().getType().equals(EntityType.PLAYER) && p.getItemInHand().isSimilar(hoe)) {
                    if (!frozen.contains(e.getRightClicked().getName())) {
                    String name = e.getRightClicked().getName();
                    Player freeze = Bukkit.getPlayerExact(name);
                    freeze.addPotionEffect(new PotionEffect(PotionEffectType.SLOW,10000,10000));
                    freeze.sendMessage(colorize("&cYou have been frozen! Please join our teamspeak within 5 minutes or you will be banned! IP: Coming Soon!"));
                    else if (frozen.contains(e.getRightClicked().getName())) {
                    Player freeze = (Player) e.getRightClicked();
                    freeze.sendMessage(colorize("&aYou have been unfrozen! Thank you for your compliance!"));
    The DEBUG message is never set yet I have registered events
  8. electronicboy

    IRC Staff


    Your if-else statement is also busted in that if it returns false for the first check (e.g. if the entity is actually a player (And, you really should use instanceof for safety)), you'll be casting the clicked entity to Player, which might not be the case.
  9. Ma ybe wrong, but didn´t you just forget the @EventHandler Annotation?
    (I never used Listeners in my Main class, so i´m not sure.)

    Oh wow... i just see now that the above post tells you the same thing... I didn´t read the @-Mark as i thought he was linking a user..xD
    • Like Like x 1