Solved Removing Arrows

Discussion in 'Spigot Plugin Development' started by LazerShadow, Jun 18, 2015.

Thread Status:
Not open for further replies.
  1. I have to have a repeating task to remove all arrows off the ground and stuck in players every 15 seconds. How do I do this?
     
  2. Code (Text):
            BukkitScheduler scheduler = Bukkit.getScheduler();
            scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
                public void run() {
                    for (World world : Bukkit.getWorlds()) {
                        for (Entity entity : world.getEntities()) {
                            if (entity instanceof Arrow) {
                                Arrow arrow = (Arrow)entity;
                                arrow.remove();
                            }
                        }
                    }
                }
            },0, 20*15);
     
  3. The code @danenco posted will remove any arrows from the ground. If you also want to remove arrows that are stuck in players you will need to use a bit of NMS code:
    PHP:
    ((CraftPlayer) player).getHandle().getDataWatcher().watch(9, (byte) 0);
    You can iterate over all online players and do this to each of them.
     
    • Like Like x 1
  4. To remove stuck arrows in a player:
    Code (Text):
    ((CraftPlayer)getPlayer()).getHandle().getDataWatcher().watch(9, (byte) 0);
    To remove all arrows from the ground:
    Code (Text):
    for (Arrow arrow : currentWorld.getEntitiesByClass<Arrow.class>) {
      Vector vector = arrow.getVelocity();
      // Check if the arrow is moving
      if (vecor.getX() != 0 || vecor.getY() != 0 || vecor.getZ() != 0) {
        continue;
      }
      arrow.remove();
    }
     
  5. Removing all arrows will also remove flying arrows, see my answer for a way to heck if the arrow is mid-flight. Also, iterating over all entities can become memory intensive.
     
  6. There is also a handy Arrow#isOnGround() method that could be used if you didn't want to check velocities.
     
    • Useful Useful x 1
  7. That will cause really unnecessary cpu work, just stick to events:
    EntityDamagedByEntityEvent (for removing arrows in players) and ProjectileLandEvent (for removing arrows stuck in ground).
     
    • Informative Informative x 2
    • Agree Agree x 1
  8. updated code for 1.9 or above

    Code:
    Code (Text):
    ((CraftPlayer)player).getHandle().getDataWatcher().set(new DataWatcherObject<>(10, DataWatcherRegistry.b),0);
     
    • Useful Useful x 3
    • Like Like x 2
Thread Status:
Not open for further replies.