How to keep a chunk in memory?

Discussion in 'Plugin Development' started by Baba43, Feb 10, 2013.

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

    Baba43

    Hi,

    I'm using RemotEntities to create a player-like npc. My problem is that my npcs disappear after some time and another user told me that this might happen if the chunk gets unloaded.

    So the question is: What is the most effective way to keep a chunk alive?
    I tried to cancel the ChunkUnloadEvent but that was no good idea :eek:
     
  2. Offline

    danthonywalker

    Cancelling ChunkUnloadEvent every time would drop your server's performance very very quickly.

    Instead try listening to ChunkUnloadEvent and then (psuedocode)
    world.getChunk(locationOfSaidNPC).load();

    I am guessing this would keep it loaded. Either that or every tick it'll try to unload and load again. :/
     
  3. Offline

    adam753

    Cancelling the ChunkUnloadEvent will cause horrendous lag, especially if there are lots of spread-out players on the server, because it will cause every visited map chunk to stay in the server's memory until it's turned off. I can think of two potential ways to achieve what you want:

    -cancel the ChunkUnloadEvent only if the chunk contains an instance of your NPC. This would reduce the lag issue, but is still probably not the best solution if you have lots of NPCs. Actually don't do this one, the guy above me is right.

    -don't cancel the ChunkUnloadEvent, but instead when a chunk does get unloaded, store all your NPC's in that chunk into a hashmap, then when a chunk is loaded, put them all back. The list could end up getting very long and would need to be saved when the server is turned off (I assume?) but this is probably the ideal solution.
     
  4. Offline

    Baba43

    adam753
    I checked if the chunk contained my npc (currently its only one) but the event got fired again and again so I had to stop my server and remove the plugin.

    I like your second idea.. I only want my NPC to stay there so it might be enough to recreate it (using remotentities) everytime the chunk gets loaded.

    Thank you so far.
     
Thread Status:
Not open for further replies.

Share This Page