Tick regions api

This commit is contained in:
MrHua269
2025-01-27 13:15:24 +08:00
parent 9425fd9ec0
commit 56b428ddc4
3 changed files with 422 additions and 0 deletions

View File

@@ -0,0 +1,200 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: MrHua269 <wangxyper@163.com>
Date: Mon, 27 Jan 2025 13:01:59 +0800
Subject: [PATCH] Tick regions api
diff --git a/src/main/java/me/earthme/luminol/api/RegionStats.java b/src/main/java/me/earthme/luminol/api/RegionStats.java
new file mode 100644
index 0000000000000000000000000000000000000000..96147cace1550d14c682258dab0397587dcf76a4
--- /dev/null
+++ b/src/main/java/me/earthme/luminol/api/RegionStats.java
@@ -0,0 +1,25 @@
+package me.earthme.luminol.api;
+
+/**
+ * A simple package of folia's tick region state.It linked to the RegionStats of the nms part so that</br>
+ * You could call these methods to get the status of this tick region</br>
+ */
+public interface RegionStats {
+ /**
+ * Get the entity count in this tick region
+ * @return the entity count
+ */
+ int getEntityCount();
+
+ /**
+ * Get the player count in this tick region
+ * @return the player count
+ */
+ int getPlayerCount();
+
+ /**
+ * Get the chunk count in this tick region
+ * @return the chunk count
+ */
+ int getChunkCount();
+}
diff --git a/src/main/java/me/earthme/luminol/api/ThreadedRegion.java b/src/main/java/me/earthme/luminol/api/ThreadedRegion.java
new file mode 100644
index 0000000000000000000000000000000000000000..38972ac3d05ccbbb134f751a8d018b9c53ad10b5
--- /dev/null
+++ b/src/main/java/me/earthme/luminol/api/ThreadedRegion.java
@@ -0,0 +1,50 @@
+package me.earthme.luminol.api;
+
+import org.bukkit.Location;
+import org.bukkit.World;
+
+import javax.annotation.Nullable;
+
+/**
+ * A mirror of folia's ThreadedRegion</br>
+ * Including some handy methods to get the information of the tick region</br>
+ * Note: You should call these methods inside this tick region's thread context
+ */
+public interface ThreadedRegion {
+ /**
+ * Get the center chunk pos of this tick region</br>
+ * Note:</br>
+ * 1.Global region will return a null value(But we don't finish the global region yet()</br>
+ * 2.You should call these methods inside this tick region's thread context
+ * @return The center chunk pos
+ */
+ @Nullable
+ Location getCenterChunkPos();
+
+ /**
+ * Get the dead section percent of this tick region
+ * Note: </br>
+ * 1.Dead percent is mean the percent of the unloaded chunk count of this tick region, which is also used for determine
+ * that the tick region should or not check for splitting</br>
+ * 2.You should call these methods inside this tick region's thread context
+ * @return The dead section percent
+ */
+ double getDeadSectionPercent();
+
+ /**
+ * Get the tick region data of this tick region</br>
+ * Note:</br>
+ * 1.You should call this method inside this tick region's thread context</br>
+ * 2.You should call these methods inside this tick region's thread context
+ * @return The tick region data
+ */
+ TickRegionData getTickRegionData();
+
+ /**
+ * Get the world of this tick region</br>
+ * Note: Global region will return a null value too
+ * @return The world of this tick region
+ */
+ @Nullable
+ World getWorld();
+}
diff --git a/src/main/java/me/earthme/luminol/api/ThreadedRegionizer.java b/src/main/java/me/earthme/luminol/api/ThreadedRegionizer.java
new file mode 100644
index 0000000000000000000000000000000000000000..ff31a68a019fd9e5e687e6818f8729f4950bc060
--- /dev/null
+++ b/src/main/java/me/earthme/luminol/api/ThreadedRegionizer.java
@@ -0,0 +1,56 @@
+package me.earthme.luminol.api;
+
+import org.bukkit.Location;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.Collection;
+
+/**
+ * A mirror of folia's ThreadedRegionizer
+ */
+public interface ThreadedRegionizer {
+ /**
+ * Get all the tick regions
+ * @return Temporary copied collection of all tick regions
+ */
+ Collection<ThreadedRegion> getAllRegions();
+
+ /**
+ * Get the tick region at the given chunk coordinates
+ * @param chunkX Chunk X
+ * @param chunkZ Chunk Z
+ * @return The tick region at the given chunk coordinates
+ */
+ @Nullable
+ ThreadedRegion getAtSynchronized(int chunkX, int chunkZ);
+
+ /**
+ * Get the tick region at the given chunk coordinates
+ * @param chunkX Chunk X
+ * @param chunkZ Chunk Z
+ * @return The tick region at the given chunk coordinates
+ */
+ @Nullable
+ ThreadedRegion getAtUnSynchronized(int chunkX, int chunkZ);
+
+ /**
+ * Get the tick region at the given location
+ * @param pos The location
+ * @return The tick region at the given location
+ */
+ @Nullable
+ default ThreadedRegion getAtSynchronized(@NotNull Location pos) {
+ return this.getAtSynchronized(pos.getBlockX() >> 4, pos.getBlockZ() >> 4);
+ }
+
+ /**
+ * Get the tick region at the given location
+ * @param pos The location
+ * @return The tick region at the given location
+ */
+ @Nullable
+ default ThreadedRegion getAtUnSynchronized(@NotNull Location pos) {
+ return this.getAtUnSynchronized(pos.getBlockX() >> 4, pos.getBlockZ() >> 4);
+ }
+}
diff --git a/src/main/java/me/earthme/luminol/api/TickRegionData.java b/src/main/java/me/earthme/luminol/api/TickRegionData.java
new file mode 100644
index 0000000000000000000000000000000000000000..ecde4462b08d701b8bff9f26902f17754cf791dd
--- /dev/null
+++ b/src/main/java/me/earthme/luminol/api/TickRegionData.java
@@ -0,0 +1,26 @@
+package me.earthme.luminol.api;
+
+import org.bukkit.World;
+
+/**
+ * A mirror of folia's tick region data
+ */
+public interface TickRegionData {
+ /**
+ * Get the world it's currently holding
+ * @return the world
+ */
+ World getWorld();
+
+ /**
+ * Get the current tick count
+ * @return the current tick count
+ */
+ long getCurrentTickCount();
+
+ /**
+ * Get the region stats
+ * @return the region stats
+ */
+ RegionStats getRegionStats();
+}
diff --git a/src/main/java/org/bukkit/World.java b/src/main/java/org/bukkit/World.java
index e99fa923d35b6dda0b02968bdcf6b43552517ea4..33d22416f4097cf42947f29a90916863a309a828 100644
--- a/src/main/java/org/bukkit/World.java
+++ b/src/main/java/org/bukkit/World.java
@@ -4392,4 +4392,8 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
}
}
}
+
+ // Luminol start - Tick regions api
+ me.earthme.luminol.api.ThreadedRegionizer getThreadedRegionizer();
+ // Luminol end - Tick regions api
}

