-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #226 from radiant-ai/fix_lag_on_player_and_chunk_t…
…otal Improve performance of chunk loaded metric and offline player count
- Loading branch information
Showing
6 changed files
with
82 additions
and
4 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/main/java/de/sldk/mc/collectors/LoadedChunksCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package de.sldk.mc.collectors; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.World; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.world.ChunkLoadEvent; | ||
import org.bukkit.event.world.ChunkUnloadEvent; | ||
import org.bukkit.event.world.WorldUnloadEvent; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class LoadedChunksCollector implements Listener { | ||
|
||
private final Map<String, Integer> loadedChunksTotalMap = new HashMap<>(); | ||
|
||
@EventHandler | ||
public void onChunkLoad(ChunkLoadEvent event) { | ||
World world = event.getWorld(); | ||
Integer currentCount = loadedChunksTotalMap.get(world.getName()); | ||
if (currentCount == null) { | ||
loadedChunksTotalMap.put(world.getName(), world.getLoadedChunks().length); | ||
} | ||
else { | ||
loadedChunksTotalMap.put(world.getName(), currentCount + 1); | ||
} | ||
} | ||
|
||
@EventHandler | ||
public void onChunkUnload(ChunkUnloadEvent event) { | ||
World world = event.getWorld(); | ||
Integer currentCount = loadedChunksTotalMap.get(world.getName()); | ||
if (currentCount == null) { | ||
loadedChunksTotalMap.put(world.getName(), world.getLoadedChunks().length); | ||
} | ||
else { | ||
loadedChunksTotalMap.put(world.getName(), currentCount - 1); | ||
} | ||
} | ||
|
||
@EventHandler | ||
public void onWorldUnload(WorldUnloadEvent event) { | ||
World world = event.getWorld(); | ||
loadedChunksTotalMap.remove(world.getName()); | ||
} | ||
|
||
public int getLoadedChunkTotal(String worldName) { | ||
return loadedChunksTotalMap.computeIfAbsent(worldName, k -> { | ||
World world = Bukkit.getWorld(worldName); | ||
if (world == null) { | ||
return 0; | ||
} | ||
return world.getLoadedChunks().length; | ||
}); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...ain/java/de/sldk/mc/tps/TpsCollector.java → ...a/de/sldk/mc/collectors/TpsCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...java/de/sldk/mc/tps/TpsCollectorTest.java → .../sldk/mc/collectors/TpsCollectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters