From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Euphyllia Bierque <bierque.euphyllia@gmail.com>
Date: Tue, 28 Jan 2025 16:35:13 -0800
Subject: [PATCH] Add TPS From Region


diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
index 1635250e7df04b91f5a0ac6442bf5fa46157bf03..bb1a8220174a2882c7056733536f57586044dc75 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
@@ -3339,4 +3339,68 @@ public final class CraftServer implements Server {
         this.console.addPluginAllowingSleep(plugin.getName(), value);
     }
     // Paper end - API to check if the server is sleeping
+    // Folia start - region TPS API
+    /**
+     * Gets the TPS from the region which owns the specified location, or {@code null} if no region owns
+     * the specified location.
+     *
+     * @param location The location for which to get the TPS
+     * @return TPS (5s, 15s, 1m, 5m, 15m), or null if the region doesn't exist
+     */
+    @Override
+    public double[] getRegionTPS(Location location) {
+        Preconditions.checkArgument(location != null, "Location cannot be null");
+
+        return this.getRegionTPS(location.getWorld(), location.getBlockX() >> 4, location.getBlockZ() >> 4);
+    }
+
+    /**
+     * Gets the TPS from the region which owns the specified chunk, or {@code null} if no region owns
+     * the specified location.
+     *
+     * @param chunk - The specified chunk
+     * @return TPS (5s, 15s, 1m, 5m, 15m), or null if the region doesn't exist
+     */
+    @Override
+    public double[] getRegionTPS(org.bukkit.Chunk chunk) {
+        Preconditions.checkArgument(chunk != null, "Chunk cannot be null");
+
+        return this.getRegionTPS(chunk.getWorld(), chunk.getX(), chunk.getZ());
+    }
+
+    /**
+     * Gets the TPS from the region which owns the specified chunk, or {@code null} if no region owns
+     * the specified location.
+     *
+     * @param world - World containing the chunk
+     * @param chunkX - X-coordinate of the chunk
+     * @param chunkZ - Z-coordinate of the chunk
+     * @return TPS (5s, 15s, 1m, 5m, 15m), or null if the region doesn't exist
+     */
+    @Override
+    public double[] getRegionTPS(World world, int chunkX, int chunkZ) {
+        Preconditions.checkArgument(world != null, "World cannot be null");
+
+        return getTPSFromRegion(((CraftWorld)world).getHandle(), chunkX, chunkZ);
+    }
+
+    private static double[] getTPSFromRegion(ServerLevel world, int chunkX, int chunkZ) {
+        io.papermc.paper.threadedregions.ThreadedRegionizer.ThreadedRegion<io.papermc.paper.threadedregions.TickRegions.TickRegionData, io.papermc.paper.threadedregions.TickRegions.TickRegionSectionData>
+            region = world.regioniser.getRegionAtSynchronised(chunkX, chunkZ);
+        if (region == null) {
+            return null;
+        } else {
+            io.papermc.paper.threadedregions.TickRegions.TickRegionData regionData = region.getData();
+            final long currTime = System.nanoTime();
+            final io.papermc.paper.threadedregions.TickRegionScheduler.RegionScheduleHandle regionScheduleHandle = regionData.getRegionSchedulingHandle();
+            return new double[] {
+                regionScheduleHandle.getTickReport5s(currTime).tpsData().segmentAll().average(),
+                regionScheduleHandle.getTickReport15s(currTime).tpsData().segmentAll().average(),
+                regionScheduleHandle.getTickReport1m(currTime).tpsData().segmentAll().average(),
+                regionScheduleHandle.getTickReport5m(currTime).tpsData().segmentAll().average(),
+                regionScheduleHandle.getTickReport15m(currTime).tpsData().segmentAll().average(),
+            };
+        }
+    }
+    // Folia end - region TPS API
 }
