Skip to content

Commit

Permalink
Fix some bugs with command components and add hasPage to paginator
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Jun 26, 2022
1 parent 5e0709e commit 596940a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
19 changes: 13 additions & 6 deletions src/main/java/net/citizensnpcs/api/util/Messaging.java
Expand Up @@ -25,6 +25,7 @@
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.chat.hover.content.Text;

public class Messaging {
Expand Down Expand Up @@ -104,6 +105,9 @@ private static void parseAndSendComponents(CommandSender sender, String message)
Matcher m = COMPONENT_MATCHER.matcher(message);
int end = 0;
while (m.find()) {
if (m.start() != end) {
builder.append(MESSAGE_COLOUR + message.substring(end, m.start()));
}
String text = m.group(1);
String type = m.group(2);
String command = m.group(3);
Expand All @@ -119,20 +123,23 @@ private static void parseAndSendComponents(CommandSender sender, String message)
action = ClickEvent.Action.SUGGEST_COMMAND;
break;
}
builder.append(text);
end = m.end();
if (action != null) {
builder.underlined(true);
builder.event(new ClickEvent(action, command));
text = MESSAGE_COLOUR + ChatColor.UNDERLINE + text;
}
if (m.groupCount() > 3) {
TextComponent tc = new TextComponent(text);
if (action != null) {
tc.setClickEvent(new ClickEvent(action, command));
}
builder.append(tc);
end = m.end();
if (m.groupCount() > 3 && m.group(4) != null) {
builder.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(m.group(4).substring(1))));
} else {
builder.event((HoverEvent) null);
}
}
if (end - 1 < message.length()) {
builder.append(message.substring(end));
builder.append(MESSAGE_COLOUR + message.substring(end));
builder.event((ClickEvent) null);
builder.event((HoverEvent) null);
builder.underlined(false);
Expand Down
40 changes: 39 additions & 1 deletion src/main/java/net/citizensnpcs/api/util/Paginator.java
@@ -1,6 +1,7 @@
package net.citizensnpcs.api.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.bukkit.command.CommandSender;
Expand All @@ -9,6 +10,20 @@ public class Paginator {
private boolean console;
private String header;
private final List<String> lines = new ArrayList<String>();
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);
Expand All @@ -19,6 +34,11 @@ public Paginator console(boolean console) {
return this;
}

public Paginator enablePageSwitcher() {
pageSwitcher = true;
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
Expand All @@ -29,7 +49,16 @@ public String getPageText(int page) {
int startIndex = linesPerPage * page - linesPerPage;
int endIndex = page * linesPerPage;

String text = header == null ? "" : wrapHeader("[[" + header + " <f>" + page + "/" + pages);
String pageDisplay = page + "/" + pages;
if (pageSwitcher) {
if (page > 1) {
pageDisplay = "<<<f>< :command(page " + (page - 1) + "):Previous page>><f>" + pageDisplay;
}
if (pages > 1 && page != pages) {
pageDisplay = pageDisplay + "<<<f> >:command(page " + (page + 1) + "):Next page>>";
}
}
String text = header == null ? "" : wrapHeader("[[" + header + " <f>" + pageDisplay);

if (lines.size() < endIndex)
endIndex = lines.size();
Expand All @@ -39,6 +68,15 @@ public String getPageText(int page) {
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;
Expand Down

0 comments on commit 596940a

Please sign in to comment.