diff --git a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/adventure/MindustryAudience.kt b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/adventure/MindustryAudience.kt index ba596047..fa498533 100644 --- a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/adventure/MindustryAudience.kt +++ b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/adventure/MindustryAudience.kt @@ -17,44 +17,68 @@ */ package com.xpdustry.imperium.mindustry.adventure -import mindustry.gen.Call +import com.google.common.collect.Iterables +import java.util.function.Consumer +import java.util.function.Predicate import net.kyori.adventure.audience.Audience +import net.kyori.adventure.audience.ForwardingAudience import net.kyori.adventure.text.ComponentLike -fun Audience.sendInfoMessage(message: ComponentLike) = forEachAudience { - if (it is MindustryPlayerAudience) { - val renderer = MindustryComponentRenderer() - it.flattener.flatten(message.asComponent(), renderer) - Call.infoMessage(it.player.con, renderer.toString()) +interface MindustryAudience : Audience { + + fun sendInfoMessage(message: ComponentLike) = Unit + + fun showHUD(message: ComponentLike) = Unit + + fun hideHUD() = Unit + + fun sendAlert(message: ComponentLike) = Unit + + fun sendToast(icon: Char, message: ComponentLike) = Unit + + fun forEachMindustryAudience(action: Consumer) { + action.accept(this) } -} -fun Audience.showHUD(message: ComponentLike) = forEachAudience { - if (it is MindustryPlayerAudience) { - val renderer = MindustryComponentRenderer() - it.flattener.flatten(message.asComponent(), renderer) - Call.setHudTextReliable(it.player.con, renderer.toString()) + fun filterMindustryAudience(filter: Predicate): MindustryAudience { + return if (filter.test(this)) this else EMPTY } -} -fun Audience.hideHUD() = forEachAudience { - if (it is MindustryPlayerAudience) { - Call.hideHudText(it.player.con) + companion object { + val EMPTY: MindustryAudience = ForwardingMindustryAudience { emptyList() } } } -fun Audience.sendAnnouncement(message: ComponentLike) = forEachAudience { - if (it is MindustryPlayerAudience) { - val renderer = MindustryComponentRenderer() - it.flattener.flatten(message.asComponent(), renderer) - Call.announce(renderer.toString()) +internal fun interface ForwardingMindustryAudience : MindustryAudience, ForwardingAudience { + + override fun audiences(): Iterable + + override fun sendInfoMessage(message: ComponentLike) { + for (audience in audiences()) audience.sendInfoMessage(message) + } + + override fun showHUD(message: ComponentLike) { + for (audience in audiences()) audience.showHUD(message) + } + + override fun hideHUD() { + for (audience in audiences()) audience.hideHUD() } -} -fun Audience.sendWarningToast(icon: Char, message: ComponentLike) = forEachAudience { - if (it is MindustryPlayerAudience) { - val renderer = MindustryComponentRenderer() - it.flattener.flatten(message.asComponent(), renderer) - Call.warningToast(it.player.con, icon.code, renderer.toString()) + override fun sendAlert(message: ComponentLike) { + for (audience in audiences()) audience.sendAlert(message) } + + override fun sendToast(icon: Char, message: ComponentLike) { + for (audience in audiences()) audience.sendToast(icon, message) + } + + override fun forEachMindustryAudience(action: Consumer) { + for (audience in audiences()) action.accept(audience) + } + + override fun filterMindustryAudience(filter: Predicate): MindustryAudience = + ForwardingMindustryAudience { + Iterables.filter(audiences(), filter::test) + } } diff --git a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/adventure/MindustryAudienceProvider.kt b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/adventure/MindustryAudienceProvider.kt index 8ce3881c..4ef3ac99 100644 --- a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/adventure/MindustryAudienceProvider.kt +++ b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/adventure/MindustryAudienceProvider.kt @@ -27,8 +27,6 @@ import fr.xpdustry.distributor.api.util.Tristate import java.util.UUID import kotlin.jvm.optionals.getOrDefault import mindustry.game.EventType -import net.kyori.adventure.audience.Audience -import net.kyori.adventure.audience.ForwardingAudience import net.kyori.adventure.key.Key import net.kyori.adventure.platform.AudienceProvider import net.kyori.adventure.text.flattener.ComponentFlattener @@ -36,7 +34,21 @@ import net.kyori.adventure.text.flattener.ComponentFlattener internal lateinit var IMPERIUM_AUDIENCE_PROVIDER: MindustryAudienceProvider interface MindustryAudienceProvider : AudienceProvider { - fun player(playerId: MUUID): Audience = player(muuidToUuid(playerId.uuid)) + fun player(playerId: MUUID): MindustryAudience = player(muuidToUuid(playerId.uuid)) + + override fun all(): MindustryAudience + + override fun console(): MindustryAudience + + override fun players(): MindustryAudience + + override fun player(playerId: UUID): MindustryAudience + + override fun permission(permission: String): MindustryAudience + + override fun world(world: Key): MindustryAudience + + override fun server(serverName: String): MindustryAudience } class SimpleMindustryAudienceProvider(private val flattener: ComponentFlattener) : @@ -55,18 +67,17 @@ class SimpleMindustryAudienceProvider(private val flattener: ComponentFlattener) players.remove(muuidToUuid(event.player.uuid())) } - override fun close() = Unit - - override fun all(): Audience = ForwardingAudience { players.values + console } + override fun all(): MindustryAudience = ForwardingMindustryAudience { players.values + console } - override fun console(): Audience = console + override fun console(): MindustryAudience = console - override fun players(): Audience = ForwardingAudience { players.values } + override fun players(): MindustryAudience = ForwardingMindustryAudience { players.values } - override fun player(playerId: UUID): Audience = players[playerId] ?: Audience.empty() + override fun player(playerId: UUID): MindustryAudience = + players[playerId] ?: MindustryAudience.EMPTY - override fun permission(permission: String): Audience = - players().filterAudience { + override fun permission(permission: String): MindustryAudience = + players().filterMindustryAudience { it.pointers() .get(MUUID_POINTER) .map { muuid -> @@ -77,12 +88,14 @@ class SimpleMindustryAudienceProvider(private val flattener: ComponentFlattener) .getOrDefault(false) } - override fun world(world: Key): Audience = + override fun world(world: Key): MindustryAudience = if (world.namespace() == "mindustry" && world.value() == "local") players() - else Audience.empty() + else MindustryAudience.EMPTY - override fun server(serverName: String): Audience = - if (serverName == "local") players() else Audience.empty() + override fun server(serverName: String): MindustryAudience = + if (serverName == "local") players() else MindustryAudience.EMPTY override fun flattener(): ComponentFlattener = flattener + + override fun close() = Unit } diff --git a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/adventure/MindustryComponentRenderer.kt b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/adventure/MindustryComponentRenderer.kt index b72e3599..77f83fec 100644 --- a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/adventure/MindustryComponentRenderer.kt +++ b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/adventure/MindustryComponentRenderer.kt @@ -20,7 +20,7 @@ package com.xpdustry.imperium.mindustry.adventure import net.kyori.adventure.text.flattener.FlattenerListener import net.kyori.adventure.text.format.Style -class MindustryComponentRenderer : FlattenerListener { +internal class MindustryComponentRenderer : FlattenerListener { private val builder = StringBuilder() override fun pushStyle(style: Style) { @@ -32,9 +32,7 @@ class MindustryComponentRenderer : FlattenerListener { } override fun popStyle(style: Style) { - if (style.color() != null) { - builder.append("[]") - } + if (style.color() != null) builder.append("[]") } override fun toString(): String = builder.toString() diff --git a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/adventure/MindustryConsoleAudience.kt b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/adventure/MindustryConsoleAudience.kt index b2289aa2..6f62e1a0 100644 --- a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/adventure/MindustryConsoleAudience.kt +++ b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/adventure/MindustryConsoleAudience.kt @@ -18,7 +18,6 @@ package com.xpdustry.imperium.mindustry.adventure import arc.util.Log -import net.kyori.adventure.audience.Audience import net.kyori.adventure.audience.MessageType import net.kyori.adventure.identity.Identity import net.kyori.adventure.text.Component @@ -26,7 +25,7 @@ import net.kyori.adventure.text.flattener.ComponentFlattener import net.kyori.adventure.text.serializer.ansi.ANSIComponentSerializer import net.kyori.ansi.ColorLevel -class MindustryConsoleAudience(flattener: ComponentFlattener) : Audience { +class MindustryConsoleAudience(flattener: ComponentFlattener) : MindustryAudience { private val renderer = ANSIComponentSerializer.builder() .flattener(flattener) diff --git a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/adventure/MindustryPlayerAudience.kt b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/adventure/MindustryPlayerAudience.kt index 4339fdd4..6a1faa49 100644 --- a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/adventure/MindustryPlayerAudience.kt +++ b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/adventure/MindustryPlayerAudience.kt @@ -27,7 +27,6 @@ import mindustry.game.Team import mindustry.gen.Call import mindustry.gen.Player import mindustry.gen.Sounds -import net.kyori.adventure.audience.Audience import net.kyori.adventure.audience.MessageType import net.kyori.adventure.identity.Identity import net.kyori.adventure.key.Key @@ -35,15 +34,16 @@ import net.kyori.adventure.pointer.Pointer import net.kyori.adventure.pointer.Pointers import net.kyori.adventure.sound.Sound as AdventureSound import net.kyori.adventure.text.Component +import net.kyori.adventure.text.ComponentLike import net.kyori.adventure.text.flattener.ComponentFlattener val MUUID_POINTER = Pointer.pointer(MUUID::class.java, Key.key("distributor", "muuid")) val TEAM_POINTER = Pointer.pointer(Team::class.java, Key.key("mindustry", "team")) class MindustryPlayerAudience( - internal val player: Player, - internal val flattener: ComponentFlattener -) : Audience { + private val player: Player, + private val flattener: ComponentFlattener +) : MindustryAudience { private val pointers: Pointers = Pointers.builder() .withStatic(MUUID_POINTER, MUUID.of(player)) @@ -61,12 +61,6 @@ class MindustryPlayerAudience( player.sendMessage(renderer.toString()) } - override fun sendActionBar(message: Component) { - val renderer = MindustryComponentRenderer() - flattener.flatten(message, renderer) - Call.setHudTextReliable(player.con, renderer.toString()) - } - override fun playSound(sound: AdventureSound, emitter: AdventureSound.Emitter) { if (emitter == AdventureSound.Emitter.self()) { playSound(sound) @@ -85,6 +79,34 @@ class MindustryPlayerAudience( override fun pointers(): Pointers = pointers + override fun sendInfoMessage(message: ComponentLike) { + val renderer = MindustryComponentRenderer() + flattener.flatten(message.asComponent(), renderer) + Call.infoMessage(player.con, renderer.toString()) + } + + override fun showHUD(message: ComponentLike) { + val renderer = MindustryComponentRenderer() + flattener.flatten(message.asComponent(), renderer) + Call.setHudTextReliable(player.con, renderer.toString()) + } + + override fun hideHUD() { + Call.hideHudText(player.con) + } + + override fun sendAlert(message: ComponentLike) { + val renderer = MindustryComponentRenderer() + flattener.flatten(message.asComponent(), renderer) + Call.announce(renderer.toString()) + } + + override fun sendToast(icon: Char, message: ComponentLike) { + val renderer = MindustryComponentRenderer() + flattener.flatten(message.asComponent(), renderer) + Call.warningToast(player.con, icon.code, renderer.toString()) + } + companion object { private val SOUND_LOOKUP = buildMap { Sounds::class.staticProperties.forEach {