Skip to content
Merged
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 @@ -40,6 +40,7 @@
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.PrimitiveIterator;
import java.util.Random;
Expand Down Expand Up @@ -105,6 +106,14 @@ public class BookBot extends Module {
.build()
);

private final Setting<Boolean> wordWrap = sgGeneral.add(new BoolSetting.Builder()
.name("word-wrap")
.description("Prevents words from being cut in the middle of lines.")
.defaultValue(true)
.visible(() -> mode.get() == Mode.File)
.build()
);

private File file = new File(MeteorClient.FOLDER, "bookbot.txt");
private final PointerBuffer filters;

Expand Down Expand Up @@ -252,66 +261,110 @@ private void onTick(TickEvent.Post event) {
private void writeBook(PrimitiveIterator.OfInt chars) {
ArrayList<String> pages = new ArrayList<>();
ArrayList<RawFilteredPair<Text>> filteredPages = new ArrayList<>();
TextHandler.WidthRetriever widthRetriever = ((TextHandlerAccessor) mc.textRenderer.getTextHandler()).meteor$getWidthRetriever();

int maxPages = mode.get() == Mode.File ? 100 : this.pages.get();

int pageIndex = 0;
int lineIndex = 0;

final StringBuilder page = new StringBuilder();

float lineWidth = 0;

while (chars.hasNext()) {
int c = chars.nextInt();

if (c == '\r' || c == '\n') {
page.append('\n');
lineWidth = 0;
lineIndex++;
} else {
float charWidth = widthRetriever.getWidth(c, Style.EMPTY);
if (wordWrap.get() && mode.get() == Mode.File) {
StringBuilder text = new StringBuilder();
while (chars.hasNext()) {
text.appendCodePoint(chars.nextInt());
}

// Reached end of line
if (lineWidth + charWidth > 114f) {
// Use mc's own word wrapping logic
List<StringVisitable> wrappedLines = mc.textRenderer.wrapLinesWithoutLanguage(Text.literal(text.toString()), 114);
processLinesToPages(wrappedLines, pages, filteredPages, maxPages);
} else {
// Non-word-wrapping logic
TextHandler.WidthRetriever widthRetriever = ((TextHandlerAccessor) mc.textRenderer.getTextHandler()).meteor$getWidthRetriever();
int pageIndex = 0;
int lineIndex = 0;
final StringBuilder page = new StringBuilder();
float lineWidth = 0;

while (chars.hasNext()) {
int c = chars.nextInt();

if (c == '\r' || c == '\n') {
page.append('\n');
lineWidth = charWidth;
lineWidth = 0;
lineIndex++;
// Wrap to next line, unless wrapping to next page
if (lineIndex != 14) page.appendCodePoint(c);
} else if (lineWidth == 0f && c == ' ') {
continue; // Prevent leading space from text wrapping
} else {
lineWidth += charWidth;
page.appendCodePoint(c);
float charWidth = widthRetriever.getWidth(c, Style.EMPTY);

// Reached end of line
if (lineWidth + charWidth > 114f) {
page.append('\n');
lineWidth = charWidth;
lineIndex++;
// Wrap to next line, unless wrapping to next page
if (lineIndex != 14) page.appendCodePoint(c);
} else if (lineWidth == 0f && c == ' ') {
continue; // Prevent leading space from text wrapping
} else {
lineWidth += charWidth;
page.appendCodePoint(c);
}
}

// Reached end of page
if (lineIndex == 14) {
filteredPages.add(RawFilteredPair.of(Text.of(page.toString())));
pages.add(page.toString());
page.setLength(0);
pageIndex++;
lineIndex = 0;

// No more pages
if (pageIndex == maxPages) break;

// Wrap to next page
if (c != '\r' && c != '\n') {
page.appendCodePoint(c);
}
}
}

// Reached end of page
if (lineIndex == 14) {
// No more characters, end current page
if (!page.isEmpty() && pageIndex != maxPages) {
filteredPages.add(RawFilteredPair.of(Text.of(page.toString())));
pages.add(page.toString());
page.setLength(0);
}
}

createBook(pages, filteredPages);
}

private void processLinesToPages(List<StringVisitable> lines, ArrayList<String> pages, ArrayList<RawFilteredPair<Text>> filteredPages, int maxPages) {
int pageIndex = 0;
int lineIndex = 0;
StringBuilder currentPage = new StringBuilder();

for (StringVisitable line : lines) {
String lineText = line.getString();

if (currentPage.length() > 0) {
currentPage.append('\n');
}
currentPage.append(lineText);
lineIndex++;

if (lineIndex == 14) {
filteredPages.add(RawFilteredPair.of(Text.of(currentPage.toString())));
pages.add(currentPage.toString());
currentPage.setLength(0);
pageIndex++;
lineIndex = 0;

// No more pages
if (pageIndex == maxPages) break;

// Wrap to next page
if (c != '\r' && c != '\n') {
page.appendCodePoint(c);
}
}
}

// No more characters, end current page
if (!page.isEmpty() && pageIndex != maxPages) {
filteredPages.add(RawFilteredPair.of(Text.of(page.toString())));
pages.add(page.toString());
if (!currentPage.isEmpty() && pageIndex < maxPages) {
filteredPages.add(RawFilteredPair.of(Text.of(currentPage.toString())));
pages.add(currentPage.toString());
}
}

private void createBook(ArrayList<String> pages, ArrayList<RawFilteredPair<Text>> filteredPages) {
// Get the title with count
String title = name.get();
if (count.get() && bookCount != 0) title += " #" + bookCount;
Expand Down