Skip to content

Commit

Permalink
Fix port issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Erdragh committed Apr 27, 2024
1 parent 437d576 commit 0309910
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class MinecraftHandler(private val server: MinecraftServer) : ListenerAdapter()
fun sendChatToDiscord(player: ServerPlayer?, message: Component) {
if (shuttingDown.get()) return

val attachments = message.toFlatList().mapNotNull {
val attachments = message.toFlatList(message.style).mapNotNull {
it.style.hoverEvent
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ package dev.erdragh.astralbot.util
import dev.erdragh.astralbot.config.AstralBotConfig
import dev.erdragh.astralbot.config.AstralBotTextConfig
import net.minecraft.ChatFormatting
import net.minecraft.network.chat.ClickEvent
import net.minecraft.network.chat.Component
import net.minecraft.network.chat.HoverEvent
import net.minecraft.network.chat.MutableComponent
import net.minecraft.network.chat.*
import org.commonmark.node.*
import org.commonmark.renderer.NodeRenderer
import java.util.Stack
import java.util.*

class ComponentRenderer : AbstractVisitor(), NodeRenderer {
companion object {
Expand All @@ -25,7 +22,7 @@ class ComponentRenderer : AbstractVisitor(), NodeRenderer {
}
}

private var currentComponent: MutableComponent = Component.empty()
private var currentComponent: MutableComponent = TextComponent("")
private val prefixes: Stack<Component> = Stack()
private var listHolder: ListHolder? = null
private var shouldAddBlock: Boolean = false
Expand Down Expand Up @@ -77,14 +74,14 @@ class ComponentRenderer : AbstractVisitor(), NodeRenderer {
if (heading == null) return

block()
childIntoCurrent(Component.empty().withStyle(ChatFormatting.BOLD)) {
childIntoCurrent(TextComponent("").withStyle(ChatFormatting.BOLD)) {
visitChildren(heading)
}
block()
}

override fun visit(emphasis: Emphasis?) {
childIntoCurrent(Component.empty().withStyle(ChatFormatting.ITALIC)) {
childIntoCurrent(TextComponent("").withStyle(ChatFormatting.ITALIC)) {
super.visit(emphasis)
}
}
Expand All @@ -100,7 +97,7 @@ class ComponentRenderer : AbstractVisitor(), NodeRenderer {
}

override fun visit(strongEmphasis: StrongEmphasis?) {
childIntoCurrent(Component.empty().withStyle(ChatFormatting.BOLD)) {
childIntoCurrent(TextComponent("").withStyle(ChatFormatting.BOLD)) {
super.visit(strongEmphasis)
}
}
Expand All @@ -126,9 +123,9 @@ class ComponentRenderer : AbstractVisitor(), NodeRenderer {
}

override fun visit(blockQuote: BlockQuote?) {
prefixes.push(Component.literal("> ").withStyle(ChatFormatting.DARK_GRAY).withStyle { it.withItalic(false) })
prefixes.push(TextComponent("> ").withStyle(ChatFormatting.DARK_GRAY).withStyle { it.withItalic(false) })
block()
childIntoCurrent(Component.empty().withStyle(ChatFormatting.GRAY, ChatFormatting.ITALIC)) {
childIntoCurrent(TextComponent("").withStyle(ChatFormatting.GRAY, ChatFormatting.ITALIC)) {
visitChildren(blockQuote)
}
prefixes.pop()
Expand All @@ -138,19 +135,19 @@ class ComponentRenderer : AbstractVisitor(), NodeRenderer {
override fun visit(code: Code?) {
if (code == null) return
append(
Component.literal("`${code.literal}`")
TextComponent("`${code.literal}`")
.withStyle(ChatFormatting.YELLOW)
)
}

override fun visit(fencedCodeBlock: FencedCodeBlock?) {
if (fencedCodeBlock == null) return
append(Component.literal(fencedCodeBlock.literal).withStyle(ChatFormatting.YELLOW))
append(TextComponent(fencedCodeBlock.literal).withStyle(ChatFormatting.YELLOW))
}

override fun visit(indentedCodeBlock: IndentedCodeBlock?) {
if (indentedCodeBlock == null) return
append(Component.literal(indentedCodeBlock.literal).withStyle(ChatFormatting.YELLOW))
append(TextComponent(indentedCodeBlock.literal).withStyle(ChatFormatting.YELLOW))
}

override fun visit(bulletList: BulletList?) {
Expand Down Expand Up @@ -193,7 +190,7 @@ class ComponentRenderer : AbstractVisitor(), NodeRenderer {
val contentIndent = listItem.contentIndent
append(marker)
append(" ".repeat(contentIndent - marker.length))
prefixes.push(Component.literal(" ".repeat(contentIndent)))
prefixes.push(TextComponent(" ".repeat(contentIndent)))

if (listItem.firstChild != null) {
// not an empty list
Expand Down Expand Up @@ -226,17 +223,17 @@ class ComponentRenderer : AbstractVisitor(), NodeRenderer {
}

private fun formatLink(node: Node?, title: String?, destination: String) {
childIntoCurrent(Component.empty()
childIntoCurrent(TextComponent("")
.withStyle(ChatFormatting.BLUE, ChatFormatting.UNDERLINE)
.withStyle {
if (AstralBotConfig.urlAllowed(destination)) {
it
.withClickEvent(ClickEvent(ClickEvent.Action.OPEN_URL, destination))
.withHoverEvent(HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.literal(destination)))
.withHoverEvent(HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent(destination)))
} else {
it
.withColor(ChatFormatting.RED)
.withHoverEvent(HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.literal(AstralBotTextConfig.GENERIC_BLOCKED.get())))
.withHoverEvent(HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent(AstralBotTextConfig.GENERIC_BLOCKED.get())))
}
}
) {
Expand All @@ -260,6 +257,6 @@ class ComponentRenderer : AbstractVisitor(), NodeRenderer {
}

private fun append(literal: String) {
append(Component.literal(literal))
append(TextComponent(literal))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import dev.erdragh.astralbot.config.AstralBotConfig
import dev.erdragh.astralbot.config.AstralBotTextConfig
import net.dv8tion.jda.api.EmbedBuilder
import net.dv8tion.jda.api.entities.MessageEmbed
import net.minecraft.network.chat.ClickEvent
import net.minecraft.network.chat.Component
import net.minecraft.network.chat.HoverEvent
import net.minecraft.network.chat.MutableComponent
import net.minecraft.network.chat.*
import net.minecraft.world.entity.EntityType
import net.minecraft.world.entity.player.Player
import net.minecraft.world.item.ItemStack
Expand All @@ -26,7 +23,7 @@ val urlPattern: Pattern = Pattern.compile(
)

fun formatMarkdownToComponent(md: String): MutableComponent {
if (!AstralBotConfig.ENABLE_MARKDOWN_PARSING.get()) return Component.literal(md)
if (!AstralBotConfig.ENABLE_MARKDOWN_PARSING.get()) return TextComponent(md)

val parser = Parser.builder().build()
val parsed = parser.parse(md)
Expand All @@ -36,7 +33,7 @@ fun formatMarkdownToComponent(md: String): MutableComponent {
}

fun formatComponentToMarkdown(comp: Component): String {
return comp.toFlatList()
return comp.toFlatList(comp.style)
.map {
var formatted = it.string

Expand Down Expand Up @@ -81,7 +78,7 @@ fun formatHoverText(text: Component): MessageEmbed {
fun formatHoverItems(stack: ItemStack, knownItems: MutableList<ItemStack>, player: Player?): MessageEmbed? {
if (knownItems.contains(stack)) return null
knownItems.add(stack)
val tooltip = stack.getTooltipLines(player, TooltipFlag.NORMAL).map(::formatComponentToMarkdown)
val tooltip = stack.getTooltipLines(player, TooltipFlag.Default.NORMAL).map(::formatComponentToMarkdown)
return EmbedBuilder()
.setTitle("${tooltip[0]} ${if (stack.count > 1) "(${stack.count})" else ""}")
.setDescription(tooltip.drop(1).let {
Expand Down
4 changes: 2 additions & 2 deletions fabric/src/main/kotlin/dev/erdragh/astralbot/fabric/BotMod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package dev.erdragh.astralbot.fabric
import dev.erdragh.astralbot.*
import dev.erdragh.astralbot.commands.minecraft.registerMinecraftCommands
import dev.erdragh.astralbot.config.AstralBotConfig
import dev.erdragh.astralbot.fabric.event.ServerMessageEvents
import dev.erdragh.astralbot.config.AstralBotTextConfig
import dev.erdragh.astralbot.fabric.event.ServerMessageEvents
import dev.erdragh.astralbot.handlers.DiscordMessageComponent
import net.fabricmc.api.ModInitializer
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback
Expand All @@ -29,7 +29,7 @@ object BotMod : ModInitializer {
}

ServerMessageEvents.CHAT_MESSAGE.register { message, player, _ ->
minecraftHandler?.sendChatToDiscord(player, message.serverContent())
minecraftHandler?.sendChatToDiscord(player, message)
}
ServerMessageEvents.GAME_MESSAGE.register { _, message, _ ->
if (message !is DiscordMessageComponent) {
Expand Down

0 comments on commit 0309910

Please sign in to comment.