diff --git a/src/main/java/net/minestom/server/MinecraftServer.java b/src/main/java/net/minestom/server/MinecraftServer.java index 61db0d05e..17e4df539 100644 --- a/src/main/java/net/minestom/server/MinecraftServer.java +++ b/src/main/java/net/minestom/server/MinecraftServer.java @@ -36,7 +36,6 @@ public class MinecraftServer { public static final String THREAD_NAME_BENCHMARK = "Ms-Benchmark"; public static final String THREAD_NAME_MAIN_UPDATE = "Ms-MainUpdate"; - public static final int THREAD_COUNT_MAIN_UPDATE = 1; // Keep it to 1 public static final String THREAD_NAME_PACKET_WRITER = "Ms-PacketWriterPool"; public static final int THREAD_COUNT_PACKET_WRITER = 2; diff --git a/src/main/java/net/minestom/server/UpdateManager.java b/src/main/java/net/minestom/server/UpdateManager.java index 7b7bf7662..ffc7ced98 100644 --- a/src/main/java/net/minestom/server/UpdateManager.java +++ b/src/main/java/net/minestom/server/UpdateManager.java @@ -12,14 +12,19 @@ import net.minestom.server.utils.thread.MinestomThread; import java.util.concurrent.ExecutorService; -public class UpdateManager { +public final class UpdateManager { private static final long KEEP_ALIVE_DELAY = 10_000; private static final long KEEP_ALIVE_KICK = 30_000; - private ExecutorService mainUpdate = new MinestomThread(MinecraftServer.THREAD_COUNT_MAIN_UPDATE, MinecraftServer.THREAD_NAME_MAIN_UPDATE); + private ExecutorService mainUpdate = new MinestomThread(1, MinecraftServer.THREAD_NAME_MAIN_UPDATE); private boolean stopRequested; + /** + * Should only be created in MinecraftServer + */ + protected UpdateManager() { + } public void start() { mainUpdate.execute(() -> { diff --git a/src/main/java/net/minestom/server/bossbar/BossBar.java b/src/main/java/net/minestom/server/bossbar/BossBar.java index 57688bc15..1cc022e24 100644 --- a/src/main/java/net/minestom/server/bossbar/BossBar.java +++ b/src/main/java/net/minestom/server/bossbar/BossBar.java @@ -12,6 +12,9 @@ import java.util.Set; import java.util.UUID; import java.util.concurrent.CopyOnWriteArraySet; +/** + * Represent a bossbar which can be showed to any player {@link #addViewer(Player)} + */ public class BossBar implements Viewable { private UUID uuid = UUID.randomUUID(); @@ -51,6 +54,8 @@ public class BossBar implements Viewable { } /** + * Get the bossbar title + * * @return the current title of the bossbar */ public String getTitle() { @@ -58,6 +63,8 @@ public class BossBar implements Viewable { } /** + * Change the bossbar title + * * @param title the new title of the bossbar */ public void setTitle(String title) { @@ -65,6 +72,8 @@ public class BossBar implements Viewable { } /** + * Get the bossbar progress + * * @return the current progress of the bossbar */ public float getProgress() { @@ -72,6 +81,8 @@ public class BossBar implements Viewable { } /** + * Change the bossbar progress + * * @param progress the new progress bar percentage * @throws IllegalArgumentException if {@code progress} is not between 0 and 1 */ @@ -83,6 +94,8 @@ public class BossBar implements Viewable { } /** + * Get the bossbar color + * * @return the current bossbar color */ public BarColor getColor() { @@ -90,6 +103,8 @@ public class BossBar implements Viewable { } /** + * Change the bossbar color + * * @param color the new color of the bossbar */ public void setColor(BarColor color) { @@ -98,6 +113,8 @@ public class BossBar implements Viewable { } /** + * Get the bossbar division + * * @return the current bossbar division */ public BarDivision getDivision() { @@ -105,6 +122,8 @@ public class BossBar implements Viewable { } /** + * Change the bossbar division + * * @param division the new bossbar division count */ public void setDivision(BarDivision division) { diff --git a/src/main/java/net/minestom/server/data/DataManager.java b/src/main/java/net/minestom/server/data/DataManager.java index c0c5c8c32..44183570c 100644 --- a/src/main/java/net/minestom/server/data/DataManager.java +++ b/src/main/java/net/minestom/server/data/DataManager.java @@ -12,7 +12,7 @@ import java.util.HashMap; import java.util.Map; import java.util.UUID; -public class DataManager { +public final class DataManager { private Map dataTypeMap = new HashMap<>(); diff --git a/src/main/java/net/minestom/server/entity/EntityManager.java b/src/main/java/net/minestom/server/entity/EntityManager.java index 0f4e24f31..82d813e5b 100644 --- a/src/main/java/net/minestom/server/entity/EntityManager.java +++ b/src/main/java/net/minestom/server/entity/EntityManager.java @@ -12,7 +12,7 @@ import java.util.Set; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ExecutorService; -public class EntityManager { +public final class EntityManager { private static InstanceManager instanceManager = MinecraftServer.getInstanceManager(); diff --git a/src/main/java/net/minestom/server/instance/Chunk.java b/src/main/java/net/minestom/server/instance/Chunk.java index a6b79aa48..4782a265e 100644 --- a/src/main/java/net/minestom/server/instance/Chunk.java +++ b/src/main/java/net/minestom/server/instance/Chunk.java @@ -31,7 +31,7 @@ import java.util.Set; import java.util.concurrent.CopyOnWriteArraySet; // TODO light data & API -public class Chunk implements Viewable { +public final class Chunk implements Viewable { private static final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager(); @@ -230,19 +230,19 @@ public class Chunk implements Viewable { // Block all chunk operation during the update IntIterator iterator = new IntOpenHashSet(updatableBlocks).iterator(); while (iterator.hasNext()) { - int index = iterator.nextInt(); - CustomBlock customBlock = getCustomBlock(index); + final int index = iterator.nextInt(); + final CustomBlock customBlock = getCustomBlock(index); // Update cooldown - UpdateOption updateOption = customBlock.getUpdateOption(); - long lastUpdate = updatableBlocksLastUpdate.get(index); - boolean hasCooldown = CooldownUtils.hasCooldown(time, lastUpdate, updateOption.getTimeUnit(), updateOption.getValue()); + final UpdateOption updateOption = customBlock.getUpdateOption(); + final long lastUpdate = updatableBlocksLastUpdate.get(index); + final boolean hasCooldown = CooldownUtils.hasCooldown(time, lastUpdate, updateOption); if (hasCooldown) continue; this.updatableBlocksLastUpdate.put(index, time); // Refresh last update time - int[] blockPos = ChunkUtils.indexToPosition(index, chunkX, chunkZ); + final int[] blockPos = ChunkUtils.indexToPosition(index, chunkX, chunkZ); int x = blockPos[0]; int y = blockPos[1]; int z = blockPos[2]; diff --git a/src/main/java/net/minestom/server/instance/Instance.java b/src/main/java/net/minestom/server/instance/Instance.java index cc2ccdcea..feac75179 100644 --- a/src/main/java/net/minestom/server/instance/Instance.java +++ b/src/main/java/net/minestom/server/instance/Instance.java @@ -256,6 +256,8 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta /** + * Get the instance dimension + * * @return the dimension of the instance */ public Dimension getDimension() { @@ -263,6 +265,8 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta } /** + * Get the instance world border + * * @return the world border linked to the instance */ public WorldBorder getWorldBorder() { @@ -270,6 +274,8 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta } /** + * Get the players in the instance + * * @return an unmodifiable list containing all the players in the instance */ public Set getPlayers() { @@ -277,6 +283,8 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta } /** + * Get the creatures in the instance + * * @return an unmodifiable list containing all the creatures in the instance */ public Set getCreatures() { @@ -284,6 +292,8 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta } /** + * Get the object entities in the instance + * * @return an unmodifiable list containing all the object entities in the instance */ public Set getObjectEntities() { @@ -291,6 +301,8 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta } /** + * Get the experience orbs in the instance + * * @return an unmodifiable list containing all the experience orbs in the instance */ public Set getExperienceOrbs() { @@ -298,6 +310,8 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta } /** + * Get the entities located in the chunk + * * @param chunk the chunk to get the entities from * @return an unmodifiable set containing all the entities in a chunk, * if {@code chunk} is null, return an empty {@link HashSet} @@ -412,6 +426,11 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta saveChunksToStorageFolder(null); } + /** + * Get the instance unique id + * + * @return the instance unique id + */ public UUID getUniqueId() { return uniqueId; } @@ -517,7 +536,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta /** * Add the specified entity to the instance entities cache *

- * Warning: this is done automatically the entity move out of his chunk + * Warning: this is done automatically when the entity move out of his chunk * * @param entity the entity to add * @param chunk the chunk where the entity will be added @@ -547,7 +566,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta /** * Remove the specified entity to the instance entities cache *

- * Warning: this is done automatically the entity move out of his chunk + * Warning: this is done automatically when the entity move out of his chunk * * @param entity the entity to remove * @param chunk the chunk where the entity will be removed @@ -604,27 +623,29 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta } /** - * Creates an explosion at the given position with the given strength. The algorithm used to compute damages is provided by {@link #getExplosionSupplier()}. - * If no {@link ExplosionSupplier} was supplied, this method will throw an {@link IllegalStateException} + * Creates an explosion at the given position with the given strength. + * The algorithm used to compute damages is provided by {@link #getExplosionSupplier()}. * * @param centerX * @param centerY * @param centerZ * @param strength + * @throws IllegalStateException If no {@link ExplosionSupplier} was supplied */ public void explode(float centerX, float centerY, float centerZ, float strength) { explode(centerX, centerY, centerZ, strength, null); } /** - * Creates an explosion at the given position with the given strength. The algorithm used to compute damages is provided by {@link #getExplosionSupplier()}. - * If no {@link ExplosionSupplier} was supplied, this method will throw an {@link IllegalStateException} + * Creates an explosion at the given position with the given strength. + * The algorithm used to compute damages is provided by {@link #getExplosionSupplier()}. * * @param centerX * @param centerY * @param centerZ * @param strength * @param additionalData data to pass to the explosion supplier + * @throws IllegalStateException If no {@link ExplosionSupplier} was supplied */ public void explode(float centerX, float centerY, float centerZ, float strength, Data additionalData) { ExplosionSupplier explosionSupplier = getExplosionSupplier(); @@ -637,7 +658,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta /** * Return the registered explosion supplier, or null if none was provided * - * @return + * @return the instance explosion supplier, null if none was provided */ public ExplosionSupplier getExplosionSupplier() { return explosionSupplier; @@ -646,7 +667,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta /** * Registers the explosion supplier to use in this instance * - * @param supplier + * @param supplier the explosion supplier */ public void setExplosionSupplier(ExplosionSupplier supplier) { this.explosionSupplier = supplier; diff --git a/src/main/java/net/minestom/server/instance/InstanceContainer.java b/src/main/java/net/minestom/server/instance/InstanceContainer.java index 90c2fc120..9d6c9ef22 100644 --- a/src/main/java/net/minestom/server/instance/InstanceContainer.java +++ b/src/main/java/net/minestom/server/instance/InstanceContainer.java @@ -224,6 +224,10 @@ public class InstanceContainer extends Instance { Chunk chunk = getChunkAt(blockPosition); + // Chunk unloaded, stop here + if (ChunkUtils.isChunkUnloaded(chunk)) + return false; + int x = blockPosition.getX(); int y = blockPosition.getY(); int z = blockPosition.getZ(); @@ -254,7 +258,7 @@ public class InstanceContainer extends Instance { chunk.getViewers().forEach(p -> { // The player who breaks the block already get particles client-side - if (customBlock == null || !(p.equals(player) && player.isCreative())) { + if (customBlock != null || !(p.equals(player) && !player.isCreative())) { p.getPlayerConnection().sendPacket(particlePacket); } }); diff --git a/src/main/java/net/minestom/server/instance/InstanceManager.java b/src/main/java/net/minestom/server/instance/InstanceManager.java index 90616d55d..debbe063b 100644 --- a/src/main/java/net/minestom/server/instance/InstanceManager.java +++ b/src/main/java/net/minestom/server/instance/InstanceManager.java @@ -12,7 +12,7 @@ import java.util.Set; import java.util.UUID; import java.util.concurrent.ExecutorService; -public class InstanceManager { +public final class InstanceManager { private ExecutorService blocksPool = new MinestomThread(MinecraftServer.THREAD_COUNT_BLOCK_UPDATE, MinecraftServer.THREAD_NAME_BLOCK_UPDATE); diff --git a/src/main/java/net/minestom/server/network/ConnectionManager.java b/src/main/java/net/minestom/server/network/ConnectionManager.java index d95ce2f31..cd864c555 100644 --- a/src/main/java/net/minestom/server/network/ConnectionManager.java +++ b/src/main/java/net/minestom/server/network/ConnectionManager.java @@ -11,7 +11,7 @@ import java.util.concurrent.CopyOnWriteArraySet; import java.util.function.Consumer; import java.util.function.Function; -public class ConnectionManager { +public final class ConnectionManager { private Set players = new CopyOnWriteArraySet<>(); private Map connectionPlayerMap = Collections.synchronizedMap(new HashMap<>()); diff --git a/src/main/java/net/minestom/server/scoreboard/TeamManager.java b/src/main/java/net/minestom/server/scoreboard/TeamManager.java index 42feaaf38..63aac80cc 100644 --- a/src/main/java/net/minestom/server/scoreboard/TeamManager.java +++ b/src/main/java/net/minestom/server/scoreboard/TeamManager.java @@ -3,7 +3,7 @@ package net.minestom.server.scoreboard; import java.util.Set; import java.util.concurrent.CopyOnWriteArraySet; -public class TeamManager { +public final class TeamManager { // Represents all registered teams private Set teams = new CopyOnWriteArraySet<>();