ChunkLoad, ChunkUnload

Discussion in 'Plugin Development' started by CXdur, Aug 15, 2014.

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

    CXdur

    Hey guys! I created a chunk protection system some time ago for a server of mine, and today I looked into my code and I realized that it's not as efficient as it should be. It's all loaded from SQL with minimum queries and stored in a class to hold data, but what I did realize though is that the chunks are always loaded. This wasn't a problem when I began, but now it seems to become one since I have like 400 protected chunks and I've increased the maximum limit of chunks.

    Currently I load it on onEnable, and I disable unloading of protected chunks through ChunkUnloadEvent.

    However, I'm curious to whether it's safe to completely disable loading from onEnable and use the ChunkLoad event instead? I would have to be really sure about this before doing anything, as I can't afford to fuck anything up.

    My point: Is it safe to NOT load chunk protections on onEnable, but instead load protections from whenever someone sees a chunk? Problem is I would have to run a query every time someone finds a chunk. What's more efficient if you get my point?
     
  2. Offline

    Garris0n

    How are you storing the protections?
     
  3. Offline

    CXdur

    garrison, through SQL, sorry for the incredibly late answer.
     
  4. Offline

    Garris0n

    I'm fairly sure (been a while) what I meant was how are you storing them in memory? Are you storing the actual Chunk instances?
     
  5. Offline

    CXdur


    Code:java
    1. public class ChunkData {
    2.  
    3. public String name;
    4. public String owner;
    5. public String world;
    6. public int x;
    7. public int z;
    8.  
    9. public boolean privat;
    10.  
    11. public ChunkData(String name, String owner, String world, int x, int z, boolean privat) {
    12. setChunkName(name);
    13. setChunkOwner(owner);
    14. setChunkWorld(world);
    15. setChunkX(x);
    16. setChunkZ(z);
    17. setPrivate(privat);
    18. }
    19.  
    20. public String getChunkName() {
    21. return name;
    22. }
    23.  
    24. public String getChunkOwner() {
    25. return owner;
    26. }
    27.  
    28. public String getChunkWorld() {
    29. return world;
    30. }
    31.  
    32. public int getChunkX() {
    33. return x;
    34. }
    35.  
    36. public int getChunkZ() {
    37. return z;
    38. }
    39.  
    40. public void setChunkName(String chunkName) {
    41. this.name = chunkName;
    42. }
    43.  
    44. public void setChunkOwner(String chunkOwner) {
    45. this.owner = chunkOwner;
    46. }
    47.  
    48. public void setChunkWorld(String chunkWorld) {
    49. this.world = chunkWorld;
    50. }
    51.  
    52. public void setChunkX(int chunkX) {
    53. this.x = chunkX;
    54. }
    55.  
    56. public void setChunkZ(int chunkZ) {
    57. this.z = chunkZ;
    58. }
    59.  
    60. public boolean isPrivate() {
    61. return privat;
    62. }
    63.  
    64. public void setPrivate(boolean bool) {
    65. this.privat = bool;
    66. }
    67. }


    public HashMap<Chunk, ChunkData> chunkData = new HashMap<Chunk, ChunkData>();
     
  6. Offline

    Garris0n

    Why are you saving it with the chunk as a key? You already have the x and z...

    Anyway, stop saving the Chunk instances and see if that fixes it. Just save the data in a set.
     
  7. Offline

    CXdur

    I don't have any performance issues. I'm saving it as a key because it's easier to get the ChunkData instance.

    I was just bored and wanted to see if it would be more efficient to load on events, but I don't think it is because it means one query and a new instance every time someone finds a chunk.
     
  8. Offline

    coasterman10

    It is still not a good idea to save the actual chunks themselves, instead you should be saving a coordinate pair, such as a ChunkKey object that has both the x and z coordinates.
     
  9. Offline

    Garris0n

    Or at least use a WeakHashMap.

    Also, I was going over memory and as such missed some parts of your post.

    First of all, don't prevent the chunks from unloading. That's going to put a ridiculous amount of strain on the server for no discernible reason.

    Second, do you really have so many protected chunks that you can't just cache which ones are protected in memory?

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

Share This Page