Getting arrow distance

Discussion in 'Plugin Development' started by brord, Dec 25, 2012.

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

    brord

    Hey guys!
    First of all, merry Christmas :D

    I got this requried feature where an arrow needs to be removed after a certain distance flown, But this seems to be impossible :/

    There could possibly be more then 100 arrows which needs to be checked, at the same time, so a scheduler wont do it.
    I could set the arrow its speed and remove it whenever its falling, but that would still require a scheduler.

    Does anyone have any fancy ideas on how to get this working?

    I also need a way to edit speed and spread of the shot arrow. The method:
    user.getWorld().spawnArrow(EyeLocation , velocity, 1f, 1f)
    allows that, but player.spawnArrow(Arrow.class) doesnt.

    Whenever i try to do this:

    Vector velocity = user.getEyeLocation().getDirection().multiply(5);
    Arrow arrow = user.launchProjectile(Arrow.class);
    arrow.setVelocity(velocity);

    It speeds up, but it gets a lil bit wird, the location where it goes to changes half way.
    [​IMG]

    As you can see right there, the arrow goes to the right.
    Im not mastered in Vectors, so i cannot understaind why this happends.
     
  2. Offline

    KingMagnus

    Make an entity move event and check it's location when it's fired, and compare it to it's location on the event if it's traveled far enough remove it
     
  3. Offline

    bobert456

    Code:
     @EventHandler
        public void onArrowHit(EntityDamageByEntityEvent  event){
        int x1,x2,z1,z2;
        Entity entity = event.getDamager();
        if(event.getEntity() instanceof Player){
        if(entity instanceof Arrow){
            Projectile arrow = (Projectile)entity;
        if(arrow.getShooter() instanceof Player){
        Player shooter = (Player) arrow.getShooter();
        Location sloc = shooter.getLocation();
        Location vloc = entity.getLocation();
        int sx = sloc.getBlockX(), sz = sloc.getBlockZ(), vx = vloc.getBlockX(), vz = vloc.getBlockZ();
        if(sx > vx){ x1 = sx; x2 = vx;}else{ x1 = vx; x2 = sx;}
        if(sz > vz){ z1 = sz; z2 = vz;}else{ z1 = vz; z2 = sz;}
        int z = z1-z2, x = x1-x2;
        int distsquared = z*z + x*x;
        dist = (int) Math.sqrt(distsquared);//Pythagorean Theorm
        if(dist > 40){
        Player p = (Player)event.getEntity();}
    And for your Speed issues
    Code:
    @EventHandler
                public void ArrowLaunch(ProjectileLaunchEvent event){
                if(event.getEntity() instanceof Arrow){
                    if(event.getEntity().getShooter() instanceof LivingEntity){
                    Entity    s = event.getEntity().getShooter();
                    Vector velocity = event.getEntity().getVelocity();
                    Vector v = velocity.multiply(2.5);
                        event.getEntity().setVelocity(v);
                    }
                }
                }
    You should be able to figure out what the code does, but just in case the top one returns the distance and the bottom sets the velocity of an arrow to 2.5 times the original. And of course you would need to add your own if statements if you dont want everybody shooting arrows faster
     
  4. Offline

    brord

    The problem is, i need to remove the arrow if it, for example reached 20 blocks.
    This could be anywhere, in the air, in a player, on the ground.
    Your code will only work If it hits a player.

    The other problem is, that if an arrow flies for 3 seconds, the Shooter could be moved.
    And you get the coordinates from the shooter on hit, so he could have moved.

    Last problem is, I spawn the arrows in the world, so ProjectileLaunchEvent will never be called :(

    I appreciate your help, But its not yet it :)
     
  5. Offline

    fireblast709

    save the location when you spawn it
     
  6. Offline

    Malikk

    You're not going to accomplish this without a scheduler. If you can't wait until it hits and need to remove it mid-flight, a scheduler is the only way to go.

    Just pass in the arrow and it's initial location.
     
  7. Offline

    brord

    aaaand?
    When could i check for the distance?
    Also, Gratzz on your first plugin :D
     
Thread Status:
Not open for further replies.

Share This Page