Fix contentpadding breaking when the index covers 2 pages
Apparently the index section transformer does not add the pages until after transformers run
@@ -8,6 +8,7 @@ import slimeknights.mantle.client.book.data.SectionData;
import slimeknights.mantle.client.book.data.content.ContentBlank;
import java.util.Iterator;
+import java.util.List;
/**
* Variant of blank pages that only adds the page on a specific side, useful to force the next page to the left or right regardless of the number of pages before
@@ -39,23 +40,39 @@ public abstract class ContentPadding extends ContentBlank {
/** Transformer to make this page type work */
public static class PaddingBookTransformer extends BookTransformer {
+ /** Regular transformer considering indexes */
public static final PaddingBookTransformer INSTANCE = new PaddingBookTransformer();
+ /** Transformer that ignores the index icon section transformer */
+ public static final PaddingBookTransformer NO_INDEX = new PaddingBookTransformer();
private PaddingBookTransformer() {}
@Override
public void transform(BookData bookData) {
// first page is on the right side
- boolean isLeft = false;
+ boolean previousLeft = true;
+
+ // index transformer does not truely add pages, so manually offset previousLeft based on that count
+ if (this != NO_INDEX) {
+ List<SectionData> visibleSections = bookData.getVisibleSections(null);
+ if (!visibleSections.isEmpty()) {
+ int count = visibleSections.size() - 1; // remove index transformer section
+ int pages = count / 9;
+ if (count % 9 == 0) pages++;
+ previousLeft = pages % 2 == 1;
+ }
+ }
+
+ // iterate through all pages, tracking whehter we are left or right
for (SectionData section : bookData.sections) {
Iterator<PageData> pageIterator = section.pages.iterator();
while (pageIterator.hasNext()) {
PageData data = pageIterator.next();
// if its left and the current page is odd, or its right and the current page is even, skip
- if (data.content instanceof ContentPadding && ((ContentPadding) data.content).isLeft() == isLeft) {
+ if (data.content instanceof ContentPadding && ((ContentPadding) data.content).isLeft() == previousLeft) {
pageIterator.remove();
} else {
- isLeft = !isLeft;
+ previousLeft = !previousLeft;
}
}
}
