Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(Spammer): improvement of custom format #4866

Open
wants to merge 1 commit into
base: nextgen
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ object ModuleSpammer : ClientModule("Spammer", Category.MISC, disableOnQuit = tr
val text = messageConverterMode.convert(if (customFormatter) {
format(chosenMessage)
} else {
"[${RandomStringUtils.randomAlphabetic(Random.nextInt(4) + 1)}] " +
"[${RandomStringUtils.randomAlphabetic(Random.nextInt(1, 5))}] " +
MessageConverterMode.RANDOM_CASE_CONVERTER.convert(chosenMessage)
})

Expand All @@ -82,34 +82,40 @@ object ModuleSpammer : ClientModule("Spammer", Category.MISC, disableOnQuit = tr
}

private fun format(text: String): String {
var formattedText = text

while (formattedText.contains("%f"))
formattedText = formattedText.insert("%f", Random.nextFloat())
while (formattedText.contains("%i"))
formattedText = formattedText.insert("%i", Random.nextInt(10000))
while (formattedText.contains("%s"))
formattedText = formattedText.insert("%s", RandomStringUtils.randomAlphabetic((4..6).random()))
var formattedText = text.replace("%f") {
Random.nextFloat()
}.replace("%i") {
Random.nextInt(10000)
}.replace("%s") {
RandomStringUtils.randomAlphabetic(Random.nextInt(4, 7))
}

if (formattedText.contains("@a")) {
val playerList = mc.networkHandler?.playerList?.filter {
it?.profile?.name != player.gameProfile?.name
}

if (!playerList.isNullOrEmpty()) {
while (formattedText.contains("@a")) {
formattedText = formattedText.insert("@a",
playerList.randomOrNull()?.profile?.name ?: break)
}
mc.networkHandler?.playerList?.mapNotNull {
it?.profile?.name.takeIf { n -> n != player.gameProfile?.name }
}?.takeIf { it.isNotEmpty() }?.let { playerNameList ->
formattedText = formattedText.replace("@a") { playerNameList.random() }
}
}

return formattedText
}

private fun String.insert(prefix: String, insert: Any): String {
return substring(0, indexOf(prefix)) +
insert.toString() + substring(indexOf(prefix) + prefix.length)
private inline fun String.replace(oldValue: String, newValueProvider: () -> Any): String {
var index = 0
val newString = StringBuilder(this)
while (true) {
index = newString.indexOf(oldValue, startIndex = index)
if (index == -1) {
break
}

val newValue = newValueProvider().toString()
newString.replace(index, index + oldValue.length, newValue)

index += newValue.length
}
return newString.toString()
}
Comment on lines +104 to 119
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What makes this better than the previous implementation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. For different keyword (%d, %f, etc), one replacement will only create one StringBuilder, the previous one will create many strings. (one %d will create 3 new strings)
  2. The indexOf now will not search from the head, next search will start from the tail of previous one like:
example%ianother%iumm
^
search 1 start
example12345another%iumm
            ^
search 2 start


enum class MessageConverterMode(override val choiceName: String, val convert: (String) -> String) : NamedChoice {
Expand Down
Loading