Skip to content

Commit

Permalink
Add API to retrieve TPS from a specified location
Browse files Browse the repository at this point in the history
Resolves #310
  • Loading branch information
Euphillya authored and Spottedleaf committed Jan 29, 2025
1 parent 5cc35a8 commit 1f0b85d
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 0 deletions.
92 changes: 92 additions & 0 deletions folia-api/paper-patches/features/0004-Add-TPS-From-Region.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Euphyllia Bierque <[email protected]>
Date: Tue, 28 Jan 2025 16:34:17 -0800
Subject: [PATCH] Add TPS From Region


diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java
index 9196b1e62b328b1e9790b966600aba9681dd0ddc..86192033979d78f6308451b024995eac963af9ab 100644
--- a/src/main/java/org/bukkit/Bukkit.java
+++ b/src/main/java/org/bukkit/Bukkit.java
@@ -2975,6 +2975,42 @@ public final class Bukkit {
return server.isGlobalTickThread();
}
// Paper end - Folia region threading API
+ // 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
+ */
+ public static double @Nullable [] getRegionTPS(@NotNull Location location) {
+ return server.getRegionTPS(location);
+ }
+
+ /**
+ * 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
+ */
+ public static double @Nullable [] getRegionTPS(@NotNull Chunk chunk) {
+ return server.getRegionTPS(chunk);
+ }
+
+ /**
+ * 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
+ */
+ public static double @Nullable [] getRegionTPS(@NotNull World world, int chunkX, int chunkZ) {
+ return server.getRegionTPS(world, chunkX, chunkZ);
+ }
+ // Folia end - region TPS API

@NotNull
public static Server.Spigot spigot() {
diff --git a/src/main/java/org/bukkit/Server.java b/src/main/java/org/bukkit/Server.java
index 11923ef0ea75f702273ba5481ac6d46cc0f17697..4cad7f842418b0702575db29cc79dcfcc207d589 100644
--- a/src/main/java/org/bukkit/Server.java
+++ b/src/main/java/org/bukkit/Server.java
@@ -2654,4 +2654,34 @@ public interface Server extends PluginMessageRecipient, net.kyori.adventure.audi
*/
void allowPausing(@NotNull org.bukkit.plugin.Plugin plugin, boolean 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
+ */
+ double @Nullable [] getRegionTPS(@NotNull Location location);
+
+ /**
+ * 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
+ */
+ double @Nullable [] getRegionTPS(@NotNull Chunk chunk);
+
+ /**
+ * 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
+ */
+ double @Nullable [] getRegionTPS(@NotNull World world, int chunkX, int chunkZ);
+ // Folia end - region TPS API
}
79 changes: 79 additions & 0 deletions folia-server/paper-patches/features/0006-Add-TPS-From-Region.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Euphyllia Bierque <[email protected]>
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 20bcfeef39746d547ef17ccf2b66d44ce1e6e354..755527ed59b50c95aeca71d0fc5fea224731ff51 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
@@ -3333,4 +3333,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
}

0 comments on commit 1f0b85d

Please sign in to comment.