Skip to content

Commit

Permalink
See if we can better handle book paging to fit more context on a line…
Browse files Browse the repository at this point in the history
…, without causing text loss after reaching character limit.
  • Loading branch information
khobbits committed Mar 29, 2014
1 parent 104caea commit f272a7e
Showing 1 changed file with 52 additions and 16 deletions.
68 changes: 52 additions & 16 deletions Essentials/src/com/earth2me/essentials/textreader/BookPager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
public class BookPager
{
private final transient IText text;
final double pageMax = 254;
final double charMax = 18.5;
final int lineMax = 12;

public BookPager(final IText text)
{
Expand Down Expand Up @@ -41,16 +44,20 @@ public List<String> getPages(final String pageStr) throws Exception
}
}

List<String> pages = new ArrayList<String>();
double pageLength = 0;

for (int lineNo = chapterstart; lineNo < chapterend; lineNo += 1)
{
String pageLine = "\u00a70" + lines.get(lineNo);
String pageLine = lines.get(lineNo);
String tempLine;
final double max = 18;

final int lineLength = pageLine.length();
double length = 0;
int pointer = 0;
int start = 0;
double weight = 1;
boolean forcePageEnd = false;

while (pointer < lineLength)
{
Expand All @@ -63,24 +70,40 @@ public List<String> getPages(final String pageStr) throws Exception
continue;
}

if (length >= max || (letter == '\u00a7' && length + 1 >= max))
if (pageLength >= pageMax)
{
length = charMax;
pageLength = 0;
forcePageEnd = true;
}

if (length >= charMax || (letter == '\u00a7' && length + 1 >= charMax))
{
int pos = pointer;
int rollback = 0;
while (pos > start && pageLine.charAt(pos) != ' ' && pageLine.charAt(pos) != "\n".charAt(0))
{
rollback++;
pos--;
}
if (pos != start)
{
pointer = pos;
pageLength -= rollback;
}

tempLine = pageLine.substring(start, pointer);
pageLines.add(tempLine);
buildPage(pages, pageLines, forcePageEnd);
forcePageEnd = false;

start = pointer;
length = 0;
pageLength += 1;
}

pageLength++;

if (letter == '\u00a7' && pointer + 1 < lineLength)
{
Character nextLetter = pageLine.charAt(pointer + 1);
Expand All @@ -94,17 +117,26 @@ public List<String> getPages(final String pageStr) throws Exception
}
pointer++;
}
else if (letter == 'i' || letter == '.' || letter == ',')
else if (letter == 'i' || letter == '.' || letter == ',' || letter == '!' || letter == ':' || letter == ';' || letter == '|')
{
length += (0.34 * weight);
}
else if (letter == 'l' || letter == '\'' || letter == '`')
{
length += (0.4 * weight);
length += (0.53 * weight);
}
else if (letter == 'l')
else if (letter == ' ' || letter == 't' || letter == 'I' || letter == '[' || letter == ']')
{
length += (0.6 * weight);
length += (0.69 * weight);
}
else if (letter == ' ' || letter == 't')
else if (letter == 'f' || letter == 'k' || letter == '"' || letter == '*'
|| letter == '(' || letter == ')' || letter == '{' || letter == '}' || letter == '<' || letter == '>')
{
length += (0.7 * weight);
length += (0.85 * weight);
}
else if (letter == '@' || letter == '~')
{
length += (1.2 * weight);
}
else
{
Expand All @@ -117,21 +149,25 @@ else if (letter == ' ' || letter == 't')
{
tempLine = pageLine.substring(start, lineLength);
pageLines.add(tempLine);
buildPage(pages, pageLines, false);
}
}

List<String> pages = new ArrayList<String>();
for (int count = 0; count < pageLines.size(); count += 12)
buildPage(pages, pageLines, true);
return pages;
}

void buildPage(List<String> pages, List<String> lines, boolean override)
{
if (override || lines.size() > lineMax)
{
StringBuilder newPage = new StringBuilder();
for (int i = count; i < count + 12 && i < pageLines.size(); i++)
for (String aline : lines)
{
newPage.append(pageLines.get(i)).append("\n");
newPage.append(aline).append("\n");
}

pages.add(newPage.toString());
lines.clear();
}

return pages;
}
}

5 comments on commit f272a7e

@kangarko
Copy link

Choose a reason for hiding this comment

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

This commit causes my book´s text to be distorted and completely broken. Before this it was working perfectly fine. Paste: http://pastebin.com/M8FMQZmJ

This is a severe problem for me and my new players who gets the book named Ultracraft.

@khobbits
Copy link
Member Author

Choose a reason for hiding this comment

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

I tried your book with the previous version, and I found several lines which were missing, that this commit fixes, looks like there was a bug with line paging however your file highlighted...

Just pushed something to fix a pager bug, see if that fixes it.
I tried your book config, and it looks like you might want to adjust your book a little, to deal with the colour formatting change.
(We no longer reset the colour on each line)

@kangarko
Copy link

Choose a reason for hiding this comment

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

Thank you,
it seems to be fixed!
But I need also to manually add some blank lines which
are not added by " " (space) now. Can you add some special
line separator line \n ? Thx.

Here is edited book: http://pastebin.com/rxH2RQCk
I need a blank new line where the "&7\n" character is.

Edit: I noticed the &r is not resetting color, is it intended to be like this?

@khobbits
Copy link
Member Author

Choose a reason for hiding this comment

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

The colour system is not made by us, we're just passing the information to the minecraft server, which passes it to the client.

The reason we have to do so much work to it first, is to work out how much text to send per page, as we have to send each page as a separate text file.

I was adding a &0 to the start of each line before, but since i've found out that colour codes count as letters in the page limit, I removed them to increase the number of letters you can use on each page.

Prior to this we weren't checking the page limits only the line limits.

You can still make blank lines by adding a colour code on an empty line.

@kangarko
Copy link

Choose a reason for hiding this comment

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

Thank you! I appreciate you work ;)

Edit: Just tested, I tried &0 or &1 but it didn´t work.

Please sign in to comment.