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?
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();
If you want to remove him from the list shouldn't you be doing near.remove(last) instead of last.remove()?
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
Google is your friend always. https://stackoverflow.com/questions/1196586/calling-remove-in-foreach-loop-in-java Example from there ^^
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
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(); }
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(); } }
not even compiling yet, just in eclipse. Code (Text): List<Entity> entities = world.getNearbyEntities(location, radius, radius, radius); gives the error...
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.
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.
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?
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.