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.
You've essentially given us nothing to go on. We need code and an explanation on what you have tried.
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"); } } }
I'm quite certain that e.getEntity() would get the player, e.getEntity() returns a LivingEntity. Have a look at the JavaDocs: https://jd.bukkit.org/org/bukkit/event/entity/EntityShootBowEvent.html
@BreezerFly Yap you're right @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
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? ^^.
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
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/
Before casting, they should check what the methods themselves return a reference to, instead of assuming
@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
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