diff --git a/proxy/src/main/kotlin/org/lanternpowered/terre/event/player/PlayerRespawnEvent.kt b/proxy/src/main/kotlin/org/lanternpowered/terre/event/player/PlayerRespawnEvent.kt new file mode 100644 index 0000000..feff848 --- /dev/null +++ b/proxy/src/main/kotlin/org/lanternpowered/terre/event/player/PlayerRespawnEvent.kt @@ -0,0 +1,20 @@ +/* + * Terre + * + * Copyright (c) LanternPowered + * Copyright (c) contributors + * + * This work is licensed under the terms of the MIT License (MIT). For + * a copy, see 'LICENSE.txt' or . + */ +package org.lanternpowered.terre.event.player + +import org.lanternpowered.terre.Player +import org.lanternpowered.terre.event.Event + +/** + * An event that is thrown when a player respawns. + */ +data class PlayerRespawnEvent( + val player: Player, +) : Event diff --git a/proxy/src/main/kotlin/org/lanternpowered/terre/impl/network/client/ClientPlayConnectionHandler.kt b/proxy/src/main/kotlin/org/lanternpowered/terre/impl/network/client/ClientPlayConnectionHandler.kt index d31d51c..8155163 100644 --- a/proxy/src/main/kotlin/org/lanternpowered/terre/impl/network/client/ClientPlayConnectionHandler.kt +++ b/proxy/src/main/kotlin/org/lanternpowered/terre/impl/network/client/ClientPlayConnectionHandler.kt @@ -14,6 +14,7 @@ import io.netty.util.concurrent.ScheduledFuture import org.lanternpowered.terre.Team import org.lanternpowered.terre.event.player.PlayerChangePvPEnabledEvent import org.lanternpowered.terre.event.player.PlayerChangeTeamEvent +import org.lanternpowered.terre.event.player.PlayerRespawnEvent import org.lanternpowered.terre.impl.Terre import org.lanternpowered.terre.impl.command.CommandManagerImpl import org.lanternpowered.terre.impl.event.TerreEventBus @@ -152,6 +153,18 @@ internal class ClientPlayConnectionHandler( return true } + if (player.health == 0 && packet.context == PlayerSpawnPacket.Context.ReviveFromDeath) { + TerreEventBus.postAsyncWithFuture(PlayerRespawnEvent(player)) + .whenCompleteAsync({ _, exception -> + if (exception != null) { + Terre.logger.error("Failed to handle player death event", exception) + } else { + serverConnection?.send(packet) + } + }, player.clientConnection.eventLoop) + return true // Do not forward + } + return false // Forward } diff --git a/proxy/src/main/kotlin/org/lanternpowered/terre/impl/player/PlayerImpl.kt b/proxy/src/main/kotlin/org/lanternpowered/terre/impl/player/PlayerImpl.kt index 9ef01d3..e7dd31b 100644 --- a/proxy/src/main/kotlin/org/lanternpowered/terre/impl/player/PlayerImpl.kt +++ b/proxy/src/main/kotlin/org/lanternpowered/terre/impl/player/PlayerImpl.kt @@ -115,7 +115,7 @@ internal class PlayerImpl( var forwardNextOwnerUpdate = false - private var health = -1 + var health = -1 private var characterStoragePersistJob: Job? = null private var characterStoragePersistQueue: Channel? = null diff --git a/test/src/main/kotlin/org/lanternpowered/terre/test/Test.kt b/test/src/main/kotlin/org/lanternpowered/terre/test/Test.kt index 65742e1..f9bbdcc 100644 --- a/test/src/main/kotlin/org/lanternpowered/terre/test/Test.kt +++ b/test/src/main/kotlin/org/lanternpowered/terre/test/Test.kt @@ -16,6 +16,7 @@ import org.lanternpowered.terre.event.connection.PlayerPostLoginEvent import org.lanternpowered.terre.event.player.PlayerChangePvPEnabledEvent import org.lanternpowered.terre.event.player.PlayerChangeTeamEvent import org.lanternpowered.terre.event.player.PlayerDeathEvent +import org.lanternpowered.terre.event.player.PlayerRespawnEvent import org.lanternpowered.terre.event.proxy.ProxyInitializeEvent import org.lanternpowered.terre.event.server.PlayerJoinServerEvent import org.lanternpowered.terre.logger.Logger @@ -55,6 +56,11 @@ class Test { logger.info { "${event.player.name} died!" } } + @Subscribe + fun onPlayerRespawn(event: PlayerRespawnEvent) { + logger.info { "${event.player.name} respawned!" } + } + /* @Subscribe fun onPlayerJoinServer(event: PlayerJoinServerEvent) {