use String::trimWhitespaces from kutil

This commit is contained in:
Bixilon 2022-09-28 21:48:47 +02:00
parent c2248a8424
commit 81f3644565
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
6 changed files with 20 additions and 20 deletions

View File

@ -16,7 +16,7 @@ package de.bixilon.minosoft.gui.rendering.gui.gui.elements.input.node
import de.bixilon.kotlinglm.vec2.Vec2i import de.bixilon.kotlinglm.vec2.Vec2i
import de.bixilon.kutil.string.StringUtil.codePointAtOrNull import de.bixilon.kutil.string.StringUtil.codePointAtOrNull
import de.bixilon.kutil.string.TextUtil import de.bixilon.kutil.string.TextUtil
import de.bixilon.kutil.string.WhitespaceUtil.removeTrailingWhitespaces import de.bixilon.kutil.string.WhitespaceUtil.trimWhitespaces
import de.bixilon.minosoft.commands.errors.ReaderError import de.bixilon.minosoft.commands.errors.ReaderError
import de.bixilon.minosoft.commands.nodes.CommandNode import de.bixilon.minosoft.commands.nodes.CommandNode
import de.bixilon.minosoft.commands.stack.CommandStack import de.bixilon.minosoft.commands.stack.CommandStack
@ -32,7 +32,6 @@ import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexConsumer
import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexOptions import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexOptions
import de.bixilon.minosoft.gui.rendering.system.window.KeyChangeTypes import de.bixilon.minosoft.gui.rendering.system.window.KeyChangeTypes
import de.bixilon.minosoft.gui.rendering.util.vec.vec2.Vec2iUtil.EMPTY import de.bixilon.minosoft.gui.rendering.util.vec.vec2.Vec2iUtil.EMPTY
import de.bixilon.minosoft.terminal.cli.CLI.removeDuplicatedWhitespaces
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
import de.bixilon.minosoft.util.logging.LogMessageType import de.bixilon.minosoft.util.logging.LogMessageType
@ -121,7 +120,7 @@ class NodeTextInputElement(
fun updateSuggestion(suggestion: Any) { fun updateSuggestion(suggestion: Any) {
val string = suggestion.toString() val string = suggestion.toString()
val value = value.removeDuplicatedWhitespaces().removeTrailingWhitespaces() val value = value.trimWhitespaces()
val overlappingLength = TextUtil.getOverlappingText(value, string) val overlappingLength = TextUtil.getOverlappingText(value, string)
var nextValue = value var nextValue = value
val lastChar = value.codePointAtOrNull(value.length - 1) val lastChar = value.codePointAtOrNull(value.length - 1)

View File

@ -41,7 +41,7 @@ class BlockDustParticle(connection: PlayConnection, position: Vec3d, velocity: V
color = 0.6f.asGray() color = 0.6f.asGray()
if (data.blockState.block.resourceLocation != MinecraftBlocks.GRASS_BLOCK) { // Just the overlay is tinted if (data.blockState.block.resourceLocation != MinecraftBlocks.GRASS_BLOCK) { // Just the overlay is tinted
connection.rendering!!.renderWindow.tintManager.getTint(data.blockState, null, blockPosition)?.getOrNull(0)?.asRGBColor()?.let { connection.rendering!!.renderWindow.tintManager.getTint(data.blockState, null, blockPosition)?.getOrNull(PARTICLE_TINT_INDEX)?.asRGBColor()?.let {
color = RGBColor(color.floatRed * it.floatRed, color.floatGreen * it.floatGreen, color.floatBlue * it.floatBlue) color = RGBColor(color.floatRed * it.floatRed, color.floatGreen * it.floatGreen, color.floatBlue * it.floatBlue)
} }
} }
@ -57,6 +57,7 @@ class BlockDustParticle(connection: PlayConnection, position: Vec3d, velocity: V
companion object : ParticleFactory<BlockDustParticle> { companion object : ParticleFactory<BlockDustParticle> {
private const val GRAY = 153 shl 16 or (153 shl 8) or 153 private const val GRAY = 153 shl 16 or (153 shl 8) or 153
private const val PARTICLE_TINT_INDEX = 0
override val RESOURCE_LOCATION: ResourceLocation = "minecraft:block".toResourceLocation() override val RESOURCE_LOCATION: ResourceLocation = "minecraft:block".toResourceLocation()
override fun build(connection: PlayConnection, position: Vec3d, velocity: Vec3d, data: ParticleData): BlockDustParticle? { override fun build(connection: PlayConnection, position: Vec3d, velocity: Vec3d, data: ParticleData): BlockDustParticle? {

View File

@ -16,7 +16,7 @@ package de.bixilon.minosoft.protocol.network.connection.play
import com.fasterxml.jackson.core.io.JsonStringEncoder import com.fasterxml.jackson.core.io.JsonStringEncoder
import com.google.common.primitives.Longs import com.google.common.primitives.Longs
import de.bixilon.kotlinglm.vec3.Vec3d import de.bixilon.kotlinglm.vec3.Vec3d
import de.bixilon.kutil.string.WhitespaceUtil.removeTrailingWhitespaces import de.bixilon.kutil.string.WhitespaceUtil.trimWhitespaces
import de.bixilon.minosoft.commands.stack.CommandStack import de.bixilon.minosoft.commands.stack.CommandStack
import de.bixilon.minosoft.data.text.BaseComponent import de.bixilon.minosoft.data.text.BaseComponent
import de.bixilon.minosoft.data.text.ChatComponent import de.bixilon.minosoft.data.text.ChatComponent
@ -32,7 +32,6 @@ import de.bixilon.minosoft.protocol.packets.c2s.play.chat.SignedChatMessageC2SP
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
import de.bixilon.minosoft.protocol.protocol.encryption.CryptManager import de.bixilon.minosoft.protocol.protocol.encryption.CryptManager
import de.bixilon.minosoft.protocol.protocol.encryption.SignatureData import de.bixilon.minosoft.protocol.protocol.encryption.SignatureData
import de.bixilon.minosoft.terminal.cli.CLI.removeDuplicatedWhitespaces
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
import de.bixilon.minosoft.util.logging.LogMessageType import de.bixilon.minosoft.util.logging.LogMessageType
@ -58,13 +57,16 @@ class ConnectionUtil(
} }
fun sendChatMessage(message: String) { fun sendChatMessage(message: String) {
val message = message.removeDuplicatedWhitespaces().removeTrailingWhitespaces() val message = message.trimWhitespaces()
if (message.isBlank()) { if (message.isBlank()) {
throw IllegalArgumentException("Chat message can not be blank!") throw IllegalArgumentException("Chat message can not be blank!")
} }
if (message.contains(ProtocolDefinition.TEXT_COMPONENT_SPECIAL_PREFIX_CHAR)) { if (message.contains(ProtocolDefinition.TEXT_COMPONENT_SPECIAL_PREFIX_CHAR)) {
throw IllegalArgumentException("Chat message must not contain chat formatting (${ProtocolDefinition.TEXT_COMPONENT_SPECIAL_PREFIX_CHAR}): $message") throw IllegalArgumentException("Chat message must not contain chat formatting (${ProtocolDefinition.TEXT_COMPONENT_SPECIAL_PREFIX_CHAR}): $message")
} }
if (message.length > connection.version.maxChatMessageSize) {
throw IllegalArgumentException("Message length (${message.length} can not exceed ${connection.version.maxChatMessageSize})")
}
if (connection.fireEvent(ChatMessageSendEvent(connection, message))) { if (connection.fireEvent(ChatMessageSendEvent(connection, message))) {
return return
} }

View File

@ -135,15 +135,16 @@ class PlayConnection(
} }
} }
} }
network::state.observe(this) { network::state.observe(this) { state ->
when (it) { when (state) {
ProtocolStates.HANDSHAKING, ProtocolStates.STATUS -> throw IllegalStateException("Invalid state!") ProtocolStates.HANDSHAKING, ProtocolStates.STATUS -> throw IllegalStateException("Invalid state!")
ProtocolStates.LOGIN -> { ProtocolStates.LOGIN -> {
state = PlayConnectionStates.LOGGING_IN this.state = PlayConnectionStates.LOGGING_IN
this.network.send(StartC2SP(this.player)) this.network.send(StartC2SP(this.player))
} }
ProtocolStates.PLAY -> { ProtocolStates.PLAY -> {
state = PlayConnectionStates.JOINING this.state = PlayConnectionStates.JOINING
if (CLI.connection == null) { if (CLI.connection == null) {
CLI.connection = this CLI.connection = this

View File

@ -33,13 +33,13 @@ object DefaultPluginHandler {
connection.pluginManager[brandChannel] = { connection.pluginManager[brandChannel] = {
connection.serverInfo.brand = it.readString() connection.serverInfo.brand = it.readString()
sendBrand(brandChannel, connection, if (connection.profiles.connection.fakeBrand) ProtocolDefinition.VANILLA_BRAND else ProtocolDefinition.MINOSOFT_BRAND) connection.sendBrand(brandChannel, if (connection.profiles.connection.fakeBrand) ProtocolDefinition.VANILLA_BRAND else ProtocolDefinition.MINOSOFT_BRAND)
} }
} }
private fun sendBrand(channel: ResourceLocation, connection: PlayConnection, brand: String) { private fun PlayConnection.sendBrand(channel: ResourceLocation, brand: String) {
val buffer = PlayOutByteBuffer(connection) val buffer = PlayOutByteBuffer(this)
buffer.writeByteArray(brand.encodeNetwork()) buffer.writeByteArray(brand.encodeNetwork())
connection.sendPacket(PluginC2SP(channel, buffer)) sendPacket(PluginC2SP(channel, buffer))
} }
} }

View File

@ -16,6 +16,7 @@ package de.bixilon.minosoft.terminal.cli
import de.bixilon.kutil.latch.CountUpAndDownLatch import de.bixilon.kutil.latch.CountUpAndDownLatch
import de.bixilon.kutil.shutdown.AbstractShutdownReason import de.bixilon.kutil.shutdown.AbstractShutdownReason
import de.bixilon.kutil.shutdown.ShutdownManager import de.bixilon.kutil.shutdown.ShutdownManager
import de.bixilon.kutil.string.WhitespaceUtil.trimWhitespaces
import de.bixilon.kutil.watcher.DataWatcher.Companion.observe import de.bixilon.kutil.watcher.DataWatcher.Companion.observe
import de.bixilon.kutil.watcher.DataWatcher.Companion.watched import de.bixilon.kutil.watcher.DataWatcher.Companion.watched
import de.bixilon.minosoft.commands.nodes.RootNode import de.bixilon.minosoft.commands.nodes.RootNode
@ -68,7 +69,7 @@ object CLI {
while (true) { while (true) {
try { try {
val line: String = reader.readLine().removeDuplicatedWhitespaces() val line: String = reader.readLine().trimWhitespaces()
if (line.isBlank()) { if (line.isBlank()) {
continue continue
} }
@ -82,10 +83,6 @@ object CLI {
} }
} }
fun String.removeDuplicatedWhitespaces(): String {
return this.replace("\\s{2,}".toRegex(), "")
}
object NodeCompleter : Completer { object NodeCompleter : Completer {
override fun complete(reader: LineReader, line: ParsedLine, candidates: MutableList<Candidate>) { override fun complete(reader: LineReader, line: ParsedLine, candidates: MutableList<Candidate>) {