[SOLVED]Remove Nearby Arrows

Discussion in 'Plugin Development' started by Ice_Sword, Feb 8, 2012.

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

    Ice_Sword

    I'm attempting to remove arrows near a player that have stopped... How would I go about doing that? This is the code I have to check for arrows so far:

    Code:java
    1.  
    2. /*
    3. * To change this template, choose Tools | Templates
    4. * and open the template in the editor.
    5. */
    6. package arrowpickup;
    7.  
    8. import java.util.List;
    9. import org.bukkit.Material;
    10. import org.bukkit.entity.Arrow;
    11. import org.bukkit.entity.Entity;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.player.PlayerMoveEvent;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.PlayerInventory;
    17. import org.bukkit.plugin.PluginManager;
    18. import org.bukkit.plugin.java.JavaPlugin;
    19.  
    20.  
    21. /**
    22. *
    23. * @author Ice_Sword
    24. */
    25. public class Arrowclass extends JavaPlugin implements Listener
    26. {
    27. public static void main(String[] args)
    28. {
    29. }
    30.  
    31.  
    32. @Override
    33. public void onEnable()
    34. {
    35. getServer().getPluginManager().registerEvents(this, this);
    36. }
    37.  
    38. @Override
    39. public void onDisable()
    40. {
    41. }
    42.  
    43.  
    44. public boolean hasArrows(List<Entity> list){
    45. for(Entity entity: list){
    46. if(entity instanceof Arrow) return true;
    47. }
    48. return false;
    49. }
    50.  
    51. @EventHandler
    52. public void onPlayerMove(PlayerMoveEvent event){
    53. List<Entity> nears = event.getPlayer().getNearbyEntities(3,4,3);
    54. //the (3,4,3) is the box to check for entities
    55. if(hasArrows(nears))
    56. {
    57.  
    58. Player player = event.getPlayer();
    59. player.getName();
    60. PlayerInventory inventory = player.getInventory();
    61. ItemStack ar = new ItemStack(Material.ARROW, 1);
    62. inventory.addItem(ar);
    63. player.sendMessage("Test.");
    64.  
    65. }
    66. }
    67.  
    68.  
    69.  
    70.  
    71. }
    72.  


    I'm trying to simulate what happens when you pick up your own arrow, but with any arrow.

    EDIT:
    I've have to stamp out the bugs, but I just added "entity.remove();" before the first return statement. And of course edited the if statement to add brackets.
     
  2. Offline

    desht

    That event handler is going to put some serious load on a busy server. It gets called every time a player moves, even slightly, and getNearbyEntities() is not a cheap operation - it creates not one but two new ArrayLists every time it's called (plus a copy of one list to the other). You're just asking for "can't keep up!" messages there.

    There's no ideal way to do this (regularly scanning for and removing entities is by its nature expensive), but I'd suggest two things:
    1. Use a repeating task, and just scan periodically (maybe once a second, maybe make it configurable so server admins can adjust depending on their server horsepower). You'd need to use world.getPlayers() to scan all players, which is also not a cheap operation, but...
    2. Look into using the low-level NMS calls to get and remove entities to avoid the object creation & array copying overhead of the Bukkit getNearbyEntities() (and getPlayers()) method. Perusing CraftEntity.java and CraftWorld.java from the craftbukkit source would be a good place to start.
     
    Ice_Sword likes this.
  3. Offline

    Ice_Sword

    I'm going to get it working, and I'll look into updating it with your suggestions. Thank you.
     
Thread Status:
Not open for further replies.

Share This Page