diff --git a/README.md b/README.md index a9d31f0..edb8732 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ The Server API is organized into several modules: - `bungeecord` - The platform-specific integration for Servers running on BungeeCord. - `common` - Contains shared classes and utilities used across different server implementations to ensure consistent behavior and reduce code duplication. + - `minestom` - The platform-specific integration for Servers running on Minestom. - `velocity` - The platform-specific integration for Servers running on Velocity. ## Integrations diff --git a/server/minestom/build.gradle.kts b/server/minestom/build.gradle.kts new file mode 100644 index 0000000..022926d --- /dev/null +++ b/server/minestom/build.gradle.kts @@ -0,0 +1,21 @@ +plugins { + id("java") +} + +repositories { + mavenCentral() +} + +dependencies { + compileOnly("net.minestom:minestom-snapshots:8f46913486") +} + +java { + toolchain { + languageVersion.set(JavaLanguageVersion.of(21)) + } + sourceCompatibility = JavaVersion.VERSION_21 + targetCompatibility = JavaVersion.VERSION_21 + +} + diff --git a/server/minestom/src/main/java/net/labymod/serverapi/server/minestom/LabyModPlayer.java b/server/minestom/src/main/java/net/labymod/serverapi/server/minestom/LabyModPlayer.java new file mode 100644 index 0000000..7045e70 --- /dev/null +++ b/server/minestom/src/main/java/net/labymod/serverapi/server/minestom/LabyModPlayer.java @@ -0,0 +1,39 @@ +/* + * MIT License + * + * Copyright (c) 2024 LabyMedia GmbH + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.labymod.serverapi.server.minestom; + +import net.labymod.serverapi.core.AbstractLabyModPlayer; +import net.labymod.serverapi.core.AbstractLabyModProtocolService; +import net.labymod.serverapi.server.common.AbstractServerLabyModProtocolService; +import net.labymod.serverapi.server.common.model.player.AbstractServerLabyModPlayer; +import net.minestom.server.entity.Player; + +import java.util.UUID; + +public class LabyModPlayer extends AbstractServerLabyModPlayer { + public LabyModPlayer(AbstractServerLabyModProtocolService protocolService, UUID uniqueId, Player player, String labyModVersion) { + super(protocolService, uniqueId, player, labyModVersion); + } +} diff --git a/server/minestom/src/main/java/net/labymod/serverapi/server/minestom/LabyModProtocolService.java b/server/minestom/src/main/java/net/labymod/serverapi/server/minestom/LabyModProtocolService.java new file mode 100644 index 0000000..c7846ba --- /dev/null +++ b/server/minestom/src/main/java/net/labymod/serverapi/server/minestom/LabyModProtocolService.java @@ -0,0 +1,176 @@ +/* + * MIT License + * + * Copyright (c) 2024 LabyMedia GmbH + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.labymod.serverapi.server.minestom; + +import net.labymod.serverapi.api.Protocol; +import net.labymod.serverapi.api.logger.NoOpProtocolPlatformLogger; +import net.labymod.serverapi.api.logger.ProtocolPlatformLogger; +import net.labymod.serverapi.api.packet.Packet; +import net.labymod.serverapi.api.payload.PayloadChannelIdentifier; +import net.labymod.serverapi.api.payload.io.PayloadReader; +import net.labymod.serverapi.api.payload.io.PayloadWriter; +import net.labymod.serverapi.core.AbstractLabyModProtocolService; +import net.labymod.serverapi.core.packet.serverbound.login.VersionLoginPacket; +import net.labymod.serverapi.server.common.AbstractServerLabyModProtocolService; +import net.labymod.serverapi.server.common.JavaProtocolLogger; +import net.labymod.serverapi.server.common.model.player.AbstractServerLabyModPlayer; +import net.labymod.serverapi.server.minestom.event.LabyModInstalledAddonsUpdateEvent; +import net.labymod.serverapi.server.minestom.event.LabyModPacketReceivedEvent; +import net.labymod.serverapi.server.minestom.event.LabyModPacketSentEvent; +import net.labymod.serverapi.server.minestom.handler.DefaultVersionLoginPacketHandler; +import net.minestom.server.MinecraftServer; +import net.minestom.server.entity.Player; +import net.minestom.server.event.player.PlayerDisconnectEvent; +import net.minestom.server.event.player.PlayerPluginMessageEvent; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Objects; +import java.util.UUID; + +public class LabyModProtocolService extends AbstractServerLabyModProtocolService { + + private static LabyModProtocolService INSTANCE; + private final ProtocolPlatformLogger logger = new Slf4jPlatformLogger(LoggerFactory.getLogger(LabyModProtocolService.class)); + private boolean initialized = false; + + private LabyModProtocolService() { + } + + public static LabyModProtocolService get() { + return INSTANCE; + } + + public static void initialize() { + INSTANCE = new LabyModProtocolService(); + INSTANCE.init(); + } + + private void init() { + if (initialized) { + throw new IllegalStateException("This protocol service is already initialized."); + } + + labyModProtocol.registerHandler( + VersionLoginPacket.class, + new DefaultVersionLoginPacketHandler(this) + ); + + this.registry().addRegisterListener( + protocol -> { + MinecraftServer.getGlobalEventHandler().addListener(PlayerPluginMessageEvent.class, event -> onPluginMessage(event, protocol)); + } + ); + MinecraftServer.getGlobalEventHandler().addListener(PlayerDisconnectEvent.class, event -> { + handlePlayerQuit(event.getPlayer().getUuid()); + }); + + initialized = true; + } + + /** + * {@inheritDoc} + */ + @Override + @ApiStatus.Internal + public void handleInstalledAddonsUpdate(AbstractServerLabyModPlayer labyModPlayer) { + MinecraftServer.getGlobalEventHandler().call(new LabyModInstalledAddonsUpdateEvent( + this, + (LabyModPlayer) labyModPlayer + )); + } + + /** + * {@inheritDoc} + */ + @Override + public void send(@NotNull PayloadChannelIdentifier identifier, @NotNull UUID recipient, @NotNull PayloadWriter writer) { + Objects.requireNonNull(identifier, "Identifier cannot be null"); + Objects.requireNonNull(recipient, "Recipient cannot be null"); + Objects.requireNonNull(writer, "Writer cannot be null"); + + Player player = MinecraftServer.getConnectionManager().getOnlinePlayerByUuid(recipient); + if (player == null) { + return; + } + + player.sendPluginMessage(identifier.toString(), writer.toByteArray()); + } + + @Override + public void afterPacketHandled(@NotNull Protocol protocol, @NotNull Packet packet, @NotNull UUID sender) { + MinecraftServer.getGlobalEventHandler().call( + new LabyModPacketReceivedEvent( + this, + protocol, + getPlayer(sender), + packet + ) + ); + } + + @Override + public void afterPacketSent(@NotNull Protocol protocol, @NotNull Packet packet, @NotNull UUID recipient) { + MinecraftServer.getGlobalEventHandler().call( + new LabyModPacketSentEvent( + this, + protocol, + getPlayer(recipient), + packet + ) + ); + } + + /** + * {@inheritDoc} + */ + @Override + public @NotNull ProtocolPlatformLogger logger() { + return logger; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isInitialized() { + return true; + } + + private void onPluginMessage(PlayerPluginMessageEvent event, Protocol protocol) { + if (!event.getIdentifier().equals(protocol.identifier().toString())) { + return; + } + + try { + PayloadReader reader = new PayloadReader(event.getMessage()); + protocol.handleIncomingPayload(event.getPlayer().getUuid(), reader); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/server/minestom/src/main/java/net/labymod/serverapi/server/minestom/Slf4jPlatformLogger.java b/server/minestom/src/main/java/net/labymod/serverapi/server/minestom/Slf4jPlatformLogger.java new file mode 100644 index 0000000..08fa0e6 --- /dev/null +++ b/server/minestom/src/main/java/net/labymod/serverapi/server/minestom/Slf4jPlatformLogger.java @@ -0,0 +1,54 @@ +package net.labymod.serverapi.server.minestom; + +import net.labymod.serverapi.api.logger.ProtocolPlatformLogger; +import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; + +public class Slf4jPlatformLogger implements ProtocolPlatformLogger { + + private final Logger logger; + + public Slf4jPlatformLogger(Logger logger) { + this.logger = logger; + } + + @Override + public void info(@NotNull String s, Object... objects) { + this.logger.info(s, objects); + } + + @Override + public void info(@NotNull String s, Throwable throwable) { + this.logger.info(s, throwable); + } + + @Override + public void warn(@NotNull String s, Object... objects) { + this.logger.warn(s, objects); + } + + @Override + public void warn(@NotNull String s, Throwable throwable) { + this.logger.warn(s, throwable); + } + + @Override + public void error(@NotNull String s, Object... objects) { + this.logger.error(s, objects); + } + + @Override + public void error(@NotNull String s, Throwable throwable) { + this.logger.error(s, throwable); + } + + @Override + public void debug(@NotNull String message, Object... objects) { + this.logger.debug(message, objects); + } + + @Override + public void debug(@NotNull String message, Throwable throwable) { + this.logger.debug(message, throwable); + } +} diff --git a/server/minestom/src/main/java/net/labymod/serverapi/server/minestom/event/LabyModInstalledAddonsUpdateEvent.java b/server/minestom/src/main/java/net/labymod/serverapi/server/minestom/event/LabyModInstalledAddonsUpdateEvent.java new file mode 100644 index 0000000..1147948 --- /dev/null +++ b/server/minestom/src/main/java/net/labymod/serverapi/server/minestom/event/LabyModInstalledAddonsUpdateEvent.java @@ -0,0 +1,55 @@ +/* + * MIT License + * + * Copyright (c) 2024 LabyMedia GmbH + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.labymod.serverapi.server.minestom.event; + +import net.labymod.serverapi.server.common.model.addon.InstalledAddonsResponse; +import net.labymod.serverapi.server.minestom.LabyModPlayer; +import net.labymod.serverapi.server.minestom.LabyModProtocolService; +import net.minestom.server.entity.Player; +import net.minestom.server.event.trait.PlayerEvent; +import org.jetbrains.annotations.NotNull; + +public record LabyModInstalledAddonsUpdateEvent( + LabyModProtocolService protocolService, + LabyModPlayer player +) implements PlayerEvent { + + public @NotNull LabyModProtocolService protocolService() { + return this.protocolService; + } + + public @NotNull LabyModPlayer labyModPlayer() { + return this.player; + } + + public @NotNull InstalledAddonsResponse installedAddons() { + return this.player.installedAddons(); + } + + @Override + public @NotNull Player getPlayer() { + return player.getPlayer(); + } +} \ No newline at end of file diff --git a/server/minestom/src/main/java/net/labymod/serverapi/server/minestom/event/LabyModPacketReceivedEvent.java b/server/minestom/src/main/java/net/labymod/serverapi/server/minestom/event/LabyModPacketReceivedEvent.java new file mode 100644 index 0000000..1bf37c3 --- /dev/null +++ b/server/minestom/src/main/java/net/labymod/serverapi/server/minestom/event/LabyModPacketReceivedEvent.java @@ -0,0 +1,65 @@ +/* + * MIT License + * + * Copyright (c) 2024 LabyMedia GmbH + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.labymod.serverapi.server.minestom.event; + +import net.labymod.serverapi.api.Protocol; +import net.labymod.serverapi.api.packet.Packet; +import net.labymod.serverapi.server.minestom.LabyModPlayer; +import net.labymod.serverapi.server.minestom.LabyModProtocolService; +import net.minestom.server.entity.Player; +import net.minestom.server.event.trait.PlayerEvent; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.UUID; + +public record LabyModPacketReceivedEvent( + LabyModProtocolService protocolService, + Protocol protocol, + LabyModPlayer player, + Packet packet +) implements PlayerEvent { + + public @NotNull LabyModProtocolService protocolService() { + return this.protocolService; + } + + public @Nullable LabyModPlayer getLabyModPlayer() { + return player; + } + + public @NotNull Protocol protocol() { + return this.protocol; + } + + public @NotNull Packet packet() { + return this.packet; + } + + @Override + public @NotNull Player getPlayer() { + return player.getPlayer(); + } +} diff --git a/server/minestom/src/main/java/net/labymod/serverapi/server/minestom/event/LabyModPacketSentEvent.java b/server/minestom/src/main/java/net/labymod/serverapi/server/minestom/event/LabyModPacketSentEvent.java new file mode 100644 index 0000000..65233ec --- /dev/null +++ b/server/minestom/src/main/java/net/labymod/serverapi/server/minestom/event/LabyModPacketSentEvent.java @@ -0,0 +1,65 @@ +/* + * MIT License + * + * Copyright (c) 2024 LabyMedia GmbH + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.labymod.serverapi.server.minestom.event; + +import net.labymod.serverapi.api.Protocol; +import net.labymod.serverapi.api.packet.Packet; +import net.labymod.serverapi.server.minestom.LabyModPlayer; +import net.labymod.serverapi.server.minestom.LabyModProtocolService; +import net.minestom.server.entity.Player; +import net.minestom.server.event.trait.PlayerEvent; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.UUID; + +public record LabyModPacketSentEvent( + LabyModProtocolService protocolService, + Protocol protocol, + LabyModPlayer player, + Packet packet +) implements PlayerEvent { + + public @NotNull LabyModProtocolService protocolService() { + return this.protocolService; + } + + public @Nullable LabyModPlayer getLabyModPlayer() { + return player; + } + + public @NotNull Protocol protocol() { + return this.protocol; + } + + public @NotNull Packet packet() { + return this.packet; + } + + @Override + public @NotNull Player getPlayer() { + return player.getPlayer(); + } +} diff --git a/server/minestom/src/main/java/net/labymod/serverapi/server/minestom/event/LabyModPlayerJoinEvent.java b/server/minestom/src/main/java/net/labymod/serverapi/server/minestom/event/LabyModPlayerJoinEvent.java new file mode 100644 index 0000000..fb5565e --- /dev/null +++ b/server/minestom/src/main/java/net/labymod/serverapi/server/minestom/event/LabyModPlayerJoinEvent.java @@ -0,0 +1,50 @@ +/* + * MIT License + * + * Copyright (c) 2024 LabyMedia GmbH + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.labymod.serverapi.server.minestom.event; + +import net.labymod.serverapi.server.minestom.LabyModPlayer; +import net.labymod.serverapi.server.minestom.LabyModProtocolService; +import net.minestom.server.entity.Player; +import net.minestom.server.event.trait.PlayerEvent; +import org.jetbrains.annotations.NotNull; + +public record LabyModPlayerJoinEvent( + LabyModProtocolService protocolService, + LabyModPlayer player +) implements PlayerEvent { + + public LabyModProtocolService protocolService() { + return protocolService; + } + + public LabyModPlayer getLabyModPlayer() { + return player; + } + + @Override + public @NotNull Player getPlayer() { + return player.getPlayer(); + } +} diff --git a/server/minestom/src/main/java/net/labymod/serverapi/server/minestom/handler/DefaultVersionLoginPacketHandler.java b/server/minestom/src/main/java/net/labymod/serverapi/server/minestom/handler/DefaultVersionLoginPacketHandler.java new file mode 100644 index 0000000..dd300d0 --- /dev/null +++ b/server/minestom/src/main/java/net/labymod/serverapi/server/minestom/handler/DefaultVersionLoginPacketHandler.java @@ -0,0 +1,66 @@ +/* + * MIT License + * + * Copyright (c) 2024 LabyMedia GmbH + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.labymod.serverapi.server.minestom.handler; + +import net.labymod.serverapi.api.packet.PacketHandler; +import net.labymod.serverapi.core.packet.serverbound.login.VersionLoginPacket; +import net.labymod.serverapi.server.minestom.LabyModPlayer; +import net.labymod.serverapi.server.minestom.LabyModProtocolService; +import net.labymod.serverapi.server.minestom.event.LabyModPlayerJoinEvent; +import net.minestom.server.MinecraftServer; +import net.minestom.server.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.UUID; + +public record DefaultVersionLoginPacketHandler( + LabyModProtocolService protocolService +) implements PacketHandler { + @Override + public void handle(@NotNull UUID sender, @NotNull VersionLoginPacket packet) { + if (protocolService.getPlayer(sender) != null) { + // player is already connected + return; + } + + Player player = MinecraftServer.getConnectionManager().getOnlinePlayerByUuid(sender); + if (player == null) { + return; + } + + LabyModPlayer labyModPlayer = new LabyModPlayer( + protocolService, + sender, + player, + packet.getVersion() + ); + + protocolService.handlePlayerJoin(labyModPlayer); + MinecraftServer.getGlobalEventHandler().call(new LabyModPlayerJoinEvent( + protocolService, + labyModPlayer + )); + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index 7b98ac1..01d1bec 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -3,7 +3,7 @@ rootProject.name = "labymod4-server-api" include(":api") include(":core") -sequenceOf("common", "bukkit", "bungeecord", "velocity").forEach { +sequenceOf("common", "bukkit", "bungeecord", "velocity", "minestom").forEach { val name = "server-$it" include(name)