Skip to content

Commit

Permalink
refactor(BookBot): allow space option, code refactors
Browse files Browse the repository at this point in the history
- make GenerationMode private
- class BookBuilder for writeBook operation
  • Loading branch information
MukjepScarlet committed Jan 3, 2025
1 parent 2e99b53 commit 51e4104
Showing 1 changed file with 72 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,9 @@ private const val MAX_LINE_WIDTH: Float = 114f
object ModuleBookBot : ClientModule("BookBot", Category.MISC, disableOnQuit = true) {
private val inventoryConstraints = tree(PlayerInventoryConstraints())

val generationMode =
choices(
"Mode",
RandomGenerationMode,
arrayOf(
RandomGenerationMode,
),
).apply { tagBy(this) }
private val generationMode = choices("Mode", GenerationMode.Random, arrayOf(GenerationMode.Random)).apply {
tagBy(this)
}

private object Sign : ToggleableConfigurable(ModuleBookBot, "Sign", true) {
val bookName by text("Name", "Generated book #%count%")
Expand All @@ -86,12 +81,12 @@ object ModuleBookBot : ClientModule("BookBot", Category.MISC, disableOnQuit = tr
chronometer.reset()
}

private val isCandidate: (ItemStack) -> Boolean = {
val component = it.get(DataComponentTypes.WRITABLE_BOOK_CONTENT)
it.item == Items.WRITABLE_BOOK && component?.pages?.isEmpty() == true
private fun isCandidate(itemStack: ItemStack): Boolean {
return itemStack.item == Items.WRITABLE_BOOK &&
itemStack.get(DataComponentTypes.WRITABLE_BOOK_CONTENT)?.pages?.isEmpty() == true
}

private val randomBook get() = findInventorySlot(isCandidate)
private val randomBook get() = findInventorySlot(::isCandidate)

@Suppress("unused")
private val scheduleInventoryAction = handler<ScheduleInventoryActionEvent> { event ->
Expand Down Expand Up @@ -135,7 +130,7 @@ object ModuleBookBot : ClientModule("BookBot", Category.MISC, disableOnQuit = tr
* @see PrimitiveIterator.OfInt
* @see GenerationMode.generate
*/
@Suppress("CognitiveComplexMethod", "NestedBlockDepth")
@Suppress("CognitiveComplexMethod")
private fun writeBook() {
if (!isCandidate(player.mainHandStack)) {
return
Expand All @@ -144,8 +139,7 @@ object ModuleBookBot : ClientModule("BookBot", Category.MISC, disableOnQuit = tr
val chars = generationMode.activeChoice.generate()
val widthRetriever = mc.textRenderer.textHandler.widthRetriever

val pages = ArrayList<String>()
val filteredPages = ArrayList<RawFilteredPair<Text>>()
val bookBuilder = BookBuilder()

var pageIndex = 0
var lineIndex = 0
Expand All @@ -155,7 +149,9 @@ object ModuleBookBot : ClientModule("BookBot", Category.MISC, disableOnQuit = tr
while (chars.hasNext()) {
val char = chars.nextInt().toChar()

if (char == '\r' || char == '\n') {
if (lineWidth == 0f && char == ' ') {
continue
} else if (char == '\r' || char == '\n') {
page.append('\n')
lineWidth = 0.0f
lineIndex++
Expand All @@ -165,17 +161,15 @@ object ModuleBookBot : ClientModule("BookBot", Category.MISC, disableOnQuit = tr
if (lineWidth + charWidth > MAX_LINE_WIDTH) {
lineIndex++
lineWidth = charWidth
appendLineBreak(page, lineIndex)
} else if (lineWidth == 0f && char == ' ') {
continue
page.appendLineBreak(lineIndex)
} else {
lineWidth += charWidth
page.appendCodePoint(char.code)
}
}

if (lineIndex == MAX_LINES_PER_PAGE) {
addPageToBook(page, pages, filteredPages)
bookBuilder.addPage(page.toString())
page.setLength(0)
pageIndex++
lineIndex = 0
Expand All @@ -185,85 +179,84 @@ object ModuleBookBot : ClientModule("BookBot", Category.MISC, disableOnQuit = tr
}

if (char != '\r' && char != '\n') {
page.appendCodePoint(char.code)
page.append(char)
}
}
}

if (page.isNotEmpty() && pageIndex != generationMode.activeChoice.pages) {
addPageToBook(page, pages, filteredPages)
bookBuilder.addPage(page.toString())
}

writeBook(Sign.bookName.replace("%count%", bookCount.toString()),
filteredPages, pages)
bookBuilder.writeBook()

bookCount++
}

private fun appendLineBreak(page: StringBuilder, lineIndex: Int) {
page.append('\n')
if (lineIndex == MAX_LINES_PER_PAGE) {
page.appendCodePoint(' '.code)
}
}
private sealed class GenerationMode(
name: String,
) : Choice(name) {
override val parent: ChoiceConfigurable<*> = ModuleBookBot.generationMode

private fun addPageToBook(
page: StringBuilder,
pages: MutableList<String>,
filteredPages: MutableList<RawFilteredPair<Text>>
) {
filteredPages.add(RawFilteredPair.of(Text.of(page.toString())))
pages.add(page.toString())
}
val pages by int("Pages", 50, 0..100)

private fun writeBook(
title: String,
filteredPages: ArrayList<RawFilteredPair<Text>>,
pages: ArrayList<String>
) {
player.mainHandStack.set(
DataComponentTypes.WRITTEN_BOOK_CONTENT,
WrittenBookContentComponent(
RawFilteredPair.of(title),
player.gameProfile.name,
0,
filteredPages,
true
)
)
abstract fun generate(): PrimitiveIterator.OfInt

player.networkHandler.sendPacket(
BookUpdateC2SPacket(
player.inventory.selectedSlot,
pages,
if (Sign.enabled) Optional.of(title) else Optional.empty()
)
)
}
}
object Random : GenerationMode("Random") {
private val asciiOnly by boolean("AsciiOnly", false)

abstract class GenerationMode(
name: String,
) : Choice(name) {
override val parent: ChoiceConfigurable<*> = ModuleBookBot.generationMode
private val allowSpace by boolean("AllowSpace", true)

abstract val pages: Int
override fun generate(): PrimitiveIterator.OfInt {
val origin = if (asciiOnly) 0x21 else 0x0800
val bound = if (asciiOnly) 0x7E else 0x10FFFF

abstract fun generate(): PrimitiveIterator.OfInt
}
return random
.ints(origin, bound)
.filter { allowSpace || !Character.isWhitespace(it) }
.iterator()
}
}
}

object RandomGenerationMode : GenerationMode("Random") {
override val pages by int("Pages", 50, 0..100)
private fun StringBuilder.appendLineBreak(lineIndex: Int) {
append('\n')
if (lineIndex == MAX_LINES_PER_PAGE) {
append(' ')
}
}

private val asciiOnly by boolean("AsciiOnly", false)
private class BookBuilder {
private val title: String = Sign.bookName.replace("%count%", bookCount.toString())
private val pageAmount: Int = generationMode.activeChoice.pages

override fun generate(): PrimitiveIterator.OfInt {
val origin = if (asciiOnly) 0x21 else 0x0800
val bound = if (asciiOnly) 0x7E else 0x10FFFF
private val pages = ArrayList<String>(pageAmount)
private val filteredPages = ArrayList<RawFilteredPair<Text>>(pageAmount)

return ModuleBookBot.random
.ints(origin, bound)
.filter { !Character.isWhitespace(it) && it.toChar() != '\r' && it.toChar() != '\n' }
.iterator()
fun addPage(page: String) {
filteredPages.add(RawFilteredPair.of(Text.literal(page)))
pages.add(page)
}

fun writeBook() {
player.mainHandStack.set(
DataComponentTypes.WRITTEN_BOOK_CONTENT,
WrittenBookContentComponent(
RawFilteredPair.of(title),
player.gameProfile.name,
0,
filteredPages,
true
)
)

player.networkHandler.sendPacket(
BookUpdateC2SPacket(
player.inventory.selectedSlot,
pages,
if (Sign.enabled) Optional.of(title) else Optional.empty()
)
)
}
}
}

0 comments on commit 51e4104

Please sign in to comment.