Skip to content

Commit

Permalink
Merge pull request #22 from timinc-cobble/tmetcalfe89/issue20
Browse files Browse the repository at this point in the history
Tmetcalfe89/issue20
  • Loading branch information
tmetcalfe89 authored Oct 25, 2023
2 parents d0331b0 + 7e8eb63 commit 72ff2db
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package us.timinc.mc.cobblemon.spawnnotification

enum class DespawnReason(val translationKey: String) {
FAINTED("fainted"),
CAPTURED("captured"),
DESPAWNED("despawned")
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ 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
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"
Expand All @@ -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)
}
Expand All @@ -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<PokemonEntity>
) {
val pokemon = evt.entity.pokemon
Expand Down Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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) }
}
}
3 changes: 3 additions & 0 deletions src/main/resources/assets/spawn_notification/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 72ff2db

Please sign in to comment.