Solved Getting Entities around a dropped item

Discussion in 'Plugin Development' started by x_Jake_s, Aug 4, 2016.

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

    x_Jake_s

    I'm creating a plugin that when you drop an item using q (obviously) it checks the dropped items location and if it lands near an entity it reports the entities name else it returns with no entities near the item. however I everytime I send the message that there are no entities near the item is sends the message for every entity in the server which makes sense considering my code but I don't know how to get around it so it only sends the message once!

    Code:
        @EventHandler
         public void onThrow(PlayerDropItemEvent event) {
             if (event.getItemDrop().getItemStack().getItemMeta().getDisplayName().equalsIgnoreCase("Radar")) {
                 event.getPlayer().sendMessage("you threw a detector.");
                 List<Entity> entities = new ArrayList<Entity>();
                 Location iloc = event.getItemDrop().getLocation();
                 for (World world : Bukkit.getServer().getWorlds()) {
                     for (Entity ent : world.getEntities()) {
                         if (!(ent instanceof Player)) {
                                 if (ent.getNearbyEntities(5, 5, 5).equals(iloc)) {
                                 event.getPlayer().sendMessage(ent.getName().toString());
                                 } else {
                                     event.getPlayer().sendMessage("no entities near the radar.");
                             }
                         }
                     }
                 }
             }
         }
    Any suggestions?

    Solved using the following code:

    Code:
                for (World world : Bukkit.getServer().getWorlds()) {
                      for (Entity ent : world.getNearbyEntities(event.getItemDrop().getLocation(), 5, 5, 5)) {
                          if (!(ent instanceof Player)) {                          
                                  event.getPlayer().sendMessage(ent.getName().toString());
                                  } else {
                                      event.getPlayer().sendMessage("Miss");
                                  }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 4, 2016
  2. Offline

    ArsenArsen

    Why do this through all of the worlds? Just getLocation().getWorld().getNearby...
    Makes it faster, also I recommend checking if getNearbyEntities size is 0 then print miss otherwise do this.

    List<entity> nearby = getLocation.getWorld.getNearbyEntities getLocation, 5, 5, 5;
    if nearby size is > than 0
    for Entity ent : nearby
    ...
    end for
    else say Miss
     
Thread Status:
Not open for further replies.

Share This Page