Skip to content

Commit

Permalink
Add PlayerRespawnEvent
Browse files Browse the repository at this point in the history
Signed-off-by: Seppe Volkaerts <[email protected]>
  • Loading branch information
Cybermaxke committed Aug 19, 2024
1 parent 553445c commit 0833d96
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Terre
*
* Copyright (c) LanternPowered <https://www.lanternpowered.org>
* Copyright (c) contributors
*
* This work is licensed under the terms of the MIT License (MIT). For
* a copy, see 'LICENSE.txt' or <https://opensource.org/licenses/MIT>.
*/
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int>? = null
Expand Down
6 changes: 6 additions & 0 deletions test/src/main/kotlin/org/lanternpowered/terre/test/Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 0833d96

Please sign in to comment.