How to detect a Punch

Discussion in 'Spigot Plugin Development' started by xXEmiXx, May 11, 2017.

  1. Hi, i am creating a plugin for Freez, and I do not know how to detect when the player hits another player. I tried with PlayerInteractAtEntityEvent and nothing, help pls!
     
  2. PlayerInteractAtEntityEvent = Represents an event that is called when a player right clicks an entity that also contains the location where the entity was clicked.

    PlayerInteractEvent = Represents an event that is called when a player interacts with an object or air, potentially fired once for each hand.

    You could easily find this at: https://hub.spigotmc.org/javadocs/spigot/
     
  3. EntityDamageByEntityEvent
     
    • Agree Agree x 3
  4. ChefJava mentions the correct event above. My apologies.
     
  5. But make sure you check if the damager is a player before you cast your damager to a player.

    Many people forget to do this.
     
    • Agree Agree x 2
  6. PlayerInteractEvent it only allows to detect if you click (left or right to the air or a block) I just want to know, the entity you hit and with what item. thx
     
  7. Well, here your answer
     
  8. Code (Text):
    @EventHandler
    public void onPunch(EntityDamagedByEntityEvent e) {
    if(e.getDamager() instanceof Player) {
    //LOGIC
     
    • Agree Agree x 1
  9. Thanks!!! And a 1 question e.getEntity = player and e.getDamager = other player??
     
  10. e.getEntity = entity damaged and e.getDamager = entity that inflicted damage
     
    • Like Like x 1
  11. It's okay?
    Code (Text):
       
    public void onPunch(EntityDamageByEntityEvent e) {
            if(e.getDamager() instanceof Player) {
               
                Player p =  (Player)e.getDamager();
               
                Player dañado = (Player)e.getEntity();
               
               
            }

        }
     
  12. don't blindly cast player to the damaged entity.
     
    • Agree Agree x 1
  13. What? I can´t understand
     
  14. The entity might not be a player. You have to check if its an instance of one before casting it. If you don't check it will produce errors if the entity isn't a player.
     
  15. If you still can understand, like this
    Code (Text):
    if(e.getDamager() instanceof Player) && (e.getEntity() instanceof Player()) {
    I recommend you actually learn Java instead of starting out with whatever you want.
     
  16. All that spacing is unnecessary.
     
  17. Code (Text):

        public void onPunch(EntityDamageByEntityEvent e) {
            if(e.getDamager() instanceof Player && e.getEntity() instanceof Player){
               
                Player p =  (Player)e.getDamager();
                Player dañado = (Player)e.getEntity();
                           
                ItemStack test =  new ItemStack(Material.INK_SACK, 0, (short) 10);
                ItemMeta test2 = test.getItemMeta();
                test2.setDisplayName("&aPunch &f%player%".replace("&", "§").replace("%player%", p.getName()));          
                test.setAmount(1);
                test.setItemMeta(test2);
                       
                if(p.getItemInHand().isSimilar(test)){
                   
                    PotionEffect testeo = new PotionEffect(PotionEffectType.BLINDNESS, Integer.MAX_VALUE, Integer.MAX_VALUE);
                   
                    testeo.apply(dañado);
                    dañado.sendMessage("It is a test! and" +p.getName());
                   
                    p.sendMessage("It is a other test! and" +dañado.getName());
               
                }
            }
        }
     
    What's wrong? I already tried it and nothing
     
  18. You need to put @EventHandler above your event.
     
  19. Add @EventHandler before the event and also why did you do item.setAmount(1); when you could have set that when you created the item stack.
     
  20. EntityDamageByEntityEvent and then check if both attacker and damager are player entities.