Lists paid signs 7 at a time, instead of all at once

This commit is contained in:
Kristian Knarvik 2022-08-05 14:58:54 +02:00
parent 337c4573e0
commit 3eee197fb1
3 changed files with 39 additions and 8 deletions

View File

@ -31,11 +31,17 @@ public class ListCommand extends TokenizedCommand {
@NotNull String[] args) {
super.onCommand(sender, command, label, args);
if (argumentSize < 1) {
displaySigns(sender);
displaySigns(sender, 0);
return true;
} else if (argumentSize < 3) {
try {
int pageNumber = Integer.parseInt(arguments.get(0)) - 1;
displaySigns(sender, pageNumber);
return true;
} catch (NumberFormatException exception) {
return parsePaidSignSelection(sender);
}
}
return false;
}
@ -43,19 +49,37 @@ public class ListCommand extends TokenizedCommand {
* Displays all available signs
*
* @param sender <p>The command sender to display the signs to</p>
* @param pageNumber <p>The page of the sign list to display</p>
*/
private void displaySigns(CommandSender sender) {
private void displaySigns(CommandSender sender, int pageNumber) {
StringBuilder signs = new StringBuilder();
Set<String> signNames = PaidSigns.getInstance().getSignManager().getAllPaidSigns().keySet();
List<String> signNameList = new ArrayList<>(signNames);
Collections.sort(signNameList);
String infoFormat = Translator.getTranslatedMessage(TranslatableMessage.PAID_SIGNS_INFO_FORMAT);
for (String signName : signNameList) {
//Display up to 10 signs per page
int signsPerPage = 7;
int alreadyDisplayed = pageNumber * signsPerPage;
for (int signIndex = alreadyDisplayed; signIndex < alreadyDisplayed + signsPerPage; signIndex++) {
if (signIndex >= signNameList.size()) {
break;
}
String signName = signNameList.get(signIndex);
signs.append(StringFormatter.replacePlaceholder(infoFormat, "{name}", signName));
}
sender.sendMessage(StringFormatter.replacePlaceholder(Translator.getTranslatedMessage(
TranslatableMessage.PAID_SIGNS_INFO), "{signs}", signs.toString()));
//Display that another page exists, if necessary
boolean hasNextPage = alreadyDisplayed + signsPerPage < signNameList.size();
String nextPagePrompt = "";
if (hasNextPage) {
nextPagePrompt = StringFormatter.replacePlaceholder(Translator.getTranslatedMessage(
TranslatableMessage.PAID_SIGNS_NEXT_PROMPT), "{nextPage}", String.valueOf((pageNumber + 2)));
}
sender.sendMessage(StringFormatter.replacePlaceholders(Translator.getTranslatedMessage(
TranslatableMessage.PAID_SIGNS_INFO), new String[]{"{signs}", "{nextPagePrompt}"},
new String[]{signs.toString(), nextPagePrompt}));
}
/**

View File

@ -65,6 +65,11 @@ public enum TranslatableMessage {
*/
PAID_SIGNS_INFO_FORMAT,
/**
* The prompt displayed when the paid sign list has more items
*/
PAID_SIGNS_NEXT_PROMPT,
/**
* The info text used to display information about a paid sign
*/

View File

@ -11,10 +11,12 @@ en:
SUCCESS_REFUNDED: "&bYou were refunded &3{cost} {unit} &bfor your broken sign"
PAID_SIGNS_INFO: |
&f---&3Paid signs&f---
{signs}
{signs}{nextPagePrompt}
&f-----------
PAID_SIGNS_INFO_FORMAT: |
&f| &b{name}
PAID_SIGNS_NEXT_PROMPT: |
&f| &3Use /lps {nextPage} to see more
PAID_SIGN_INFO: |
&f---&3Paid sign&f---
&f| &bName: &3{name}