From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Euphyllia Bierque 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 9b182315682b5795b2cb6438ea440591ddddc5a3..d2db175fd7bfc5ff38e3945c8744858265212b1d 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -3253,4 +3253,69 @@ public final class CraftServer implements Server { public void allowPausing(final Plugin plugin, final boolean value) { this.console.addPluginAllowingSleep(plugin.getName(), value); } + + // 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 + 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 }