PlayerShootBowEvent

Discussion in 'Plugin Development' started by AndrewM16921, Dec 18, 2011.

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

    AndrewM16921

    Trying to find an event that fires when a player shoots a bow and one that I can grab the arrow entity from. I can't seem to find anything like a "PlayerShootBowEvent" but there is a "PlayerEggThrowEvent" ... so how would I go about doing this for bow/arrow?
     
  2. Offline

    Jaker232

    I bet I can implement it. Just need to add in Events+ project.
     
  3. Offline

    AndrewM16921

    Was hoping like, an ArrowSpawnEvent or something like that already existed. :/
     
  4. Offline

    Jaker232

    I finished writing the class for the event. I can give you the source and tell you in PM how to set it up.
     
  5. Offline

    AndrewM16921

    Yeah I'd like that

    Hmm ... still welcoming any help on this subject. Thanks.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
  6. Offline

    user_43347

    There is one for this, I can't remember it, but I wouldn't use a custom event for this, as Minecraft and Bukkit update, your dependent on that custom event, which can break very easily.
     
  7. Offline

    Butkicker12

    Well this is after the launch.

    Code:
        public void onProjectileHit(ProjectileHitEvent ev) {
    
        }
     
  8. Offline

    AndrewM16921

    Yes, but I need it before the launch =P
     
  9. Offline

    Father Of Time

    From my research it appears that no such event exists.

    However, with a bit of elbow grease you can simple re-purpose existing events. An Arrow is an Entity, so you could use an Entity listener to listen for Entity spawning, and if that entity happens to be an Projectile (arrow specifically) then boom, you essentially have a bow being fired event. From this you would know.

    Who shot it
    where it was shot
    the arrow shot
    the velocity
    the direction
    etc.

    You would just need to add a few checks in to make sure its a fired projectile and not just an arrow spawn (like from when a skeleton drops an arrow as loot).

    This isn't the most direct way of handling this, but I would imagine totally doable (unless I've overlooked something). I hope you manage to find a solution, good luck! :D
     
  10. Offline

    AndrewM16921

    What event is for entity spawning that can detect an arrow spawning? That's what I was looking for initially. :/

    I tried ItemSpawnEvent but it didn't detect the arrow when it was shot (only when dropped).

    I did find, in Effect, Effect.BOW_SHOOT

    Hoping this will lead to finding some use for it. Checking it out now.
     
  11. Offline

    Father Of Time

    Mind providing a code sample of how you did this? Likely you just didn't place the proper checks and cast to get the server to identify the proper arrow type.
     
  12. Offline

    AndrewM16921

    Uh I've since deleted it but I'll quickly rewrite an almost-class of it

    For the record when I pasted this code in I had made a typo such that it was called "ShitListener." x_x Glad I caught that. =P

    Code:java
    1. package org.nubcraft.nubbukkit.bows;
    2.  
    3. import org.bukkit.entity.Arrow;
    4. import org.bukkit.event.entity.EntityListener;
    5. import org.bukkit.event.entity.ItemSpawnEvent;
    6. import org.nubcraft.nubbukkit.NubBukkit;
    7.  
    8. public class ShotListener extends EntityListener
    9. {
    10. private NubBukkit nb;
    11. public ShotListener(NubBukkit nb)
    12. {
    13. this.nb = nb;
    14. }
    15. @Override
    16. public void onItemSpawn(ItemSpawnEvent event)
    17. {
    18. if(event.getEntity() instanceof Arrow)
    19. nb.getServer().broadcastMessage("test");
    20. }
    21. }
    22.  
     
  13. Unfortualy there isn't any EntitySpawn event. Only CreatureSpawn and ItemSpawn and none of those would fire for an arrow :/
     
  14. Offline

    Father Of Time

    I took notice of that, but I wonder if you know you are looking for an arrow can you simply cast a Mob as a projectile, or an item as one; or does the arrow spawning not even trigger these two event?

    I'm not arguing for its plausibility, simply inquiring out of curiosity (I'm at work or I would test it).
     
  15. Offline

    AndrewM16921

    ItemSpawnEvent doesn't even trigger. I tried the broadcast outside the instanceof check, too. I don't know about CreatureSpawnEvent but if item doesn't I doubt creature would, it takes an arg of CreatureType (for which nothing therein can really represent an arrow), so I find it unlikely. I'll give it a quick test when I can though.

    So, hmm... I guess you could periodically check for arrows in the entity list and mark them as spawned and if not marked then throw a custom event ... but that seems like a bad idea... x_x
     
  16. Offline

    Father Of Time

    That definitely sounds like a "Hail Marry" play, lets hold off on that if possible.

    Can you maybe explain what exactly it is that you are trying to accomplish, there may be alternative ways to achieve your end goal.
     
  17. Offline

    ZNickq

    If it's dropped as an item, it's an Item, not a Projectile ;)
     
  18. Offline

    Father Of Time

    I was more thinking of approaching it from a mob side, but upon further inspection I noticed it inherits from Entity, not LivingEntity, so I'm sure that's just as improbable.

    Sorry if I've managed to send you on a wild goose chase, I am simply the type of programmer who just likes to try to experiment with code to experience the outcome first hand, even if it's unlikely to behave in a desired fashion.

    If it were me personally I would have simply created a basic MobSpawn and ItemSpawn event listeners, and don't place any checks what so ever, but rather simply place a console print that takes what ever entity was spawned (mob, item, what ever it doesnt matter) and prints it (entity.toString()) for every single item that spawns (temporarily of coarse).

    Although this will report EVERYTHING that is spawned, it will also show if arrows being fired ever trigger either of these events, simply spam a bunch of arrow shots and see if you notice anything in your console.

    If nothing is reported as spawning as an item or mob then I would consider it safe to assume that arrows do not trigger either event. Again, this is just how I work and may not necessarily be the most efficient or effective way to determine this.
     
  19. Offline

    AndrewM16921

    I did try that ^
    Shooting an arrow doesn't print anything, dropping an item just prints CraftItem

    My goal was to make some rewards for certain tasks in my plugin. Among these rewards were a number of 'fire arrows', 'shotgun arrows', 'ice arrows', lighting, and some other ideas. The first one I tried was the shotgun arrows, essentially a bow that fires 5 arrows at once.

    I figured, ok I detect when a player fires a bow, grab the Arrow and pretty much make 4 copies of it spaced out a little bit. And this is when I hit this roadblock. :/

    For things like fire/ice/lightning and whatnot I can easily use the projectile hit event, though.
     
  20. Offline

    Father Of Time

    I see the dilemma, I can't find anything that fits...

    The only thing I see that could possible help is what you've already suggested:
    http://jd.bukkit.org/doxygen/d9/d6d...nEvent.html#a0ffb9a1ca53cbb1373b916b6779043b8

    An event that is triggered by player animations, which has an animation type for firing a bow, but I would imagine this animation would trigger long before the arrow entity is ever created (due to the drawing back of the string).

    The only other thing I found that is interesting (but likely uesless) is this:
    It appears that the World class handles the creation and "firing" of an arrow; however I looked through the world, player and inventory events and found nothing useful.

    Sorry man, this is beginning to look like one of those "shit out of luck" moments until someone comes along and pushes an event to handle this action.

    I hope someone is able to provide you with better advice than I could, good luck with your project!
     
  21. Offline

    AndrewM16921

    Eh well... Thanks anyway ^_^
     
  22. Offline

    tomjw64

    My only suggestion(i've been having a problem similar to this, darn snowballs) here would be to go straight to what would cause a bow to fire an arrow. The more I think about this solution, the more it seems to me that it should work. I may have just solved my own problem. :p
    You could check if the player is holding a bow in their hand onPlayerInteract and if useItemInHand returns a result of ALLOW.
    This should I believe tell you "Hey, this guy is firing an arrow!". For the matter of obtaining the arrow, the answer is simple. You don't. If you DO know that they are firing an arrow, you could then cancel the PlayerInteractEvent which stops the player from shooting the arrow. Now that you have stopped them from firing the initial arrow, you then proceed to shootArrow from the player that tried to fire in the first place. And now that you have shot the arrow yourself, hooray, you can now do whatever you want with the arrow returned by shootArrow. (If you want something done right, you have to do it yourself, :p haha)
    There is one shortcoming that I can see, and that is that the power that the arrow is shot at will have to be factored in. I have little to no experience with vectors so I am afraid that I cannot help there (if it were me, I would just make the arrows go super fast :)).
    Well, that's my idea. There is no guarantee that it will work, as I just randomly thought of this and have never tried it before. I hope this or this way of approaching it does though, for us both. And if this DOES work, I don't know about you, but I'm going to try to see if it's possible to throw flaming snowballs! :p
    Hopefully I helped! :D

    Woo! Tested! It works with snowballs!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
  23. Offline

    Zeroth

    Mind posting a little code snippet? I was just trying to solve this problem yesterday and got stumped.
     
  24. Offline

    tomjw64

    np! This is my code to set snowballs on fire! Keep in mind though, it may be weird for arrows because they can have different velocities. :)
    Code:
    	public void onPlayerInteract(PlayerInteractEvent interact)
    	{
    		if(interact.getMaterial()==Material.SNOW_BALL&&interact.useItemInHand().equals(Result.ALLOW))
    		{
    			Player thrower=interact.getPlayer();
    			interact.setCancelled(true);
    			thrower.getItemInHand().setAmount(thrower.getItemInHand().getAmount() -1);
    			Snowball snowball=thrower.throwSnowball();
    			snowball.setFireTicks(100);
    		}
    	}
    oops, I forgot to reply. See my above post! :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
  25. Offline

    Zeroth

    Ah ok, I see. Thanks!
     
  26. Offline

    Chiller

    With this can you see where the snowball hits??
     
  27. Offline

    tomjw64

    I don't think so, sorry. BUT, shouldn't this work:
    Code:
    public void onProjectileHit(ProjectileHitEvent hit)
    {
        if(hit.getEntity() instanceof SnowBall)
        {
            Location loc= hit.getEntity().getLocation();
        }
    }
    or will the Snowbal already be gone?
     
  28. Offline

    Chiller

    Thats called when it hits an entity though, right?
     
  29. Offline

    tomjw64

    There is already a way to tell if a snowball hit an entity (EntityDamageByEntityEvent).
    And you would think that if that is what it was doing, then there would be some sort of variable for the entity hit by it, but there isn't. I am not sure, but I would give it a try.
     
Thread Status:
Not open for further replies.

Share This Page