mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2024-11-02 08:39:49 +01:00
Reducing CPU usage as a result of unloading chunks.
This commit is contained in:
parent
f73d0dbc40
commit
d5d6f7e860
17
src/main/java/com/gmail/nossr50/datatypes/InactiveChunk.java
Executable file
17
src/main/java/com/gmail/nossr50/datatypes/InactiveChunk.java
Executable file
@ -0,0 +1,17 @@
|
|||||||
|
package com.gmail.nossr50.datatypes;
|
||||||
|
|
||||||
|
import org.bukkit.Chunk;
|
||||||
|
|
||||||
|
public class InactiveChunk {
|
||||||
|
public Chunk chunk;
|
||||||
|
public int inactiveTime = 0;
|
||||||
|
|
||||||
|
public InactiveChunk(Chunk chunk, int inactiveTime) {
|
||||||
|
this.chunk = chunk;
|
||||||
|
this.inactiveTime = inactiveTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public InactiveChunk(Chunk chunk) {
|
||||||
|
this(chunk, 0);
|
||||||
|
}
|
||||||
|
}
|
@ -8,23 +8,24 @@ import java.util.Map.Entry;
|
|||||||
import org.bukkit.Chunk;
|
import org.bukkit.Chunk;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
|
|
||||||
|
import com.gmail.nossr50.datatypes.InactiveChunk;
|
||||||
import com.gmail.nossr50.mcMMO;
|
import com.gmail.nossr50.mcMMO;
|
||||||
|
|
||||||
public class ChunkletUnloader implements Runnable {
|
public class ChunkletUnloader implements Runnable {
|
||||||
private static Map<Chunk, Integer> unloadedChunks = new HashMap<Chunk, Integer>();
|
private static Map<String, InactiveChunk> unloadedChunks = new HashMap<String, InactiveChunk>();
|
||||||
private static int minimumInactiveTime = 60; //Should be a multiple of RUN_INTERVAL for best performance
|
private static int minimumInactiveTime = 60; //Should be a multiple of RUN_INTERVAL for best performance
|
||||||
public static final int RUN_INTERVAL = 20;
|
public static final int RUN_INTERVAL = 20;
|
||||||
|
|
||||||
public static void addToList(Chunk chunk) {
|
public static void addToList(Chunk chunk) {
|
||||||
//Unfortunately we can't use Map.contains() because Chunks are always new objects
|
if (chunk == null || chunk.getWorld() == null)
|
||||||
//This method isn't efficient enough for me
|
return;
|
||||||
for (Chunk otherChunk : unloadedChunks.keySet()) {
|
|
||||||
if (chunk.getX() == otherChunk.getX() && chunk.getZ() == otherChunk.getZ()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
unloadedChunks.put(chunk, 0);
|
String key = chunk.getWorld().getName() + "," + chunk.getX() + "," + chunk.getZ();
|
||||||
|
|
||||||
|
if (unloadedChunks.containsKey(key))
|
||||||
|
return;
|
||||||
|
|
||||||
|
unloadedChunks.put(key, new InactiveChunk(chunk));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void addToList(int cx, int cz, World world) {
|
public static void addToList(int cx, int cz, World world) {
|
||||||
@ -33,24 +34,35 @@ public class ChunkletUnloader implements Runnable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
for (Iterator<Entry<Chunk, Integer>> unloadedChunkIterator = unloadedChunks.entrySet().iterator() ; unloadedChunkIterator.hasNext() ; ) {
|
for (Iterator<Entry<String, InactiveChunk>> unloadedChunkIterator = unloadedChunks.entrySet().iterator() ; unloadedChunkIterator.hasNext() ; ) {
|
||||||
Entry<Chunk, Integer> entry = unloadedChunkIterator.next();
|
Entry<String, InactiveChunk> entry = unloadedChunkIterator.next();
|
||||||
Chunk chunk = entry.getKey();
|
|
||||||
|
if (entry.getKey() == null || entry.getValue() == null) {
|
||||||
|
unloadedChunkIterator.remove();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (entry.getValue().chunk == null) {
|
||||||
|
unloadedChunkIterator.remove();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Chunk chunk = entry.getValue().chunk;
|
||||||
|
|
||||||
if (!chunk.isLoaded()) {
|
if (!chunk.isLoaded()) {
|
||||||
int inactiveTime = entry.getValue() + RUN_INTERVAL;
|
int inactiveTime = entry.getValue().inactiveTime + RUN_INTERVAL;
|
||||||
|
|
||||||
//Chunklets are unloaded only if their chunk has been unloaded for minimumInactiveTime
|
//Chunklets are unloaded only if their chunk has been unloaded for minimumInactiveTime
|
||||||
if (inactiveTime >= minimumInactiveTime) {
|
if (inactiveTime >= minimumInactiveTime) {
|
||||||
if (mcMMO.placeStore == null)
|
if (mcMMO.placeStore == null)
|
||||||
continue;
|
return;
|
||||||
|
|
||||||
mcMMO.placeStore.unloadChunk(chunk.getX(), chunk.getZ(), chunk.getWorld());
|
mcMMO.placeStore.unloadChunk(chunk.getX(), chunk.getZ(), chunk.getWorld());
|
||||||
unloadedChunkIterator.remove();
|
unloadedChunkIterator.remove();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
unloadedChunks.put(entry.getKey(), inactiveTime);
|
entry.getValue().inactiveTime = inactiveTime;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
//Just remove the entry if the chunk has been reloaded.
|
//Just remove the entry if the chunk has been reloaded.
|
||||||
|
Loading…
Reference in New Issue
Block a user