/
Paginator.java
110 lines (91 loc) · 3.14 KB
/
Paginator.java
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
package net.citizensnpcs.api.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.bukkit.command.CommandSender;
public class Paginator {
private boolean console;
private String header;
private final List<String> lines = new ArrayList<String>();
private String pageCommand;
private boolean pageSwitcher;
public Paginator() {
}
public Paginator(Collection<String> lines) {
this.lines.addAll(lines);
}
public Paginator(int initialLinesOfText) {
for (int i = 0; i < initialLinesOfText; i++) {
lines.add("");
}
}
public void addLine(String line) {
lines.add(line);
}
public Paginator console(boolean console) {
this.console = console;
return this;
}
public Paginator enablePageSwitcher() {
pageSwitcher = true;
pageCommand = "page $page";
return this;
}
public Paginator enablePageSwitcher(String command) {
pageSwitcher = true;
pageCommand = command;
return this;
}
public String getPageText(int page) {
int linesPerPage = console ? 200 : LINES_PER_PAGE;
int pages = (int) (Math.ceil((double) lines.size() / linesPerPage) == 0 ? 1
: Math.ceil((double) lines.size() / linesPerPage));
if (page <= 0 || page > pages)
return null;
int startIndex = linesPerPage * page - linesPerPage;
int endIndex = page * linesPerPage;
String pageDisplay = page + "/" + pages;
if (pageSwitcher) {
if (page > 1) {
pageDisplay = "<<<f>< :command(" + pageCommand.replace("$page", "" + (page - 1))
+ "):Previous page>><f>" + pageDisplay;
}
if (pages > 1 && page != pages) {
pageDisplay = pageDisplay + "<<<f> >:command(" + pageCommand.replace("$page", "" + (page + 1))
+ "):Next page>>";
}
}
String text = header == null ? "" : wrapHeader("[[" + header + " <f>" + pageDisplay);
if (lines.size() < endIndex)
endIndex = lines.size();
for (String line : lines.subList(startIndex, endIndex)) {
text += "\n" + line;
}
return text;
}
public boolean hasPage(int page) {
int linesPerPage = console ? 200 : LINES_PER_PAGE;
int pages = (int) (Math.ceil((double) lines.size() / linesPerPage) == 0 ? 1
: Math.ceil((double) lines.size() / linesPerPage));
if (page <= 0 || page > pages)
return false;
return true;
}
public Paginator header(String header) {
this.header = header;
return this;
}
public boolean sendPage(CommandSender sender, int page) {
String text = getPageText(page);
if (text != null) {
Messaging.send(sender, text);
return true;
} else {
return false;
}
}
public static String wrapHeader(Object string) {
return "[[=====[ " + string.toString() + " [[]=====";
}
private static final int LINES_PER_PAGE = 9;
}