chat: correct max length per version

This commit is contained in:
Bixilon 2022-02-05 16:46:35 +01:00
parent 45db95af26
commit faa70657ee
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
2 changed files with 11 additions and 7 deletions

View File

@ -59,4 +59,5 @@ class Version(
val flattened: Boolean = versionId >= ProtocolDefinition.FLATTING_VERSION_ID
val hasOffhand: Boolean = versionId >= V_15W31A
val maxPacketLength = (versionId < ProtocolVersions.V_1_17_1_RC2).decide(1 shl 21, 1 shl 23)
val maxChatMessageSize = (versionId < ProtocolVersions.V_16W38A).decide(100, 256)
}

View File

@ -46,7 +46,7 @@ class ChatElement(guiRenderer: GUIRenderer) : Element(guiRenderer), LayoutedElem
private val profile = connection.profiles.hud
private val chatProfile = profile.chat
private val messages = TextFlowElement(guiRenderer, 20000).apply { parent = this@ChatElement }
private val input = TextInputElement(guiRenderer, maxLength = 256)
private val input = TextInputElement(guiRenderer, maxLength = connection.version.maxChatMessageSize)
private var active = false
set(value) {
field = value
@ -133,13 +133,17 @@ class ChatElement(guiRenderer: GUIRenderer) : Element(guiRenderer), LayoutedElem
input.onCharPress(char)
}
private fun submit() {
if (input.value.isNotBlank()) {
connection.sendPacket(ChatMessageC2SP(input.value))
}
input.value = ""
guiRenderer.gui.pop()
}
override fun onSpecialKey(key: InputSpecialKey, type: KeyChangeTypes) {
if (key == InputSpecialKey.KEY_ENTER && type == KeyChangeTypes.PRESS) {
if (input.value.isNotBlank()) {
connection.sendPacket(ChatMessageC2SP(input.value))
input.value = ""
}
guiRenderer.gui.pop()
return submit()
}
input.onSpecialKey(key, type)
}
@ -153,7 +157,6 @@ class ChatElement(guiRenderer: GUIRenderer) : Element(guiRenderer), LayoutedElem
input.tick()
}
companion object : HUDBuilder<LayoutedGUIElement<ChatElement>>, GUIBuilder<LayoutedGUIElement<ChatElement>> {
override val RESOURCE_LOCATION: ResourceLocation = "minosoft:chat_hud".toResourceLocation()
private const val CHAT_INPUT_HEIGHT = Font.TOTAL_CHAR_HEIGHT * 3 + Font.CHAR_MARGIN * 2