Use PlotQuery in Visit

This commit is contained in:
Alexander Söderberg 2020-05-24 00:27:38 +02:00 committed by Alexander Söderberg
parent 76913d4a78
commit 0ac6383c2c
4 changed files with 105 additions and 16 deletions

View File

@ -36,13 +36,13 @@ import com.plotsquared.core.plot.flag.implementations.UntrustedVisitFlag;
import com.plotsquared.core.util.MainUtil; import com.plotsquared.core.util.MainUtil;
import com.plotsquared.core.util.MathMan; import com.plotsquared.core.util.MathMan;
import com.plotsquared.core.util.Permissions; import com.plotsquared.core.util.Permissions;
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.RunnableVal2;
import com.plotsquared.core.util.task.RunnableVal3; import com.plotsquared.core.util.task.RunnableVal3;
import com.plotsquared.core.util.uuid.UUIDHandler; import com.plotsquared.core.util.uuid.UUIDHandler;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
@ -72,9 +72,11 @@ public class Visit extends Command {
args = args[0].split(":"); args = args[0].split(":");
} }
int page = Integer.MIN_VALUE; int page = Integer.MIN_VALUE;
Collection<Plot> unsorted = null;
PlotArea sortByArea = player.getApplicablePlotArea(); PlotArea sortByArea = player.getApplicablePlotArea();
boolean shouldSortByArea = Settings.Teleport.PER_WORLD_VISIT; boolean shouldSortByArea = Settings.Teleport.PER_WORLD_VISIT;
final PlotQuery query = PlotQuery.newQuery();
switch (args.length) { switch (args.length) {
case 3: case 3:
if (!MathMan.isInteger(args[1])) { if (!MathMan.isInteger(args[1])) {
@ -96,7 +98,7 @@ public class Visit extends Command {
Captions.COMMAND_SYNTAX.send(player, getUsage()); Captions.COMMAND_SYNTAX.send(player, getUsage());
return CompletableFuture.completedFuture(false); return CompletableFuture.completedFuture(false);
} }
unsorted = PlotSquared.get().getBasePlots(user); query.ownedBy(user).whereBasePlot();
shouldSortByArea = true; shouldSortByArea = true;
break; break;
} }
@ -108,21 +110,21 @@ public class Visit extends Command {
} }
if (page == Integer.MIN_VALUE && user == null && MathMan.isInteger(args[0])) { if (page == Integer.MIN_VALUE && user == null && MathMan.isInteger(args[0])) {
page = Integer.parseInt(args[0]); page = Integer.parseInt(args[0]);
unsorted = PlotSquared.get().getBasePlots(player); query.ownedBy(player).whereBasePlot();
break; break;
} }
if (user != null) { if (user != null) {
unsorted = PlotSquared.get().getBasePlots(user); query.ownedBy(player).whereBasePlot();
} else { } else {
Plot plot = MainUtil.getPlotFromString(player, args[0], true); Plot plot = MainUtil.getPlotFromString(player, args[0], true);
if (plot != null) { if (plot != null) {
unsorted = Collections.singletonList(plot.getBasePlot(false)); query.withPlot(plot);
} }
} }
break; break;
case 0: case 0:
page = 1; page = 1;
unsorted = PlotSquared.get().getPlots(player); query.ownedBy(player);
break; break;
default: default:
@ -130,25 +132,33 @@ public class Visit extends Command {
if (page == Integer.MIN_VALUE) { if (page == Integer.MIN_VALUE) {
page = 1; page = 1;
} }
if (unsorted == null || unsorted.isEmpty()) {
// We get the query once,
// then we get it another time further on
final List<Plot> unsorted = query.asList();
if (unsorted.isEmpty()) {
Captions.FOUND_NO_PLOTS.send(player); Captions.FOUND_NO_PLOTS.send(player);
return CompletableFuture.completedFuture(false); return CompletableFuture.completedFuture(false);
} }
unsorted = new ArrayList<>(unsorted);
if (unsorted.size() > 1) { if (unsorted.size() > 1) {
unsorted.removeIf(plot -> !plot.isBasePlot()); query.whereBasePlot();
} }
if (page < 1 || page > unsorted.size()) { if (page < 1 || page > unsorted.size()) {
Captions.NOT_VALID_NUMBER.send(player, "(1, " + unsorted.size() + ")"); Captions.NOT_VALID_NUMBER.send(player, "(1, " + unsorted.size() + ")");
return CompletableFuture.completedFuture(false); return CompletableFuture.completedFuture(false);
} }
List<Plot> plots;
if (shouldSortByArea) { if (shouldSortByArea) {
plots = PlotSquared.get() query.relativeToArea(sortByArea).withSortingStrategy(SortingStrategy.SORT_BY_CREATION);
.sortPlots(unsorted, PlotSquared.SortType.CREATION_DATE, sortByArea);
} else { } else {
plots = PlotSquared.get().sortPlotsByTemp(unsorted); query.withSortingStrategy(SortingStrategy.SORT_BY_TEMP);
} }
final List<Plot> plots = query.asList();
final Plot plot = plots.get(page - 1); final Plot plot = plots.get(page - 1);
if (!plot.hasOwner()) { if (!plot.hasOwner()) {
if (!Permissions.hasPermission(player, Captions.PERMISSION_VISIT_UNOWNED)) { if (!Permissions.hasPermission(player, Captions.PERMISSION_VISIT_UNOWNED)) {
@ -177,6 +187,7 @@ public class Visit extends Command {
return CompletableFuture.completedFuture(false); return CompletableFuture.completedFuture(false);
} }
} }
confirm.run(this, () -> plot.teleportPlayer(player, TeleportCause.COMMAND, result -> { confirm.run(this, () -> plot.teleportPlayer(player, TeleportCause.COMMAND, result -> {
if (result) { if (result) {
whenDone.run(Visit.this, CommandResult.SUCCESS); whenDone.run(Visit.this, CommandResult.SUCCESS);

View File

@ -0,0 +1,46 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* 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.util.query;
import com.plotsquared.core.plot.Plot;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.Collections;
class FixedPlotProvider implements PlotProvider {
private final Plot plot;
FixedPlotProvider(@NotNull final Plot plot) {
this.plot = plot;
}
@Override public Collection<Plot> getPlots() {
return Collections.singleton(plot);
}
}

View File

@ -38,6 +38,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -60,6 +61,7 @@ public final class PlotQuery {
private PlotProvider plotProvider = new GlobalPlotProvider(); private PlotProvider plotProvider = new GlobalPlotProvider();
private SortingStrategy sortingStrategy = SortingStrategy.NO_SORTING; private SortingStrategy sortingStrategy = SortingStrategy.NO_SORTING;
private PlotArea priorityArea; private PlotArea priorityArea;
private Comparator<Plot> plotComparator;
private PlotQuery() { private PlotQuery() {
} }
@ -151,6 +153,17 @@ public final class PlotQuery {
return this; return this;
} }
/**
* Query with a pre-defined result
*
* @return The query instance
*/
@NotNull public PlotQuery withPlot(@NotNull final Plot plot) {
Preconditions.checkNotNull(plot, "Plot may not be null");
this.plotProvider = new FixedPlotProvider(plot);
return this;
}
/** /**
* Query for base plots only * Query for base plots only
* *
@ -228,6 +241,19 @@ public final class PlotQuery {
return this; return this;
} }
/**
* Use a custom comparator to sort the results
*
* @param comparator Comparator
* @return The query instance
*/
@NotNull public PlotQuery sorted(@NotNull final Comparator<Plot> comparator) {
Preconditions.checkNotNull(comparator, "Comparator may not be null");
this.sortingStrategy = SortingStrategy.COMPARATOR;
this.plotComparator = comparator;
return this;
}
/** /**
* Defines the area around which plots may be sorted, depending on the * Defines the area around which plots may be sorted, depending on the
* sorting strategy * sorting strategy
@ -313,6 +339,8 @@ public final class PlotQuery {
}); });
} else if (this.sortingStrategy == SortingStrategy.SORT_BY_CREATION) { } else if (this.sortingStrategy == SortingStrategy.SORT_BY_CREATION) {
return PlotSquared.get().sortPlots(result, PlotSquared.SortType.CREATION_DATE, this.priorityArea); return PlotSquared.get().sortPlots(result, PlotSquared.SortType.CREATION_DATE, this.priorityArea);
} else if (this.sortingStrategy == SortingStrategy.COMPARATOR) {
result.sort(this.plotComparator);
} }
return result; return result;
} }

View File

@ -48,5 +48,9 @@ public enum SortingStrategy {
/** /**
* Sort by creation date * Sort by creation date
*/ */
SORT_BY_CREATION SORT_BY_CREATION,
/**
* Sort using a comparator
*/
COMPARATOR;
} }