Solved Is there a way for me to use PlayerInteractEvent instead of EntityShootBow?

Discussion in 'Plugin Development' started by man_in_matrix, Aug 21, 2021.

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

    man_in_matrix

    So I have this code for a "shotgun bow" which works perfectly but I want it to work on click instead of when an arrow gets shot so then it's instant (kinda like a gun[where clicking is pulling the trigger]) is there a way to change the code to work with PlayerInteractevent instead of EntityShootBow?

    Code:
    package me.neo.terminator.Listeners;
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityShootBowEvent;
    
    public class ItemUse implements Listener {
    
        @SuppressWarnings("unused")
        @EventHandler
          public void onShoot(EntityShootBowEvent e) {
            if(e.getProjectile() instanceof Arrow) {
                if(e.getEntity() instanceof Player) {
                    if (e.getBow() != null && e.getBow().getItemMeta() != null && e.getBow().getItemMeta().getDisplayName().equals(ChatColor.GOLD + "Shotgun")) {
                      
                        Arrow arrow = (Arrow) e.getProjectile();
                      
                        Arrow arrow1 = e.getEntity().getWorld().spawn(e.getEntity().getEyeLocation(), Arrow.class);
                        arrow1.setDamage(arrow.getDamage());
                        arrow1.setKnockbackStrength(arrow.getKnockbackStrength());
                        arrow1.setShooter(e.getEntity());
                        arrow1.setVelocity(arrow.getVelocity().rotateAroundY(Math.toRadians(30)));
                      
                        Arrow arrow2 = e.getEntity().getWorld().spawn(e.getEntity().getEyeLocation(), Arrow.class);
                        arrow2.setDamage(arrow.getDamage());
                        arrow2.setKnockbackStrength(arrow.getKnockbackStrength());
                        arrow2.setShooter(e.getEntity());
                        arrow2.setVelocity(arrow.getVelocity().rotateAroundY(Math.toRadians(-30)));
                    }
                }
            }
        }
    }
    help would be appreciated :D (I am still learning plugin development but am making projects to learn)
    [NOT ASKING FOR SPOONFEED JUST NEED TO KNOW IF THERE IS A WAY]
    This is on 1.16.5 btw.

    So I figured out the problem myself gonna post it here for anybody who has the same issue.

    I first checked for the item and made sure it's not null to avoid a ton of exceptions.

    then if its the right item I used player.launchProjectile(Arrow.class) and casted that to arrow

    Arrow arrow = player.launchProjectile(Arrow.class);

    I then changed the e.getEntity() to e.getPlayer() which fixed the other issues

    Code:
    package me.neo.terminator.Listeners;
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityShootBowEvent;
    
    public class ItemUse implements Listener {
    
        @SuppressWarnings("unused")
        @EventHandler
          public void onShoot(EntityShootBowEvent e) {
            if(e.getProjectile() instanceof Arrow) {
                if(e.getEntity() instanceof Player) {
                    if (e.getBow() != null && e.getBow().getItemMeta() != null && e.getBow().getItemMeta().getDisplayName().equals(ChatColor.GOLD + "Shotgun")) {
                     
                        Arrow arrow = (Arrow) e.getProjectile();
                     
                        Arrow arrow1 = e.getEntity().getWorld().spawn(e.getEntity().getEyeLocation(), Arrow.class);
                        arrow1.setDamage(arrow.getDamage());
                        arrow1.setKnockbackStrength(arrow.getKnockbackStrength());
                        arrow1.setShooter(e.getEntity());
                        arrow1.setVelocity(arrow.getVelocity().rotateAroundY(Math.toRadians(30)));
                     
                        Arrow arrow2 = e.getEntity().getWorld().spawn(e.getEntity().getEyeLocation(), Arrow.class);
                        arrow2.setDamage(arrow.getDamage());
                        arrow2.setKnockbackStrength(arrow.getKnockbackStrength());
                        arrow2.setShooter(e.getEntity());
                        arrow2.setVelocity(arrow.getVelocity().rotateAroundY(Math.toRadians(-30)));
                    }
                }
            }
        }
    }
    This is the original code without PlayerInteractEvent

    And here is the new code with it that allows the bow to instantly shoot

    Code:
    package me.neo.terminator.Listeners;
    
    import java.util.List;
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    
        public class ItemUse implements Listener {
       
        @SuppressWarnings("unused")
        @EventHandler
        public void onUse(PlayerInteractEvent event) {
            if(event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) {
            Player player = event.getPlayer();
           
            ItemStack heldItem = player.getInventory().getItemInMainHand();
              ItemMeta meta = heldItem.getItemMeta();
              if (meta == null)
                return;
              List<String> strings = meta.getLore();
              if (strings == null)
                return;
              if(player.getInventory().getItemInMainHand().getItemMeta().getDisplayName().contains(ChatColor.GOLD + "Shotgun")) {
           
                
           
           
           
            
               
               
                Arrow arrow = player.launchProjectile(Arrow.class);
               
               
                Arrow arrow1 = event.getPlayer().getWorld().spawn(event.getPlayer().getEyeLocation(), Arrow.class);
                arrow1.setDamage(arrow.getDamage());
                arrow1.setKnockbackStrength(arrow.getKnockbackStrength());
                arrow1.setShooter(event.getPlayer());
                arrow1.setVelocity(arrow.getVelocity().rotateAroundY(Math.toRadians(10)));
               
                Arrow arrow2 = event.getPlayer().getWorld().spawn(event.getPlayer().getEyeLocation(), Arrow.class);
                arrow2.setDamage(arrow.getDamage());
                arrow2.setKnockbackStrength(arrow.getKnockbackStrength());
                arrow2.setShooter(event.getPlayer());
                arrow2.setVelocity(arrow.getVelocity().rotateAroundY(Math.toRadians(-10)));
               
               
               
               
             
       
          }
        }
      }
    }
        
    Hope this helps people who need it :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 22, 2021
Thread Status:
Not open for further replies.

Share This Page