diff --git a/Core/src/main/java/com/plotsquared/core/command/HomeCommand.java b/Core/src/main/java/com/plotsquared/core/command/HomeCommand.java new file mode 100644 index 000000000..779d97ef2 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/command/HomeCommand.java @@ -0,0 +1,169 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.plotsquared.core.command; + +import com.plotsquared.core.PlotSquared; +import com.plotsquared.core.configuration.Captions; +import com.plotsquared.core.events.TeleportCause; +import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.plot.Plot; +import com.plotsquared.core.plot.PlotArea; +import com.plotsquared.core.plot.PlotId; +import com.plotsquared.core.util.MainUtil; +import com.plotsquared.core.util.MathMan; +import com.plotsquared.core.util.query.PlotQuery; +import com.plotsquared.core.util.query.SortingStrategy; +import com.plotsquared.core.util.task.RunnableVal2; +import com.plotsquared.core.util.task.RunnableVal3; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.concurrent.CompletableFuture; + +@CommandDeclaration(command = "home", + description = "Teleport to your plot(s)", + permission = "plots.home", + usage = "/plot home [||| ]", + aliases = {"h"}, + requiredType = RequiredType.PLAYER, + category = CommandCategory.TELEPORT) +public class HomeCommand extends Command { + public HomeCommand() { + super(MainCommand.getInstance(), true); + } + + private void home(@NotNull final PlotPlayer player, + @NotNull final PlotQuery query, final int page, + final RunnableVal3 confirm, + final RunnableVal2 whenDone) { + List plots = query.asList(); + if (plots.isEmpty()) { + Captions.FOUND_NO_PLOTS.send(player); + return; + } else if (plots.size() < page) { + MainUtil.sendMessage(player, + String.format(Captions.NUMBER_NOT_IN_RANGE.getTranslated(), "1", plots.size())); + return; + } + Plot plot = plots.get(page - 1); + confirm.run(this, () -> plot.teleportPlayer(player, TeleportCause.COMMAND, result -> { + if (result) { + whenDone.run(this, CommandResult.SUCCESS); + } else { + whenDone.run(HomeCommand.this, CommandResult.FAILURE); + } + }), () -> whenDone.run(HomeCommand.this, CommandResult.FAILURE)); + } + + @NotNull private PlotQuery query(@NotNull final PlotPlayer player) { + // everything plots need to have in common here + return PlotQuery.newQuery().ownedBy(player).whereBasePlot(); + } + + @Override public CompletableFuture execute(PlotPlayer player, String[] args, + RunnableVal3 confirm, + RunnableVal2 whenDone) throws CommandException { + // /plot home (or page, whatever it's called) + // /plot home + // /plot home <[area;]x;y> + // /plot home + // /plot home + if (args.length > 2) { + Captions.COMMAND_SYNTAX.send(player, getUsage()); + return CompletableFuture.completedFuture(false); + } + PlotQuery query = query(player); + int page = 1; // page = index + 1 + String identifier; + switch (args.length) { + case 1: + identifier = args[0]; + if (MathMan.isInteger(identifier)) { + try { + page = Integer.parseInt(identifier); + } catch (NumberFormatException ignored) { + Captions.NOT_A_NUMBER.send(player, identifier); + return CompletableFuture.completedFuture(false); + } + query.withSortingStrategy(SortingStrategy.SORT_BY_CREATION); + break; + } + // either plot id or alias + Plot fromId = MainUtil.getPlotFromString(player, identifier, false); + if (fromId != null && fromId.isOwner(player.getUUID())) { + // it was a valid plot id + query.withPlot(fromId); + break; + } + // it wasn't a valid plot id, trying to find plot by alias + query.withAlias(identifier); + break; + case 2: + // we assume args[0] is a plot area and args[1] an identifier + PlotArea plotArea = PlotSquared.get().getPlotAreaByString(args[0]); + identifier = args[1]; + if (plotArea == null) { + // invalid command, therefore no plots + query.noPlots(); + break; + } + query.inArea(plotArea); + if (MathMan.isInteger(identifier)) { + // identifier is a page number + try { + page = Integer.parseInt(identifier); + } catch (NumberFormatException ignored) { + Captions.NOT_A_NUMBER.send(player, identifier); + return CompletableFuture.completedFuture(false); + } + query.withSortingStrategy(SortingStrategy.SORT_BY_CREATION); + break; + } + // identifier needs to be a plot id then + PlotId id = PlotId.fromStringOrNull(identifier); + if (id == null) { + // invalid command, therefore no plots + query.noPlots(); + break; + } + // we can try to get this plot + Plot plot = plotArea.getPlot(id); + if (plot == null) { + query.noPlots(); + break; + } + // as the query already filters by owner, this is fine + query.withPlot(plot); + break; + case 0: + query.withSortingStrategy(SortingStrategy.SORT_BY_CREATION); + break; + } + home(player, query, page, confirm, whenDone); + return CompletableFuture.completedFuture(true); + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/command/MainCommand.java b/Core/src/main/java/com/plotsquared/core/command/MainCommand.java index a5c42b117..0df008815 100644 --- a/Core/src/main/java/com/plotsquared/core/command/MainCommand.java +++ b/Core/src/main/java/com/plotsquared/core/command/MainCommand.java @@ -77,6 +77,7 @@ public class MainCommand extends Command { new RegenAllRoads(); new Claim(); new Auto(); + new HomeCommand(); new Visit(); new Set(); new Clear(); 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 f5243b4ba..e2faaf19e 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Visit.java +++ b/Core/src/main/java/com/plotsquared/core/command/Visit.java @@ -54,8 +54,8 @@ import java.util.concurrent.TimeoutException; @CommandDeclaration(command = "visit", permission = "plots.visit", description = "Visit someones plot", - usage = "/plot visit [|||] [#]", - aliases = {"v", "tp", "teleport", "goto", "home", "h", "warp"}, + usage = "/plot visit <||> [#]", + aliases = {"v", "tp", "teleport", "goto", "warp"}, requiredType = RequiredType.PLAYER, category = CommandCategory.TELEPORT) public class Visit extends Command { @@ -155,7 +155,7 @@ public class Visit extends Command { switch (args.length) { // /p v [...] [...] case 3: - if (!MathMan.isInteger(args[1])) { + if (!MathMan.isInteger(args[2])) { Captions.NOT_VALID_NUMBER.send(player, "(1, ∞)"); Captions.COMMAND_SYNTAX.send(player, getUsage()); return CompletableFuture.completedFuture(false); @@ -188,24 +188,13 @@ public class Visit extends Command { } page = Integer.parseInt(args[1]); // /p v [page] - // /p v [page] // /p v [page] // /p v [page] case 1: final String[] finalArgs = args; int finalPage = page; - // Try to determine whether the given argument is a username - // or an ordinal - boolean isNumber = false; - if (args[0].length() < 2) { - isNumber = true; - } else if (args[0].length() <= 4 && MathMan.isInteger(args[0])) { - // Check if it's an all-digit username that is stored in cache - final UUIDMapping mapping = PlotSquared.get().getImpromptuUUIDPipeline().getImmediately(args[0]); - // If no UUID could be found, then we assume it's a number and not a username - isNumber = mapping == null; - } - if (!isNumber && args[0].length() >= 2 && !args[0].contains(";") && !args[0].contains(",")) { + + if (args[0].length() >= 2 && !args[0].contains(";") && !args[0].contains(",")) { PlotSquared.get().getImpromptuUUIDPipeline().getSingle(args[0], (uuid, throwable) -> { if (throwable instanceof TimeoutException) { // The request timed out @@ -214,61 +203,31 @@ public class Visit extends Command { // It was a valid UUID but the player has no plots MainUtil.sendMessage(player, Captions.PLAYER_NO_PLOTS); } else if (uuid == null) { - if (finalPage == Integer.MIN_VALUE && MathMan.isInteger(finalArgs[0])) { - // The argument was a number, so we assume it's the page number - int parsedPage; - try { - parsedPage = Integer.parseInt(finalArgs[0]); - } catch (final Throwable t) { - MainUtil.sendMessage(player, Captions.NOT_A_NUMBER, finalArgs[0]); - return; - } - this.visit(player, PlotQuery.newQuery().ownedBy(player).whereBasePlot(), null, - confirm, whenDone, parsedPage); - } else { - // Try to parse a plot - final Plot plot = MainUtil.getPlotFromString(player, finalArgs[0], true); - if (plot == null) { - MainUtil.sendMessage(player, Captions.NOT_VALID_PLOT_ID); - return; - } - this.visit(player, PlotQuery.newQuery().withPlot(plot), null, confirm, whenDone, 1); - } + // player not found + MainUtil.sendMessage(player, Captions.INVALID_PLAYER, finalArgs[0]); } else { this.visit(player, PlotQuery.newQuery().ownedBy(uuid).whereBasePlot(), null, confirm, whenDone, finalPage); } }); } else { - if (finalPage == Integer.MIN_VALUE && MathMan.isInteger(finalArgs[0])) { - // The argument was a number, so we assume it's the page number - int parsedPage; - try { - parsedPage = Integer.parseInt(finalArgs[0]); - this.visit(player, PlotQuery.newQuery().ownedBy(player).whereBasePlot(), null, confirm, - whenDone, parsedPage); - } catch (final Throwable throwable) { - MainUtil.sendMessage(player, Captions.NOT_A_NUMBER, finalArgs[0]); - } - } else { - // Try to parse a plot - final Plot plot = MainUtil.getPlotFromString(player, finalArgs[0], true); - if (plot != null) { - this.visit(player, PlotQuery.newQuery().withPlot(plot), null, confirm, whenDone, 1); - } + // Try to parse a plot + final Plot plot = MainUtil.getPlotFromString(player, finalArgs[0], true); + if (plot != null) { + this.visit(player, PlotQuery.newQuery().withPlot(plot), null, confirm, whenDone, 1); } } break; case 0: - // /p v - this.visit(player, PlotQuery.newQuery().ownedBy(player), null, confirm, whenDone); - break; + // /p v is invalid + Captions.COMMAND_SYNTAX.send(player, getUsage()); + return CompletableFuture.completedFuture(false); default: } return CompletableFuture.completedFuture(true); } - public Collection tab(PlotPlayer player, String[] args, boolean space) { + @Override public Collection tab(PlotPlayer player, String[] args, boolean space) { final List completions = new LinkedList<>(); switch (args.length - 1) { case 0: