From 35861bb810e31d2dbe992a9e40179b06ea8b612a Mon Sep 17 00:00:00 2001 From: xGinko Date: Sun, 24 Dec 2023 21:43:54 +0100 Subject: [PATCH] add explaining comments --- .../anarchyexploitfixes/utils/TPSCache.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/utils/TPSCache.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/utils/TPSCache.java index dd809ba8a..18a1d026c 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/utils/TPSCache.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/utils/TPSCache.java @@ -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); } @@ -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 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(); } }