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?
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?
Code:java public class ChunkData { public String name; public String owner; public String world; public int x; public int z; public boolean privat; public ChunkData(String name, String owner, String world, int x, int z, boolean privat) { setChunkName(name); setChunkOwner(owner); setChunkWorld(world); setChunkX(x); setChunkZ(z); setPrivate(privat); } public String getChunkName() { return name; } public String getChunkOwner() { return owner; } public String getChunkWorld() { return world; } public int getChunkX() { return x; } public int getChunkZ() { return z; } public void setChunkName(String chunkName) { this.name = chunkName; } public void setChunkOwner(String chunkOwner) { this.owner = chunkOwner; } public void setChunkWorld(String chunkWorld) { this.world = chunkWorld; } public void setChunkX(int chunkX) { this.x = chunkX; } public void setChunkZ(int chunkZ) { this.z = chunkZ; } public boolean isPrivate() { return privat; } public void setPrivate(boolean bool) { this.privat = bool; }} public HashMap<Chunk, ChunkData> chunkData = new HashMap<Chunk, ChunkData>();
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.
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.
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.
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.