summaryrefslogtreecommitdiffstats
path: root/Essentials/src/net/ess3/utils/textreader/TextPager.java
blob: eea8f30e739c182fbcb4d3e1f7f8926b69ca057c (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package net.ess3.utils.textreader;

import java.util.List;
import java.util.Locale;
import java.util.Map;
import net.ess3.I18n;
import static net.ess3.I18n._;
import org.bukkit.command.CommandSender;


public class TextPager
{
	private final IText text;
	private final 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 CommandSender sender)
	{
		List<String> lines = text.getLines();
		List<String> chapters = text.getChapters();
		Map<String, Integer> bookmarks = text.getBookmarks();

		if (bookmarks.isEmpty())
		{
			int page = 1;
			try
			{
				page = Integer.parseInt(pageStr);
			}
			catch (Exception ex)
			{
				page = 1;
			}
			if (page < 1)
			{
				page = 1;
			}

			final int start = onePage ? 0 : (page - 1) * 9;
			final int pages = lines.size() / 9 + (lines.size() % 9 > 0 ? 1 : 0);
			if (!onePage)
			{
				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 if (chapterPageStr != null)
				{
					content.append(I18n.capitalCase(commandName)).append(": ");
					content.append(chapterPageStr);
				}
				else
				{
					content.append(I18n.capitalCase(commandName));
				}
				sender.sendMessage(_("§e ---- §6{2} §e--§6 Page §c{0}§6/§c{1} §e----", page, pages, content));
			}
			for (int i = start; i < lines.size() && i < start + (onePage ? 20 : 9); i++)
			{
				sender.sendMessage(lines.get(i));
			}
			if (!onePage && page < pages)
			{
				sender.sendMessage(_("§6Type§c /{0} {1} §6to read the next page.", commandName, page + 1));
			}
			return;
		}

		if (pageStr == null || pageStr.isEmpty() || pageStr.matches("[0-9]+"))
		{
			if (lines.get(0).startsWith("#"))
			{
				if (onePage)
				{
					return;
				}
				sender.sendMessage(_("Select chapter:"));
				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 (Exception 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)
				{

					sender.sendMessage(_("§e ---- §6{2} §e--§6 Page §c{0}§6/§c{1} §e----", page, pages, I18n.capitalCase(commandName)));
				}
				for (int i = start; i < end && i < start + (onePage ? 20 : 9); i++)
				{
					sender.sendMessage(lines.get(i));
				}
				if (!onePage && page < pages)
				{
					sender.sendMessage(_("§6Type§c /{0} {1} §6to read the next page.", commandName, page + 1));
				}
				return;
			}
		}

		int chapterpage = 0;
		if (chapterPageStr != null)
		{
			try
			{
				chapterpage = Integer.parseInt(chapterPageStr) - 1;
			}
			catch (Exception ex)
			{
				chapterpage = 0;
			}
			if (chapterpage < 0)
			{
				chapterpage = 0;
			}
		}

		if (!bookmarks.containsKey(pageStr.toLowerCase(Locale.ENGLISH)))
		{
			sender.sendMessage(_("Unknown chapter."));
			return;
		}
		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;
			}
		}
		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)
		{
			sender.sendMessage(_("§6Chapter {0}, page §c{1}§6 of §c{2}§6:", pageStr, page, pages));
		}
		for (int i = start; i < chapterend && i < start + (onePage ? 20 : 9); i++)
		{
			sender.sendMessage(lines.get(i));
		}
		if (!onePage && page < pages)
		{
			sender.sendMessage(_("§6Type§c /{0} {1} §6to read the next page.", commandName, pageStr + " " + (page + 1)));
		}
	}
}