diff --git a/src/main/java/net/minestom/server/ServerFlag.java b/src/main/java/net/minestom/server/ServerFlag.java index e685ddf9c..9f7a164cc 100644 --- a/src/main/java/net/minestom/server/ServerFlag.java +++ b/src/main/java/net/minestom/server/ServerFlag.java @@ -1,6 +1,6 @@ package net.minestom.server; -import net.minestom.server.utils.PropertyUtils; +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -12,47 +12,96 @@ import org.jetbrains.annotations.Nullable; public final class ServerFlag { // Server Behavior - public static final int SERVER_TICKS_PER_SECOND = Integer.getInteger("minestom.tps", 20); - public static final int SERVER_MAX_TICK_CATCH_UP = Integer.getInteger("minestom.max-tick-catch-up", 5); - public static final int CHUNK_VIEW_DISTANCE = Integer.getInteger("minestom.chunk-view-distance", 8); - public static final int ENTITY_VIEW_DISTANCE = Integer.getInteger("minestom.entity-view-distance", 5); - public static final int ENTITY_SYNCHRONIZATION_TICKS = Integer.getInteger("minestom.entity-synchronization-ticks", 20); - public static final int WORKER_COUNT = Integer.getInteger("minestom.workers", Runtime.getRuntime().availableProcessors()); - public static final int MAX_PACKET_SIZE = Integer.getInteger("minestom.max-packet-size", 2_097_151); // 3 bytes var-int - public static final int SOCKET_SEND_BUFFER_SIZE = Integer.getInteger("minestom.send-buffer-size", 262_143); - public static final int SOCKET_RECEIVE_BUFFER_SIZE = Integer.getInteger("minestom.receive-buffer-size", 32_767); - public static final int POOLED_BUFFER_SIZE = Integer.getInteger("minestom.pooled-buffer-size", 262_143); - public static final int PLAYER_PACKET_PER_TICK = Integer.getInteger("minestom.packet-per-tick", 20); - public static final int PLAYER_PACKET_QUEUE_SIZE = Integer.getInteger("minestom.packet-queue-size", 1000); - public static final int SEND_LIGHT_AFTER_BLOCK_PLACEMENT_DELAY = Integer.getInteger("minestom.send-light-after-block-placement-delay", 100); - public static final long KEEP_ALIVE_DELAY = Long.getLong("minestom.keep-alive-delay", 10_000); - public static final long KEEP_ALIVE_KICK = Long.getLong("minestom.keep-alive-kick", 30_000); - public static final long LOGIN_PLUGIN_MESSAGE_TIMEOUT = Long.getLong("minestom.login-plugin-message-timeout", 5_000); + public static final Boolean SHUTDOWN_ON_SIGNAL = booleanProperty("minestom.shutdown-on-signal", true); + public static final int SERVER_TICKS_PER_SECOND = intProperty("minestom.tps", 20); + public static final int SERVER_MAX_TICK_CATCH_UP = intProperty("minestom.max-tick-catch-up", 5); + public static final int CHUNK_VIEW_DISTANCE = intProperty("minestom.chunk-view-distance", 8); + public static final int ENTITY_VIEW_DISTANCE = intProperty("minestom.entity-view-distance", 5); + public static final int ENTITY_SYNCHRONIZATION_TICKS = intProperty("minestom.entity-synchronization-ticks", 20); + public static final int WORKER_COUNT = intProperty("minestom.workers", Runtime.getRuntime().availableProcessors()); + public static final int MAX_PACKET_SIZE = intProperty("minestom.max-packet-size", 2_097_151); // 3 bytes var-int + public static final int SOCKET_SEND_BUFFER_SIZE = intProperty("minestom.send-buffer-size", 262_143); + public static final int SOCKET_RECEIVE_BUFFER_SIZE = intProperty("minestom.receive-buffer-size", 32_767); + public static final int POOLED_BUFFER_SIZE = intProperty("minestom.pooled-buffer-size", 262_143); + public static final int PLAYER_PACKET_PER_TICK = intProperty("minestom.packet-per-tick", 20); + public static final int PLAYER_PACKET_QUEUE_SIZE = intProperty("minestom.packet-queue-size", 1000); + public static final int SEND_LIGHT_AFTER_BLOCK_PLACEMENT_DELAY = intProperty("minestom.send-light-after-block-placement-delay", 100); + public static final long KEEP_ALIVE_DELAY = longProperty("minestom.keep-alive-delay", 10_000); + public static final long KEEP_ALIVE_KICK = longProperty("minestom.keep-alive-kick", 30_000); + public static final long LOGIN_PLUGIN_MESSAGE_TIMEOUT = longProperty("minestom.login-plugin-message-timeout", 5_000); + + // Chunk update + public static final float MIN_CHUNKS_PER_TICK = floatProperty("minestom.chunk-queue.min-per-tick", 0.01f); + public static final float MAX_CHUNKS_PER_TICK = floatProperty("minestom.chunk-queue.max-per-tick", 64.0f); + public static final float CHUNKS_PER_TICK_MULTIPLIER = floatProperty("minestom.chunk-queue.multiplier", 1f); // Packet sending optimizations - public static final boolean GROUPED_PACKET = PropertyUtils.getBoolean("minestom.grouped-packet", true); - public static final boolean CACHED_PACKET = PropertyUtils.getBoolean("minestom.cached-packet", true); - public static final boolean VIEWABLE_PACKET = PropertyUtils.getBoolean("minestom.viewable-packet", true); + public static final boolean GROUPED_PACKET = booleanProperty("minestom.grouped-packet", true); + public static final boolean CACHED_PACKET = booleanProperty("minestom.cached-packet", true); + public static final boolean VIEWABLE_PACKET = booleanProperty("minestom.viewable-packet", true); // Tags - public static final boolean TAG_HANDLER_CACHE_ENABLED = PropertyUtils.getBoolean("minestom.tag-handler-cache", true); - public static final boolean SERIALIZE_EMPTY_COMPOUND = PropertyUtils.getBoolean("minestom.serialization.serialize-empty-nbt-compound", false); + public static final boolean TAG_HANDLER_CACHE_ENABLED = booleanProperty("minestom.tag-handler-cache", true); + public static final boolean SERIALIZE_EMPTY_COMPOUND = booleanProperty("minestom.serialization.serialize-empty-nbt-compound", false); // Online Mode - public static final @NotNull String AUTH_URL = System.getProperty("minestom.auth.url", "https://sessionserver.mojang.com/session/minecraft/hasJoined"); + public static final @NotNull String AUTH_URL = stringProperty("minestom.auth.url", "https://sessionserver.mojang.com/session/minecraft/hasJoined"); // World - public static final int WORLD_BORDER_SIZE = Integer.getInteger("minestom.world-border-size", 29999984); + public static final int WORLD_BORDER_SIZE = intProperty("minestom.world-border-size", 29999984); // Maps - public static final @NotNull String MAP_RGB_MAPPING = System.getProperty("minestom.map.rgbmapping", "lazy"); - public static final @Nullable String MAP_RGB_REDUCTION = System.getProperty("minestom.map.rgbreduction"); // Only used if rgb mapping is "approximate" + public static final @NotNull String MAP_RGB_MAPPING = stringProperty("minestom.map.rgbmapping", "lazy"); + public static final @Nullable String MAP_RGB_REDUCTION = stringProperty("minestom.map.rgbreduction"); // Only used if rgb mapping is "approximate" // Experimental/Unstable - public static final boolean REGISTRY_LATE_REGISTER = Boolean.getBoolean("minestom.registry.late-register"); - public static final boolean REGISTRY_UNSAFE_OPS = Boolean.getBoolean("minestom.registry.unsafe-ops"); - public static final boolean EVENT_NODE_ALLOW_MULTIPLE_PARENTS = Boolean.getBoolean("minestom.event.multiple-parents"); + public static final boolean REGISTRY_LATE_REGISTER = booleanProperty("minestom.registry.late-register"); + public static final boolean REGISTRY_UNSAFE_OPS = booleanProperty("minestom.registry.unsafe-ops"); + public static final boolean EVENT_NODE_ALLOW_MULTIPLE_PARENTS = booleanProperty("minestom.event.multiple-parents"); - private ServerFlag() {} + public static boolean INSIDE_TEST = booleanProperty("minestom.inside-test", false); + private ServerFlag() { + } + + private static boolean booleanProperty(String name) { + return Boolean.getBoolean(name); + } + + private static boolean booleanProperty(String name, boolean defaultValue) { + boolean result = defaultValue; + try { + final String value = System.getProperty(name); + if (value != null) result = Boolean.parseBoolean(value); + } catch (IllegalArgumentException | NullPointerException ignored) { + } + return result; + } + + @Contract("_, null -> null; _, !null -> !null") + private static String stringProperty(@NotNull String name, @Nullable String defaultValue) { + return System.getProperty(name, defaultValue); + } + + private static String stringProperty(@NotNull String name) { + return System.getProperty(name); + } + + private static int intProperty(String name, int defaultValue) { + return Integer.getInteger(name, defaultValue); + } + + private static long longProperty(String name, long defaultValue) { + return Long.getLong(name, defaultValue); + } + + private static Float floatProperty(String name, Float defaultValue) { + Float result = defaultValue; + try { + final String value = System.getProperty(name); + if (value != null) result = Float.parseFloat(value); + } catch (IllegalArgumentException | NullPointerException ignored) { + } + return result; + } } diff --git a/src/main/java/net/minestom/server/ServerProcessImpl.java b/src/main/java/net/minestom/server/ServerProcessImpl.java index db8afe0b1..0d0cdf325 100644 --- a/src/main/java/net/minestom/server/ServerProcessImpl.java +++ b/src/main/java/net/minestom/server/ServerProcessImpl.java @@ -37,7 +37,6 @@ import net.minestom.server.thread.Acquirable; import net.minestom.server.thread.ThreadDispatcher; import net.minestom.server.timer.SchedulerManager; import net.minestom.server.utils.PacketUtils; -import net.minestom.server.utils.PropertyUtils; import net.minestom.server.utils.collection.MappedCollection; import net.minestom.server.utils.nbt.BinaryTagSerializer; import net.minestom.server.world.DimensionType; @@ -55,7 +54,6 @@ import java.util.concurrent.atomic.AtomicReference; final class ServerProcessImpl implements ServerProcess { private static final Logger LOGGER = LoggerFactory.getLogger(ServerProcessImpl.class); - private static final Boolean SHUTDOWN_ON_SIGNAL = PropertyUtils.getBoolean("minestom.shutdown-on-signal", true); private final ExceptionManager exception; @@ -329,7 +327,7 @@ final class ServerProcessImpl implements ServerProcess { LOGGER.info(MinecraftServer.getBrandName() + " server started successfully."); // Stop the server on SIGINT - if (SHUTDOWN_ON_SIGNAL) Runtime.getRuntime().addShutdownHook(new Thread(this::stop)); + if (ServerFlag.SHUTDOWN_ON_SIGNAL) Runtime.getRuntime().addShutdownHook(new Thread(this::stop)); } @Override diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 64892b84c..1b57ef3e2 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -87,7 +87,6 @@ import net.minestom.server.thread.Acquirable; import net.minestom.server.timer.Scheduler; import net.minestom.server.utils.MathUtils; import net.minestom.server.utils.PacketUtils; -import net.minestom.server.utils.PropertyUtils; import net.minestom.server.utils.async.AsyncUtils; import net.minestom.server.utils.chunk.ChunkUpdateLimitChecker; import net.minestom.server.utils.chunk.ChunkUtils; @@ -129,10 +128,6 @@ public class Player extends LivingEntity implements CommandSender, Localizable, private static final Component REMOVE_MESSAGE = Component.text("You have been removed from the server without reason.", NamedTextColor.RED); private static final Component MISSING_REQUIRED_RESOURCE_PACK = Component.text("Required resource pack was not loaded.", NamedTextColor.RED); - private static final float MIN_CHUNKS_PER_TICK = PropertyUtils.getFloat("minestom.chunk-queue.min-per-tick", 0.01f); - private static final float MAX_CHUNKS_PER_TICK = PropertyUtils.getFloat("minestom.chunk-queue.max-per-tick", 64.0f); - private static final float CHUNKS_PER_TICK_MULTIPLIER = PropertyUtils.getFloat("minestom.chunk-queue.multiplier", 1f); - // Magic values: https://wiki.vg/Entity_statuses#Player private static final int STATUS_ENABLE_REDUCED_DEBUG_INFO = 22; private static final int STATUS_DISABLE_REDUCED_DEBUG_INFO = 23; @@ -781,8 +776,8 @@ public class Player extends LivingEntity implements CommandSender, Localizable, public void onChunkBatchReceived(float newTargetChunksPerTick) { // logger.debug("chunk batch received player={} chunks/tick={} lead={}", username, newTargetChunksPerTick, chunkBatchLead); chunkBatchLead -= 1; - targetChunksPerTick = Float.isNaN(newTargetChunksPerTick) ? MIN_CHUNKS_PER_TICK : MathUtils.clamp( - newTargetChunksPerTick * CHUNKS_PER_TICK_MULTIPLIER, MIN_CHUNKS_PER_TICK, MAX_CHUNKS_PER_TICK); + targetChunksPerTick = Float.isNaN(newTargetChunksPerTick) ? ServerFlag.MIN_CHUNKS_PER_TICK : MathUtils.clamp( + newTargetChunksPerTick * ServerFlag.CHUNKS_PER_TICK_MULTIPLIER, ServerFlag.MIN_CHUNKS_PER_TICK, ServerFlag.MAX_CHUNKS_PER_TICK); // Beyond the first batch we can preemptively send up to 10 (matching mojang server) if (maxChunkBatchLead == 1) maxChunkBatchLead = 10; @@ -808,7 +803,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable, if (chunkQueue.isEmpty() || chunkBatchLead >= maxChunkBatchLead) return; // Increment the pending chunk count by the target chunks per tick - pendingChunkCount = Math.min(pendingChunkCount + targetChunksPerTick, MAX_CHUNKS_PER_TICK); + pendingChunkCount = Math.min(pendingChunkCount + targetChunksPerTick, ServerFlag.MAX_CHUNKS_PER_TICK); if (pendingChunkCount < 1) return; // Cant send anything chunkQueueLock.lock(); diff --git a/src/main/java/net/minestom/server/network/ConnectionManager.java b/src/main/java/net/minestom/server/network/ConnectionManager.java index 2231cadd5..14a58b754 100644 --- a/src/main/java/net/minestom/server/network/ConnectionManager.java +++ b/src/main/java/net/minestom/server/network/ConnectionManager.java @@ -27,7 +27,6 @@ import net.minestom.server.network.plugin.LoginPluginMessageProcessor; import net.minestom.server.registry.StaticProtocolObject; import net.minestom.server.utils.StringUtils; import net.minestom.server.utils.async.AsyncUtils; -import net.minestom.server.utils.debug.DebugUtils; import net.minestom.server.utils.validate.Check; import org.jctools.queues.MessagePassingQueue; import org.jctools.queues.MpscUnboundedArrayQueue; @@ -207,7 +206,7 @@ public final class ConnectionManager { final Player player = playerProvider.createPlayer(uuid, username, connection); this.connectionPlayerMap.put(connection, player); var future = transitionLoginToConfig(player); - if (DebugUtils.INSIDE_TEST) future.join(); + if (ServerFlag.INSIDE_TEST) future.join(); return player; } @@ -390,7 +389,7 @@ public final class ConnectionManager { CompletableFuture spawnFuture = player.UNSAFE_init(); // Required to get the exact moment the player spawns - if (DebugUtils.INSIDE_TEST) spawnFuture.join(); + if (ServerFlag.INSIDE_TEST) spawnFuture.join(); }); } diff --git a/src/main/java/net/minestom/server/utils/PropertyUtils.java b/src/main/java/net/minestom/server/utils/PropertyUtils.java deleted file mode 100644 index 25a796af4..000000000 --- a/src/main/java/net/minestom/server/utils/PropertyUtils.java +++ /dev/null @@ -1,36 +0,0 @@ -package net.minestom.server.utils; - -import org.jetbrains.annotations.ApiStatus; -import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -@ApiStatus.Internal -public final class PropertyUtils { - private PropertyUtils() {} - - public static boolean getBoolean(String name, boolean defaultValue) { - boolean result = defaultValue; - try { - final String value = System.getProperty(name); - if (value != null) result = Boolean.parseBoolean(value); - } catch (IllegalArgumentException | NullPointerException ignored) { - } - return result; - } - - @Contract("_, null -> null; _, !null -> !null") - public static String getString(@NotNull String name, @Nullable String defaultValue) { - return System.getProperty(name, defaultValue); - } - - public static Float getFloat(String name, Float defaultValue) { - Float result = defaultValue; - try { - final String value = System.getProperty(name); - if (value != null) result = Float.parseFloat(value); - } catch (IllegalArgumentException | NullPointerException ignored) { - } - return result; - } -} diff --git a/src/main/java/net/minestom/server/utils/debug/DebugUtils.java b/src/main/java/net/minestom/server/utils/debug/DebugUtils.java deleted file mode 100644 index 26596b100..000000000 --- a/src/main/java/net/minestom/server/utils/debug/DebugUtils.java +++ /dev/null @@ -1,45 +0,0 @@ -package net.minestom.server.utils.debug; - -import net.minestom.server.utils.PropertyUtils; -import org.jetbrains.annotations.ApiStatus; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Utils class useful for debugging purpose. - */ -@ApiStatus.Internal -public final class DebugUtils { - public static boolean INSIDE_TEST = PropertyUtils.getBoolean("minestom.inside-test", false); - - private final static Logger LOGGER = LoggerFactory.getLogger(DebugUtils.class); - - private static final String LINE_SEPARATOR = System.getProperty("line.separator"); - - /** - * Prints the current thread stack trace elements. - */ - public static synchronized void printStackTrace() { - StackTraceElement[] elements = Thread.currentThread().getStackTrace(); - - StringBuilder stringBuilder = new StringBuilder(); - - stringBuilder.append("START STACKTRACE"); - stringBuilder.append(LINE_SEPARATOR); - - for (int i = 0; i < Integer.MAX_VALUE; i++) { - if (i >= elements.length) - break; - - final StackTraceElement element = elements[i]; - final String line = element.getClassName() + "." + element.getMethodName() + " (line:" + element.getLineNumber() + ")"; - stringBuilder.append(line); - stringBuilder.append(LINE_SEPARATOR); - } - - stringBuilder.append("END STACKTRACE"); - - LOGGER.info(stringBuilder.toString()); - } - -} diff --git a/src/test/java/net/minestom/server/InsideTest.java b/src/test/java/net/minestom/server/InsideTest.java index 4fd8bc46b..9adbea2e1 100644 --- a/src/test/java/net/minestom/server/InsideTest.java +++ b/src/test/java/net/minestom/server/InsideTest.java @@ -1,6 +1,5 @@ package net.minestom.server; -import net.minestom.server.utils.debug.DebugUtils; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -8,6 +7,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; public class InsideTest { @Test public void inside() { - assertTrue(DebugUtils.INSIDE_TEST); + assertTrue(ServerFlag.INSIDE_TEST); } }