Code cleanup

This commit is contained in:
Felix Cravic 2020-06-02 14:43:31 +02:00
parent 50373505b3
commit 0da78193c5
11 changed files with 72 additions and 24 deletions

View File

@ -36,7 +36,6 @@ public class MinecraftServer {
public static final String THREAD_NAME_BENCHMARK = "Ms-Benchmark"; public static final String THREAD_NAME_BENCHMARK = "Ms-Benchmark";
public static final String THREAD_NAME_MAIN_UPDATE = "Ms-MainUpdate"; 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 String THREAD_NAME_PACKET_WRITER = "Ms-PacketWriterPool";
public static final int THREAD_COUNT_PACKET_WRITER = 2; public static final int THREAD_COUNT_PACKET_WRITER = 2;

View File

@ -12,14 +12,19 @@ import net.minestom.server.utils.thread.MinestomThread;
import java.util.concurrent.ExecutorService; 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_DELAY = 10_000;
private static final long KEEP_ALIVE_KICK = 30_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; private boolean stopRequested;
/**
* Should only be created in MinecraftServer
*/
protected UpdateManager() {
}
public void start() { public void start() {
mainUpdate.execute(() -> { mainUpdate.execute(() -> {

View File

@ -12,6 +12,9 @@ import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.CopyOnWriteArraySet;
/**
* Represent a bossbar which can be showed to any player {@link #addViewer(Player)}
*/
public class BossBar implements Viewable { public class BossBar implements Viewable {
private UUID uuid = UUID.randomUUID(); 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 * @return the current title of the bossbar
*/ */
public String getTitle() { public String getTitle() {
@ -58,6 +63,8 @@ public class BossBar implements Viewable {
} }
/** /**
* Change the bossbar title
*
* @param title the new title of the bossbar * @param title the new title of the bossbar
*/ */
public void setTitle(String title) { 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 * @return the current progress of the bossbar
*/ */
public float getProgress() { public float getProgress() {
@ -72,6 +81,8 @@ public class BossBar implements Viewable {
} }
/** /**
* Change the bossbar progress
*
* @param progress the new progress bar percentage * @param progress the new progress bar percentage
* @throws IllegalArgumentException if {@code progress} is not between 0 and 1 * @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 * @return the current bossbar color
*/ */
public BarColor getColor() { public BarColor getColor() {
@ -90,6 +103,8 @@ public class BossBar implements Viewable {
} }
/** /**
* Change the bossbar color
*
* @param color the new color of the bossbar * @param color the new color of the bossbar
*/ */
public void setColor(BarColor color) { public void setColor(BarColor color) {
@ -98,6 +113,8 @@ public class BossBar implements Viewable {
} }
/** /**
* Get the bossbar division
*
* @return the current bossbar division * @return the current bossbar division
*/ */
public BarDivision getDivision() { public BarDivision getDivision() {
@ -105,6 +122,8 @@ public class BossBar implements Viewable {
} }
/** /**
* Change the bossbar division
*
* @param division the new bossbar division count * @param division the new bossbar division count
*/ */
public void setDivision(BarDivision division) { public void setDivision(BarDivision division) {

View File

@ -12,7 +12,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
public class DataManager { public final class DataManager {
private Map<Class, DataType> dataTypeMap = new HashMap<>(); private Map<Class, DataType> dataTypeMap = new HashMap<>();

View File

@ -12,7 +12,7 @@ import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
public class EntityManager { public final class EntityManager {
private static InstanceManager instanceManager = MinecraftServer.getInstanceManager(); private static InstanceManager instanceManager = MinecraftServer.getInstanceManager();

View File

@ -31,7 +31,7 @@ import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.CopyOnWriteArraySet;
// TODO light data & API // TODO light data & API
public class Chunk implements Viewable { public final class Chunk implements Viewable {
private static final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager(); private static final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager();
@ -230,19 +230,19 @@ public class Chunk implements Viewable {
// Block all chunk operation during the update // Block all chunk operation during the update
IntIterator iterator = new IntOpenHashSet(updatableBlocks).iterator(); IntIterator iterator = new IntOpenHashSet(updatableBlocks).iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
int index = iterator.nextInt(); final int index = iterator.nextInt();
CustomBlock customBlock = getCustomBlock(index); final CustomBlock customBlock = getCustomBlock(index);
// Update cooldown // Update cooldown
UpdateOption updateOption = customBlock.getUpdateOption(); final UpdateOption updateOption = customBlock.getUpdateOption();
long lastUpdate = updatableBlocksLastUpdate.get(index); final long lastUpdate = updatableBlocksLastUpdate.get(index);
boolean hasCooldown = CooldownUtils.hasCooldown(time, lastUpdate, updateOption.getTimeUnit(), updateOption.getValue()); final boolean hasCooldown = CooldownUtils.hasCooldown(time, lastUpdate, updateOption);
if (hasCooldown) if (hasCooldown)
continue; continue;
this.updatableBlocksLastUpdate.put(index, time); // Refresh last update time 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 x = blockPos[0];
int y = blockPos[1]; int y = blockPos[1];
int z = blockPos[2]; int z = blockPos[2];

View File

@ -256,6 +256,8 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
/** /**
* Get the instance dimension
*
* @return the dimension of the instance * @return the dimension of the instance
*/ */
public Dimension getDimension() { 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 * @return the world border linked to the instance
*/ */
public WorldBorder getWorldBorder() { 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 * @return an unmodifiable list containing all the players in the instance
*/ */
public Set<Player> getPlayers() { public Set<Player> 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 * @return an unmodifiable list containing all the creatures in the instance
*/ */
public Set<EntityCreature> getCreatures() { public Set<EntityCreature> 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 * @return an unmodifiable list containing all the object entities in the instance
*/ */
public Set<ObjectEntity> getObjectEntities() { public Set<ObjectEntity> 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 * @return an unmodifiable list containing all the experience orbs in the instance
*/ */
public Set<ExperienceOrb> getExperienceOrbs() { public Set<ExperienceOrb> 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 * @param chunk the chunk to get the entities from
* @return an unmodifiable set containing all the entities in a chunk, * @return an unmodifiable set containing all the entities in a chunk,
* if {@code chunk} is null, return an empty {@link HashSet} * if {@code chunk} is null, return an empty {@link HashSet}
@ -412,6 +426,11 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
saveChunksToStorageFolder(null); saveChunksToStorageFolder(null);
} }
/**
* Get the instance unique id
*
* @return the instance unique id
*/
public UUID getUniqueId() { public UUID getUniqueId() {
return uniqueId; return uniqueId;
} }
@ -517,7 +536,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
/** /**
* Add the specified entity to the instance entities cache * Add the specified entity to the instance entities cache
* <p> * <p>
* 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 entity the entity to add
* @param chunk the chunk where the entity will be added * @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 * Remove the specified entity to the instance entities cache
* <p> * <p>
* 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 entity the entity to remove
* @param chunk the chunk where the entity will be removed * @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()}. * Creates an explosion at the given position with the given strength.
* If no {@link ExplosionSupplier} was supplied, this method will throw an {@link IllegalStateException} * The algorithm used to compute damages is provided by {@link #getExplosionSupplier()}.
* *
* @param centerX * @param centerX
* @param centerY * @param centerY
* @param centerZ * @param centerZ
* @param strength * @param strength
* @throws IllegalStateException If no {@link ExplosionSupplier} was supplied
*/ */
public void explode(float centerX, float centerY, float centerZ, float strength) { public void explode(float centerX, float centerY, float centerZ, float strength) {
explode(centerX, centerY, centerZ, strength, null); 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()}. * Creates an explosion at the given position with the given strength.
* If no {@link ExplosionSupplier} was supplied, this method will throw an {@link IllegalStateException} * The algorithm used to compute damages is provided by {@link #getExplosionSupplier()}.
* *
* @param centerX * @param centerX
* @param centerY * @param centerY
* @param centerZ * @param centerZ
* @param strength * @param strength
* @param additionalData data to pass to the explosion supplier * @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) { public void explode(float centerX, float centerY, float centerZ, float strength, Data additionalData) {
ExplosionSupplier explosionSupplier = getExplosionSupplier(); 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 the registered explosion supplier, or null if none was provided
* *
* @return * @return the instance explosion supplier, null if none was provided
*/ */
public ExplosionSupplier getExplosionSupplier() { public ExplosionSupplier getExplosionSupplier() {
return explosionSupplier; return explosionSupplier;
@ -646,7 +667,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
/** /**
* Registers the explosion supplier to use in this instance * Registers the explosion supplier to use in this instance
* *
* @param supplier * @param supplier the explosion supplier
*/ */
public void setExplosionSupplier(ExplosionSupplier supplier) { public void setExplosionSupplier(ExplosionSupplier supplier) {
this.explosionSupplier = supplier; this.explosionSupplier = supplier;

View File

@ -224,6 +224,10 @@ public class InstanceContainer extends Instance {
Chunk chunk = getChunkAt(blockPosition); Chunk chunk = getChunkAt(blockPosition);
// Chunk unloaded, stop here
if (ChunkUtils.isChunkUnloaded(chunk))
return false;
int x = blockPosition.getX(); int x = blockPosition.getX();
int y = blockPosition.getY(); int y = blockPosition.getY();
int z = blockPosition.getZ(); int z = blockPosition.getZ();
@ -254,7 +258,7 @@ public class InstanceContainer extends Instance {
chunk.getViewers().forEach(p -> { chunk.getViewers().forEach(p -> {
// The player who breaks the block already get particles client-side // 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); p.getPlayerConnection().sendPacket(particlePacket);
} }
}); });

View File

@ -12,7 +12,7 @@ import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ExecutorService; 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); private ExecutorService blocksPool = new MinestomThread(MinecraftServer.THREAD_COUNT_BLOCK_UPDATE, MinecraftServer.THREAD_NAME_BLOCK_UPDATE);

View File

@ -11,7 +11,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
public class ConnectionManager { public final class ConnectionManager {
private Set<Player> players = new CopyOnWriteArraySet<>(); private Set<Player> players = new CopyOnWriteArraySet<>();
private Map<PlayerConnection, Player> connectionPlayerMap = Collections.synchronizedMap(new HashMap<>()); private Map<PlayerConnection, Player> connectionPlayerMap = Collections.synchronizedMap(new HashMap<>());

View File

@ -3,7 +3,7 @@ package net.minestom.server.scoreboard;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.CopyOnWriteArraySet;
public class TeamManager { public final class TeamManager {
// Represents all registered teams // Represents all registered teams
private Set<Team> teams = new CopyOnWriteArraySet<>(); private Set<Team> teams = new CopyOnWriteArraySet<>();