Teleport arrow

Discussion in 'Plugin Development' started by Kenaex, Feb 19, 2018.

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

    Kenaex

    Hello,

    I am working on a plugin that teleports the player to the point where the arrow lands.
    I'm planning to make this work by enchanting arrows or crafting certain arrows.

    I know how to do all of this, however I have got one problem:

    How can I establish a kind of connection between the arrow item and the fired projectile, because I want to check whether the fired arrow item has a certain enchantment/metadata while using the ProjectileHitEvent

    Edit: If anyone has a better idea than using the ProjectileHitEvent I'm open for anything, but I thought that would be the best.

    Regards, Felix
     
    Last edited: Feb 19, 2018
  2. Offline

    oliver276

    I've not done much Bukkit in a while, but there's a EntityFireBowEvent (https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/event/entity/EntityShootBowEvent.html), which you could check to see if they have the enchantment on the bow (I'm not too sure how you'd check what arrow was used; it isn't parsed in the event). From there you're given the projectile. You can set some custom Metadata (containing the Player UUID or something), and using the ProjectileHitEvent you could check for custom metadata, and teleport the player whose UUID is there to the spot where it landed.

    I'll have a think about checking the arrow and see if I come up with anything
     
  3. Offline

    Kenaex

    Edit: That way works fine but it's just not what I'm looking for. What I wish it to be like is that there's a certain enchantment on the Arrow item.

    Thank you anyway
     
  4. Offline

    Lorinthio

    I have a code snippet I've used I can supply. Gimme a minute to grab it

    Whelp now TimTower will merge my posts and I'll get a warning... wish I could delete this post
    Looks like this was fixed

    Code:
    @EventHandler(ignoreCancelled = true)
        public void onBowShoot(EntityShootBowEvent event){
            if(event.getProjectile() instanceof Arrow){
                Arrow arrow = (Arrow) event.getProjectile();
    
                if(event.getEntity() instanceof Player) {
                    ItemStack arrowItem = getArrowBeingShot((Player) event.getEntity());
                }
            }
        }
    
        private ItemStack getArrowBeingShot(Player player){
            for(ItemStack item : player.getInventory().getContents())
                if(item != null && item.getType() == Material.ARROW)
                    return item;
            return null;
        }
    You can use this snippet to get the arrow ItemStack on shot, and can do whatever checks you need on it
    This fires before the arrow is removed, so you don't need to handle removal logic, or if the arrow is gone. It will all work as desired!
     
    Last edited: Feb 20, 2018
  5. Offline

    MattTheBeast

    @Kenaex
    From what I understand you whant to make some kind of enchant on a bow that makes you teleport to the arrow it shoots.

    In the case you must make a EntityShotBowEvent, get the ItemStack bow that the player just shot from, check to see if it has your enchant, if it does add the shot projectile to a list. Then you must make a projectileHitEvent, if your list contains the arrow entity then you know it was a projectile that was shot using a bow with your enchant.
     
  6. Offline

    Kenaex

    @Lorinthio I will try using that, thanks

    @MattTheBeast No, that's not it. I want to have an enchanted ARROW item, not a bow, but thanks anyway

    Edit: @Lorinthio 's idea worked just fine! Thank you very much
     
    Last edited: Feb 21, 2018
    Lorinthio likes this.
  7. Offline

    Lorinthio

    Please mark this as solved!
     
    skiithaw likes this.
Thread Status:
Not open for further replies.

Share This Page