diff --git a/minestom-extensions/src/main/java/net/hollowcube/minestom/extensions/ExtensionBootstrap.java b/minestom-extensions/src/main/java/net/hollowcube/minestom/extensions/ExtensionBootstrap.java index f811555..4ae4000 100644 --- a/minestom-extensions/src/main/java/net/hollowcube/minestom/extensions/ExtensionBootstrap.java +++ b/minestom-extensions/src/main/java/net/hollowcube/minestom/extensions/ExtensionBootstrap.java @@ -1,5 +1,6 @@ package net.hollowcube.minestom.extensions; +import net.minestom.server.Auth; import net.minestom.server.MinecraftServer; import net.minestom.server.extensions.ExtensionManager; import net.minestom.server.utils.validate.Check; @@ -11,8 +12,28 @@ public final class ExtensionBootstrap { private static ExtensionManager extensions = null; + /** + * Initialises the server with offline-mode authentication. + * + * @return the bootstrap to {@link #start(String, int)} once setup is done + */ public static @NotNull ExtensionBootstrap init() { - return new ExtensionBootstrap(MinecraftServer.init()); + return init(new Auth.Offline()); + } + + /** + * Initialises the server with the given authentication mode. + * + *
The {@link Auth} is bound to the {@code ServerProcess} by {@link MinecraftServer#init(Auth)} + * and cannot be swapped afterwards, so a server that sits behind a Velocity proxy has to pass + * {@link Auth.Velocity} here - there is no way to switch it on later. + * + * @param auth how incoming connections are authenticated, e.g. {@link Auth.Velocity} for a + * server behind a Velocity proxy + * @return the bootstrap to {@link #start(String, int)} once setup is done + */ + public static @NotNull ExtensionBootstrap init(@NotNull Auth auth) { + return new ExtensionBootstrap(MinecraftServer.init(auth)); } public static @NotNull ExtensionManager getExtensionManager() { diff --git a/minestom-extensions/src/test/java/net/hollowcube/minestom/extensions/ExtensionBootstrapAuthTest.java b/minestom-extensions/src/test/java/net/hollowcube/minestom/extensions/ExtensionBootstrapAuthTest.java new file mode 100644 index 0000000..3096ad4 --- /dev/null +++ b/minestom-extensions/src/test/java/net/hollowcube/minestom/extensions/ExtensionBootstrapAuthTest.java @@ -0,0 +1,39 @@ +package net.hollowcube.minestom.extensions; + +import net.minestom.server.Auth; +import net.minestom.server.MinecraftServer; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertSame; + +/** + * Pins that {@link ExtensionBootstrap} hands the caller's {@link Auth} to + * {@link MinecraftServer#init(Auth)}. + * + *
This is the whole point of the {@code init(Auth)} overload and it cannot be worked around from + * the outside: the {@code ServerProcess} keeps the {@link Auth} it was built with, so a bootstrap + * that silently called {@code MinecraftServer.init()} would leave every server behind a Velocity + * proxy on offline-mode authentication with no way to correct it afterwards. + */ +class ExtensionBootstrapAuthTest { + + @Test + @DisplayName("init(Auth) binds the given auth to the server process") + void initWithAuthBindsGivenAuth() { + Auth.Velocity velocity = new Auth.Velocity("a-velocity-secret"); + + ExtensionBootstrap.init(velocity); + + assertSame(velocity, MinecraftServer.process().auth()); + } + + @Test + @DisplayName("init() keeps defaulting to offline mode") + void initWithoutAuthDefaultsToOffline() { + ExtensionBootstrap.init(); + + assertInstanceOf(Auth.Offline.class, MinecraftServer.process().auth()); + } +}