Split visit command into home and visit

This commit is contained in:
Hannes Greule 2020-07-06 18:13:23 +02:00 committed by Alexander Söderberg
parent 9e9db0f1df
commit c058614fcc
3 changed files with 185 additions and 56 deletions

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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 [<page>|<alias>|<area;x;y>|<area> <x;y>]",
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<Command, Runnable, Runnable> confirm,
final RunnableVal2<Command, CommandResult> whenDone) {
List<Plot> 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<Boolean> execute(PlotPlayer<?> player, String[] args,
RunnableVal3<Command, Runnable, Runnable> confirm,
RunnableVal2<Command, CommandResult> whenDone) throws CommandException {
// /plot home <number> (or page, whatever it's called)
// /plot home <alias>
// /plot home <[area;]x;y>
// /plot home <area> <x;y>
// /plot home <area> <page>
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);
}
}

View File

@ -77,6 +77,7 @@ public class MainCommand extends Command {
new RegenAllRoads(); new RegenAllRoads();
new Claim(); new Claim();
new Auto(); new Auto();
new HomeCommand();
new Visit(); new Visit();
new Set(); new Set();
new Clear(); new Clear();

View File

@ -54,8 +54,8 @@ import java.util.concurrent.TimeoutException;
@CommandDeclaration(command = "visit", @CommandDeclaration(command = "visit",
permission = "plots.visit", permission = "plots.visit",
description = "Visit someones plot", description = "Visit someones plot",
usage = "/plot visit [<player>|<alias>|<world>|<id>] [#]", usage = "/plot visit <<player>|<alias>|<world>> [#]",
aliases = {"v", "tp", "teleport", "goto", "home", "h", "warp"}, aliases = {"v", "tp", "teleport", "goto", "warp"},
requiredType = RequiredType.PLAYER, requiredType = RequiredType.PLAYER,
category = CommandCategory.TELEPORT) category = CommandCategory.TELEPORT)
public class Visit extends Command { public class Visit extends Command {
@ -155,7 +155,7 @@ public class Visit extends Command {
switch (args.length) { switch (args.length) {
// /p v [...] [...] <page> // /p v [...] [...] <page>
case 3: case 3:
if (!MathMan.isInteger(args[1])) { if (!MathMan.isInteger(args[2])) {
Captions.NOT_VALID_NUMBER.send(player, "(1, ∞)"); Captions.NOT_VALID_NUMBER.send(player, "(1, ∞)");
Captions.COMMAND_SYNTAX.send(player, getUsage()); Captions.COMMAND_SYNTAX.send(player, getUsage());
return CompletableFuture.completedFuture(false); return CompletableFuture.completedFuture(false);
@ -188,24 +188,13 @@ public class Visit extends Command {
} }
page = Integer.parseInt(args[1]); page = Integer.parseInt(args[1]);
// /p v <name> [page] // /p v <name> [page]
// /p v <page> [page]
// /p v <uuid> [page] // /p v <uuid> [page]
// /p v <plot> [page] // /p v <plot> [page]
case 1: case 1:
final String[] finalArgs = args; final String[] finalArgs = args;
int finalPage = page; int finalPage = page;
// Try to determine whether the given argument is a username
// or an ordinal if (args[0].length() >= 2 && !args[0].contains(";") && !args[0].contains(",")) {
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(",")) {
PlotSquared.get().getImpromptuUUIDPipeline().getSingle(args[0], (uuid, throwable) -> { PlotSquared.get().getImpromptuUUIDPipeline().getSingle(args[0], (uuid, throwable) -> {
if (throwable instanceof TimeoutException) { if (throwable instanceof TimeoutException) {
// The request timed out // The request timed out
@ -214,61 +203,31 @@ public class Visit extends Command {
// It was a valid UUID but the player has no plots // It was a valid UUID but the player has no plots
MainUtil.sendMessage(player, Captions.PLAYER_NO_PLOTS); MainUtil.sendMessage(player, Captions.PLAYER_NO_PLOTS);
} else if (uuid == null) { } else if (uuid == null) {
if (finalPage == Integer.MIN_VALUE && MathMan.isInteger(finalArgs[0])) { // player not found
// The argument was a number, so we assume it's the page number MainUtil.sendMessage(player, Captions.INVALID_PLAYER, finalArgs[0]);
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);
}
} else { } else {
this.visit(player, PlotQuery.newQuery().ownedBy(uuid).whereBasePlot(), null, confirm, whenDone, finalPage); this.visit(player, PlotQuery.newQuery().ownedBy(uuid).whereBasePlot(), null, confirm, whenDone, finalPage);
} }
}); });
} else { } else {
if (finalPage == Integer.MIN_VALUE && MathMan.isInteger(finalArgs[0])) { // Try to parse a plot
// The argument was a number, so we assume it's the page number final Plot plot = MainUtil.getPlotFromString(player, finalArgs[0], true);
int parsedPage; if (plot != null) {
try { this.visit(player, PlotQuery.newQuery().withPlot(plot), null, confirm, whenDone, 1);
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);
}
} }
} }
break; break;
case 0: case 0:
// /p v // /p v is invalid
this.visit(player, PlotQuery.newQuery().ownedBy(player), null, confirm, whenDone); Captions.COMMAND_SYNTAX.send(player, getUsage());
break; return CompletableFuture.completedFuture(false);
default: default:
} }
return CompletableFuture.completedFuture(true); return CompletableFuture.completedFuture(true);
} }
public Collection<Command> tab(PlotPlayer player, String[] args, boolean space) { @Override public Collection<Command> tab(PlotPlayer player, String[] args, boolean space) {
final List<Command> completions = new LinkedList<>(); final List<Command> completions = new LinkedList<>();
switch (args.length - 1) { switch (args.length - 1) {
case 0: case 0: