Bow Event

Discussion in 'Plugin Development' started by mrdude123, Dec 21, 2015.

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

    mrdude123

    So when a player shoots a bow I want them to get a message in chat notifying them that they shot an arrow. I've tried everything so far and I've been unable to figure out what's wrong. Source code is available upon request. Thanks.
     
  2. Offline

    Tecno_Wizard

    You've essentially given us nothing to go on. We need code and an explanation on what you have tried.
     
  3. Offline

    DoggyCode™

    Source code requested.
     
  4. Offline

    mrdude123

    Here ye go.
    Code:
    package plugin.thecerealkill3r.bows;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityShootBowEvent;
    
    public class BowEvents implements Listener{
       
        @EventHandler
        public void onPlayerShoot(EntityShootBowEvent e)
          {
            Arrow a = (Arrow) e.getEntity();
            Player shooter = (Player) a.getShooter();
           
            if(!(e.getEntity() instanceof Arrow)){
                return;
            }else{
                shooter.sendMessage("Test");
            }
          }
          }
    
     
  5. Offline

    JoaoBM

    @BreezerFly Yap you're right :D

    @mrdude123 Here's a little tip: Whenever you're doing e.getSomething (or something else) look at the return value.

    http://imgur.com/LWLn8vN

    As you can see, for example, when you do e.getEntity, you see after the : the return value. In this case it's a LivingEntity. Since an arrow is not a LivingEntity, your code won't work :p
     
  6. Offline

    mrdude123

    @JoaoBM Any idea as to what I could do as a substitution then? I'm quite lost here.
     
  7. Rewrite your method using the information from the JavaDocs I linked :)
     
  8. Offline

    mrdude123

  9. Offline

    JoaoBM

    @mrdude123 Tell us here if you afterall made it :p
     
  10. Hey man. What about:
    Code:
    package plugin.thecerealkill3r.bows;
    import org.bukkit.Material;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityShootBowEvent;
    public class BowEvents implements Listener{
      
        @EventHandler
        public void onPlayerShoot(EntityShootBowEvent e)
          {
            Arrow a = (Arrow) e.getEntity();
            Player shooter = (Player) a.getShooter();
          
                shooter.sendMessage("Test");
            }
          }
          }
    Also have you registered this Listener to your Main class? ^^.
     
  11. If you had read the rest of the thread, you'd know that e.getEntity() does NOT return the arrow, but the Entity that fired the arrow, meaning the player. Read: https://jd.bukkit.org/org/bukkit/event/entity/EntityShootBowEvent.html
     
  12. Offline

    mine-care

    I think we are missing something here... This something is called "Java". As i and furthermore we have been shouting all over these forums, learn java before starting with bukkit.
    The pieces of code above are essentially error generators.
    Read about casting: https://howtoprogramwithjava.com/java-cast/
     
  13. Before casting, they should check what the methods themselves return a reference to, instead of assuming :p
     
  14. Offline

    mine-care

    @BreezerFly Indeed, its part of learning Java as well. the reason why i pointed to Casting is because thats the missused technique here and is also a way to point to the docs and show how they can be used productively :)
     
  15. Offline

    Gorbit99

    You could just go off the first code variant and just check the item in the players hands.
    Pseudo code:
    Event runs
    check item in players hands
    Do Stuff
     
Thread Status:
Not open for further replies.

Share This Page