Skip to content

Commit

Permalink
Fix logic
Browse files Browse the repository at this point in the history
  • Loading branch information
wtlgo committed Nov 26, 2023
1 parent 0d43db8 commit d659d43
Showing 1 changed file with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,28 @@ import dev.mikchan.mcnp.motd.MOTDPlugin
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.server.ServerListPingEvent
import org.bukkit.util.CachedServerIcon

internal class MOTDListener(private val plugin: MOTDPlugin) : Listener {
@EventHandler(ignoreCancelled = true)
fun onServerListPingEvent(event: ServerListPingEvent) {
if (!plugin.config.enabled) return
val motd = plugin.motdManager.getRandom() ?: return
var icon: CachedServerIcon? = null

if (motd.firstLine != null || motd.secondLine != null) {
val firstLine = motd.firstLine?.let { plugin.formatter.format(it) } ?: ""
val secondLine = motd.secondLine?.let { plugin.formatter.format(it) } ?: ""
event.motd = "${firstLine}\n${secondLine}"
}

val image = motd.image?.let {
if (plugin.config.randomImages) {
plugin.imageManager.getRandom()
} else {
null
plugin.motdManager.getRandom()?.apply {
if (firstLine != null || secondLine != null) {
val firstLine = firstLine?.let { plugin.formatter.format(it) } ?: ""
val secondLine = secondLine?.let { plugin.formatter.format(it) } ?: ""
event.motd = "${firstLine}\n${secondLine}"
}
}

if (image != null) {
event.setServerIcon(image)
icon = image
}

icon ?: if (plugin.config.randomImages) {
plugin.imageManager.getRandom()
} else {
null
}?.apply { event.setServerIcon(this) }
}
}

0 comments on commit d659d43

Please sign in to comment.