diff --git a/src/main/kotlin/us/timinc/mc/cobblemon/spawnnotification/DespawnReason.kt b/src/main/kotlin/us/timinc/mc/cobblemon/spawnnotification/DespawnReason.kt new file mode 100644 index 0000000..a39cf8d --- /dev/null +++ b/src/main/kotlin/us/timinc/mc/cobblemon/spawnnotification/DespawnReason.kt @@ -0,0 +1,7 @@ +package us.timinc.mc.cobblemon.spawnnotification + +enum class DespawnReason(val translationKey: String) { + FAINTED("fainted"), + CAPTURED("captured"), + DESPAWNED("despawned") +} \ No newline at end of file diff --git a/src/main/kotlin/us/timinc/mc/cobblemon/spawnnotification/SpawnNotification.kt b/src/main/kotlin/us/timinc/mc/cobblemon/spawnnotification/SpawnNotification.kt index b2e154c..f68573f 100644 --- a/src/main/kotlin/us/timinc/mc/cobblemon/spawnnotification/SpawnNotification.kt +++ b/src/main/kotlin/us/timinc/mc/cobblemon/spawnnotification/SpawnNotification.kt @@ -6,6 +6,7 @@ import com.cobblemon.mod.common.entity.pokemon.PokemonEntity import com.cobblemon.mod.common.pokemon.Pokemon import com.cobblemon.mod.common.util.playSoundServer import net.fabricmc.api.ModInitializer +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents import net.minecraft.sound.SoundCategory import net.minecraft.sound.SoundEvent import net.minecraft.text.Text @@ -13,6 +14,7 @@ import net.minecraft.util.Identifier import net.minecraft.util.math.BlockPos import net.minecraft.world.World import us.timinc.mc.cobblemon.spawnnotification.config.SpawnNotificationConfig +import us.timinc.mc.cobblemon.spawnnotification.util.Broadcast object SpawnNotification : ModInitializer { const val MOD_ID = "spawn_notification" @@ -31,7 +33,7 @@ object SpawnNotification : ModInitializer { val pokemon = evt.entity.pokemon if (pokemon.isPlayerOwned()) return@subscribe - broadcastSpawn(config, evt) + broadcastSpawn(evt) if (config.playShinySound && pokemon.shiny) { playShinySound(evt.ctx.world, evt.ctx.position) } @@ -41,10 +43,32 @@ object SpawnNotification : ModInitializer { playShinySound(evt.pokemonEntity.world, evt.pokemonEntity.blockPos) } } + CobblemonEvents.POKEMON_CAPTURED.subscribe { evt -> + broadcastDespawn(evt.pokemon, DespawnReason.CAPTURED) + } + CobblemonEvents.POKEMON_FAINTED.subscribe { evt -> + broadcastDespawn(evt.pokemon, DespawnReason.FAINTED) + } + ServerEntityEvents.ENTITY_UNLOAD.register{ entity, _ -> + if (entity !is PokemonEntity) return@register + + broadcastDespawn(entity.pokemon, DespawnReason.DESPAWNED) + } + } + + private fun broadcastDespawn( + pokemon: Pokemon, + reason: DespawnReason + ) { + if (config.broadcastDespawns && (pokemon.shiny || pokemon.isLegendary())) { + Broadcast.broadcastMessage(Text.translatable( + "$MOD_ID.notification.${reason.translationKey}", + pokemon.getDisplayName() + )) + } } private fun broadcastSpawn( - config: SpawnNotificationConfig, evt: SpawnEvent ) { val pokemon = evt.entity.pokemon @@ -85,15 +109,9 @@ object SpawnNotification : ModInitializer { Text.translatable("dimension.${level.dimensionKey.value.toTranslationKey()}") )) - evt.entity.server!!.worlds.forEach { world -> - world.players.forEach { player -> - player.sendMessage(messageComponent) - } - } + Broadcast.broadcastMessage(messageComponent) } else { - level.players.forEach { player -> - player.sendMessage(messageComponent) - } + Broadcast.broadcastMessage(level, messageComponent) } } diff --git a/src/main/kotlin/us/timinc/mc/cobblemon/spawnnotification/config/SpawnNotificationConfig.kt b/src/main/kotlin/us/timinc/mc/cobblemon/spawnnotification/config/SpawnNotificationConfig.kt index 171c5c1..869bce1 100644 --- a/src/main/kotlin/us/timinc/mc/cobblemon/spawnnotification/config/SpawnNotificationConfig.kt +++ b/src/main/kotlin/us/timinc/mc/cobblemon/spawnnotification/config/SpawnNotificationConfig.kt @@ -20,6 +20,7 @@ class SpawnNotificationConfig { // @Comment("Whether or not to play the PLA shiny sound when a player sends out a shiny") val playShinySoundPlayer = false val announceCrossDimensions = false + val broadcastDespawns = false class Builder { companion object { diff --git a/src/main/kotlin/us/timinc/mc/cobblemon/spawnnotification/util/Broadcast.kt b/src/main/kotlin/us/timinc/mc/cobblemon/spawnnotification/util/Broadcast.kt new file mode 100644 index 0000000..86db5a7 --- /dev/null +++ b/src/main/kotlin/us/timinc/mc/cobblemon/spawnnotification/util/Broadcast.kt @@ -0,0 +1,17 @@ +package us.timinc.mc.cobblemon.spawnnotification.util + +import com.cobblemon.mod.common.util.server +import net.minecraft.server.world.ServerWorld +import net.minecraft.text.Text + +object Broadcast { + fun broadcastMessage(message: Text) { + val serverInstance = server()?:return + serverInstance.sendMessage(message) + serverInstance.playerManager.playerList.forEach { it.sendMessage(message) } + } + + fun broadcastMessage(level: ServerWorld, message: Text) { + level.players.forEach { it.sendMessage(message) } + } +} \ No newline at end of file diff --git a/src/main/resources/assets/spawn_notification/lang/en_us.json b/src/main/resources/assets/spawn_notification/lang/en_us.json index bd0dd97..1a11472 100644 --- a/src/main/resources/assets/spawn_notification/lang/en_us.json +++ b/src/main/resources/assets/spawn_notification/lang/en_us.json @@ -5,6 +5,9 @@ "spawn_notification.notification.coords": " at (%d, %d, %d)", "spawn_notification.notification.biome": " in a %d biome", "spawn_notification.notification.dimension": " in %d", + "spawn_notification.notification.fainted": "%d fainted", + "spawn_notification.notification.captured": "%d was captured", + "spawn_notification.notification.despawned": "%d despawned", "spawn_notification.shinysoundsubtitle": "Shiny spawned", "dimension.minecraft.overworld": "the Overworld", "dimension.minecraft.the_nether": "the Nether",