Unstupidify Visit

This commit is contained in:
Alexander Söderberg 2020-05-24 20:18:02 +02:00
parent 172bd6f0f2
commit 69cfb431b1
No known key found for this signature in database
GPG Key ID: C0207FF7EA146678
3 changed files with 149 additions and 107 deletions

View File

@ -27,7 +27,6 @@ package com.plotsquared.core.command;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.configuration.Captions;
import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.events.TeleportCause;
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.Plot;
@ -40,14 +39,13 @@ 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.Collection;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
@CommandDeclaration(command = "visit",
permission = "plots.visit",
@ -66,19 +64,13 @@ public class Visit extends Command {
return tabOf(player, args, space, getUsage());
}
@Override
public CompletableFuture<Boolean> execute(final PlotPlayer player, String[] args,
RunnableVal3<Command, Runnable, Runnable> confirm,
final RunnableVal2<Command, CommandResult> whenDone) throws CommandException {
if (args.length == 1 && args[0].contains(":")) {
args = args[0].split(":");
private void visit(@NotNull final PlotPlayer player, @NotNull final PlotQuery query, final PlotArea sortByArea,
final RunnableVal3<Command, Runnable, Runnable> confirm, final RunnableVal2<Command, CommandResult> whenDone) {
this.visit(player, query, sortByArea, confirm, whenDone, 1);
}
final PlotQuery query = PlotQuery.newQuery();
final PlotArea[] sortByArea = new PlotArea[] {player.getApplicablePlotArea()};
final AtomicBoolean shouldSortByArea = new AtomicBoolean(Settings.Teleport.PER_WORLD_VISIT);
final Consumer<Integer> pageConsumer = page -> {
private void visit(@NotNull final PlotPlayer player, @NotNull final PlotQuery query, final PlotArea sortByArea,
final RunnableVal3<Command, Runnable, Runnable> confirm, final RunnableVal2<Command, CommandResult> whenDone, int page) {
// We get the query once,
// then we get it another time further on
final List<Plot> unsorted = query.asList();
@ -92,13 +84,17 @@ public class Visit extends Command {
query.whereBasePlot();
}
if (page == Integer.MIN_VALUE) {
page = 1;
}
if (page < 1 || page > unsorted.size()) {
Captions.NOT_VALID_NUMBER.send(player, "(1, " + unsorted.size() + ")");
MainUtil.sendMessage(player, String.format("(1, %d)", unsorted.size()));
return;
}
if (shouldSortByArea.get()) {
query.relativeToArea(sortByArea[0]).withSortingStrategy(SortingStrategy.SORT_BY_CREATION);
if (sortByArea != null) {
query.relativeToArea(sortByArea).withSortingStrategy(SortingStrategy.SORT_BY_CREATION);
} else {
query.withSortingStrategy(SortingStrategy.SORT_BY_TEMP);
}
@ -141,77 +137,121 @@ public class Visit extends Command {
whenDone.run(Visit.this, CommandResult.FAILURE);
}
}), () -> whenDone.run(Visit.this, CommandResult.FAILURE));
};
}
final int[] page = new int[]{Integer.MIN_VALUE};
@Override
public CompletableFuture<Boolean> execute(final PlotPlayer player,
String[] args,
final RunnableVal3<Command, Runnable, Runnable> confirm,
final RunnableVal2<Command, CommandResult> whenDone) throws CommandException {
if (args.length == 1 && args[0].contains(":")) {
args = args[0].split(":");
}
PlotArea sortByArea;
int page = Integer.MIN_VALUE;
switch (args.length) {
// /p v [...] [...] <page>
case 3:
if (!MathMan.isInteger(args[1])) {
Captions.NOT_VALID_NUMBER.send(player, "(1, ∞)");
Captions.COMMAND_SYNTAX.send(player, getUsage());
return CompletableFuture.completedFuture(false);
}
page[0] = Integer.parseInt(args[2]);
page = Integer.parseInt(args[2]);
// /p v <name> <area> [page]
// /p v <name> [page]
case 2:
if (page[0] != Integer.MIN_VALUE || !MathMan.isInteger(args[1])) {
sortByArea[0] = PlotSquared.get().getPlotAreaByString(args[1]);
if (sortByArea[0] == null) {
if (page != Integer.MIN_VALUE || !MathMan.isInteger(args[1])) {
sortByArea = PlotSquared.get().getPlotAreaByString(args[1]);
if (sortByArea == null) {
Captions.NOT_VALID_NUMBER.send(player, "(1, ∞)");
Captions.COMMAND_SYNTAX.send(player, getUsage());
return CompletableFuture.completedFuture(false);
}
final PlotArea finalSortByArea = sortByArea;
int finalPage1 = page;
MainUtil.getUUIDsFromString(args[0], (uuids, throwable) -> {
if (throwable instanceof TimeoutException) {
Captions.FETCHING_PLAYERS_TIMEOUT.send(player);
} else if (throwable != null || uuids.size() != 1) {
Captions.COMMAND_SYNTAX.send(player, getUsage());
} else {
query.ownedBy(uuids.toArray(new UUID[0])[0]).whereBasePlot();
shouldSortByArea.set(true);
pageConsumer.accept(page[0]);
final UUID uuid = uuids.toArray(new UUID[0])[0];
this.visit(player, PlotQuery.newQuery().ownedBy(uuid).whereBasePlot(), finalSortByArea, confirm, whenDone, finalPage1);
}
});
break;
}
page[0] = Integer.parseInt(args[1]);
page = Integer.parseInt(args[1]);
// /p v <name> [page]
// /p v <page> [page]
// /p v <uuid> [page]
// /p v <plot> [page]
case 1:
final String[] finalArgs = args;
final Consumer<UUID> uuidConsumer = uuid -> {
if (page[0] == Integer.MIN_VALUE && uuid == null && MathMan.isInteger(finalArgs[0])) {
page[0] = Integer.parseInt(finalArgs[0]);
query.ownedBy(player).whereBasePlot();
} else {
if (uuid != null) {
query.ownedBy(player).whereBasePlot();
} else {
Plot plot = MainUtil.getPlotFromString(player, finalArgs[0], true);
if (plot != null) {
query.withPlot(plot);
}
}
}
pageConsumer.accept(page[0]);
};
int finalPage = page;
if (args[0].length() >= 2) {
PlotSquared.get().getImpromptuUUIDPipeline().getSingle(args[0], (uuid, throwable) -> {
if (throwable instanceof TimeoutException) {
// The request timed out
MainUtil.sendMessage(player, Captions.FETCHING_PLAYERS_TIMEOUT);
} else if (uuid != null && !PlotSquared.get().hasPlot(uuid)) {
uuidConsumer.accept(null);
// 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 {
uuidConsumer.accept(uuid);
// 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 {
this.visit(player, PlotQuery.newQuery().ownedBy(uuid).whereBasePlot(), null, confirm, whenDone, finalPage);
}
});
} else {
uuidConsumer.accept(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]);
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) {
MainUtil.sendMessage(player, Captions.NOT_VALID_PLOT_ID);
} else {
this.visit(player, PlotQuery.newQuery().withPlot(plot), null, confirm, whenDone, 1);
}
}
}
break;
case 0:
query.ownedBy(player);
pageConsumer.accept(1);
// /p v
this.visit(player, PlotQuery.newQuery().ownedBy(player), null, confirm, whenDone);
break;
default:
}

View File

@ -445,6 +445,7 @@ public enum Captions implements Caption {
NOT_VALID_WORLD("$2That is not a valid world (case sensitive)", "Errors"),
NOT_VALID_PLOT_WORLD("$2That is not a valid plot area (case sensitive)", "Errors"),
NO_PLOTS("$2You don't have any plots", "Errors"),
PLAYER_NO_PLOTS("$2That player does not own any plots", "Errors"),
WAIT_FOR_TIMER("$2A set block timer is bound to either the current plot or you. Please wait for it to finish", "Errors"),
TILE_ENTITY_CAP_REACHED("$2The total number of tile entities in this chunk may not exceed $1%s", "Errors"),
//</editor-fold>

View File

@ -288,14 +288,15 @@ public final class PlotQuery {
} else {
final Collection<Plot> plots = this.plotProvider.getPlots();
result = new ArrayList<>(plots.size());
for (final Plot plot : plots) {
outer: for (final Plot plot : plots) {
for (final PlotFilter filter : this.filters) {
if (filter.accepts(plot)) {
if (!filter.accepts(plot)) {
continue outer;
}
}
result.add(plot);
}
}
}
}
if (this.sortingStrategy == SortingStrategy.NO_SORTING) {
return result;
} else if (this.sortingStrategy == SortingStrategy.SORT_BY_TEMP) {