Skip to content

Commit

Permalink
Parse TextComponent hover values depending on action
Browse files Browse the repository at this point in the history
Also turn all string actions to lowercase, otherwise `byName` does not find them
  • Loading branch information
srockw committed Nov 29, 2023
1 parent b28e8ab commit 26d6230
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions src/main/kotlin/com/chattriggers/ctjs/api/message/TextComponent.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.chattriggers.ctjs.api.message

import com.chattriggers.ctjs.MCEntity
import com.chattriggers.ctjs.api.entity.Entity
import com.chattriggers.ctjs.api.inventory.Item
import com.chattriggers.ctjs.api.inventory.ItemType
import com.chattriggers.ctjs.api.render.Renderer
import gg.essential.universal.UChat
import net.minecraft.item.ItemStack
import net.minecraft.text.*
import net.minecraft.util.Formatting
import java.util.*
Expand Down Expand Up @@ -104,7 +109,7 @@ class TextComponent : Text {
fun setClickAction(value: Any?) = apply {
clickAction = when (value) {
is ClickEvent.Action -> value
is String -> ClickEvent.Action.byName(value)
is String -> ClickEvent.Action.byName(value.lowercase())
null -> null
else -> error(
"TextComponent.setClickAction() expects a String, ClickEvent.Action, or null, but got " +
Expand Down Expand Up @@ -149,7 +154,7 @@ class TextComponent : Text {
* @param value the click value
*/
fun setClick(action: String, value: String?) = apply {
setClick(ClickEvent.Action.byName(action), value)
setClick(ClickEvent.Action.byName(action.lowercase()), value)
}

/**
Expand All @@ -169,7 +174,7 @@ class TextComponent : Text {
fun setHoverAction(value: Any?) = apply {
hoverAction = when (value) {
is HoverEvent.Action<*> -> value
is String -> HoverEvent.Action.byName(value)
is String -> HoverEvent.Action.byName(value.lowercase())
null -> null
else -> error(
"TextComponent.setHoverAction() expects a String, HoverEvent.Action, or null, but got " +
Expand All @@ -190,9 +195,14 @@ class TextComponent : Text {
* Sets the value to be used by the hover action. The value is interpreted according to [hoverAction]
*/
fun setHoverValue(value: Any?) = apply {
hoverValue = if (hoverAction == HoverEvent.Action.SHOW_TEXT && value != null) {
from(value)
} else value
hoverValue = value?.let {
when (hoverAction) {
HoverEvent.Action.SHOW_TEXT -> from(value)
HoverEvent.Action.SHOW_ITEM -> parseItemContent(value)
HoverEvent.Action.SHOW_ENTITY -> parseEntityContent(value)
else -> value
}
}

reInstanceHover()
}
Expand All @@ -215,7 +225,7 @@ class TextComponent : Text {
* @param value the hover value
*/
fun setHover(action: String, value: Any?) = apply {
setHover(HoverEvent.Action.byName(action) as HoverEvent.Action<*>, value)
setHover(HoverEvent.Action.byName(action.lowercase()) as HoverEvent.Action<*>, value)
}

/**
Expand Down Expand Up @@ -252,6 +262,23 @@ class TextComponent : Text {
Message(this).actionBar()
}

private fun parseItemContent(obj: Any?) = when (obj) {
is ItemStack -> obj
is Item -> obj.toMC()
is String -> ItemType(obj).asItem().toMC()
else -> null
}?.let(HoverEvent::ItemStackContent)

private fun parseEntityContent(obj: Any?): HoverEvent.EntityContent? {
if (obj is String) return HoverEvent.EntityContent.parse(from(obj))

return when (obj) {
is MCEntity -> obj
is Entity -> obj.toMC()
else -> null
}?.let { HoverEvent.EntityContent(it.type, it.uuid, it.name) }
}

private fun reInstance() {
component = Text.literal(text.formatIf(formatted))

Expand Down

0 comments on commit 26d6230

Please sign in to comment.