Player damage doesn't work in conjunction with diamond hoe

Discussion in 'Spigot Plugin Development' started by XThou, Jan 7, 2020.

  1. Hello, I am attempting to develop a weapon that shoots a particle beam followed by an explosion and deals damage to any players within 5 blocks of the explosion. Everything is working smoothly with the exception of the player.damage(10.0, player) function in conjunction with the diamond hoe. Throughout my testing, I changed the material type to other items (primarily a stick) and everything functioned smoothly and exactly as it was programmed. I don't know what else to do so I figured I would come here to possibly seek out the assistance of some better-informed parties. I would also like to state that this is my very first post on the spigot forums and I preemptively apologize for any semantic faults. Thank you in advance!

    My code:
    Code (Java):
        @EventHandler
        public void interactionTest(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            Location loc = player.getLocation();
       
            if(player.getInventory().getItemInHand().getType() == Material.DIAMOND_HOE) { // getItemInHand deprecated but getItemInMainHand doesn't work either
                if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                    event.setCancelled(true); // ADDED THIS IN ATTEMPT TO FIX BUT DID NOT WORK
                    HashSet<Material> transparent = new HashSet<Material>();
                    transparent.add(Material.AIR);
                    Block block = player.getTargetBlock(transparent, 10);
                    Beam.shootBeam(player, 10, Beam.FLAME); //Particle Effects
                    ExplodeEffect effect = new ExplodeEffect(Core.effectManager); //Particle Effects
                    effect.setLocation(block.getLocation()); //Particle Effects
                    effect.start(); //Particle Effects
               
                    for(Entity e : Bukkit.getServer().getWorld(player.getWorld().getName()).getEntities()) {
                        if(e instanceof Player) {
                            if(e.getLocation().distance(block.getLocation()) <= 5) {
                                ((Player) e).damage(10.0); // THE ISSUE WHEN USING A DIAMOND HOE
                           
                            }
                       
                        }
                   
                    }
           
                }
            }
       
        }
     
  2. Are you trying to test it with right click, or left click?

    I think that it's not working because the right click is already defined by default on minecraft to harvest, but I'm not sure of that

    btw you should use

    Code (Java):
    for (Entity ent : player.getLocation().getWorld().getNearbyEntities(player.getLocation(), 5, 5, 5) {
    if (ent instanceof Player) {

    Player near = (Player) ent;
     
    near.damage(10D);
    }
    }
    Because yours, would damage all the entities in the player world, and I'm sure you don't want that
     
  3. I've tested left click and it too does not work. I should also clarify that default player.damage(10d) does function properly, but when you add a player [player.damage(10d, player)] it for whatever reason fails when the diamond hoe is used. The getting entities code you suggested doesn't work with what I'm doing.
     
  4. What's the version you're using?
     
  5. Spigot 1.14.4

    Also for the entities code, I have this to ensure that not all entities are damaged:
    Code (Java):
    if(e.getLocation().distance(block.getLocation()) <= 5) {
     

  6. If you try with "diamond_spade"?

    nvm deprecated
     
  7. Shovel seems to work.
    From what I can tell something with the diamond hoe prohibits me from assigning the attacking player in the .damage() method.
     
  8. Are you trying to interact at players, or blocks?
     
  9. I'm right-clicking while facing the direction of another player from about 8 blocks away. So technically a block/air.
     
  10. Anyway try hooking InteractAtEntity event and check if the entity is a player
     
  11. That probably wouldn't work since its functioning via item interaction not entity interaction, but thanks anyway.