Add constant for null future

This commit is contained in:
TheMode 2021-07-11 03:35:17 +02:00
parent 7cf5821341
commit 6cdf8a9ab9
5 changed files with 15 additions and 11 deletions

View File

@ -40,6 +40,7 @@ import net.minestom.server.potion.TimedPotion;
import net.minestom.server.tag.Tag; import net.minestom.server.tag.Tag;
import net.minestom.server.tag.TagHandler; import net.minestom.server.tag.TagHandler;
import net.minestom.server.thread.ThreadProvider; import net.minestom.server.thread.ThreadProvider;
import net.minestom.server.utils.async.AsyncUtils;
import net.minestom.server.utils.chunk.ChunkUtils; import net.minestom.server.utils.chunk.ChunkUtils;
import net.minestom.server.utils.entity.EntityUtils; import net.minestom.server.utils.entity.EntityUtils;
import net.minestom.server.utils.player.PlayerUtils; import net.minestom.server.utils.player.PlayerUtils;
@ -844,8 +845,8 @@ public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, Da
* *
* @param instance the new instance of the entity * @param instance the new instance of the entity
* @param spawnPosition the spawn position for the entity. * @param spawnPosition the spawn position for the entity.
* @throws IllegalStateException if {@code instance} has not been registered in {@link InstanceManager}
* @return * @return
* @throws IllegalStateException if {@code instance} has not been registered in {@link InstanceManager}
*/ */
public CompletableFuture<Void> setInstance(@NotNull Instance instance, @NotNull Pos spawnPosition) { public CompletableFuture<Void> setInstance(@NotNull Instance instance, @NotNull Pos spawnPosition) {
Check.stateCondition(!instance.isRegistered(), Check.stateCondition(!instance.isRegistered(),
@ -860,7 +861,7 @@ public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, Da
instance.UNSAFE_addEntity(this); instance.UNSAFE_addEntity(this);
spawn(); spawn();
EventDispatcher.call(new EntitySpawnEvent(this, instance)); EventDispatcher.call(new EntitySpawnEvent(this, instance));
return CompletableFuture.completedFuture(null); return AsyncUtils.NULL_FUTURE;
} }
public CompletableFuture<Void> setInstance(@NotNull Instance instance, @NotNull Point spawnPosition) { public CompletableFuture<Void> setInstance(@NotNull Instance instance, @NotNull Point spawnPosition) {

View File

@ -69,7 +69,7 @@ import net.minestom.server.sound.SoundCategory;
import net.minestom.server.sound.SoundEvent; import net.minestom.server.sound.SoundEvent;
import net.minestom.server.stat.PlayerStatistic; import net.minestom.server.stat.PlayerStatistic;
import net.minestom.server.utils.*; import net.minestom.server.utils.*;
import net.minestom.server.utils.chunk.ChunkCallback; import net.minestom.server.utils.async.AsyncUtils;
import net.minestom.server.utils.chunk.ChunkUtils; import net.minestom.server.utils.chunk.ChunkUtils;
import net.minestom.server.utils.entity.EntityUtils; import net.minestom.server.utils.entity.EntityUtils;
import net.minestom.server.utils.identity.NamedAndIdentified; import net.minestom.server.utils.identity.NamedAndIdentified;
@ -519,7 +519,8 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
* <p> * <p>
* Be aware that because chunk operations are expensive, * Be aware that because chunk operations are expensive,
* it is possible for this method to be non-blocking when retrieving chunks is required. * it is possible for this method to be non-blocking when retrieving chunks is required.
* @param instance the new player instance *
* @param instance the new player instance
* @param spawnPosition the new position of the player * @param spawnPosition the new position of the player
* @return * @return
*/ */
@ -550,7 +551,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
// The player already has the good version of all the chunks. // The player already has the good version of all the chunks.
// We just need to refresh his entity viewing list and add him to the instance // We just need to refresh his entity viewing list and add him to the instance
spawnPlayer(instance, spawnPosition, false, false, false); spawnPlayer(instance, spawnPosition, false, false, false);
return CompletableFuture.completedFuture(null); return AsyncUtils.NULL_FUTURE;
} }
} }

