Skip to content

Commit

Permalink
add explaining comments
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Dec 24, 2023
1 parent 1fd168a commit 35861bb
Showing 1 changed file with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,12 @@ final class Folia implements TPSCache {

@Override
public double getGlobalTPS() {
final TickRegionScheduler.RegionScheduleHandle regionHandle = RegionizedServer.getGlobalTickData();
// Get region handle and check if there is already a cached tps for it
final TickRegionScheduler.RegionScheduleHandle
regionHandle = RegionizedServer.getGlobalTickData();
Double tps = this.cached_tps.getIfPresent(regionHandle);
if (tps == null) {
// If nothing is cached yet, get tps and add to cache
tps = regionHandle.getTickReport15s(System.nanoTime()).tpsData().segmentAll().average();
this.cached_tps.put(regionHandle, tps);
}
Expand All @@ -86,28 +89,38 @@ public double getGlobalTPS() {

@Override
public double getTPS(Location location) {
if (location == null) return getGlobalTPS();
return getTPS(location.getWorld(), location.getBlockX() >> 4, location.getBlockZ() >> 4);
}

@Override
public double getTPS(World world, int chunkX, int chunkZ) {
if (world == null) return getGlobalTPS();
// Use AtomicDouble because we need a value from the lambda
// Init with 20 (perfect) tps, so we have a valid and safe fallback value
AtomicDouble atomic_tps = new AtomicDouble(20.0);
// Update atomic double via region scheduler execute method so usage of TickRegionScheduler.getCurrentRegion()
// happens faster and on the thread of the region that owns the location.
this.server.getRegionScheduler().execute(plugin, world, chunkX, chunkZ, () -> {
// Get the potential separate region that owns the location
final ThreadedRegionizer.ThreadedRegion<TickRegions.TickRegionData, TickRegions.TickRegionSectionData>
currentRegion = TickRegionScheduler.getCurrentRegion();
// If not happening on a separate region, it must mean we're on the main region
if (currentRegion == null) {
atomic_tps.set(getGlobalTPS());
return;
}
final TickRegionScheduler.RegionScheduleHandle regionHandle = currentRegion.getData().getRegionSchedulingHandle();
// Get region handle and check if there is already a cached tps for it
final TickRegionScheduler.RegionScheduleHandle
regionHandle = currentRegion.getData().getRegionSchedulingHandle();
Double tps = this.cached_tps.getIfPresent(regionHandle);
if (tps == null) {
// If nothing is cached yet, get tps and add to cache
tps = regionHandle.getTickReport15s(System.nanoTime()).tpsData().segmentAll().average();
this.cached_tps.put(regionHandle, tps);
}
atomic_tps.set(tps);
});

return atomic_tps.get();
}
}
Expand Down

0 comments on commit 35861bb

Please sign in to comment.