Added chunk view API (#10398)

This commit is contained in:
Florian Beck 2024-04-20 19:56:59 +02:00 committed by GitHub
parent 3af1346520
commit a033033b7a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 120 additions and 0 deletions

View File

@ -0,0 +1,58 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Flo0 <flo.roma@web.de>
Date: Mon, 8 Apr 2024 16:22:07 +0200
Subject: [PATCH] API for checking sent chunks
diff --git a/src/main/java/org/bukkit/entity/Player.java b/src/main/java/org/bukkit/entity/Player.java
index c6cb4f17469a8f2e60dd3e28d41402851ce5fb21..d048ae07cc33fd77d128cc1ebf88b0804969fa3c 100644
--- a/src/main/java/org/bukkit/entity/Player.java
+++ b/src/main/java/org/bukkit/entity/Player.java
@@ -3707,6 +3707,47 @@ public interface Player extends HumanEntity, Conversable, OfflinePlayer, PluginM
void resetIdleDuration();
// Paper end
+ // Paper start - Add chunk view API
+ /**
+ * Gets the a set of chunk keys for all chunks that have been sent to the player.
+ *
+ * @return an immutable set of chunk keys
+ * @apiNote currently marked as experimental to gather feedback regarding the returned set being an immutable copy
+ * vs it potentially being an unmodifiable view of the set chunks.
+ */
+ @ApiStatus.Experimental
+ java.util.@NotNull @org.jetbrains.annotations.Unmodifiable Set<Long> getSentChunkKeys();
+
+ /**
+ * Gets the set of chunks that have been sent to the player.
+ *
+ * @return an immutable set of chunks
+ * @apiNote currently marked as experimental to gather feedback regarding the returned set being an immutable copy
+ * vs it potentially being an unmodifiable view of the set chunks.
+ */
+ @ApiStatus.Experimental
+ java.util.@NotNull @org.jetbrains.annotations.Unmodifiable Set<org.bukkit.Chunk> getSentChunks();
+
+ /**
+ * Checks if the player has been sent a specific chunk.
+ *
+ * @param chunk the chunk to check
+ * @return true if the player has been sent the chunk, false otherwise
+ */
+ default boolean isChunkSent(@NotNull org.bukkit.Chunk chunk) {
+ return this.isChunkSent(chunk.getChunkKey());
+ }
+
+ /**
+ * Checks if the player has been sent a specific chunk.
+ *
+ * @param chunkKey the chunk key to check
+ * @return true if the player has been sent the chunk, false otherwise
+ * @see org.bukkit.Chunk#getChunkKey()
+ */
+ boolean isChunkSent(long chunkKey);
+ // Paper end
+
@NotNull
@Override
Spigot spigot();

View File

@ -0,0 +1,62 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Flo0 <flo.roma@web.de>
Date: Mon, 8 Apr 2024 16:43:16 +0200
Subject: [PATCH] API for checking sent chunks
diff --git a/src/main/java/io/papermc/paper/chunk/system/RegionizedPlayerChunkLoader.java b/src/main/java/io/papermc/paper/chunk/system/RegionizedPlayerChunkLoader.java
index 1b090f1e79b996e52097afc49c1cec85936653e6..bf3c5efa0d58c58a5b0b6b73880aaf03c8a37c12 100644
--- a/src/main/java/io/papermc/paper/chunk/system/RegionizedPlayerChunkLoader.java
+++ b/src/main/java/io/papermc/paper/chunk/system/RegionizedPlayerChunkLoader.java
@@ -1107,6 +1107,11 @@ public class RegionizedPlayerChunkLoader {
// now all tickets should be removed, which is all of our external state
}
+
+ // For external checks
+ public it.unimi.dsi.fastutil.longs.LongOpenHashSet getSentChunksRaw() {
+ return this.sentChunks;
+ }
}
// TODO rebase into util patch
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
index 4aa6084fb3540f7811f13e09b5d11e2e0cf48220..d5dd3c0d50ad34fe339f7aaaba0652ec69441e44 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
@@ -3390,6 +3390,35 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
}
// Paper end
+ // Paper start - Add chunk view API
+ @Override
+ public Set<java.lang.Long> getSentChunkKeys() {
+ org.spigotmc.AsyncCatcher.catchOp("accessing sent chunks");
+ return it.unimi.dsi.fastutil.longs.LongSets.unmodifiable(
+ this.getHandle().chunkLoader.getSentChunksRaw().clone()
+ );
+ }
+
+ @Override
+ public Set<org.bukkit.Chunk> getSentChunks() {
+ org.spigotmc.AsyncCatcher.catchOp("accessing sent chunks");
+ final it.unimi.dsi.fastutil.longs.LongOpenHashSet rawChunkKeys = this.getHandle().chunkLoader.getSentChunksRaw();
+ final it.unimi.dsi.fastutil.objects.ObjectOpenHashSet<org.bukkit.Chunk> chunks = new it.unimi.dsi.fastutil.objects.ObjectOpenHashSet<>(rawChunkKeys.size());
+ final org.bukkit.World world = this.getWorld();
+
+ final it.unimi.dsi.fastutil.longs.LongIterator iter = this.getHandle().chunkLoader.getSentChunksRaw().longIterator();
+ while (iter.hasNext()) chunks.add(world.getChunkAt(iter.nextLong(), false));
+
+ return it.unimi.dsi.fastutil.objects.ObjectSets.unmodifiable(chunks);
+ }
+
+ @Override
+ public boolean isChunkSent(final long chunkKey) {
+ org.spigotmc.AsyncCatcher.catchOp("accessing sent chunks");
+ return this.getHandle().chunkLoader.getSentChunksRaw().contains(chunkKey);
+ }
+ // Paper end
+
public Player.Spigot spigot()
{
return this.spigot;