diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobosses/bosses/EcoBoss.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobosses/bosses/EcoBoss.kt index 39f0185..69dbbe2 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobosses/bosses/EcoBoss.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobosses/bosses/EcoBoss.kt @@ -190,6 +190,18 @@ class EcoBoss( map } + private val commands: Map> = run { + val map = mutableMapOf>() + + for (value in BossLifecycle.values()) { + map[value] = config.getSubsections("messages.${value.name.lowercase()}").map { + LocalCommands.fromConfig(it) + } + } + + map + } + private val commandRewards: Map> = run { val map = mutableMapOf>() @@ -324,6 +336,8 @@ class EcoBoss( fun handleLifecycle(lifecycle: BossLifecycle, location: Location, entity: LivingEntity?) { sounds[lifecycle]?.play(location) messages[lifecycle]?.forEach { it.broadcast(location, entity?.topDamagers ?: emptyList()) } + commands[lifecycle]?.forEach { it.dispatch(location, entity?.topDamagers ?: emptyList()) } + } fun processRewards(event: BossKillEvent) { diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobosses/util/LocalCommands.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobosses/util/LocalCommands.kt new file mode 100644 index 0000000..fbc5e24 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecobosses/util/LocalCommands.kt @@ -0,0 +1,50 @@ +package com.willfp.ecobosses.util + +import com.willfp.eco.core.config.interfaces.Config +import com.willfp.eco.util.NumberUtils +import com.willfp.eco.util.formatEco +import com.willfp.eco.util.savedDisplayName +import com.willfp.ecobosses.EcoBossesPlugin +import org.bukkit.Bukkit +import org.bukkit.Location + +data class LocalCommands( + val commands: Iterable, +) { + fun dispatch(location: Location, topDamagers: List) { + val toDispatch = commands.toMutableList() + toDispatch.replaceAll { + var command = it + + for (i in 1..20) { + val damager = topDamagers.getOrNull(i - 1) + val damage = if (damager?.damage != null) NumberUtils.format(damager.damage) else + EcoBossesPlugin.instance.langYml.getFormattedString("na") + val player = if (damager?.uuid != null) Bukkit.getOfflinePlayer(damager.uuid).savedDisplayName else + EcoBossesPlugin.instance.langYml.getFormattedString("na") + + command = command.replace("%damage_${i}%", damage) + .replace("%damage_${i}_player%", player) + } + + command = command.replace("%x%", location.blockX.toString()) + .replace("%y%", location.blockY.toString()) + .replace("%z%", location.blockZ.toString()) + + command.formatEco() + } + + for (s in toDispatch) { + Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), s); + } + + } + + companion object { + fun fromConfig(config: Config): LocalCommands { + return LocalCommands( + config.getStrings("commands"), + ) + } + } +}