Skip to content

Commit

Permalink
Implement Bukkit#getTps
Browse files Browse the repository at this point in the history
Note that this requires the caller to be on a ticking region
or the global region, otherwise it will throw
UnsupportedOperationException.

Now the function returns the TPS for the current region.
  • Loading branch information
Spottedleaf committed Jan 29, 2025
1 parent 1f0b85d commit 87f8bd3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ 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
index b254cab566110c590ba22b0bd48cc165bfd7ae27..bcd17eba9798747010ed96903992939a284199df 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 {
@@ -3349,4 +3349,68 @@ public final class CraftServer implements Server {
this.console.addPluginAllowingSleep(plugin.getName(), value);
}
// Paper end - API to check if the server is sleeping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,37 @@
//Preconditions.checkState(!this.console.isIteratingOverLevels, "Cannot unload a world while worlds are being ticked"); // Paper - Cat - Temp disable. We'll see how this goes.
if (world == null) {
return false;
@@ -3083,11 +_,27 @@

@Override
public double[] getTPS() {
+ // Folia start - region threading
+ ca.spottedleaf.concurrentutil.scheduler.SchedulerThreadPool.SchedulableTick task = io.papermc.paper.threadedregions.TickRegionScheduler.getCurrentTickingTask();
+ if (task == null) {
+ // might be on the shutdown thread, try retrieving the current region
+ if (io.papermc.paper.threadedregions.TickRegionScheduler.getCurrentRegion() != null) {
+ // we are on the shutdown thread
+ task = io.papermc.paper.threadedregions.TickRegionScheduler.getCurrentRegion().getData().getRegionSchedulingHandle();
+ }
+ }
+ if (!(task instanceof io.papermc.paper.threadedregions.TickRegionScheduler.RegionScheduleHandle tickHandle)) {
+ throw new UnsupportedOperationException("Not on any region");
+ }
+
+ // 1m, 5m, 15m
+ long currTime = System.nanoTime();
return new double[] {
- net.minecraft.server.MinecraftServer.getServer().tps1.getAverage(),
- net.minecraft.server.MinecraftServer.getServer().tps5.getAverage(),
- net.minecraft.server.MinecraftServer.getServer().tps15.getAverage()
+ tickHandle.getTickReport1m(currTime).tpsData().segmentAll().average(),
+ tickHandle.getTickReport5m(currTime).tpsData().segmentAll().average(),
+ tickHandle.getTickReport15m(currTime).tpsData().segmentAll().average(),
};
+ // Folia end - region threading
}

// Paper start - adventure sounds
@@ -3258,7 +_,7 @@

@Override
Expand Down

0 comments on commit 87f8bd3

Please sign in to comment.