From b6ab689b1071e3eb2bacec93e1d94bea2ad3f12b Mon Sep 17 00:00:00 2001 From: TheMode Date: Sun, 15 Aug 2021 17:50:38 +0200 Subject: [PATCH] Style cleanup --- .../minestom/server/instance/Instance.java | 38 ++++++------------- .../server/instance/InstanceContainer.java | 8 ++-- 2 files changed, 17 insertions(+), 29 deletions(-) diff --git a/src/main/java/net/minestom/server/instance/Instance.java b/src/main/java/net/minestom/server/instance/Instance.java index 128b47c66..c4443de78 100644 --- a/src/main/java/net/minestom/server/instance/Instance.java +++ b/src/main/java/net/minestom/server/instance/Instance.java @@ -447,8 +447,7 @@ public abstract class Instance implements BlockGetter, BlockSetter, Tickable, Ta * @return an unmodifiable {@link Set} containing all the players in the instance */ @Override - @NotNull - public Set getPlayers() { + public @NotNull Set<@NotNull Player> getPlayers() { return Collections.unmodifiableSet(players); } @@ -457,8 +456,7 @@ public abstract class Instance implements BlockGetter, BlockSetter, Tickable, Ta * * @return an unmodifiable {@link Set} containing all the creatures in the instance */ - @NotNull - public Set getCreatures() { + public @NotNull Set<@NotNull EntityCreature> getCreatures() { return Collections.unmodifiableSet(creatures); } @@ -467,8 +465,7 @@ public abstract class Instance implements BlockGetter, BlockSetter, Tickable, Ta * * @return an unmodifiable {@link Set} containing all the experience orbs in the instance */ - @NotNull - public Set getExperienceOrbs() { + public @NotNull Set<@NotNull ExperienceOrb> getExperienceOrbs() { return Collections.unmodifiableSet(experienceOrbs); } @@ -479,7 +476,7 @@ public abstract class Instance implements BlockGetter, BlockSetter, Tickable, Ta * @return an unmodifiable {@link Set} containing all the entities in a chunk, * if {@code chunk} is unloaded, return an empty {@link HashSet} */ - public @NotNull Set getChunkEntities(Chunk chunk) { + public @NotNull Set<@NotNull Entity> getChunkEntities(Chunk chunk) { if (!ChunkUtils.isLoaded(chunk)) return Collections.emptySet(); final Set entities; @@ -587,7 +584,7 @@ public abstract class Instance implements BlockGetter, BlockSetter, Tickable, Ta // Load the chunk if not already (or throw an error if auto chunk load is disabled) loadOptionalChunk(entityPosition).thenAccept(chunk -> { - Check.notNull(chunk, "You tried to spawn an entity in an unloaded chunk, " + entityPosition); + Check.notNull(chunk, "You tried to spawn an entity in an unloaded chunk, {0}", entityPosition); UNSAFE_addEntityToChunk(entity, chunk); }); }); @@ -602,10 +599,7 @@ public abstract class Instance implements BlockGetter, BlockSetter, Tickable, Ta */ @ApiStatus.Internal public void UNSAFE_removeEntity(@NotNull Entity entity) { - final Instance entityInstance = entity.getInstance(); - if (entityInstance != this) - return; - + if (entity.getInstance() != this) return; RemoveEntityFromInstanceEvent event = new RemoveEntityFromInstanceEvent(this, entity); EventDispatcher.callCancellable(event, () -> { // Remove this entity from players viewable list and send delete entities packet @@ -697,31 +691,24 @@ public abstract class Instance implements BlockGetter, BlockSetter, Tickable, Ta callback.accept(this); } } - // Time { this.worldAge++; - this.time += timeRate; - - // time needs to be send to players + // time needs to be sent to players if (timeUpdate != null && !Cooldown.hasCooldown(time, lastTimeUpdate, timeUpdate)) { PacketUtils.sendGroupedPacket(getPlayers(), createTimePacket()); this.lastTimeUpdate = time; } } - // Tick event { // Process tick events - InstanceTickEvent chunkTickEvent = new InstanceTickEvent(this, time, lastTickAge); - EventDispatcher.call(chunkTickEvent); - + EventDispatcher.call(new InstanceTickEvent(this, time, lastTickAge)); // Set last tick age - lastTickAge = time; + this.lastTickAge = time; } - this.worldBorder.update(); } @@ -776,8 +763,7 @@ public abstract class Instance implements BlockGetter, BlockSetter, Tickable, Ta * * @return the instance explosion supplier, null if none was provided */ - @Nullable - public ExplosionSupplier getExplosionSupplier() { + public @Nullable ExplosionSupplier getExplosionSupplier() { return explosionSupplier; } @@ -797,8 +783,8 @@ public abstract class Instance implements BlockGetter, BlockSetter, Tickable, Ta * * @return the instance space */ - @NotNull - public PFInstanceSpace getInstanceSpace() { + @ApiStatus.Internal + public @NotNull PFInstanceSpace getInstanceSpace() { return instanceSpace; } diff --git a/src/main/java/net/minestom/server/instance/InstanceContainer.java b/src/main/java/net/minestom/server/instance/InstanceContainer.java index 23d4ec1c8..ae3089bd2 100644 --- a/src/main/java/net/minestom/server/instance/InstanceContainer.java +++ b/src/main/java/net/minestom/server/instance/InstanceContainer.java @@ -82,7 +82,7 @@ public class InstanceContainer extends Instance { } @Override - public synchronized void setBlock(int x, int y, int z, @NotNull Block block) { + public void setBlock(int x, int y, int z, @NotNull Block block) { final Chunk chunk = getChunkAt(x, z); if (ChunkUtils.isLoaded(chunk)) { UNSAFE_setBlock(chunk, x, y, z, block, null, null); @@ -106,8 +106,8 @@ public class InstanceContainer extends Instance { * @param z the block Z * @param block the block to place */ - private void UNSAFE_setBlock(@NotNull Chunk chunk, int x, int y, int z, @NotNull Block block, - @Nullable BlockHandler.Placement placement, @Nullable BlockHandler.Destroy destroy) { + private synchronized void UNSAFE_setBlock(@NotNull Chunk chunk, int x, int y, int z, @NotNull Block block, + @Nullable BlockHandler.Placement placement, @Nullable BlockHandler.Destroy destroy) { if (chunk.isReadOnly()) return; synchronized (chunk) { // Refresh the last block change time @@ -261,7 +261,9 @@ public class InstanceContainer extends Instance { CompletableFuture completableFuture = new CompletableFuture<>(); final IChunkLoader loader = chunkLoader; final Runnable retriever = () -> loader.loadChunk(this, chunkX, chunkZ) + // create the chunk from scratch (with the generator) if the loader couldn't .thenCompose(chunk -> chunk != null ? CompletableFuture.completedFuture(chunk) : createChunk(chunkX, chunkZ)) + // cache the retrieved chunk (in the next instance tick for thread-safety) .whenComplete((chunk, throwable) -> scheduleNextTick(instance -> { cacheChunk(chunk); EventDispatcher.call(new InstanceChunkLoadEvent(this, chunkX, chunkZ));