Rework config

This commit is contained in:
Noel Németh 2022-06-21 15:56:58 +02:00
parent 6f36d2404a
commit ea3936b03b
11 changed files with 90 additions and 66 deletions

View File

@ -8,7 +8,7 @@ import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.minestom.demo.commands.*;
import net.minestom.server.ConfigurationManager;
import net.minestom.server.Configuration;
import net.minestom.server.MinecraftServer;
import net.minestom.server.command.CommandManager;
import net.minestom.server.event.player.PlayerChatEvent;
@ -31,7 +31,11 @@ import java.time.Duration;
public class Main {
public static void main(String[] args) {
MinecraftServer minecraftServer = MinecraftServer.init();
MinecraftServer minecraftServer = MinecraftServer.init(Configuration.builder()
.setSystemChatType(ChatTypeBuilder.builder(ChatType.SYSTEM.key())
.chat(ChatDecoration.content("SYSTEM: %s", Style.style(NamedTextColor.AQUA)
.font(Key.key("minecraft:uniform")))))
.build());
BlockManager blockManager = MinecraftServer.getBlockManager();
@ -110,13 +114,6 @@ public class Main {
));
});
final ConfigurationManager conf = MinecraftServer.getConfigurationManager();
// conf.PLAYER_CHAT_TYPE.set(ChatTypeBuilder.builder(ChatType.CHAT.key())
// .chat(ChatDecoration.full("%s | %s> %s", Style.style(NamedTextColor.DARK_RED))));
conf.SYSTEM_CHAT_TYPE.set(ChatTypeBuilder.builder(ChatType.SYSTEM.key())
.chat(ChatDecoration.content("SYSTEM: %s", Style.style(NamedTextColor.AQUA)
.font(Key.key("minecraft:uniform")))));
PlayerInit.init();
OptifineSupport.enable();

View File

@ -0,0 +1,53 @@
package net.minestom.server;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.registry.dynamic.chat.ChatDecoration;
import net.minestom.server.registry.dynamic.chat.ChatType;
import net.minestom.server.registry.dynamic.chat.ChatTypeBuilder;
public record Configuration(boolean requireValidPlayerPublicKey, Component missingPlayerPublicKeyMessage,
Component invalidPlayerPublicKeyMessage, ChatTypeBuilder playerChatType,
ChatTypeBuilder systemChatType) {
public static Builder builder() {
return new Builder();
}
public static class Builder {
private boolean requireValidPlayerPublicKey = false;
private Component missingPlayerPublicKeyMessage = Component.text("Missing public key!", NamedTextColor.RED);
private Component invalidPlayerPublicKeyMessage = Component.text("Invalid public key!", NamedTextColor.RED);
private ChatTypeBuilder playerChatType = ChatTypeBuilder.builder(ChatType.CHAT.key()).chat(ChatDecoration.contentWithSender("chat.type.text"));
private ChatTypeBuilder systemChatType = ChatTypeBuilder.builder(ChatType.SYSTEM.key()).chat();
public Builder setRequireValidPlayerPublicKey(boolean requireValidPlayerPublicKey) {
this.requireValidPlayerPublicKey = requireValidPlayerPublicKey;
return this;
}
public Builder setMissingPlayerPublicKeyMessage(Component missingPlayerPublicKeyMessage) {
this.missingPlayerPublicKeyMessage = missingPlayerPublicKeyMessage;
return this;
}
public Builder setInvalidPlayerPublicKeyMessage(Component invalidPlayerPublicKeyMessage) {
this.invalidPlayerPublicKeyMessage = invalidPlayerPublicKeyMessage;
return this;
}
public Builder setPlayerChatType(ChatTypeBuilder playerChatType) {
this.playerChatType = playerChatType;
return this;
}
public Builder setSystemChatType(ChatTypeBuilder systemChatType) {
this.systemChatType = systemChatType;
return this;
}
public Configuration build() {
return new Configuration(requireValidPlayerPublicKey, missingPlayerPublicKeyMessage, invalidPlayerPublicKeyMessage, playerChatType, systemChatType);
}
}
}

View File

