getting the block an arrow hits

Discussion in 'Spigot Plugin Development' started by xlis1, May 29, 2017.

  1. so i have some code that is supposed to get the block an arrow hits, and it works... sometimes. (its in a projectile hit event)


    Code (Text):
    else if (e.getEntity() instanceof Arrow) {
                      Location loc1 = e.getEntity().getLocation();
                      if(loc1.getBlock().getType() == Material.GLASS) { loc1.getBlock().setType(Material.STONE); }
                     
                      BlockIterator b = new BlockIterator(loc1.getWorld(), loc1.toVector(), e.getEntity().getVelocity().normalize(), 0, 1);
                      Block hit = null;
                      while (b.hasNext()){
                          hit = b.next();
                      if(hit.getType() == Material.GLASS) {
                          break;
                      }
                      }
                     
                     hit.setType(Material.STONE);
                   
                   
                   
                  }


    this works, but only occasionally, sometimes it places the stone behind the arrow, or on the arrow instead of replacing the block it hits. how do i get this to work consistently?
     
    #1 xlis1, May 29, 2017
    Last edited: May 29, 2017
  2. Why not just use the getHitBlock() inside the ProjectileHitEvent class?
     
    • Agree Agree x 2
  3. that gets the block the arrow shaft is in, not the tip
     
  4. Get the shaft block and switch to the block right infront of it? (To get the tip block)
     
  5. yeah but how do you know what block is "in front of it" as it could be inn any direction
     
  6. you might use Yaw and Pitch from Projectile Location to see Projectile direction