Search code examples
javaeclipseserverminecraftbukkit

(MC bukkit server) Right-clicking with specific item to trigger an event


public void arrowstick(PlayerInteractEvent event) {
        Player p = event.getPlayer();
            if(p.getInventory().getItemInMainHand().getType().equals(Material.STICK)){
                if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){

                Arrow ar = p.getWorld().spawn(p.getLocation(), Arrow.class);
                ar.setShooter(p);


                }

            }

    }

What I want to do here is shoot out an arrow if the player right-clicks with a stick in his hands. I cant seem to find a solution for this, does anyone have an idea about what I might be doing wrong? Im sorry if its a small and dumb mistake, its currently 2 am here so yeah, hope you guys can help


Solution

  • I coded it for you here:

    public class Arrow extends JavaPlugin implements Listener {
    
        public void onEnable() {
            this.getServer().getPluginManager().registerEvents(this, this);
        }
    
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            if (p.getInventory().getItemInHand().getType() == Material.STICK) {
                if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                    p.launchProjectile(Arrow.class);
                }
            }
        }
    
    }
    

    If you just copy the listener in a new class dont forget to make it a implements Listener and to register the Listener.