onShoot event..

Discussion in 'Plugin Development' started by Brexima, Apr 12, 2013.

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

    Brexima

    Code:
    package me.barisaraci.Naruto;
     
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.EntityShootBowEvent;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class NarutoPlugin extends JavaPlugin implements Listener{
        public static NarutoPlugin plugin;
        public final Logger logger = Logger.getLogger("Minecraft");
       
        @Override
        public void onDisable(){
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " v. " + pdfFile.getVersion() + " deaktiflestirildi!");
        }
       
        @Override
        public void onEnable(){
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " v. " + pdfFile.getVersion() + " aktiflestirildi!");
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
       
        @EventHandler
        public void onShoot(EntityShootBowEvent evt){
            if(evt.getEntity() instanceof Arrow) {
                Arrow arrow = (Arrow) evt.getEntity();
                    if(arrow.getShooter() instanceof Player) {
                        Player shooter = (Player) arrow.getShooter();
                        shooter.sendMessage("bowpulled");
                        if(shooter.getItemInHand().getType() == Material.BOW) {
                            evt.setCancelled(true);
                            shooter.teleport(arrow.getLocation());
                            shooter.sendMessage("bowpulled");
                        }
                    }
                }
        }
     
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerClick(PlayerInteractEvent event){
            Player oyuncu = event.getPlayer();
            if(event.getPlayer().getItemInHand().getTypeId() == Material.ARROW.getId() && (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)){
              oyuncu.shootArrow();
              oyuncu.sendMessage("bowshooted");
            }
        }
    }
    
    in this code playerclick event is working and sending bowshooted message but onshoot event not working i tried other events onshoot event instead but the second event not working only onplayerclick working. i think problem about listeners but not sure what u think ? :confused:
     
  2. Offline

    mcoder

    event.getEntity() can never be an Arrow, since Arrows (usually ;) ) don't shoot bows.

    event.getEntity() returns the shooter, which could be your player.
    event.getBow() returns the ItemStack with the bow that's used to shoot.
    event.getProjectile() returns the Projectile shot, in most Cases the Arrow.

    useing this in your code as example would look like this (written from memovr, not tested):

    Code:java
    1.  
    2. @EventHandler
    3. public void onShoot(EntityShootBowEvent evt){
    4. if(evt.getEntity() instanceof Player) {
    5. Player shooter = evt.getEntity();
    6. if(evt.getProjectile instanceof Arrow) {
    7. Arrow arrow = (Arrow) evt.getProjectile();
    8. shooter.sendMessage("bowpulled");
    9. if(evt.getBow().getType() == Material.BOW) {
    10. evt.setCancelled(true);
    11. shooter.teleport(arrow.getLocation());
    12. shooter.sendMessage("bowpulled");
    13. }
    14. }
    15. }
    16. }
    17.  


    This code should work (if i don't have any typos in it ;) ), but probably won't do what you want. Since the Arrow is fired from the location the player is currently at, it will port him to the same location he is in right now.

    To get the location where the shot arrow lands, you would have to use something like ProjectileHitEvent.

    regards, mcoder
     
  3. Offline

    Brexima

    mcoder
    thank you so much, with
    this -> Player shooter = evt.getEntity(); -> Player shooter = (Player) evt.getEntity();
    and this -> if(evt.getProjectile instanceof Arrow) { -> if(evt.getProjectile() instanceof Arrow) {
    fix it worked. and i did that teleportation thing just for test. the thing what i want to do is if i click left with arrow in my hand it throw arrow and after it if i click right with arrow in my hand i teleport to arrow's location(arrow on air).

    Code:
        @EventHandler
        public void onShoot(EntityShootBowEvent evt){
            if(evt.getEntity() instanceof Player) {
                Player shooter = (Player) evt.getEntity();
                if(evt.getProjectile() instanceof Arrow) {
                    Arrow arrow = (Arrow) evt.getProjectile();
                    shooter.sendMessage("bowpulled");
                    if(evt.getBow().getType() == Material.BOW) {
                        evt.setCancelled(true);
                        shooter.teleport(arrow.getLocation());
                        shooter.sendMessage("bowpulled");
                    }
                }
            }
        }
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerClick(PlayerInteractEvent event){
            Player oyuncu = event.getPlayer();
            if(event.getPlayer().getItemInHand().getTypeId() == Material.ARROW.getId() && (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK)){
              oyuncu.shootArrow();
              oyuncu.sendMessage("bowshooted");
            }
        }
    }
    i tried this but after i throw the arrow with left click its not see the onShoot event =( so how can i do that, i hope u understand me :)
     
  4. Offline

    ScottGambler

    Try this:

    Code:
     if(event.getPlayer().getItemInHand().getTypeId() == 262
    &&(event.getAction() == Action.LEFT_CLICK_AIR ||
    event.getAction() == Action.LEFT_CLICK_BLOCK)){
    Hope this works.
     
  5. Offline

    Brexima

    i didn't know the arrow's id :) im still searching answer for up
     
  6. Offline

    mcoder

    I'm sorry, I don't fully understand. How can you throw an arrow by left clickling?
     
  7. Offline

    Brexima

    mcoder
    when i left click with arrow im throwing a arrow with oyuncu.shootArrow(); function. this is working but i want to teleport to arrow's location with right click with arrow.
     
  8. Offline

    mcoder

    Then you can store the entity id of the shot arrow when you call this function. Then use the projectileHitEvent and compare the id of the projectile which hit and the one you sotred. if they match, you know the location and can teleport on right klick.
     
  9. Offline

    Brexima

    mcoder
    is it possible to teleport the arrow when arrow on air ? i mean teleport to arrow before the hit anywhere.

    and how can i store the arrow's id ? save it to a int or something like that ? because onshoot event dont perceive shootarrow function =(

    i did it :) thank you for everything

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  10. Offline

    mcoder

  11. Offline

    Brexima

    the problem was onShoot, i changed it with onLaunch and use LaunchProjectileEvent i save the arrow entity and use the entity's location at onPlayerClick like u say :)
     
Thread Status:
Not open for further replies.

Share This Page