text: correctly split translated components with oder dependent placeholders

This commit is contained in:
Bixilon 2021-04-26 14:40:59 +02:00
parent 3d891f6e88
commit b6909dd928
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
5 changed files with 49 additions and 10 deletions

View File

@ -46,14 +46,41 @@ open class MinecraftLanguage : Translator {
override fun translate(key: String?, parent: TextComponent?, vararg data: Any?): ChatComponent {
val placeholder = this.data[key] ?: return ChatComponent.valueOf(null, parent, key.toString() + "->" + data.toString())
val split = placeholder.split("%s") // ToDo: Split correctly
val ret = BaseComponent()
for ((index, part) in split.withIndex()) {
val arguments: MutableList<Any?> = mutableListOf()
var splitPlaceholder: List<String> = listOf()
FORMATTER_ORDER_REGEX.find(placeholder)?.let {
if (it.groupValues.isEmpty()) {
// this is not the correct formatter
return@let
}
splitPlaceholder = placeholder.split(FORMATTER_ORDER_REGEX)
for ((index, part) in it.groupValues.withIndex()) {
if (index % 2 == 0) {
continue
}
val dataIndex = part.toInt() - 1
if (dataIndex < 0 || dataIndex > data.size) {
arguments += null
continue
}
arguments += data[dataIndex]
}
}
placeholder.split(FORMATTER_SPLIT_REGEX).let {
if (splitPlaceholder.isEmpty()) {
splitPlaceholder = it
}
arguments.addAll(data.toList())
}
for ((index, part) in splitPlaceholder.withIndex()) {
ret.parts.add(ChatComponent.valueOf(this, parent, part))
if (index < data.size) {
ret.parts.add(ChatComponent.valueOf(this, parent, data[index]))
ret.parts.add(ChatComponent.valueOf(this, parent, arguments[index]))
}
}
@ -63,4 +90,10 @@ open class MinecraftLanguage : Translator {
override fun toString(): String {
return language
}
companion object {
private val FORMATTER_ORDER_REGEX = "%(\\w+)\\\$[sd]".toRegex() // %1$s fell from a high place
private val FORMATTER_SPLIT_REGEX = "%[ds]".toRegex() // %s fell from a high place
}
}

View File

@ -115,7 +115,7 @@ open class TextComponent(
override fun getJavaFXText(nodes: ObservableList<Node>): ObservableList<Node> {
val text = Text(this.message)
val color = this.color ?: ChatColors.WHITE
val color = this.color ?: ProtocolDefinition.DEFAULT_COLOR
text.fill = Color.WHITE
if (Minosoft.getConfig().config.chat.colored) {
text.fill = Color.rgb(color.red, color.green, color.blue)
@ -164,7 +164,7 @@ open class TextComponent(
override fun prepareRender(startPosition: Vec2i, offset: Vec2i, renderWindow: RenderWindow, textElement: LabelNode, z: Int, setProperties: TextSetProperties, getProperties: TextGetProperties) {
val color = this.color ?: ChatColors.WHITE
val color = this.color ?: ProtocolDefinition.DEFAULT_COLOR
// bring chars in right order and reverse them if right bound

View File

@ -49,7 +49,7 @@ class Camera(
) : ScreenResizeCallback {
private var mouseSensitivity = Minosoft.getConfig().config.game.camera.moseSensitivity
private val walkingSpeed get() = connection.player.baseAbilities.walkingSpeed * ProtocolDefinition.TICKS_PER_SECOND
private val flyingSpeed get() = connection.player.baseAbilities.flyingSpeed * ProtocolDefinition.TICKS_PER_SECOND
private val flyingSpeed get() = connection.player.baseAbilities.flyingSpeed * ProtocolDefinition.TICKS_PER_SECOND
var cameraPosition = Vec3(0.0f, 0.0f, 0.0f)
private var lastMouseX = 0.0
private var lastMouseY = 0.0

View File

@ -13,12 +13,12 @@
package de.bixilon.minosoft.protocol.packets.s2c.play
import de.bixilon.minosoft.data.ChatTextPositions
import de.bixilon.minosoft.data.text.ChatColors
import de.bixilon.minosoft.data.text.ChatComponent
import de.bixilon.minosoft.modding.event.events.ChatMessageReceivingEvent
import de.bixilon.minosoft.protocol.network.connection.PlayConnection
import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket
import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
import de.bixilon.minosoft.protocol.protocol.ProtocolVersions
import de.bixilon.minosoft.util.logging.Log
import de.bixilon.minosoft.util.logging.LogLevels
@ -39,7 +39,7 @@ class ChatMessageS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket() {
sender = buffer.readUUID()
}
}
message.applyDefaultColor(ChatColors.WHITE)
message.applyDefaultColor(ProtocolDefinition.DEFAULT_COLOR)
}
override fun handle(connection: PlayConnection) {

View File

@ -102,7 +102,13 @@ object Log {
message.printStackTrace(PrintWriter(stringWriter))
ChatComponent.valueOf(raw = stringWriter.toString())
}
is String -> ChatComponent.valueOf(raw = message.format(*formatting))
is String -> {
if (formatting.isNotEmpty()) {
ChatComponent.valueOf(raw = message.format(*formatting))
} else {
ChatComponent.valueOf(raw = message)
}
}
else -> ChatComponent.valueOf(raw = message)
}