Skip to content

Commit

Permalink
feat: Improve MindustryAudience
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed Nov 3, 2023
1 parent 0ac46e7 commit 9cc0e65
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<MindustryAudience>) {
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>): 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<MindustryAudience>

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<MindustryAudience>) {
for (audience in audiences()) action.accept(audience)
}

override fun filterMindustryAudience(filter: Predicate<MindustryAudience>): MindustryAudience =
ForwardingMindustryAudience {
Iterables.filter(audiences(), filter::test)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,28 @@ 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

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) :
Expand All @@ -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 ->
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
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
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@ 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
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))
Expand All @@ -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)
Expand All @@ -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 {
Expand Down

0 comments on commit 9cc0e65

Please sign in to comment.