View File

@@ -0,0 +1,19 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: MrHua269 <wangxyper@163.com>
Date: Mon, 27 Jan 2025 13:01:55 +0800
Subject: [PATCH] Tick regions api
diff --git a/io/papermc/paper/threadedregions/ThreadedRegionizer.java b/io/papermc/paper/threadedregions/ThreadedRegionizer.java
index 604385af903845d966382ad0a4168798e4ed4a0e..7ac803ba9706b65a0125b6e00983fe6a7947991a 100644
--- a/io/papermc/paper/threadedregions/ThreadedRegionizer.java
+++ b/io/papermc/paper/threadedregions/ThreadedRegionizer.java
@@ -819,7 +819,7 @@ public final class ThreadedRegionizer<R extends ThreadedRegionizer.ThreadedRegio
return this.deadSections.size() == this.sectionByKey.size();
}
- private final double getDeadSectionPercent() {
+ public final double getDeadSectionPercent() { // Luminol - Threaded regions api
return (double)this.deadSections.size() / (double)this.sectionByKey.size();
}

View File

@@ -0,0 +1,203 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: MrHua269 <wangxyper@163.com>
Date: Mon, 27 Jan 2025 13:01:56 +0800
Subject: [PATCH] Tick regions api
diff --git a/src/main/java/me/earthme/luminol/api/impl/RegionStatsImpl.java b/src/main/java/me/earthme/luminol/api/impl/RegionStatsImpl.java
new file mode 100644
index 0000000000000000000000000000000000000000..30d4e7b6a433974c5047148f35973eb31c82d424
--- /dev/null
+++ b/src/main/java/me/earthme/luminol/api/impl/RegionStatsImpl.java
@@ -0,0 +1,27 @@
+package me.earthme.luminol.api.impl;
+
+import io.papermc.paper.threadedregions.TickRegions;
+import me.earthme.luminol.api.RegionStats;
+
+public class RegionStatsImpl implements RegionStats {
+ private final TickRegions.RegionStats internal;
+
+ public RegionStatsImpl(TickRegions.RegionStats internal) {
+ this.internal = internal;
+ }
+
+ @Override
+ public int getEntityCount() {
+ return this.internal.getEntityCount();
+ }
+
+ @Override
+ public int getPlayerCount() {
+ return this.internal.getPlayerCount();
+ }
+
+ @Override
+ public int getChunkCount() {
+ return this.internal.getChunkCount();
+ }
+}
diff --git a/src/main/java/me/earthme/luminol/api/impl/ThreadedRegionImpl.java b/src/main/java/me/earthme/luminol/api/impl/ThreadedRegionImpl.java
new file mode 100644
index 0000000000000000000000000000000000000000..a1380abacf7bc0fe106982d9b4cd7bbc44e4e375
--- /dev/null
+++ b/src/main/java/me/earthme/luminol/api/impl/ThreadedRegionImpl.java
@@ -0,0 +1,47 @@
+package me.earthme.luminol.api.impl;
+
+import io.papermc.paper.threadedregions.ThreadedRegionizer;
+import io.papermc.paper.threadedregions.TickRegions;
+import me.earthme.luminol.api.ThreadedRegion;
+import me.earthme.luminol.api.TickRegionData;
+import net.minecraft.world.level.ChunkPos;
+import org.bukkit.Location;
+import org.bukkit.World;
+
+import javax.annotation.Nullable;
+
+public class ThreadedRegionImpl implements ThreadedRegion {
+ private final ThreadedRegionizer.ThreadedRegion<TickRegions.TickRegionData, TickRegions.TickRegionSectionData> internal;
+
+ public ThreadedRegionImpl(ThreadedRegionizer.ThreadedRegion<TickRegions.TickRegionData, TickRegions.TickRegionSectionData> internal) {
+ this.internal = internal;
+ }
+
+ @Nullable
+ @Override
+ public Location getCenterChunkPos() {
+ final ChunkPos centerChunkPos = this.internal.getCenterChunk();
+
+ if (centerChunkPos == null) {
+ return null;
+ }
+
+ return new Location(this.internal.regioniser.world.getWorld(), centerChunkPos.getMiddleBlockX(), 0, centerChunkPos.getMiddleBlockZ());
+ }
+
+ @Override
+ public double getDeadSectionPercent() {
+ return this.internal.getDeadSectionPercent();
+ }
+
+ @Override
+ public TickRegionData getTickRegionData() {
+ return new TickRegionDataImpl(this.internal.getData());
+ }
+
+ @Nullable
+ @Override
+ public World getWorld() {
+ return this.internal.regioniser.world.getWorld();
+ }
+}
diff --git a/src/main/java/me/earthme/luminol/api/impl/ThreadedRegionizerImpl.java b/src/main/java/me/earthme/luminol/api/impl/ThreadedRegionizerImpl.java
new file mode 100644
index 0000000000000000000000000000000000000000..203ba60216febd8c19315afd89140d5400213f67
--- /dev/null
+++ b/src/main/java/me/earthme/luminol/api/impl/ThreadedRegionizerImpl.java
@@ -0,0 +1,53 @@
+package me.earthme.luminol.api.impl;
+
+import io.papermc.paper.threadedregions.TickRegions;
+import me.earthme.luminol.api.ThreadedRegion;
+import me.earthme.luminol.api.ThreadedRegionizer;
+import net.minecraft.server.level.ServerLevel;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+public class ThreadedRegionizerImpl implements ThreadedRegionizer {
+ private final ServerLevel internal;
+
+ public ThreadedRegionizerImpl(ServerLevel internal) {
+ this.internal = internal;
+ }
+
+ @Override
+ public Collection<ThreadedRegion> getAllRegions() {
+ final List<ThreadedRegion> ret = new ArrayList<>();
+
+ this.internal.regioniser.computeForAllRegions(region -> {
+ final ThreadedRegion wrapped = new ThreadedRegionImpl(region);
+
+ ret.add(wrapped);
+ });
+
+ return ret;
+ }
+
+ @Override
+ public ThreadedRegion getAtSynchronized(int chunkX, int chunkZ) {
+ final io.papermc.paper.threadedregions.ThreadedRegionizer.ThreadedRegion<TickRegions.TickRegionData, TickRegions.TickRegionSectionData> got = this.internal.regioniser.getRegionAtSynchronised(chunkX, chunkZ);
+
+ if (got == null) {
+ return null;
+ }
+
+ return new ThreadedRegionImpl(got);
+ }
+
+ @Override
+ public ThreadedRegion getAtUnSynchronized(int chunkX, int chunkZ) {
+ final io.papermc.paper.threadedregions.ThreadedRegionizer.ThreadedRegion<TickRegions.TickRegionData, TickRegions.TickRegionSectionData> got = this.internal.regioniser.getRegionAtUnsynchronised(chunkX, chunkZ);
+
+ if (got == null) {
+ return null;
+ }
+
+ return new ThreadedRegionImpl(got);
+ }
+}
diff --git a/src/main/java/me/earthme/luminol/api/impl/TickRegionDataImpl.java b/src/main/java/me/earthme/luminol/api/impl/TickRegionDataImpl.java
new file mode 100644
index 0000000000000000000000000000000000000000..cf51350ccabeda97e9eff41ccd30c58e836fe041
--- /dev/null
+++ b/src/main/java/me/earthme/luminol/api/impl/TickRegionDataImpl.java
@@ -0,0 +1,30 @@
+package me.earthme.luminol.api.impl;
+
+import io.papermc.paper.threadedregions.TickRegions;
+import me.earthme.luminol.api.RegionStats;
+import me.earthme.luminol.api.TickRegionData;
+import org.bukkit.World;
+
+public class TickRegionDataImpl implements TickRegionData {
+ private final TickRegions.TickRegionData internal;
+
+ public TickRegionDataImpl(TickRegions.TickRegionData internal) {
+ this.internal = internal;
+ }
+
+ @Override
+ public World getWorld() {
+ return this.internal.world.getWorld();
+ }
+
+ @Override
+ public long getCurrentTickCount() {
+ return this.internal.getCurrentTick();
+ }
+
+ @Override
+ public RegionStats getRegionStats() {
+ return new RegionStatsImpl(this.internal.getRegionStats());
+ }
+
+}
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
index b88d48b79b97b22adbfb22a3b289237737a5fb5f..e0e283698ff621c4799ce9f50d86e7159cad4265 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
@@ -2517,4 +2517,11 @@ public class CraftWorld extends CraftRegionAccessor implements World {
return this.adventure$pointers;
}
// Paper end
+
+ // Luminol start - Tick regions api
+ @Override
+ public me.earthme.luminol.api.ThreadedRegionizer getThreadedRegionizer() {
+ return new me.earthme.luminol.api.impl.ThreadedRegionizerImpl(this.world);
+ }
+ // Luminol end
}