Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
samolego committed Feb 9, 2022
1 parent 8f17e7b commit b4db310
Showing 1 changed file with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.quiltservertools.blockbotdiscord.config

import com.google.gson.JsonParser
import com.mojang.authlib.GameProfile
import com.uchuhimo.konf.Config
import com.uchuhimo.konf.ConfigSpec
Expand All @@ -11,13 +12,15 @@ import io.github.quiltservertools.blockbotapi.sender.PlayerMessageSender
import io.github.quiltservertools.blockbotdiscord.utility.getTextures
import io.github.quiltservertools.blockbotdiscord.utility.literal
import io.github.quiltservertools.blockbotdiscord.utility.summary
import net.fabricmc.loader.api.FabricLoader
import net.minecraft.advancement.Advancement
import net.minecraft.server.MinecraftServer
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.text.LiteralText
import net.minecraft.text.MutableText
import net.minecraft.text.Text
import net.minecraft.util.Formatting
import java.util.*

object ChatRelaySpec : ConfigSpec() {
val allowMentions by required<Boolean>()
Expand Down Expand Up @@ -55,9 +58,12 @@ object ChatRelaySpec : ConfigSpec() {
val webhookName by required<String>()
val webhookAvatar by required<String>()
val playerAvatarUrl by required<String>()
val tailorAvatarUrl by required<String>()
}
}

val usingFabricTailor: Boolean = FabricLoader.getInstance().isModLoaded("fabrictailor")

fun Config.formatDiscordMessage(sender: MessageSender, message: String): String =
formatDiscordRelayMessage(sender, message, config[ChatRelaySpec.DiscordMessageFormatSpec.messageFormat])

Expand Down Expand Up @@ -185,13 +191,30 @@ fun Config.getReplyMsg(
), server
)

fun Config.getWebhookChatRelayAvatar(gameProfile: GameProfile): String =
PlaceholderAPI.parsePredefinedText(
fun Config.getWebhookChatRelayAvatar(gameProfile: GameProfile): String {
val textures = gameProfile.getTextures()

if (usingFabricTailor && textures != null) {
// FabricTailor is active, use its textures
val decode = JsonParser.parseString(String(Base64.getDecoder().decode(textures)))
val skinUrl = decode.asJsonObject.get("textures").asJsonObject.get("SKIN").asJsonObject.get("url").asString

return PlaceholderAPI.parsePredefinedText(
this[ChatRelaySpec.WebhookSpec.tailorAvatarUrl].literal(),
PlaceholderAPI.ALT_PLACEHOLDER_PATTERN_CUSTOM,
mapOf(
"texture" to skinUrl.split("/").last().literal()
)
).string
}

return PlaceholderAPI.parsePredefinedText(
this[ChatRelaySpec.WebhookSpec.playerAvatarUrl].literal(),
PlaceholderAPI.ALT_PLACEHOLDER_PATTERN_CUSTOM,
mapOf(
"uuid" to gameProfile.id.toString().literal(),
"username" to gameProfile.name.literal(),
"texture" to (gameProfile.getTextures()?.literal() ?: LiteralText(""))
"texture" to (textures?.literal() ?: LiteralText(""))
)
).string
}

0 comments on commit b4db310

Please sign in to comment.