@ -1,30 +0,0 @@
package net.minestom.server;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.registry.dynamic.chat.ChatDecoration;
import net.minestom.server.registry.dynamic.chat.ChatType;
import net.minestom.server.registry.dynamic.chat.ChatTypeBuilder;
import net.minestom.server.utils.FinalObject;
import org.jetbrains.annotations.ApiStatus;
/**
* Can be used to change Minestom defaults before {@link MinecraftServer#start} is called.
*/
public final class ConfigurationManager {
public final FinalObject<Boolean> REQUIRE_VALID_PLAYER_PUBLIC_KEY = new FinalObject<>();
public final FinalObject<Component> MISSING_PLAYER_PUBLIC_KEY = new FinalObject<>();
public final FinalObject<Component> INVALID_PLAYER_PUBLIC_KEY = new FinalObject<>();
public final FinalObject<ChatTypeBuilder> PLAYER_CHAT_TYPE = new FinalObject<>();
public final FinalObject<ChatTypeBuilder> SYSTEM_CHAT_TYPE = new FinalObject<>();
@ApiStatus.Internal
public void initDefaults() {
REQUIRE_VALID_PLAYER_PUBLIC_KEY.optionalSet(false);
MISSING_PLAYER_PUBLIC_KEY.optionalSet(Component.text("Missing public key!", NamedTextColor.RED));
INVALID_PLAYER_PUBLIC_KEY.optionalSet(Component.text("Invalid public key!", NamedTextColor.RED));
PLAYER_CHAT_TYPE.optionalSet(() -> ChatTypeBuilder.builder(ChatType.CHAT.key())
.chat(ChatDecoration.contentWithSender("chat.type.text")));
SYSTEM_CHAT_TYPE.optionalSet(() -> ChatTypeBuilder.builder(ChatType.SYSTEM.key()).chat());
}
}

View File

