From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Aikar 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 2986793d6e3e71ae900bbd3293af288d4ad0b503..9517091f601f362f80ff6062f53eff760f040e6c 100644 --- a/src/main/java/org/bukkit/World.java +++ b/src/main/java/org/bukkit/World.java @@ -977,6 +977,509 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient } // Paper end - additional getNearbyEntities API + // Paper start - async chunks API + /** + * 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(since = "1.13.1") + public static interface ChunkLoadCallback extends java.util.function.Consumer { + public void onLoad(@NotNull Chunk chunk); + + // backwards compat to old api + @Override + default void accept(@NotNull 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 - floor(world coordinate / 16) + * @param z Chunk Z-coordinate of the chunk - floor(world coordinate / 16) + * @param cb Callback to receive the chunk when it is loaded. + * will be executed synchronously + */ + @Deprecated(since = "1.13.1") + public default void getChunkAtAsync(int x, int z, @NotNull ChunkLoadCallback cb) { + this.getChunkAtAsync(x, z, (java.util.function.Consumer)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. + * + * 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(since = "1.13.1") + public default void getChunkAtAsync(@NotNull Location loc, @NotNull ChunkLoadCallback cb) { + this.getChunkAtAsync(loc.getBlockX() >> 4, loc.getBlockZ() >> 4, 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. + * + * 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(since = "1.13.1") + public default void getChunkAtAsync(@NotNull Block block, @NotNull ChunkLoadCallback cb) { + this.getChunkAtAsync(block.getX() >> 4, block.getZ() >> 4, 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 {@link java.util.function.Consumer} will always be executed synchronously + * on the main Server Thread. + * + * @param x Chunk X-coordinate of the chunk - floor(world coordinate / 16) + * @param z Chunk Z-coordinate of the chunk - floor(world coordinate / 16) + * @param cb Callback to receive the chunk when it is loaded. + * will be executed synchronously + */ + default void getChunkAtAsync(final int x, final int z, final @NotNull Consumer cb) { + this.getChunkAtAsync(x, z, true, 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 {@link java.util.function.Consumer} will always be executed synchronously + * on the main Server Thread. + * + * @param x Chunk X-coordinate of the chunk - floor(world coordinate / 16) + * @param z Chunk Z-coordinate of the chunk - floor(world coordinate / 16) + * @param gen Should we generate a chunk if it doesn't exist or not + * @param cb Callback to receive the chunk when it is loaded. + * will be executed synchronously + */ + default void getChunkAtAsync(final int x, final int z, final boolean gen, final @NotNull Consumer cb) { + this.getChunkAtAsync(x, z, gen, false, 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 {@link java.util.function.Consumer} will always be executed synchronously + * on the main Server Thread. + * + * @param x Chunk X-coordinate of the chunk - floor(world coordinate / 16) + * @param z Chunk Z-coordinate of the chunk - floor(world coordinate / 16) + * @param gen Should we generate a chunk if it doesn't exist or not + * @param urgent If true, the chunk may be prioritised to be loaded above other chunks in queue + * @param cb Callback to receive the chunk when it is loaded. + * will be executed synchronously + */ + void getChunkAtAsync(final int x, final int z, final boolean gen, final boolean urgent, final @NotNull Consumer cb); + + /** + * Requests all chunks with x between [minX, maxZ] and z + * between [minZ, maxZ] to be loaded. + * + * This method makes no guarantee on how fast the chunk will load, + * and will invoke the callback at possibly a later time. + * + * You should use this method if you need chunks loaded but do not need them + * immediately, and you wish to let the server control the speed + * of chunk loads, keeping performance in mind. + * + * The {@link Runnable} will always be executed synchronously + * on the main Server Thread, and when invoked all chunks requested will be loaded. + * + * @param minX Minimum chunk X-coordinate of the chunk - floor(world coordinate / 16) + * @param minZ Minimum chunk Z-coordinate of the chunk - floor(world coordinate / 16) + * @param maxX Maximum chunk X-coordinate of the chunk - floor(world coordinate / 16) + * @param maxZ Maximum chunk Z-coordinate of the chunk - floor(world coordinate / 16) + * @param urgent If true, the chunks may be prioritised to be loaded above other chunks in queue + * @param cb Callback to invoke when all chunks are loaded. + * Will be executed synchronously + * @see Chunk + */ + void getChunksAtAsync(final int minX, final int minZ, final int maxX, final int maxZ, final boolean urgent, + final @NotNull Runnable 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. + * + * 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 + */ + default void getChunkAtAsync(final @NotNull Location loc, final @NotNull Consumer cb) { + this.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. + * + * The {@link java.util.function.Consumer} will always be executed synchronously + * on the main Server Thread. + * + * @param loc Location of the chunk + * @param gen Should the chunk generate if it doesn't exist + * @param cb Callback to receive the chunk when it is loaded. + * will be executed synchronously + */ + default void getChunkAtAsync(final @NotNull Location loc, final boolean gen, final @NotNull Consumer cb) { + this.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. + * + * 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 + */ + default void getChunkAtAsync(final @NotNull Block block, final @NotNull Consumer cb) { + this.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. + * + * 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 gen Should the chunk generate if it doesn't exist + * @param cb Callback to receive the chunk when it is loaded. + * will be executed synchronously + */ + default void getChunkAtAsync(final @NotNull Block block, final boolean gen, final @NotNull Consumer cb) { + this.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 + */ + default @NotNull java.util.concurrent.CompletableFuture getChunkAtAsync(final @NotNull Location loc) { + return this.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 if it doesn't exist + * @return Future that will resolve when the chunk is loaded + */ + default @NotNull java.util.concurrent.CompletableFuture getChunkAtAsync(final @NotNull Location loc, final boolean gen) { + return this.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 + */ + default @NotNull java.util.concurrent.CompletableFuture getChunkAtAsync(final @NotNull Block block) { + return this.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 if it doesn't exist + * @return Future that will resolve when the chunk is loaded + */ + default @NotNull java.util.concurrent.CompletableFuture getChunkAtAsync(final @NotNull Block block, final boolean gen) { + return this.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 Chunk X-coordinate of the chunk - floor(world coordinate / 16) + * @param z Chunk Z-coordinate of the chunk - floor(world coordinate / 16) + * @return Future that will resolve when the chunk is loaded + */ + default @NotNull java.util.concurrent.CompletableFuture getChunkAtAsync(final int x, final int z) { + return this.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 - floor(world coordinate / 16) + * @param z Chunk Z-coordinate of the chunk - floor(world coordinate / 16) + * @param gen Should we generate a chunk if it doesn't exist or not + * @return Future that will resolve when the chunk is loaded + */ + default @NotNull java.util.concurrent.CompletableFuture getChunkAtAsync(final int x, final int z, final boolean gen) { + return this.getChunkAtAsync(x, z, gen, false); + } + + /** + * 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 + */ + default @NotNull java.util.concurrent.CompletableFuture getChunkAtAsyncUrgently(final @NotNull Location loc) { + return this.getChunkAtAsync((int) Math.floor(loc.getX()) >> 4, (int) Math.floor(loc.getZ()) >> 4, true, 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 if it doesn't exist + * @return Future that will resolve when the chunk is loaded + */ + default @NotNull java.util.concurrent.CompletableFuture getChunkAtAsyncUrgently(final @NotNull Location loc, final boolean gen) { + return this.getChunkAtAsync((int) Math.floor(loc.getX()) >> 4, (int) Math.floor(loc.getZ()) >> 4, gen, 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 + * @return Future that will resolve when the chunk is loaded + */ + default @NotNull java.util.concurrent.CompletableFuture getChunkAtAsyncUrgently(final @NotNull Block block) { + return this.getChunkAtAsync(block.getX() >> 4, block.getZ() >> 4, true, 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 if it doesn't exist + * @return Future that will resolve when the chunk is loaded + */ + default @NotNull java.util.concurrent.CompletableFuture getChunkAtAsyncUrgently(final @NotNull Block block, final boolean gen) { + return this.getChunkAtAsync(block.getX() >> 4, block.getZ() >> 4, gen, 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 X Coord + * @param z Z Coord + * @return Future that will resolve when the chunk is loaded + */ + default @NotNull java.util.concurrent.CompletableFuture getChunkAtAsyncUrgently(final int x, final int z) { + return this.getChunkAtAsync(x, z, true, true); + } + + default @NotNull java.util.concurrent.CompletableFuture getChunkAtAsync(int x, int z, boolean gen, boolean urgent) { + java.util.concurrent.CompletableFuture ret = new java.util.concurrent.CompletableFuture<>(); + this.getChunkAtAsync(x, z, gen, urgent, ret::complete); + return ret; + } + // Paper end - async chunks API + /** * Get a list of all players in this World * diff --git a/src/main/java/org/bukkit/entity/Entity.java b/src/main/java/org/bukkit/entity/Entity.java index 45f06224d476551267e9b083985051ae9954d756..b808044403e6b38d1801aefde2176d630c747a64 100644 --- a/src/main/java/org/bukkit/entity/Entity.java +++ b/src/main/java/org/bukkit/entity/Entity.java @@ -168,6 +168,39 @@ public interface Entity extends Metadatable, CommandSender, Nameable, Persistent */ public boolean teleport(@NotNull Entity destination, @NotNull 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 + */ + default java.util.concurrent.@NotNull CompletableFuture teleportAsync(final @NotNull Location loc) { + return this.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 + */ + default java.util.concurrent.@NotNull CompletableFuture teleportAsync(final @NotNull Location loc, final @NotNull TeleportCause cause) { + final class Holder { + static final io.papermc.paper.entity.TeleportFlag[] EMPTY_FLAGS = new io.papermc.paper.entity.TeleportFlag[0]; + } + return this.teleportAsync(loc, cause, Holder.EMPTY_FLAGS); + } + + /** + * 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 + * @param teleportFlags Flags to be used in this teleportation + * @return A future that will be completed with the result of the teleport + */ + java.util.concurrent.@NotNull CompletableFuture teleportAsync(@NotNull Location loc, @NotNull TeleportCause cause, @NotNull io.papermc.paper.entity.TeleportFlag @NotNull... teleportFlags); + // Paper end + /** * Returns a list of entities within a bounding box centered around this * entity