Paper/Spigot-API-Patches/0135-Async-Chunks-API.patch

403 lines
17 KiB
Diff
Raw Normal View History

From c56c68eed04866793c411e6dc5eeeabe2f73ace1 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Mon, 29 Feb 2016 17:43:33 -0600
Subject: [PATCH] Async Chunks API
Adds API's to load or generate chunks asynchronously.
Also adds utility methods to Entity to teleport asynchronously.
diff --git a/src/main/java/org/bukkit/World.java b/src/main/java/org/bukkit/World.java
index 807bd7fc..51cf7fd3 100644
--- a/src/main/java/org/bukkit/World.java
+++ b/src/main/java/org/bukkit/World.java
2018-10-27 06:02:22 +02:00
@@ -152,6 +152,352 @@ public interface World extends PluginMessageRecipient, Metadatable {
public default Chunk getChunkAt(long chunkKey) {
return getChunkAt((int) chunkKey, (int) (chunkKey >> 32));
}
+
+ /**
+ * This is the Legacy API before Java 8 was supported. Java 8 Consumer is provided,
+ * as well as future support
+ *
+ * Used by {@link World#getChunkAtAsync(Location,ChunkLoadCallback)} methods
+ * to request a {@link Chunk} to be loaded, with this callback receiving
+ * the chunk when it is finished.
+ *
+ * This callback will be executed on synchronously on the main thread.
+ *
+ * Timing and order this callback is fired is intentionally not defined and
+ * and subject to change.
+ *
+ * @deprecated Use either the Future or the Consumer based methods
+ */
+ @Deprecated
+ public static interface ChunkLoadCallback extends java.util.function.Consumer<Chunk> {
+ public void onLoad(Chunk chunk);
+
+ // backwards compat to old api
+ @Override
+ default void accept(Chunk chunk) {
+ onLoad(chunk);
+ }
+ }
+
+ /**
+ * Requests a {@link Chunk} to be loaded at the given coordinates
+ *
+ * This method makes no guarantee on how fast the chunk will load,
+ * and will return the chunk to the callback at a later time.
+ *
+ * You should use this method if you need a chunk but do not need it
+ * immediately, and you wish to let the server control the speed
+ * of chunk loads, keeping performance in mind.
+ *
+ * The {@link ChunkLoadCallback} will always be executed synchronously
+ * on the main Server Thread.
+ *
+ * @deprecated Use either the Future or the Consumer based methods
+ * @param x Chunk X-coordinate of the chunk - (world coordinate / 16)
+ * @param z Chunk Z-coordinate of the chunk - (world coordinate / 16)
+ * @param cb Callback to receive the chunk when it is loaded.
+ * will be executed synchronously
+ */
+ @Deprecated
+ public default void getChunkAtAsync(int x, int z, ChunkLoadCallback cb) {
+ getChunkAtAsync(x, z, true).thenAccept(cb::onLoad);
+ }
+
+ /**
+ * Requests a {@link Chunk} to be loaded at the given {@link Location}
+ *
+ * This method makes no guarantee on how fast the chunk will load,
+ * and will return the chunk to the callback at a later time.
+ *
+ * You should use this method if you need a chunk but do not need it
+ * immediately, and you wish to let the server control the speed
+ * of chunk loads, keeping performance in mind.
+ *
+ * The {@link ChunkLoadCallback} will always be executed synchronously
+ * on the main Server Thread.
+ *
+ * @deprecated Use either the Future or the Consumer based methods
+ * @param loc Location of the chunk
+ * @param cb Callback to receive the chunk when it is loaded.
+ * will be executed synchronously
+ */
+ @Deprecated
+ public default void getChunkAtAsync(Location loc, ChunkLoadCallback cb) {
+ getChunkAtAsync(loc, true).thenAccept(cb::onLoad);
+ }
+
+ /**
+ * Requests {@link Chunk} to be loaded that contains the given {@link Block}
+ *
+ * This method makes no guarantee on how fast the chunk will load,
+ * and will return the chunk to the callback at a later time.
+ *
+ * You should use this method if you need a chunk but do not need it
+ * immediately, and you wish to let the server control the speed
+ * of chunk loads, keeping performance in mind.
+ *
+ * The {@link ChunkLoadCallback} will always be executed synchronously
+ * on the main Server Thread.
+ *
+ * @deprecated Use either the Future or the Consumer based methods
+ * @param block Block to get the containing chunk from
+ * @param cb Callback to receive the chunk when it is loaded.
+ * will be executed synchronously
+ */
+ @Deprecated
+ public default void getChunkAtAsync(Block block, ChunkLoadCallback cb) {
+ getChunkAtAsync(block, true).thenAccept(cb::onLoad);
+ }
+
+ /**
+ * Requests a {@link Chunk} to be loaded at the given coordinates
+ *
+ * This method makes no guarantee on how fast the chunk will load,
+ * and will return the chunk to the callback at a later time.
+ *
+ * You should use this method if you need a chunk but do not need it
+ * immediately, and you wish to let the server control the speed
+ * of chunk loads, keeping performance in mind.
+ *
2018-10-25 15:38:19 +02:00
+ * The {@link java.util.function.Consumer} will always be executed synchronously
+ * on the main Server Thread.
+ *
+ * @param x Chunk X-coordinate of the chunk - (world coordinate / 16)
+ * @param z Chunk Z-coordinate of the chunk - (world coordinate / 16)
+ * @param cb Callback to receive the chunk when it is loaded.
+ * will be executed synchronously
+ */
+ public default void getChunkAtAsync(int x, int z, java.util.function.Consumer<Chunk> cb) {
+ getChunkAtAsync(x, z, true).thenAccept(cb);
+ }
+
+ /**
+ * Requests a {@link Chunk} to be loaded at the given coordinates
+ *
+ * This method makes no guarantee on how fast the chunk will load,
+ * and will return the chunk to the callback at a later time.
+ *
+ * You should use this method if you need a chunk but do not need it
+ * immediately, and you wish to let the server control the speed
+ * of chunk loads, keeping performance in mind.
+ *
2018-10-25 15:38:19 +02:00
+ * The {@link java.util.function.Consumer} will always be executed synchronously
+ * on the main Server Thread.
+ *
+ * @param x Chunk X-coordinate of the chunk - (world coordinate / 16)
+ * @param z Chunk Z-coordinate of the chunk - (world coordinate / 16)
2018-10-25 15:38:19 +02:00
+ * @param gen Should we generate a chunk if it doesn't exists or not
+ * @param cb Callback to receive the chunk when it is loaded.
+ * will be executed synchronously
+ */
+ public default void getChunkAtAsync(int x, int z, boolean gen, java.util.function.Consumer<Chunk> cb) {
+ getChunkAtAsync(x, z, gen).thenAccept(cb);
+ }
+
+ /**
+ * Requests a {@link Chunk} to be loaded at the given {@link Location}
+ *
+ * This method makes no guarantee on how fast the chunk will load,
+ * and will return the chunk to the callback at a later time.
+ *
+ * You should use this method if you need a chunk but do not need it
+ * immediately, and you wish to let the server control the speed
+ * of chunk loads, keeping performance in mind.
+ *
2018-10-25 15:38:19 +02:00
+ * The {@link java.util.function.Consumer} will always be executed synchronously
+ * on the main Server Thread.
+ *
+ * @param loc Location of the chunk
+ * @param cb Callback to receive the chunk when it is loaded.
+ * will be executed synchronously
+ */
+ public default void getChunkAtAsync(Location loc, java.util.function.Consumer<Chunk> cb) {
+ getChunkAtAsync((int)Math.floor(loc.getX()) >> 4, (int)Math.floor(loc.getZ()) >> 4, true, cb);
+ }
+
+ /**
+ * Requests a {@link Chunk} to be loaded at the given {@link Location}
+ *
+ * This method makes no guarantee on how fast the chunk will load,
+ * and will return the chunk to the callback at a later time.
+ *
+ * You should use this method if you need a chunk but do not need it
+ * immediately, and you wish to let the server control the speed
+ * of chunk loads, keeping performance in mind.
+ *
2018-10-25 15:38:19 +02:00
+ * The {@link java.util.function.Consumer} will always be executed synchronously
+ * on the main Server Thread.
+ *
+ * @param loc Location of the chunk
2018-10-25 15:38:19 +02:00
+ * @param gen Should the chunk generate
+ * @param cb Callback to receive the chunk when it is loaded.
+ * will be executed synchronously
+ */
+ public default void getChunkAtAsync(Location loc, boolean gen, java.util.function.Consumer<Chunk> cb) {
+ getChunkAtAsync((int)Math.floor(loc.getX()) >> 4, (int)Math.floor(loc.getZ()) >> 4, gen, cb);
+ }
+
+ /**
+ * Requests {@link Chunk} to be loaded that contains the given {@link Block}
+ *
+ * This method makes no guarantee on how fast the chunk will load,
+ * and will return the chunk to the callback at a later time.
+ *
+ * You should use this method if you need a chunk but do not need it
+ * immediately, and you wish to let the server control the speed
+ * of chunk loads, keeping performance in mind.
+ *
2018-10-25 15:38:19 +02:00
+ * The {@link java.util.function.Consumer} will always be executed synchronously
+ * on the main Server Thread.
+ *
+ * @param block Block to get the containing chunk from
+ * @param cb Callback to receive the chunk when it is loaded.
+ * will be executed synchronously
+ */
+ public default void getChunkAtAsync(Block block, java.util.function.Consumer<Chunk> cb) {
+ getChunkAtAsync(block.getX() >> 4, block.getZ() >> 4, true, cb);
+ }
+
+ /**
+ * Requests {@link Chunk} to be loaded that contains the given {@link Block}
+ *
+ * This method makes no guarantee on how fast the chunk will load,
+ * and will return the chunk to the callback at a later time.
+ *
+ * You should use this method if you need a chunk but do not need it
+ * immediately, and you wish to let the server control the speed
+ * of chunk loads, keeping performance in mind.
+ *
2018-10-25 15:38:19 +02:00
+ * The {@link java.util.function.Consumer} will always be executed synchronously
+ * on the main Server Thread.
+ *
+ * @param block Block to get the containing chunk from
2018-10-25 15:38:19 +02:00
+ * @param gen Should the chunk generate
+ * @param cb Callback to receive the chunk when it is loaded.
+ * will be executed synchronously
+ */
+ public default void getChunkAtAsync(Block block, boolean gen, java.util.function.Consumer<Chunk> cb) {
+ getChunkAtAsync(block.getX() >> 4, block.getZ() >> 4, gen, cb);
+ }
+
+ /**
+ * Requests a {@link Chunk} to be loaded at the given coordinates
+ *
+ * This method makes no guarantee on how fast the chunk will load,
+ * and will return the chunk to the callback at a later time.
+ *
+ * You should use this method if you need a chunk but do not need it
+ * immediately, and you wish to let the server control the speed
+ * of chunk loads, keeping performance in mind.
+ *
+ * The future will always be executed synchronously
+ * on the main Server Thread.
+ * @param loc Location to load the corresponding chunk from
+ * @return Future that will resolve when the chunk is loaded
+ */
+ public default java.util.concurrent.CompletableFuture<Chunk> getChunkAtAsync(Location loc) {
+ return getChunkAtAsync((int)Math.floor(loc.getX()) >> 4, (int)Math.floor(loc.getZ()) >> 4, true);
+ }
+
+ /**
+ * Requests a {@link Chunk} to be loaded at the given coordinates
+ *
+ * This method makes no guarantee on how fast the chunk will load,
+ * and will return the chunk to the callback at a later time.
+ *
+ * You should use this method if you need a chunk but do not need it
+ * immediately, and you wish to let the server control the speed
+ * of chunk loads, keeping performance in mind.
+ *
+ * The future will always be executed synchronously
+ * on the main Server Thread.
+ * @param loc Location to load the corresponding chunk from
+ * @param gen Should the chunk generate
+ * @return Future that will resolve when the chunk is loaded
+ */
+ public default java.util.concurrent.CompletableFuture<Chunk> getChunkAtAsync(Location loc, boolean gen) {
+ return getChunkAtAsync((int)Math.floor(loc.getX()) >> 4, (int)Math.floor(loc.getZ()) >> 4, gen);
+ }
+
+ /**
+ * Requests a {@link Chunk} to be loaded at the given coordinates
+ *
+ * This method makes no guarantee on how fast the chunk will load,
+ * and will return the chunk to the callback at a later time.
+ *
+ * You should use this method if you need a chunk but do not need it
+ * immediately, and you wish to let the server control the speed
+ * of chunk loads, keeping performance in mind.
+ *
+ * The future will always be executed synchronously
+ * on the main Server Thread.
+ * @param block Block to load the corresponding chunk from
+ * @return Future that will resolve when the chunk is loaded
+ */
+ public default java.util.concurrent.CompletableFuture<Chunk> getChunkAtAsync(Block block) {
+ return getChunkAtAsync(block.getX() >> 4, block.getZ() >> 4, true);
+ }
+
+ /**
+ * Requests a {@link Chunk} to be loaded at the given coordinates
+ *
+ * This method makes no guarantee on how fast the chunk will load,
+ * and will return the chunk to the callback at a later time.
+ *
+ * You should use this method if you need a chunk but do not need it
+ * immediately, and you wish to let the server control the speed
+ * of chunk loads, keeping performance in mind.
+ *
+ * The future will always be executed synchronously
+ * on the main Server Thread.
+ * @param block Block to load the corresponding chunk from
+ * @param gen Should the chunk generate
+ * @return Future that will resolve when the chunk is loaded
+ */
+ public default java.util.concurrent.CompletableFuture<Chunk> getChunkAtAsync(Block block, boolean gen) {
+ return getChunkAtAsync(block.getX() >> 4, block.getZ() >> 4, gen);
+ }
+
+ /**
+ * Requests a {@link Chunk} to be loaded at the given coordinates
+ *
+ * This method makes no guarantee on how fast the chunk will load,
+ * and will return the chunk to the callback at a later time.
+ *
+ * You should use this method if you need a chunk but do not need it
+ * immediately, and you wish to let the server control the speed
+ * of chunk loads, keeping performance in mind.
+ *
+ * The future will always be executed synchronously
+ * on the main Server Thread.
+ *
+ * @param x X Coord
+ * @param z Z Coord
+ * @return Future that will resolve when the chunk is loaded
+ */
+ public default java.util.concurrent.CompletableFuture<Chunk> getChunkAtAsync(int x, int z) {
+ return getChunkAtAsync(x, z, true);
+ }
+
+ /**
+ * Requests a {@link Chunk} to be loaded at the given coordinates
+ *
+ * This method makes no guarantee on how fast the chunk will load,
+ * and will return the chunk to the callback at a later time.
+ *
+ * You should use this method if you need a chunk but do not need it
+ * immediately, and you wish to let the server control the speed
+ * of chunk loads, keeping performance in mind.
+ *
+ * The future will always be executed synchronously
+ * on the main Server Thread.
+ *
+ * @param x Chunk X-coordinate of the chunk - (world coordinate / 16)
+ * @param z Chunk Z-coordinate of the chunk - (world coordinate / 16)
+ * @param gen Should we generate a chunk if it doesn't exists or not
+ * @return Future that will resolve when the chunk is loaded
+ */
+ public java.util.concurrent.CompletableFuture<Chunk> getChunkAtAsync(int x, int z, boolean gen);
// Paper end
/**
diff --git a/src/main/java/org/bukkit/entity/Entity.java b/src/main/java/org/bukkit/entity/Entity.java
Updated Upstream (Bukkit/CraftBukkit/Spigot) Upstream has released updates that appears to apply and compile correctly. This update has not been tested by PaperMC and as with ANY update, please do your own testing Warning: this commit contains more mapping changes from upstream, As always, ensure that you have working backups and test this build before deployment; Developers working on paper will, yet again, need to delete their work/Minecraft/1.13.2 folder Bukkit Changes: 7fca5fd4 SPIGOT-4558: Preserve user order in the face of copied defaults in configurations 15c9b1eb Ignore spurious slot IDs sent by client, e.g. in enchanting tables 5d2a10c5 SPIGOT-3747: Add API for force loaded chunks d6dd2bb3 SPIGOT-3538: Add getHitBlockFace for ProjectileHitEvent 771db4aa SPIGOT-794: Call EntityPlaceEvent for Minecart placement 55462509 Add InventoryView#getSlotType 2f3ce5b6 Remove EntityTransformEvent and CustomItemTagContainer from draft API f04ad7b6 Make ProjectileLaunchEvent extend EntitySpawnEvent ccb85808 Define EntitySpawnEvent b8cc3ebe Add PlayerItemDamageEvent 184a495d Ease ClassLoader Deadlocks Where Possible 11ac4728 Expand Boolean Prompt Values in Conversation API aae62d51 Added getAllSessionData() to the Conversation API. 9290ff91 Add InventoryView#getInventory API 995e530f Add API to get / set base arrow damage CraftBukkit Changes: c4a67eed SPIGOT-4556: Fix plugins closing inventory during drop events 5be2ddcb Replace version constants with methods to prevent compiler inlining a5b9c7b3 Use API method to create offset command completions 2bc7d1df SPIGOT-3747: Add API for force loaded chunks a408f375 SPIGOT-3538: Add getHitBlockFace for ProjectileHitEvent b54b9409 SPIGOT-2864: Make Arrow / Item setTicksLived behave like FallingBlock 79ded7a8 SPIGOT-1811: Death message not shown on respawn screen b4a4f15d SPIGOT-943: InventoryCloseEvent called on death regardless of open inventory 0afed592 SPIGOT-794: Call EntityPlaceEvent for Minecart placement 2b2d084a Add InventoryView#getSlotType 01a9959a Do not use deprecated ItemSpawnEvent constructor 9642498d SPIGOT-4547: Call EntitySpawnEvent as general spawn fallback event 963f4a5f Add PlayerItemDamageEvent 63db0445 Add API to get / set base arrow damage 531c25d7 Add CraftMagicNumbers.MAPPINGS_VERSION for use by NMS plugins d05c8b14 Mappings Update bd36e200 SPIGOT-4551: Ignore invalid attribute modifier slots Spigot Changes: 518206a1 Remove redundant trove depend 1959ad21 MC-11211,SPIGOT-4552: Fix placing double slabs at y = 255 29ab5e43 SPIGOT-3661: Allow arguments in restart-script 7cc46316 SPIGOT-852: Growth modifiers for beetroots, potatoes, carrots 82e117e1 Squelch "fatal: Resolve operation not in progress" message 0a1a68e7 Mappings Update & Patch Rebuild
2019-01-01 04:15:55 +01:00
index 2dd7a03c..59787c47 100644
--- a/src/main/java/org/bukkit/entity/Entity.java
+++ b/src/main/java/org/bukkit/entity/Entity.java
2018-10-27 06:02:22 +02:00
@@ -135,6 +135,28 @@ public interface Entity extends Metadatable, CommandSender, Nameable {
*/
public boolean teleport(Entity destination, TeleportCause cause);
+ // Paper start
+ /**
+ * Loads/Generates(in 1.13+) the Chunk asynchronously, and then teleports the entity when the chunk is ready.
+ * @param loc Location to teleport to
+ * @return A future that will be completed with the result of the teleport
+ */
+ public default java.util.concurrent.CompletableFuture<Boolean> teleportAsync(Location loc) {
+ return teleportAsync(loc, TeleportCause.PLUGIN);
+ }
+ /**
+ * Loads/Generates(in 1.13+) the Chunk asynchronously, and then teleports the entity when the chunk is ready.
+ * @param loc Location to teleport to
+ * @param cause Reason for teleport
+ * @return A future that will be completed with the result of the teleport
+ */
+ public default java.util.concurrent.CompletableFuture<Boolean> teleportAsync(Location loc, TeleportCause cause) {
+ java.util.concurrent.CompletableFuture<Boolean> future = new java.util.concurrent.CompletableFuture<>();
+ loc.getWorld().getChunkAtAsync(loc).thenAccept((chunk) -> future.complete(teleport(loc, cause)));
+ return future;
+ }
+ // Paper end
+
/**
* Returns a list of entities within a bounding box centered around this
* entity
--
2.21.0