Skip to content

Commit

Permalink
Add PlayerDeathEvent
Browse files Browse the repository at this point in the history
Signed-off-by: Seppe Volkaerts <[email protected]>
  • Loading branch information
Cybermaxke committed Aug 18, 2024
1 parent 419f442 commit dfaac70
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 7 deletions.
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 dies.
*/
data class PlayerDeathEvent(
val player: Player,
) : Event
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import org.lanternpowered.terre.impl.network.packet.PlayerActivePacket
import org.lanternpowered.terre.impl.network.packet.PlayerChatMessagePacket
import org.lanternpowered.terre.impl.network.packet.PlayerCommandPacket
import org.lanternpowered.terre.impl.network.packet.PlayerDeathPacket
import org.lanternpowered.terre.impl.network.packet.PlayerHealthPacket
import org.lanternpowered.terre.impl.network.packet.PlayerHurtPacket
import org.lanternpowered.terre.impl.network.packet.PlayerInfoPacket
import org.lanternpowered.terre.impl.network.packet.PlayerInventorySlotPacket
Expand Down Expand Up @@ -118,6 +119,10 @@ internal interface ConnectionHandler {
return false
}

fun handle(packet: PlayerHealthPacket): Boolean {
return false
}

fun handle(packet: PlayerDeathPacket): Boolean {
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import org.lanternpowered.terre.impl.network.packet.SpeechBubblePacket
import org.lanternpowered.terre.impl.network.packet.StatusPacket
import org.lanternpowered.terre.impl.network.packet.ItemUpdateOwnerPacket
import org.lanternpowered.terre.impl.network.packet.NpcUpdatePacket
import org.lanternpowered.terre.impl.network.packet.PlayerHealthPacket
import org.lanternpowered.terre.impl.network.packet.PlayerInventorySlotPacket
import org.lanternpowered.terre.impl.network.packet.PlayerPvPPacket
import org.lanternpowered.terre.impl.network.packet.ProjectileUpdatePacket
Expand Down Expand Up @@ -87,6 +88,7 @@ internal object ConnectionHandlerBindings {
bind<PlayerUpdatePacket>(ConnectionHandler::handle)
bind<PlayerTeamPacket>(ConnectionHandler::handle)
bind<PlayerPvPPacket>(ConnectionHandler::handle)
bind<PlayerHealthPacket>(ConnectionHandler::handle)
bind<SimpleItemUpdatePacket>(ConnectionHandler::handle)
bind<InstancedItemUpdatePacket>(ConnectionHandler::handle)
bind<ShimmeredItemUpdatePacket>(ConnectionHandler::handle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import org.lanternpowered.terre.impl.network.packet.ItemUpdatePacket
import org.lanternpowered.terre.impl.network.packet.NpcUpdatePacket
import org.lanternpowered.terre.impl.network.packet.PlayerActivePacket
import org.lanternpowered.terre.impl.network.packet.PlayerChatMessagePacket
import org.lanternpowered.terre.impl.network.packet.PlayerHealthPacket
import org.lanternpowered.terre.impl.network.packet.PlayerInfoPacket
import org.lanternpowered.terre.impl.network.packet.PlayerInventorySlotPacket
import org.lanternpowered.terre.impl.network.packet.PlayerPvPPacket
Expand Down Expand Up @@ -143,6 +144,13 @@ internal class ServerPlayConnectionHandler(
return false // Forward
}

override fun handle(packet: PlayerHealthPacket): Boolean {
if (packet.playerId == player.playerId) {
return player.handleHealth(packet, clientConnection)
}
return true // Forward
}

override fun handle(packet: ProjectileUpdatePacket): Boolean {
val projectile = player.trackedProjectiles[packet.id]
projectile.active = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ internal class ClientPlayConnectionHandler(
player.serverConnection?.isWorldInitialized = true
player.position = packet.position.toFloat()

if (packet.respawnContext == PlayerSpawnPacket.Context.SpawningIntoWorld) {
if (packet.context == PlayerSpawnPacket.Context.SpawningIntoWorld) {
serverConnection?.send(packet)
val playerId = packet.playerId
val team = player.team
Expand Down Expand Up @@ -183,6 +183,16 @@ internal class ClientPlayConnectionHandler(
return true
}

override fun handle(packet: PlayerHealthPacket): Boolean {
if (packet.playerId == player.playerId) {
val connection = player.serverConnection?.ensureConnected()
if (connection != null) {
return player.handleHealth(packet, connection)
}
}
return false
}

override fun handle(packet: PlayerPvPPacket): Boolean {
if (packet.playerId != player.playerId)
return false // Forward
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal data class PlayerSpawnPacket(
val respawnTimeRemaining: Int,
val numberOfDeathsPvE: Int,
val numberOfDeathsPvP: Int,
val respawnContext: Context
val context: Context
) : Packet {

enum class Context {
Expand All @@ -41,7 +41,7 @@ internal val PlayerSpawnEncoder = PacketEncoder<PlayerSpawnPacket> { buf, packet
buf.writeIntLE(packet.respawnTimeRemaining)
buf.writeShortLE(packet.numberOfDeathsPvE)
buf.writeShortLE(packet.numberOfDeathsPvP)
buf.writeByte(packet.respawnContext.ordinal)
buf.writeByte(packet.context.ordinal)
}

internal val PlayerSpawnDecoder = PacketDecoder { buf ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal val PlayerSpawn238Encoder = PacketEncoder<PlayerSpawnPacket> { buf, pac
buf.writePlayerId(packet.playerId)
buf.writeShortVec2i(packet.position)
buf.writeIntLE(packet.respawnTimeRemaining)
buf.writeByte(packet.respawnContext.ordinal)
buf.writeByte(packet.context.ordinal)
}

internal val PlayerSpawn238Decoder = PacketDecoder { buf ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.lanternpowered.terre.dispatcher.launchAsync
import org.lanternpowered.terre.event.character.InitCharacterStorageEvent
import org.lanternpowered.terre.event.connection.PlayerLoginEvent
import org.lanternpowered.terre.event.connection.PlayerPostLoginEvent
import org.lanternpowered.terre.event.player.PlayerDeathEvent
import org.lanternpowered.terre.event.server.PlayerJoinServerEvent
import org.lanternpowered.terre.event.server.PlayerLeaveServerEvent
import org.lanternpowered.terre.impl.ProxyImpl
Expand All @@ -43,6 +44,7 @@ import org.lanternpowered.terre.impl.network.packet.NpcUpdatePacket
import org.lanternpowered.terre.impl.network.packet.PlayerActivePacket
import org.lanternpowered.terre.impl.network.packet.PlayerChatMessagePacket
import org.lanternpowered.terre.impl.network.packet.PlayerCommandPacket
import org.lanternpowered.terre.impl.network.packet.PlayerHealthPacket
import org.lanternpowered.terre.impl.network.packet.PlayerInfoPacket
import org.lanternpowered.terre.impl.network.packet.PlayerInventorySlotPacket
import org.lanternpowered.terre.impl.network.packet.PlayerPvPPacket
Expand Down Expand Up @@ -71,6 +73,7 @@ import org.lanternpowered.terre.util.AABB
import java.net.InetSocketAddress
import java.util.UUID
import java.util.concurrent.CompletableFuture
import kotlin.math.max

internal class PlayerImpl(
val clientConnection: Connection,
Expand Down Expand Up @@ -112,6 +115,8 @@ internal class PlayerImpl(

var forwardNextOwnerUpdate = false

private var health = -1

private var characterStoragePersistJob: Job? = null
private var characterStoragePersistQueue: Channel<Int>? = null

Expand Down Expand Up @@ -202,7 +207,7 @@ internal class PlayerImpl(
/**
* Deferred that will be updated when the player is cleaned up.
*/
var cleanedUp = CompletableFuture<Unit>()
private var cleanedUp = CompletableFuture<Unit>()

/**
* The status text that is currently being shown, and a counter of packets
Expand Down Expand Up @@ -489,7 +494,7 @@ internal class PlayerImpl(
serverClientUniqueId = event.clientUniqueId
}
connection.accept()
Terre.logger.debug("Successfully established connection to backend server: ${server.info}")
Terre.logger.debug { "Successfully established connection to backend server: ${server.info}" }
}, clientConnection.eventLoop)
}
}, clientConnection.eventLoop)
Expand Down Expand Up @@ -568,6 +573,7 @@ internal class PlayerImpl(
}

private fun resetClient() {
health = -1
for (player in trackedPlayers) {
if (player.active)
clientConnection.send(PlayerActivePacket(player.id, false))
Expand All @@ -593,4 +599,23 @@ internal class PlayerImpl(
}
trackedTeleportPylons.clear()
}

fun handleHealth(packet: PlayerHealthPacket, connection: Connection): Boolean {
val health = max(0, packet.current)
val previousHealth = this.health
this.health = health
if (previousHealth != -1 && health == 0 && previousHealth != 0) {
TerreEventBus.postAsyncWithFuture(PlayerDeathEvent(this))
.whenCompleteAsync({ _, exception ->
if (exception != null) {
Terre.logger.error("Failed to handle player death event", exception)
} else {
connection.send(packet)
}
}, connection.eventLoop)
return false // Do not forward
}
return true // Forward
}

}
2 changes: 1 addition & 1 deletion test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ dependencies {
implementation(project(":terre-proxy"))
implementation(project(":terre-portals"))
implementation(project(":terre-characters"))
implementation(project(":terre-tshock-users"))
// implementation(project(":terre-tshock-users"))
}
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 @@ -15,6 +15,7 @@ import org.lanternpowered.terre.event.chat.ServerChatEvent
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.proxy.ProxyInitializeEvent
import org.lanternpowered.terre.event.server.PlayerJoinServerEvent
import org.lanternpowered.terre.logger.Logger
Expand Down Expand Up @@ -49,6 +50,11 @@ class Test {
event.cancelled = true
}

@Subscribe
fun onPlayerDeath(event: PlayerDeathEvent) {
logger.info { "${event.player.name} died!" }
}

/*
@Subscribe
fun onPlayerJoinServer(event: PlayerJoinServerEvent) {
Expand Down

0 comments on commit dfaac70

Please sign in to comment.