hashmap cleanup?

Discussion in 'Plugin Development' started by undeadmach1ne, Oct 4, 2011.

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

    undeadmach1ne

    i have a hashmap in which i add the uuid and health of mobs that are spawned (i am changing their health and this is the best way i could think to 'remember' it later). i am saving the hashmap to a .dat file so that i can still remember them across reloads/shutdowns, and i remove them from the map when they show up in onEntityDeath. the problem is that my .dat file seems to be slowly growing over time...which leads me to believe that bukkit is removing mobs outside of entitydeath events and that those mobs are still in my hashmap.

    does anyone know a quick way i can verify that the mobs in my map still exist, and remove them if they dont? (this could be hard if the mobs are in chunks/worlds that are not currently in memory)

    does bukkit clear out mobs at any point without using an entitydeath event? if there is a mob culling that happens behind the scenes, can i hook into it?

    perhaps im doing this all wrong too. is there a better way to remember individual mobs and their health across shutdowns/reloads of the server?

    its also possible that my hashmap is not swelling with mobs that dont exist and that this growth in the .dat file will eventually stop once the map contains 'all' the currently spawned mobs. i doubt this is the case though.

    any help, suggestions or anything is much appreciated. thanks :)
     
  2. Offline

    LaLa

    Use this to get all the mobs in the map, and then check if the mob still exists

    Code:java
    1. if (!getServer().getWorld(worldname).getEntities().contains(insert entity here)){//Entity doesn't exist}


    A more effective way of storing mob data would be of course mysql or sqlite.
     
  3. Offline

    Celeixen

    When a mob despawns entitydeath isnt called, just on entity spawn run through the hashmap & see if they exist.
     
  4. Offline

    Afforess

    onEntityDeath. Remove the UUID's then.
     
  5. Offline

    undeadmach1ne

    wouldnt a database be overkill for such temporary info? for the code you posted, how would i check the list of entities that .getEntities() returns against my hashmap? what if the entity existed in another world...would they be removed from the map too if i did this check (since they arent in this world we are checking)?

    this is what i am trying to learn how to do :) i would like to do it onDisable() right before i save the map to a .dat, in case its takes a lot of cpu or ram to do the checks and cleanup...that raises the question, are all the entities in a world stored in that worlds files? can i check a world that is not loaded/has no players in it etc and get a full list?

    i am doing this, but it seems that entities are being removed from the game in other ways that i am not catching and staying the hashmap...
     
  6. Offline

    LaLa

    This should scan through each entity, and then scan each world for that entity

    Code:java
    1. // This is the hashmap of your entities. Won't be the same as this
    2. HashMap<Entity, Boolean> en = new HashMap<Entity, Boolean>();
    3. for (int i = en.size(); i > 0; i--){
    4. for (int j = getServer().getWorlds().size(); j > 0; j--){
    5. if (!getServer().getWorld(getServer().getWorlds().get(j-1).getName()).getEntities().contains(en.get(i-1))){
    6. // Do whatever to remove the entity from your .dat file
    7. }
    8. }
    9. }
     
    undeadmach1ne likes this.
  7. Offline

    Celeixen

    I didnt think despawning counted as entity death?
     
  8. Offline

    undeadmach1ne

    ill try that tonight. thanks :)

    @LaLa
    that seems to have worked perfectly. my hashmap is hovering around 2500 entries across multiple shutdowns/restarts of the server and everything seems to be working fine. thanks :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  9. Offline

    undeadmach1ne

    well i was wrong :) after a bit of testing i realized it wasnt working so i read up more and i think if i could use (myhashmap).retainAll(map of all entities in all worlds) i would be good to go, i just cant get uuid from getWorld().getEntities(). is this what @LaLa means with "// Do whatever to remove the entity from your .dat file" or am i missing the proper way to do it? i cant seem to get any way to match the output from the getEntities with my hashmap keys (uuids).
     
  10. Offline

    Celeixen

    Can you post the code on how you are saving the hashmap & how your loading it.
     
  11. Offline

    undeadmach1ne

    they save and load fine, the problem is that you cant compare a hashmap whos key is uuid to a hashmap whos key is entity, and i cant figure out how to get uuids from the entities that i can compare to. i am a noob though, there is a good chance i running in the wrong direction here too :p
     
Thread Status:
Not open for further replies.

Share This Page