Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #26 #39

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}