From 1b40cea51fa3199718142b35b371bbfcff1fa705 Mon Sep 17 00:00:00 2001 From: RedstoneFuture Date: Wed, 20 Mar 2024 20:00:22 +0100 Subject: [PATCH] Feat: adding "last" argument for Visit cmd to choose the highest number (#4219) * Refactoring, Adding "last" argument to visit cmd * Adding reversed plot format * fixing tab-completion of "last" argument * reformatting the code-style --- .../com/plotsquared/core/command/Visit.java | 84 ++++++++++++------- 1 file changed, 53 insertions(+), 31 deletions(-) diff --git a/Core/src/main/java/com/plotsquared/core/command/Visit.java b/Core/src/main/java/com/plotsquared/core/command/Visit.java index c1a03a559..1eeea894d 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Visit.java +++ b/Core/src/main/java/com/plotsquared/core/command/Visit.java @@ -77,6 +77,7 @@ public class Visit extends Command { query.whereBasePlot(); } + // without specified argument if (page == Integer.MIN_VALUE) { page = 1; } @@ -94,10 +95,15 @@ public class Visit extends Command { final List plots = query.asList(); + // Conversion of reversed page argument + if (page < 0) { + page = (plots.size() + 1) + page; + } + if (plots.isEmpty()) { player.sendMessage(TranslatableCaption.of("invalid.found_no_plots")); return; - } else if (plots.size() < page || page < 1) { + } else if (page > plots.size() || page < 1) { player.sendMessage( TranslatableCaption.of("invalid.number_not_in_range"), TagResolver.builder() @@ -188,34 +194,22 @@ public class Visit extends Command { int page = Integer.MIN_VALUE; switch (args.length) { - // /p v + // /p v case 3: - if (!MathMan.isInteger(args[2])) { - player.sendMessage( - TranslatableCaption.of("invalid.not_valid_number"), - TagResolver.resolver("value", Tag.inserting(Component.text("(1, ∞)"))) - ); - player.sendMessage( - TranslatableCaption.of("commandconfig.command_syntax"), - TagResolver.resolver("value", Tag.inserting(Component.text(getUsage()))) - ); + if (isInvalidPageNr(args[2])) { + sendInvalidPageNrMsg(player); return CompletableFuture.completedFuture(false); } - page = Integer.parseInt(args[2]); - // /p v [page] - // /p v [page] + page = getPageNr(args[2]); + // /p v [page] + // /p v [page] case 2: - if (page != Integer.MIN_VALUE || !MathMan.isInteger(args[1])) { + // If "case 3" is already through or the argument is not a page number: + // -> /p v [page] + if (page != Integer.MIN_VALUE || isInvalidPageNr(args[1])) { sortByArea = this.plotAreaManager.getPlotAreaByString(args[1]); if (sortByArea == null) { - player.sendMessage( - TranslatableCaption.of("invalid.not_valid_number"), - TagResolver.resolver("value", Tag.inserting(Component.text("(1, ∞)"))) - ); - player.sendMessage( - TranslatableCaption.of("commandconfig.command_syntax"), - TagResolver.resolver("value", Tag.inserting(Component.text(getUsage()))) - ); + sendInvalidPageNrMsg(player); return CompletableFuture.completedFuture(false); } @@ -249,16 +243,13 @@ public class Visit extends Command { }); break; } - try { - page = Integer.parseInt(args[1]); - } catch (NumberFormatException ignored) { - player.sendMessage( - TranslatableCaption.of("invalid.not_a_number"), - TagResolver.resolver("value", Tag.inserting(Component.text(args[1]))) - ); + // -> /p v + if (isInvalidPageNr(args[1])) { + sendInvalidPageNrMsg(player); return CompletableFuture.completedFuture(false); } - // /p v [page] + page = getPageNr(args[1]); + // /p v [page] // /p v [page] // /p v [page] // /p v @@ -326,6 +317,35 @@ public class Visit extends Command { return CompletableFuture.completedFuture(true); } + private boolean isInvalidPageNr(String arg) { + if (MathMan.isInteger(arg)) { + return false; + } else if (arg.equals("last") || arg.equals("n")) { + return false; + } + return true; + } + + private int getPageNr(String arg) { + if (MathMan.isInteger(arg)) { + return Integer.parseInt(arg); + } else if (arg.equals("last") || arg.equals("n")) { + return -1; + } + return Integer.MIN_VALUE; + } + + private void sendInvalidPageNrMsg(PlotPlayer player) { + player.sendMessage( + TranslatableCaption.of("invalid.not_valid_number"), + TagResolver.resolver("value", Tag.inserting(Component.text("(1, ∞)"))) + ); + player.sendMessage( + TranslatableCaption.of("commandconfig.command_syntax"), + TagResolver.resolver("value", Tag.inserting(Component.text(getUsage()))) + ); + } + @Override public Collection tab(PlotPlayer player, String[] args, boolean space) { final List completions = new ArrayList<>(); @@ -334,6 +354,7 @@ public class Visit extends Command { case 1 -> { completions.addAll( TabCompletions.completeAreas(args[1])); + completions.addAll(TabCompletions.asCompletions("last")); if (args[1].isEmpty()) { // if no input is given, only suggest 1 - 3 completions.addAll( @@ -344,6 +365,7 @@ public class Visit extends Command { TabCompletions.completeNumbers(args[1], 10, 999)); } case 2 -> { + completions.addAll(TabCompletions.asCompletions("last")); if (args[2].isEmpty()) { // if no input is given, only suggest 1 - 3 completions.addAll(