diff --git a/Core/src/main/java/com/intellectualcrafters/plot/PS.java b/Core/src/main/java/com/intellectualcrafters/plot/PS.java index ae5c7eca7..c14067b4e 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/PS.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/PS.java @@ -1961,6 +1961,28 @@ public class PS{ return null; } } + + /** + * Get Plots based on alias + * + * @param alias to search plots + * @param worldname to filter alias to a specific world [optional] null means all worlds + * + * @return Set<{@link Plot}> empty if nothing found + */ + public Set getPlotsByAlias(String alias, String worldname) { + Set result = new HashSet<>(); + + if (alias != null) { + for (Plot plot : getPlots()) { + if (alias.equals(plot.getAlias()) && (worldname == null || worldname.equals(plot.getWorldName()))) { + result.add(plot); + } + } + } + + return result; + } public Set getPlotAreas(String world, RegionWrapper region) { PlotArea[] areas = manager.getPlotAreas(world, region); diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Visit.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Visit.java index 49bbd944e..d71b768d7 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Visit.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/Visit.java @@ -5,6 +5,7 @@ import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotArea; +import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.RunnableVal2; import com.intellectualcrafters.plot.object.RunnableVal3; @@ -17,6 +18,7 @@ import com.plotsquared.general.commands.CommandDeclaration; import java.util.Collection; import java.util.Collections; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.UUID; @@ -31,6 +33,9 @@ import java.util.UUID; category = CommandCategory.TELEPORT) public class Visit extends Command { + private static final int PAGE_OUT_OF_RANGE = -998899; // this is to flag Page argument is to long. Can occur if someone enters a large number (some player uses numeric names) + private final int MaxPageRange = 100; + public Visit() { super(MainCommand.getInstance(), true); } @@ -50,54 +55,48 @@ public class Visit extends Command { PlotArea sortByArea = player.getApplicablePlotArea(); boolean shouldSortByArea = Settings.Teleport.PER_WORLD_VISIT; switch (args.length) { - case 3: - if (!MathMan.isInteger(args[1])) { - C.NOT_VALID_NUMBER.send(player, "(1, ∞)"); - C.COMMAND_SYNTAX.send(player, getUsage()); - return; - } - page = Integer.parseInt(args[2]); - case 2: - if (!MathMan.isInteger(args[1])) { - sortByArea = PS.get().getPlotAreaByString(args[1]); - if (sortByArea == null) { - C.NOT_VALID_NUMBER.send(player, "(1, ∞)"); - C.COMMAND_SYNTAX.send(player, getUsage()); - return; - } - UUID user = UUIDHandler.getUUIDFromString(args[0]); - if (user == null) { - C.COMMAND_SYNTAX.send(player, getUsage()); - return; - } - unsorted = PS.get().getBasePlots(user); - shouldSortByArea = true; - break; - } - page = Integer.parseInt(args[1]); - case 1: - UUID user = (args.length == 2 || !MathMan.isInteger(args[0])) ? UUIDHandler.getUUIDFromString(args[0]) : null; - if (page == Integer.MIN_VALUE && user == null && MathMan.isInteger(args[0])) { - page = Integer.parseInt(args[0]); - unsorted = PS.get().getBasePlots(player); - break; - } - if (user != null) { - unsorted = PS.get().getBasePlots(user); - } else { - Plot plot = MainUtil.getPlotFromString(player, args[0], true); - if (plot != null) { - unsorted = Collections.singletonList(plot.getBasePlot(false)); - } - } - break; - case 0: - page = 1; - unsorted = PS.get().getPlots(player); - break; - default: - + case 2: + if (MathMan.isInteger(args[1])) { + page = tryReadPageIdFromArg(player, args[1]); + } else { + C.COMMAND_SYNTAX.send(player, getUsage()); + return; + } + // don't add break here. we handle the first argument in case 1 + case 1: + boolean isCorrectSyntaxWithoutResults = false; + Collection plots = new HashSet(); + if (args[0] != null) { + plots = getPlotsFromSingleArgument(args[0], sortByArea); + if (!plots.isEmpty()) { + unsorted = plots; + } else { + if (MathMan.isInteger(args[0])) { + page = tryReadPageIdFromArg(player, args[0]); + + if (page != PAGE_OUT_OF_RANGE && page != Integer.MIN_VALUE) { + unsorted = PS.get().getPlots(player); + } + } else { + // we know now syntax is correct but no results. + isCorrectSyntaxWithoutResults = true; + } + } + } + + if (!isCorrectSyntaxWithoutResults && plots.isEmpty() && page == Integer.MIN_VALUE) { + C.COMMAND_SYNTAX.send(player, getUsage()); + return; + } + break; + case 0: + unsorted = PS.get().getPlots(player); + break; + default: + C.COMMAND_SYNTAX.send(player, getUsage()); + return; } + if (page == Integer.MIN_VALUE) { page = 1; } @@ -112,7 +111,7 @@ public class Visit extends Command { } } if (page < 1 || page > unsorted.size()) { - C.NOT_VALID_NUMBER.send(player, "(1, " + unsorted.size() + ")"); + C.NOT_VALID_NUMBER.send(player, "(1 - " + unsorted.size() + ")"); return; } List plots; @@ -160,4 +159,65 @@ public class Visit extends Command { }); } + /** + * Get Plots based on an unknown argument type + * Check order: Player, Alias, World, PlotId + * @param argument to search plots + *
+     * Samples:
+     * p h xPlotPlayerGuy	= Player [numeric] [alphanumeric] [UUID]
+     * p h MyPlantage		= Alias  [numeric] [alphanumeric] [Set]
+     * p h PlotworldLarge	= World  [numeric] [alphanumeric] [PlotArea]
+     * p h 1:1		= plotId [numeric] (1,1|1;2)      [PlotId]
+     * 
+ * @param applicablePlotArea the area from the player invoked the command + * @return Collection<{@link Plot}> empty if nothing found + */ + private Collection getPlotsFromSingleArgument(String argument, PlotArea applicablePlotArea) { + + Collection result = new HashSet(); + UUID user = UUIDHandler.getUUIDFromString(argument); + if (user != null) { + result = PS.get().getBasePlots(user); + } else { + result = PS.get().getPlotsByAlias(argument, applicablePlotArea.worldname); + } + + if (result.isEmpty()) { + PlotArea plotArea = PS.get().getPlotArea(argument, "0,0"); + if (plotArea != null) { + result = plotArea.getBasePlots(); + if(result.isEmpty()) { + result = Collections.singletonList(plotArea.getPlot(new PlotId(0, 0))); + } + } + } + + if (result.isEmpty()) { + PlotId plotId = PlotId.fromString(argument); + if (plotId != null) { + result = Collections.singletonList(applicablePlotArea.getPlot(plotId)); + } + } + + return result; + } + + private int tryReadPageIdFromArg(PlotPlayer player, String arg) { + int page = Integer.MIN_VALUE; + + try { + if (MathMan.isInteger(arg)) { + page = Integer.parseInt(arg); + if (page > MaxPageRange) { + page = PAGE_OUT_OF_RANGE; + } + } + } catch (Exception ignored) { + page = PAGE_OUT_OF_RANGE; + C.NOT_VALID_NUMBER.send(player, "(1, ∞)"); + } + + return page; + } }