@ -72,15 +72,19 @@ public final class MinecraftServer {
private static Difficulty difficulty = Difficulty.NORMAL;
public static MinecraftServer init() {
updateProcess();
return init(Configuration.builder().build());
}
public static MinecraftServer init(Configuration configuration) {
updateProcess(configuration);
return new MinecraftServer();
}
@ApiStatus.Internal
public static ServerProcess updateProcess() {
public static ServerProcess updateProcess(Configuration configuration) {
ServerProcess process;
try {
process = new ServerProcessImpl();
process = new ServerProcessImpl(configuration);
serverProcess = process;
} catch (IOException e) {
throw new RuntimeException(e);
@ -191,7 +195,7 @@ public final class MinecraftServer {
return serverProcess.dynamicRegistry();
}
public static ConfigurationManager getConfigurationManager() {
public static Configuration getConfiguration() {
return serverProcess.configuration();
}
@ -328,12 +332,12 @@ public final class MinecraftServer {
* @param address the server address
* @throws IllegalStateException if called before {@link #init()} or if the server is already running
*/
public void start(@NotNull SocketAddress address) {
public static void start(@NotNull SocketAddress address) {
serverProcess.start(address);
new TickSchedulerThread(serverProcess).start();
}
public void start(@NotNull String address, int port) {
public static void start(@NotNull String address, int port) {
start(new InetSocketAddress(address, port));
}

View File

@ -141,7 +141,7 @@ public interface ServerProcess extends Snapshotable {
@NotNull DynamicRegistryManager dynamicRegistry();
@NotNull ConfigurationManager configuration();
@NotNull Configuration configuration();
void start(@NotNull SocketAddress socketAddress);

View File

@ -70,12 +70,13 @@ final class ServerProcessImpl implements ServerProcess {
private final ThreadDispatcher<Chunk> dispatcher;
private final Ticker ticker;
private final DynamicRegistryManager dynamicRegistry;
private final ConfigurationManager configurationManager;
private final Configuration configuration;
private final AtomicBoolean started = new AtomicBoolean();
private final AtomicBoolean stopped = new AtomicBoolean();
public ServerProcessImpl() throws IOException {
public ServerProcessImpl(Configuration configuration) throws IOException {
this.configuration = configuration;
this.exception = new ExceptionManager();
this.extension = new ExtensionManager(this);
this.connection = new ConnectionManager();
@ -99,7 +100,6 @@ final class ServerProcessImpl implements ServerProcess {
this.dispatcher = ThreadDispatcher.singleThread();
this.ticker = new TickerImpl();
this.dynamicRegistry = new DynamicRegistryManager();
this.configurationManager = new ConfigurationManager();
}
@Override
@ -213,8 +213,8 @@ final class ServerProcessImpl implements ServerProcess {
}
@Override
public @NotNull ConfigurationManager configuration() {
return configurationManager;
public @NotNull Configuration configuration() {
return configuration;
}
@Override
@ -223,8 +223,6 @@ final class ServerProcessImpl implements ServerProcess {
throw new IllegalStateException("Server already started");
}
configurationManager.initDefaults();
extension.start();
extension.gotoPreInit();

View File

@ -2,7 +2,7 @@ package net.minestom.server.network.packet.client.login;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.ConfigurationManager;
import net.minestom.server.Configuration;
import net.minestom.server.MinecraftServer;
import net.minestom.server.crypto.PlayerPublicKey;
import net.minestom.server.entity.Player;
@ -35,16 +35,16 @@ public record LoginStartPacket(@NotNull String username, @Nullable PlayerPublicK
@Override
public void process(@NotNull PlayerConnection connection) {
connection.setPlayerPublicKey(publicKey);
final ConfigurationManager config = MinecraftServer.getConfigurationManager();
final Configuration config = MinecraftServer.getConfiguration();
if (config.REQUIRE_VALID_PLAYER_PUBLIC_KEY.get()) {
if (config.requireValidPlayerPublicKey()) {
if (publicKey != null) {
if (!publicKey.isValid()) {
connection.sendPacket(new LoginDisconnectPacket(config.INVALID_PLAYER_PUBLIC_KEY.get()));
connection.sendPacket(new LoginDisconnectPacket(config.invalidPlayerPublicKeyMessage()));
connection.disconnect();
}
} else {
connection.sendPacket(new LoginDisconnectPacket(config.MISSING_PLAYER_PUBLIC_KEY.get()));
connection.sendPacket(new LoginDisconnectPacket(config.missingPlayerPublicKeyMessage()));
connection.disconnect();
}
}

View File

@ -1,7 +1,7 @@
package net.minestom.server.registry.dynamic;
import net.kyori.adventure.key.Key;
import net.minestom.server.ConfigurationManager;
import net.minestom.server.Configuration;
import net.minestom.server.MinecraftServer;
import net.minestom.server.registry.NBTRepresentable;
import net.minestom.server.registry.dynamic.chat.ChatType;
@ -79,8 +79,8 @@ public final class DynamicRegistryManager implements NBTRepresentable {
@ApiStatus.Internal
public void initDefaults() {
final ConfigurationManager config = MinecraftServer.getConfigurationManager();
((DynamicChatTypeImpl) ChatType.CHAT).setBackingType(register(config.PLAYER_CHAT_TYPE.get()));
((DynamicChatTypeImpl) ChatType.SYSTEM).setBackingType(register(config.SYSTEM_CHAT_TYPE.get()));
final Configuration config = MinecraftServer.getConfiguration();
((DynamicChatTypeImpl) ChatType.CHAT).setBackingType(register(config.playerChatType()));
((DynamicChatTypeImpl) ChatType.SYSTEM).setBackingType(register(config.systemChatType()));
}
}

View File

@ -13,7 +13,7 @@ public class ServerProcessTest {
@Test
public void init() {
AtomicReference<ServerProcess> process = new AtomicReference<>();
assertDoesNotThrow(() -> process.set(MinecraftServer.updateProcess()));
assertDoesNotThrow(() -> process.set(MinecraftServer.updateProcess(Configuration.builder().build())));
assertDoesNotThrow(() -> process.get().start(new InetSocketAddress("localhost", 25565)));
assertThrows(Exception.class, () -> process.get().start(new InetSocketAddress("localhost", 25566)));
assertDoesNotThrow(() -> process.get().stop());
@ -21,7 +21,7 @@ public class ServerProcessTest {
@Test
public void tick() {
var process = MinecraftServer.updateProcess();
var process = MinecraftServer.updateProcess(Configuration.builder().build());
process.start(new InetSocketAddress("localhost", 25565));
var ticker = process.ticker();
assertDoesNotThrow(() -> ticker.tick(System.currentTimeMillis()));

View File

@ -1,5 +1,6 @@
package net.minestom.server.api;
import net.minestom.server.Configuration;
import net.minestom.server.MinecraftServer;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
@ -10,6 +11,6 @@ final class EnvParameterResolver extends TypeBasedParameterResolver<Env> {
@Override
public Env resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
return new EnvImpl(MinecraftServer.updateProcess());
return new EnvImpl(MinecraftServer.updateProcess(Configuration.builder().build()));
}
}

View File

@ -1,5 +1,6 @@
package net.minestom.server.event;
import net.minestom.server.Configuration;
import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.EntityType;
@ -77,7 +78,7 @@ public class EventNodeMapTest {
@Test
public void entityLocal() {
var process = MinecraftServer.updateProcess();
var process = MinecraftServer.updateProcess(Configuration.builder().build());
var node = process.eventHandler();
var entity = new Entity(EntityType.ZOMBIE);