From 0ac6383c2cf3bc6fe687f70a35876e209db0608d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Sun, 24 May 2020 00:27:38 +0200 Subject: [PATCH] Use PlotQuery in Visit --- .../com/plotsquared/core/command/Visit.java | 41 +++++++++++------ .../core/util/query/FixedPlotProvider.java | 46 +++++++++++++++++++ .../core/util/query/PlotQuery.java | 28 +++++++++++ .../core/util/query/SortingStrategy.java | 6 ++- 4 files changed, 105 insertions(+), 16 deletions(-) create mode 100644 Core/src/main/java/com/plotsquared/core/util/query/FixedPlotProvider.java 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 372130a2d..442c40aeb 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Visit.java +++ b/Core/src/main/java/com/plotsquared/core/command/Visit.java @@ -36,13 +36,13 @@ import com.plotsquared.core.plot.flag.implementations.UntrustedVisitFlag; import com.plotsquared.core.util.MainUtil; import com.plotsquared.core.util.MathMan; 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.RunnableVal3; import com.plotsquared.core.util.uuid.UUIDHandler; -import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.List; import java.util.UUID; import java.util.concurrent.CompletableFuture; @@ -72,9 +72,11 @@ public class Visit extends Command { args = args[0].split(":"); } int page = Integer.MIN_VALUE; - Collection unsorted = null; PlotArea sortByArea = player.getApplicablePlotArea(); boolean shouldSortByArea = Settings.Teleport.PER_WORLD_VISIT; + + final PlotQuery query = PlotQuery.newQuery(); + switch (args.length) { case 3: if (!MathMan.isInteger(args[1])) { @@ -96,7 +98,7 @@ public class Visit extends Command { Captions.COMMAND_SYNTAX.send(player, getUsage()); return CompletableFuture.completedFuture(false); } - unsorted = PlotSquared.get().getBasePlots(user); + query.ownedBy(user).whereBasePlot(); shouldSortByArea = true; break; } @@ -108,21 +110,21 @@ public class Visit extends Command { } if (page == Integer.MIN_VALUE && user == null && MathMan.isInteger(args[0])) { page = Integer.parseInt(args[0]); - unsorted = PlotSquared.get().getBasePlots(player); + query.ownedBy(player).whereBasePlot(); break; } if (user != null) { - unsorted = PlotSquared.get().getBasePlots(user); + query.ownedBy(player).whereBasePlot(); } else { Plot plot = MainUtil.getPlotFromString(player, args[0], true); if (plot != null) { - unsorted = Collections.singletonList(plot.getBasePlot(false)); + query.withPlot(plot); } } break; case 0: page = 1; - unsorted = PlotSquared.get().getPlots(player); + query.ownedBy(player); break; default: @@ -130,25 +132,33 @@ public class Visit extends Command { if (page == Integer.MIN_VALUE) { page = 1; } - if (unsorted == null || unsorted.isEmpty()) { + + // We get the query once, + // then we get it another time further on + final List unsorted = query.asList(); + + if (unsorted.isEmpty()) { Captions.FOUND_NO_PLOTS.send(player); return CompletableFuture.completedFuture(false); } - unsorted = new ArrayList<>(unsorted); + if (unsorted.size() > 1) { - unsorted.removeIf(plot -> !plot.isBasePlot()); + query.whereBasePlot(); } + if (page < 1 || page > unsorted.size()) { Captions.NOT_VALID_NUMBER.send(player, "(1, " + unsorted.size() + ")"); return CompletableFuture.completedFuture(false); } - List plots; + if (shouldSortByArea) { - plots = PlotSquared.get() - .sortPlots(unsorted, PlotSquared.SortType.CREATION_DATE, sortByArea); + query.relativeToArea(sortByArea).withSortingStrategy(SortingStrategy.SORT_BY_CREATION); } else { - plots = PlotSquared.get().sortPlotsByTemp(unsorted); + query.withSortingStrategy(SortingStrategy.SORT_BY_TEMP); } + + final List plots = query.asList(); + final Plot plot = plots.get(page - 1); if (!plot.hasOwner()) { if (!Permissions.hasPermission(player, Captions.PERMISSION_VISIT_UNOWNED)) { @@ -177,6 +187,7 @@ public class Visit extends Command { return CompletableFuture.completedFuture(false); } } + confirm.run(this, () -> plot.teleportPlayer(player, TeleportCause.COMMAND, result -> { if (result) { whenDone.run(Visit.this, CommandResult.SUCCESS); diff --git a/Core/src/main/java/com/plotsquared/core/util/query/FixedPlotProvider.java b/Core/src/main/java/com/plotsquared/core/util/query/FixedPlotProvider.java new file mode 100644 index 000000000..3bf79c783 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/util/query/FixedPlotProvider.java @@ -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 . + */ +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 getPlots() { + return Collections.singleton(plot); + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/util/query/PlotQuery.java b/Core/src/main/java/com/plotsquared/core/util/query/PlotQuery.java index 67abfd1fa..6c90e969f 100644 --- a/Core/src/main/java/com/plotsquared/core/util/query/PlotQuery.java +++ b/Core/src/main/java/com/plotsquared/core/util/query/PlotQuery.java @@ -38,6 +38,7 @@ import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.HashSet; import java.util.LinkedList; import java.util.List; @@ -60,6 +61,7 @@ public final class PlotQuery { private PlotProvider plotProvider = new GlobalPlotProvider(); private SortingStrategy sortingStrategy = SortingStrategy.NO_SORTING; private PlotArea priorityArea; + private Comparator plotComparator; private PlotQuery() { } @@ -151,6 +153,17 @@ public final class PlotQuery { 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 * @@ -228,6 +241,19 @@ public final class PlotQuery { return this; } + /** + * Use a custom comparator to sort the results + * + * @param comparator Comparator + * @return The query instance + */ + @NotNull public PlotQuery sorted(@NotNull final Comparator 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 * sorting strategy @@ -313,6 +339,8 @@ public final class PlotQuery { }); } else if (this.sortingStrategy == SortingStrategy.SORT_BY_CREATION) { return PlotSquared.get().sortPlots(result, PlotSquared.SortType.CREATION_DATE, this.priorityArea); + } else if (this.sortingStrategy == SortingStrategy.COMPARATOR) { + result.sort(this.plotComparator); } return result; } diff --git a/Core/src/main/java/com/plotsquared/core/util/query/SortingStrategy.java b/Core/src/main/java/com/plotsquared/core/util/query/SortingStrategy.java index 2e6351dcc..7a70b46fd 100644 --- a/Core/src/main/java/com/plotsquared/core/util/query/SortingStrategy.java +++ b/Core/src/main/java/com/plotsquared/core/util/query/SortingStrategy.java @@ -48,5 +48,9 @@ public enum SortingStrategy { /** * Sort by creation date */ - SORT_BY_CREATION + SORT_BY_CREATION, + /** + * Sort using a comparator + */ + COMPARATOR; }