mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-15 18:34:56 -04:00
text: correctly split translated components with oder dependent placeholders
This commit is contained in:
parent
3d891f6e88
commit
b6909dd928
@ -46,14 +46,41 @@ open class MinecraftLanguage : Translator {
|
|||||||
override fun translate(key: String?, parent: TextComponent?, vararg data: Any?): ChatComponent {
|
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 placeholder = this.data[key] ?: return ChatComponent.valueOf(null, parent, key.toString() + "->" + data.toString())
|
||||||
|
|
||||||
val split = placeholder.split("%s") // ToDo: Split correctly
|
|
||||||
|
|
||||||
val ret = BaseComponent()
|
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))
|
ret.parts.add(ChatComponent.valueOf(this, parent, part))
|
||||||
if (index < data.size) {
|
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 {
|
override fun toString(): String {
|
||||||
return language
|
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
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -115,7 +115,7 @@ open class TextComponent(
|
|||||||
|
|
||||||
override fun getJavaFXText(nodes: ObservableList<Node>): ObservableList<Node> {
|
override fun getJavaFXText(nodes: ObservableList<Node>): ObservableList<Node> {
|
||||||
val text = Text(this.message)
|
val text = Text(this.message)
|
||||||
val color = this.color ?: ChatColors.WHITE
|
val color = this.color ?: ProtocolDefinition.DEFAULT_COLOR
|
||||||
text.fill = Color.WHITE
|
text.fill = Color.WHITE
|
||||||
if (Minosoft.getConfig().config.chat.colored) {
|
if (Minosoft.getConfig().config.chat.colored) {
|
||||||
text.fill = Color.rgb(color.red, color.green, color.blue)
|
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) {
|
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
|
// bring chars in right order and reverse them if right bound
|
||||||
|
@ -49,7 +49,7 @@ class Camera(
|
|||||||
) : ScreenResizeCallback {
|
) : ScreenResizeCallback {
|
||||||
private var mouseSensitivity = Minosoft.getConfig().config.game.camera.moseSensitivity
|
private var mouseSensitivity = Minosoft.getConfig().config.game.camera.moseSensitivity
|
||||||
private val walkingSpeed get() = connection.player.baseAbilities.walkingSpeed * ProtocolDefinition.TICKS_PER_SECOND
|
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)
|
var cameraPosition = Vec3(0.0f, 0.0f, 0.0f)
|
||||||
private var lastMouseX = 0.0
|
private var lastMouseX = 0.0
|
||||||
private var lastMouseY = 0.0
|
private var lastMouseY = 0.0
|
||||||
|
@ -13,12 +13,12 @@
|
|||||||
package de.bixilon.minosoft.protocol.packets.s2c.play
|
package de.bixilon.minosoft.protocol.packets.s2c.play
|
||||||
|
|
||||||
import de.bixilon.minosoft.data.ChatTextPositions
|
import de.bixilon.minosoft.data.ChatTextPositions
|
||||||
import de.bixilon.minosoft.data.text.ChatColors
|
|
||||||
import de.bixilon.minosoft.data.text.ChatComponent
|
import de.bixilon.minosoft.data.text.ChatComponent
|
||||||
import de.bixilon.minosoft.modding.event.events.ChatMessageReceivingEvent
|
import de.bixilon.minosoft.modding.event.events.ChatMessageReceivingEvent
|
||||||
import de.bixilon.minosoft.protocol.network.connection.PlayConnection
|
import de.bixilon.minosoft.protocol.network.connection.PlayConnection
|
||||||
import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket
|
import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket
|
||||||
import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer
|
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.protocol.protocol.ProtocolVersions
|
||||||
import de.bixilon.minosoft.util.logging.Log
|
import de.bixilon.minosoft.util.logging.Log
|
||||||
import de.bixilon.minosoft.util.logging.LogLevels
|
import de.bixilon.minosoft.util.logging.LogLevels
|
||||||
@ -39,7 +39,7 @@ class ChatMessageS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket() {
|
|||||||
sender = buffer.readUUID()
|
sender = buffer.readUUID()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
message.applyDefaultColor(ChatColors.WHITE)
|
message.applyDefaultColor(ProtocolDefinition.DEFAULT_COLOR)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun handle(connection: PlayConnection) {
|
override fun handle(connection: PlayConnection) {
|
||||||
|
@ -102,7 +102,13 @@ object Log {
|
|||||||
message.printStackTrace(PrintWriter(stringWriter))
|
message.printStackTrace(PrintWriter(stringWriter))
|
||||||
ChatComponent.valueOf(raw = stringWriter.toString())
|
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)
|
else -> ChatComponent.valueOf(raw = message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user