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.TextColor;
import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.format.TextDecoration;
import net.minestom.demo.commands.*; import net.minestom.demo.commands.*;
import net.minestom.server.ConfigurationManager; import net.minestom.server.Configuration;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import net.minestom.server.command.CommandManager; import net.minestom.server.command.CommandManager;
import net.minestom.server.event.player.PlayerChatEvent; import net.minestom.server.event.player.PlayerChatEvent;
@ -31,7 +31,11 @@ import java.time.Duration;
public class Main { public class Main {
public static void main(String[] args) { 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(); 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(); PlayerInit.init();
OptifineSupport.enable(); 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; private static Difficulty difficulty = Difficulty.NORMAL;
public static MinecraftServer init() { public static MinecraftServer init() {
updateProcess(); return init(Configuration.builder().build());
}
public static MinecraftServer init(Configuration configuration) {
updateProcess(configuration);
return new MinecraftServer(); return new MinecraftServer();
} }
@ApiStatus.Internal @ApiStatus.Internal
public static ServerProcess updateProcess() { public static ServerProcess updateProcess(Configuration configuration) {
ServerProcess process; ServerProcess process;
try { try {
process = new ServerProcessImpl(); process = new ServerProcessImpl(configuration);
serverProcess = process; serverProcess = process;
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -191,7 +195,7 @@ public final class MinecraftServer {
return serverProcess.dynamicRegistry(); return serverProcess.dynamicRegistry();
} }
public static ConfigurationManager getConfigurationManager() { public static Configuration getConfiguration() {
return serverProcess.configuration(); return serverProcess.configuration();
} }
@ -328,12 +332,12 @@ public final class MinecraftServer {
* @param address the server address * @param address the server address
* @throws IllegalStateException if called before {@link #init()} or if the server is already running * @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); serverProcess.start(address);
new TickSchedulerThread(serverProcess).start(); 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)); start(new InetSocketAddress(address, port));
} }

View File

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

View File

@ -70,12 +70,13 @@ final class ServerProcessImpl implements ServerProcess {
private final ThreadDispatcher<Chunk> dispatcher; private final ThreadDispatcher<Chunk> dispatcher;
private final Ticker ticker; private final Ticker ticker;
private final DynamicRegistryManager dynamicRegistry; private final DynamicRegistryManager dynamicRegistry;
private final ConfigurationManager configurationManager; private final Configuration configuration;
private final AtomicBoolean started = new AtomicBoolean(); private final AtomicBoolean started = new AtomicBoolean();
private final AtomicBoolean stopped = 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.exception = new ExceptionManager();
this.extension = new ExtensionManager(this); this.extension = new ExtensionManager(this);
this.connection = new ConnectionManager(); this.connection = new ConnectionManager();
@ -99,7 +100,6 @@ final class ServerProcessImpl implements ServerProcess {
this.dispatcher = ThreadDispatcher.singleThread(); this.dispatcher = ThreadDispatcher.singleThread();
this.ticker = new TickerImpl(); this.ticker = new TickerImpl();
this.dynamicRegistry = new DynamicRegistryManager(); this.dynamicRegistry = new DynamicRegistryManager();
this.configurationManager = new ConfigurationManager();
} }
@Override @Override
@ -213,8 +213,8 @@ final class ServerProcessImpl implements ServerProcess {
} }
@Override @Override
public @NotNull ConfigurationManager configuration() { public @NotNull Configuration configuration() {
return configurationManager; return configuration;
} }
@Override @Override
@ -223,8 +223,6 @@ final class ServerProcessImpl implements ServerProcess {
throw new IllegalStateException("Server already started"); throw new IllegalStateException("Server already started");
} }
configurationManager.initDefaults();
extension.start(); extension.start();
extension.gotoPreInit(); 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.Component;
import net.kyori.adventure.text.format.NamedTextColor; 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.MinecraftServer;
import net.minestom.server.crypto.PlayerPublicKey; import net.minestom.server.crypto.PlayerPublicKey;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
@ -35,16 +35,16 @@ public record LoginStartPacket(@NotNull String username, @Nullable PlayerPublicK
@Override @Override
public void process(@NotNull PlayerConnection connection) { public void process(@NotNull PlayerConnection connection) {
connection.setPlayerPublicKey(publicKey); 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 != null) {
if (!publicKey.isValid()) { if (!publicKey.isValid()) {
connection.sendPacket(new LoginDisconnectPacket(config.INVALID_PLAYER_PUBLIC_KEY.get())); connection.sendPacket(new LoginDisconnectPacket(config.invalidPlayerPublicKeyMessage()));
connection.disconnect(); connection.disconnect();
} }
} else { } else {
connection.sendPacket(new LoginDisconnectPacket(config.MISSING_PLAYER_PUBLIC_KEY.get())); connection.sendPacket(new LoginDisconnectPacket(config.missingPlayerPublicKeyMessage()));
connection.disconnect(); connection.disconnect();
} }
} }

View File

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

View File

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

View File

@ -1,5 +1,6 @@
package net.minestom.server.api; package net.minestom.server.api;
import net.minestom.server.Configuration;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext; import org.junit.jupiter.api.extension.ParameterContext;
@ -10,6 +11,6 @@ final class EnvParameterResolver extends TypeBasedParameterResolver<Env> {
@Override @Override
public Env resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) public Env resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException { 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; package net.minestom.server.event;
import net.minestom.server.Configuration;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.Entity; import net.minestom.server.entity.Entity;
import net.minestom.server.entity.EntityType; import net.minestom.server.entity.EntityType;
@ -77,7 +78,7 @@ public class EventNodeMapTest {
@Test @Test
public void entityLocal() { public void entityLocal() {
var process = MinecraftServer.updateProcess(); var process = MinecraftServer.updateProcess(Configuration.builder().build());
var node = process.eventHandler(); var node = process.eventHandler();
var entity = new Entity(EntityType.ZOMBIE); var entity = new Entity(EntityType.ZOMBIE);