removing entities from an entity list

Discussion in 'Spigot Plugin Development' started by Supercas240, Jun 14, 2018.

  1. I'm have an entity list based on a couple of factors, what im trying to do is run trough that list and based on a max amount of entities (mobs) set delete some of the entities. this is what I currently have:
    Code (Java):
    World world = Bukkit.getWorld(plugin.getConfig().getString("world"));
                            Location location = new Location(world,
                                    plugin.getConfig().getInt("x"),
                                    plugin.getConfig().getInt("y"),
                                    plugin.getConfig().getInt("z"));
                            plugin.saveConfig();
                           
                            double radius = plugin.getConfig().getInt("radius");
                            List<Entity> near = location.getWorld().getEntities();
                            for(Entity e : near) {
                                if(e.getLocation().distance(location) <= radius && e.getType() == org.bukkit.entity.EntityType.SNOWMAN) {
                                    for(int i = 0; i < near.size(); i++) {
                                          if( i >= amount) {
                                             
                                                Entity last = near.get(near.size() -1);
                                                last.remove();
    this doesn't work however, I can do system.out.println(last); and that will print out the mobs in the console so I know that part works but iterating trough the list and removing the last item of that list till the list.size is under a given constant doesn't seem to work, does anyone have an idea on how to approach this?
     
  2. You don't need to make another loop inside a loop.

    if(e.getLocation().distance(location) <= radius && e.getType() == org.bukkit.entity.EntityType.SNOWMAN) {
    e.remove();
     
  3. If you want to remove him from the list shouldn't you be doing near.remove(last) instead of last.remove()?
     
  4. Use iterator. You can't remove from list while looping it, but using iterator you can.
     
  5. no you see i don't want to remove all the entities, i want to set a max amount of entities in an area, so if i set it to 20 and there are 32 mobs it needs to kill 12

    no that just gives a null error
     
  6. could you give an example related to my code, i've never used iterators
     
  7. Google is your friend always.

    https://stackoverflow.com/questions/1196586/calling-remove-in-foreach-loop-in-java

    Example from there ^^

     
  8. okay, so i do:
    Code (Java):
        Iterator<String> j = j.iterator();
                                            while (j.hasNext()) {
                                                String s = j.next(); // must be called before you can call i.remove()
                                                if (e.getLocation().distance(location) <= radius
                                                        && e.getType() == org.bukkit.entity.EntityType.SNOWMAN && j >= amount) {
                                                j.remove();
                                            }
                                        }
    I see how it loops trough every item, but it's giving me an error on j.iterator saying its undefined
     
  9. In your case you use Entity instead of String.

    I've also learned to code using bukkit, but it would've been much easier if I knew how to actually write Java code better. So Im kinda suggesting that learning Java first is good.

    Iterator<String> j = j.iterator();

    That can't possibly work.

    Use world.getNearbyEntities rather than that thing you do it

    Then for that entity list ^^ (world.getNearbyEntities)

    Do this;
    Code (Text):
    List<Entity> entities = yourWorld.getNearbyEntities(location, radius, radius, radius)
    Iterator<Entity> i = entities.iterator();
    while (i.hasNext()) {
        Entity entity = i.next();
        // Do something
        i.remove();
    }
     
  10. Right I got the hang of this now, looking goodone more thing: currently i get the error "getNearbyEntities is undefined for the type world"

    Code (Java):
                        World world = Bukkit.getWorld(plugin.getConfig().getString("world"));
                            Location location = new Location(world,
                                    plugin.getConfig().getInt("x"),
                                    plugin.getConfig().getInt("y"),
                                    plugin.getConfig().getInt("z"));
                            plugin.saveConfig();
                            double radius = plugin.getConfig().getInt("radius");
                            List<Entity> entities = world.getNearbyEntities(location, radius, radius, radius);
                                    Iterator<Entity> i = entities.iterator();
                                    while (i.hasNext()) {
                                        Entity entity = i.next();
                                        if( entities.size() > amount) {
                                        i.remove();
                                        }
                                    }
     
  11. Could we see the error?
     
  12. "The method getNearbyEntities( location, radius, radius, radius) is undifined for world"
     
  13. With what version are you compiling?
     
  14. not even compiling yet, just in eclipse.
    Code (Text):
     List<Entity> entities = world.getNearbyEntities(location, radius, radius, radius);
    gives the error...
     
  15. Is your world a org.bukkit.World object?

    If so, update your API. This method was added 6 months after 1.8.0 was released.
     
    #16 BillyGalbreath, Jun 17, 2018
    Last edited: Jun 17, 2018
  16. does this method work with 1.8 though? cause otherwise I need to find a different way
     
  17. When are you people going to let 1.8 go? That was nearly 4 years ago.. It's over. 1.13 drops any day now. All you are doing is holding the community back from all the nicer things we've all worked hard on.
     
    • Agree Agree x 1
  18. Cause there are very specific gamemodes that are extremely popular and require features no longer supported with newer versions. But thats besides the point. Does the getNearbyEntities method work with a later 1.8 version or is it specifically a later update feature?
     
  19. No, it's not beside the point. It is the point.

    There has to be a cut-off point where legacy support is dropped. Like the Spigot Staff always says, if you want to remain on 1.8 that's your choice, but be prepared to support yourself.
     
    • Agree Agree x 1

Share This Page