summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/textreader/TextPager.java
blob: 13b8aca0017a41d9c17fee73e98dca85f2a26bc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.earth2me.essentials.textreader;

import com.earth2me.essentials.CommandSource;
import com.earth2me.essentials.I18n;
import static com.earth2me.essentials.I18n.tl;
import java.util.List;
import java.util.Locale;
import java.util.Map;


public class TextPager
{
	private final transient IText text;
	private final transient boolean onePage;

	public TextPager(final IText text)
	{
		this(text, false);
	}

	public TextPager(final IText text, final boolean onePage)
	{
		this.text = text;
		this.onePage = onePage;
	}

	public void showPage(final String pageStr, final String chapterPageStr, final String commandName, final CommandSource sender)
	{
		List<String> lines = text.getLines();
		List<String> chapters = text.getChapters();
		Map<String, Integer> bookmarks = text.getBookmarks();

		//This code deals with the initial chapter.  We use this to display the initial output or contents.
		//We also use this code to display some extra information if we don't intend to use chapters
		if (pageStr == null || pageStr.isEmpty() || pageStr.matches("[0-9]+"))
		{
			//If an info file starts with a chapter title, list the chapters
			//If not display the text up until the first chapter.
			if (!lines.isEmpty() && lines.get(0).startsWith("#"))
			{
				if (onePage)
				{
					return;
				}
				sender.sendMessage(tl("infoChapter"));
				final StringBuilder sb = new StringBuilder();
				boolean first = true;
				for (String string : chapters)
				{
					if (!first)
					{
						sb.append(", ");
					}
					first = false;
					sb.append(string);
				}
				sender.sendMessage(sb.toString());
				return;
			}
			else
			{
				int page = 1;
				try
				{
					page = Integer.parseInt(pageStr);
				}
				catch (NumberFormatException ex)
				{
					page = 1;
				}
				if (page < 1)
				{
					page = 1;
				}

				int start = onePage ? 0 : (page - 1) * 9;
				int end;
				for (end = 0; end < lines.size(); end++)
				{
					String line = lines.get(end);
					if (line.startsWith("#"))
					{
						break;
					}
				}

				int pages = end / 9 + (end % 9 > 0 ? 1 : 0);
				if (!onePage && commandName != null)
				{

					StringBuilder content = new StringBuilder();
					final String[] title = commandName.split(" ", 2);
					if (title.length > 1)
					{
						content.append(I18n.capitalCase(title[0])).append(": ");
						content.append(title[1]);
					}
					else
					{
						content.append(I18n.capitalCase(commandName));
					}
					sender.sendMessage(tl("infoPages", page, pages, content));
				}
				for (int i = start; i < end && i < start + (onePage ? 20 : 9); i++)
				{
					sender.sendMessage("§r" + lines.get(i));
				}
				if (!onePage && page < pages && commandName != null)
				{
					sender.sendMessage(tl("readNextPage", commandName, page + 1));
				}
				return;
			}
		}

		//If we have a chapter, check to see if we have a page number
		int chapterpage = 0;
		if (chapterPageStr != null)
		{
			try
			{
				chapterpage = Integer.parseInt(chapterPageStr) - 1;
			}
			catch (NumberFormatException ex)
			{
				chapterpage = 0;
			}
			if (chapterpage < 0)
			{
				chapterpage = 0;
			}
		}

		//This checks to see if we have the chapter in the index
		if (!bookmarks.containsKey(pageStr.toLowerCase(Locale.ENGLISH)))
		{
			sender.sendMessage(tl("infoUnknownChapter"));
			return;
		}

		//Since we have a valid chapter, count the number of lines in the chapter
		final int chapterstart = bookmarks.get(pageStr.toLowerCase(Locale.ENGLISH)) + 1;
		int chapterend;
		for (chapterend = chapterstart; chapterend < lines.size(); chapterend++)
		{
			final String line = lines.get(chapterend);
			if (line.length() > 0 && line.charAt(0) == '#')
			{
				break;
			}
		}

		//Display the chapter from the starting position
		final int start = chapterstart + (onePage ? 0 : chapterpage * 9);
		final int page = chapterpage + 1;
		final int pages = (chapterend - chapterstart) / 9 + ((chapterend - chapterstart) % 9 > 0 ? 1 : 0);
		if (!onePage && commandName != null)
		{
			StringBuilder content = new StringBuilder();
			content.append(I18n.capitalCase(commandName)).append(": ");
			content.append(pageStr);
			sender.sendMessage(tl("infoChapterPages", content, page, pages));
		}
		for (int i = start; i < chapterend && i < start + (onePage ? 20 : 9); i++)
		{
			sender.sendMessage("§r" + lines.get(i));
		}
		if (!onePage && page < pages && commandName != null)
		{
			sender.sendMessage(tl("readNextPage", commandName, pageStr + " " + (page + 1)));
		}
	}
}