Solved Entity removal

Discussion in 'Plugin Development' started by Dablakbandit, Dec 4, 2013.

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

    Dablakbandit

    How do i remove all entities in certain worlds?

    Thanks in advanced!
    Dablakbandit
     
  2. Offline

    scarabcoder

    Hmmmm.... I don't know exactly, but maybe something liked world.getEntities(), and use a for loop to go through and set the health to each entity to 0. I'm not good with for loops, maybe someone else can give you the code.
     
  3. Offline

    zeeveener

    getWorld("").getEntities().clear();
     
  4. Offline

    Dablakbandit

    zeeveener

    Thanks!
    Would you know to to make only remove certain entities defined in a config? So the config would look like
    Code:
    Worlds:
      World:
        Mobs: true
        Animals: True
        Drops: True
      World_nether:
        Mobs: false
        Animals: false
        Drops: false
     
  5. Offline

    zeeveener

    Code:java
    1.  
    2. for(int i = 0; i < entityList.size(); i++){
    3. Entity e = entityList.get(i);
    4. if(e.getType() == Entity.Skeleton || e.getType() == Entity.Spider ||...){
    5. if(!config.getBoolean("Worlds.World.Mobs")) entityList.remove(i);
    6. }else if(e.getType() == Entity.Cow || ...){
    7. //remove if false in config
    8. }else if(//Drop){
    9. //remove if false in config
    10. }
    11. }
     
  6. Offline

    The_Doctor_123

    Dablakbandit
    Uhhhh... no. That code simply clears a copy of a List of entities in the world. In other words, it does absolutely nothing.

    Better solution:
    Code:java
    1. for (Entity entity : someWorld.getEntities())
    2. {
    3. entity.remove();
    4. }
     
  7. Offline

    zeeveener


    You might run into a ConcurrenceException doing that. I agree it's a better solution, but still.
     
  8. Offline

    The_Doctor_123

    Garris0n likes this.
  9. Offline

    zeeveener

    Yes that's what I meant lol, I figured you'd understand my pseudo exception.
    Ok, then Dablakbandit, replace all my list.remove(i) with list.get(i).remove() and it will work properly as The_Doctor_123 has so kindly explained.
     
  10. Offline

    Garris0n

    Or just use the code The_Doctor_123 provided because the for-each loop is cleaner...
     
  11. Offline

    The_Doctor_123

  12. Offline

    zeeveener


    This isn't a style competition. Whatever he chooses to use is up to him. For all we know, he may prefer while loops to for loops lol. My for loop tries to do the type check that he requested, that's why it's messier.
     
  13. Offline

    The_Doctor_123

    zeeveener
    No, however, the code should make sense. Enhanced For Loops should be used when possible.
     
  14. Offline

    Garris0n


    Code:java
    1.  
    2. for(Entity e : world.getEntities()){
    3. if(e.getType() == Entity.Skeleton || e.getType() == Entity.Spider ||...){
    4. if(!config.getBoolean("Worlds.World.Mobs")) entityList.remove(i);
    5. }else if(e.getType() == Entity.Cow || ...){
    6. //remove if false in config
    7. }else if(//Drop){
    8. //remove if false in config
    9. }
    10. }
    11.  


    Still cleaner.

    And really, a while loop? Okay.

    Code:java
    1. Iterator<Entity> iterator = world.getEntities().iterator();
    2.  
    3. while(iterator.hasNext()){
    4.  
    5. Entity e = iterator.next()
    6.  
    7. if(e.getType() == Entity.Skeleton || e.getType() == Entity.Spider ||...){
    8. if(!config.getBoolean("Worlds.World.Mobs")) entityList.remove(i);
    9. }else if(e.getType() == Entity.Cow || ...){
    10. //remove if false in config
    11. }else if(//Drop){
    12. //remove if false in config
    13. }
    14.  
    15. }
     
  15. Offline

    zeeveener

    Like I said, Garris0n, we don't know what he prefers.
    I never said it wasn't cleaner. I am just trying to help this person figure out what they want to do with their plugin. I am not here to preach which loop is better or why, I am here to give pseudo code to help them figure it out on their own. That is the best way to be taught, in my opinion, a guiding hand.
     
  16. Offline

    The_Doctor_123

    zeeveener
    It's always best to teach it right the first time or someone may forever be confused.
     
  17. Offline

    Cirno

    Maybe this:

    Code:java
    1. Iterator<Entity> iter = world.getEntities().iterator();
    2. while(iter.hasNext()){
    3. Entity ent = iterator.next();
    4. switch(ent.getType()){
    5. case SKELETON:
    6. case SPIDER:
    7. case CREEPER:
    8. ent.remove();
    9. default:
    10. return;
    11. }
    12. }


    Not too good with the syntax of switch() statements; but supposedly, the JVM makes optimizations that make it so that switches performan faster than if-thens.
     
  18. Offline

    zeeveener


    I'm not teaching them anything. I am guiding them towards the answer.
     
  19. Offline

    Dablakbandit

    Garris0n zeeveener

    The code
    Code:java
    1. for(Entity e : world.getEntities()){
    2. if(e.getType() == Entity.Skeleton || e.getType() == Entity.Spider ||...){
    3. if(!config.getBoolean("Worlds.World.Mobs")) entityList.remove(i);
    4. }else if(e.getType() == Entity.Cow || ...){
    5. //remove if false in config
    6. }else if(//Drop){
    7. //remove if false in config
    8. }
    9. }


    Would remove yes, but what if there was a non-normal world? like a world called "Skyblocks", how you let the code be able to account for this?

    Thanks for the help,
    Dablakbandit
     
  20. Offline

    Garris0n

    Well that depends on what you're using it for. You have to get an instance of the world, there are many ways to do so.
     
  21. Offline

    Dablakbandit

    Garris0n zeeveener The_Doctor_123
    Okay so i wrote a code but it doesn't work?
    Code:java
    1. ----removed----

    i get the error
    Code:
    ----removed----
    Never mind fixed it, thanks for all the help the plugin is almost done! :)

    Thanks to all those who helped,
    Dablakbandit

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
Thread Status:
Not open for further replies.

Share This Page