View File

@ -5,6 +5,7 @@ import net.minestom.server.exception.ExceptionManager;
import net.minestom.server.instance.block.Block; import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockHandler; import net.minestom.server.instance.block.BlockHandler;
import net.minestom.server.instance.block.BlockManager; import net.minestom.server.instance.block.BlockManager;
import net.minestom.server.utils.async.AsyncUtils;
import net.minestom.server.world.biomes.Biome; import net.minestom.server.world.biomes.Biome;
import net.minestom.server.world.biomes.BiomeManager; import net.minestom.server.world.biomes.BiomeManager;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -196,7 +197,7 @@ public class AnvilLoader implements IChunkLoader {
} catch (AnvilException | IOException e) { } catch (AnvilException | IOException e) {
LOGGER.error("Failed to save chunk " + chunkX + ", " + chunkZ, e); LOGGER.error("Failed to save chunk " + chunkX + ", " + chunkZ, e);
EXCEPTION_MANAGER.handleException(e); EXCEPTION_MANAGER.handleException(e);
return CompletableFuture.completedFuture(null); return AsyncUtils.NULL_FUTURE;
} }
} }
} }
@ -206,7 +207,7 @@ public class AnvilLoader implements IChunkLoader {
} catch (AnvilException | IOException e) { } catch (AnvilException | IOException e) {
LOGGER.error("Failed to save chunk " + chunkX + ", " + chunkZ, e); LOGGER.error("Failed to save chunk " + chunkX + ", " + chunkZ, e);
EXCEPTION_MANAGER.handleException(e); EXCEPTION_MANAGER.handleException(e);
return CompletableFuture.completedFuture(null); return AsyncUtils.NULL_FUTURE;
} }
save(chunk, column); save(chunk, column);
try { try {
@ -215,9 +216,9 @@ public class AnvilLoader implements IChunkLoader {
} catch (IOException e) { } catch (IOException e) {
LOGGER.error("Failed to save chunk " + chunkX + ", " + chunkZ, e); LOGGER.error("Failed to save chunk " + chunkX + ", " + chunkZ, e);
EXCEPTION_MANAGER.handleException(e); EXCEPTION_MANAGER.handleException(e);
return CompletableFuture.completedFuture(null); return AsyncUtils.NULL_FUTURE;
} }
return CompletableFuture.completedFuture(null); return AsyncUtils.NULL_FUTURE;
} }
private void save(Chunk chunk, ChunkColumn chunkColumn) { private void save(Chunk chunk, ChunkColumn chunkColumn) {

View File

@ -1,6 +1,7 @@
package net.minestom.server.instance; package net.minestom.server.instance;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import net.minestom.server.utils.async.AsyncUtils;
import net.minestom.server.utils.thread.MinestomThread; import net.minestom.server.utils.thread.MinestomThread;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -55,7 +56,7 @@ public interface IChunkLoader {
} catch (InterruptedException e) { } catch (InterruptedException e) {
MinecraftServer.getExceptionManager().handleException(e); MinecraftServer.getExceptionManager().handleException(e);
} }
return CompletableFuture.completedFuture(null); return AsyncUtils.NULL_FUTURE;
} else { } else {
CompletableFuture<Void> completableFuture = new CompletableFuture<>(); CompletableFuture<Void> completableFuture = new CompletableFuture<>();
AtomicInteger counter = new AtomicInteger(); AtomicInteger counter = new AtomicInteger();

View File

@ -6,6 +6,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
public final class AsyncUtils { public final class AsyncUtils {
public static final CompletableFuture<Void> NULL_FUTURE = CompletableFuture.completedFuture(null);
public static @NotNull CompletableFuture<Void> runAsync(@NotNull Runnable runnable) { public static @NotNull CompletableFuture<Void> runAsync(@NotNull Runnable runnable) {
return CompletableFuture.runAsync(() -> { return CompletableFuture.runAsync(() -> {
@ -16,5 +17,4 @@ public final class AsyncUtils {
} }
}); });
} }
} }