Casting Item To Arrow

Discussion in 'Plugin Development' started by Scizzr, Jul 14, 2012.

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

    Scizzr

    So to make a long story short, I need to detect when a player picks up an arrow that's been shot. I can detect it, however am having problems casting it to an Arrow object. Here's what I have so far:
    Code:
        @EventHandler(priority = EventPriority.MONITOR)
        public void onPlayerPickupItem(final PlayerPickupItemEvent e) {
            Player p = e.getPlayer();
            Item it = e.getItem();
            ItemStack is = it.getItemStack();
            
            if (is.getType() == Material.ARROW) {
                p.sendMessage("Item is an arrow");
                if (it instanceof Projectile) { p.sendMessage("Instanceof Projectile"); }
                //^ Doesn't do anything
                if (it instanceof Arrow) { p.sendMessage("Instanceof Arrow"); }
                //^ Doesn't do anything
            }
        }
    
    Am I just doing this wrong or am I missing something? All help is appreciated.

    Right. Well, this isn't the most elegant solution, however it works:
    Code:
        @EventHandler(priority = EventPriority.MONITOR)
        public void onPlayerPickupItem(final PlayerPickupItemEvent e) {
            Player p = e.getPlayer();
            Item it = (Item)e.getItem();
            ItemStack is = it.getItemStack();
            
            if (is.getType() == Material.ARROW) {
                UUID id = it.getUniqueId();
                for (Entity ent : p.getWorld().getEntitiesByClass(Arrow.class)) {
                    if (((Arrow)ent).getUniqueId().equals(id)) {
                        //arrow.doWhateverNeedsToBeDone();
                    }
                }
            }
        }
    
    Any suggestions would still be great.

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

Share This Page