From 39fdaa367ca52a04095fad9b5110e5dc541cadd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Sat, 18 Jul 2020 16:18:23 +0200 Subject: [PATCH] Make PlotQuery iterable --- .../com/plotsquared/core/command/Cluster.java | 4 ++-- .../com/plotsquared/core/command/Target.java | 2 +- .../com/plotsquared/core/command/Trim.java | 2 +- .../com/plotsquared/core/util/MainUtil.java | 2 +- .../core/util/query/PlotQuery.java | 20 +++++++------------ 5 files changed, 12 insertions(+), 18 deletions(-) diff --git a/Core/src/main/java/com/plotsquared/core/command/Cluster.java b/Core/src/main/java/com/plotsquared/core/command/Cluster.java index 793225cf7..cfd416e8d 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Cluster.java +++ b/Core/src/main/java/com/plotsquared/core/command/Cluster.java @@ -454,7 +454,7 @@ public class Cluster extends SubCommand { cluster.getName()); } for (final Plot plot : PlotQuery.newQuery().inWorld(player2.getLocation() - .getWorldName()).ownedBy(uuid).asCollection()) { + .getWorldName()).ownedBy(uuid)) { PlotCluster current = plot.getCluster(); if (current != null && current.equals(cluster)) { plot.unclaim(); @@ -513,7 +513,7 @@ public class Cluster extends SubCommand { DBFunc.removeInvited(cluster, uuid); MainUtil.sendMessage(player, Captions.CLUSTER_REMOVED, cluster.getName()); for (final Plot plot : PlotQuery.newQuery().inWorld(player.getLocation().getWorldName()) - .ownedBy(uuid).asCollection()) { + .ownedBy(uuid)) { PlotCluster current = plot.getCluster(); if (current != null && current.equals(cluster)) { plot.unclaim(); diff --git a/Core/src/main/java/com/plotsquared/core/command/Target.java b/Core/src/main/java/com/plotsquared/core/command/Target.java index 6c1fdc6c6..3c0941c2c 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Target.java +++ b/Core/src/main/java/com/plotsquared/core/command/Target.java @@ -58,7 +58,7 @@ public class Target extends SubCommand { Plot target = null; if (StringMan.isEqualIgnoreCaseToAny(args[0], "near", "nearest")) { int distance = Integer.MAX_VALUE; - for (Plot plot : PlotQuery.newQuery().inWorld(location.getWorldName()).asCollection()) { + for (Plot plot : PlotQuery.newQuery().inWorld(location.getWorldName())) { double current = plot.getCenterSynchronous().getEuclideanDistanceSquared(location); if (current < distance) { distance = (int) current; diff --git a/Core/src/main/java/com/plotsquared/core/command/Trim.java b/Core/src/main/java/com/plotsquared/core/command/Trim.java index 3afe16c56..d8346fb00 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Trim.java +++ b/Core/src/main/java/com/plotsquared/core/command/Trim.java @@ -173,7 +173,7 @@ public class Trim extends SubCommand { int bz = cbz << 4; CuboidRegion region = RegionUtil.createRegion(bx, bx + 511, bz, bz + 511); - for (Plot plot : PlotQuery.newQuery().inWorld(world).asCollection()) { + for (Plot plot : PlotQuery.newQuery().inWorld(world)) { Location bot = plot.getBottomAbs(); Location top = plot.getExtendedTopAbs(); CuboidRegion plotReg = RegionUtil diff --git a/Core/src/main/java/com/plotsquared/core/util/MainUtil.java b/Core/src/main/java/com/plotsquared/core/util/MainUtil.java index 793643907..fb169c3e6 100644 --- a/Core/src/main/java/com/plotsquared/core/util/MainUtil.java +++ b/Core/src/main/java/com/plotsquared/core/util/MainUtil.java @@ -493,7 +493,7 @@ public class MainUtil { PlotArea area = null; String alias = null; - for (Plot plot : PlotQuery.newQuery().allPlots().asList()) { + for (Plot plot : PlotQuery.newQuery().allPlots()) { int count = 0; if (!uuids.isEmpty()) { for (UUID uuid : uuids) { 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 03c41dd8e..bc89c6ea7 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 @@ -34,19 +34,20 @@ import com.plotsquared.core.plot.Rating; import com.plotsquared.core.plot.flag.implementations.DoneFlag; import com.plotsquared.core.plot.world.PlotAreaManager; import com.plotsquared.core.util.MathMan; -import javax.annotation.Nonnull; +import org.jetbrains.annotations.NotNull; +import javax.annotation.Nonnull; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; +import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; -import java.util.function.Consumer; import java.util.function.Predicate; import java.util.stream.Stream; @@ -57,7 +58,7 @@ import java.util.stream.Stream; * The queries can be reused as no results are stored * in the query itself */ -public final class PlotQuery { +public final class PlotQuery implements Iterable { private final Collection filters = new LinkedList<>(); private final PlotAreaManager plotAreaManager; @@ -381,16 +382,6 @@ public final class PlotQuery { return this.asList(); } - /** - * Perform an action on each plot returned by the query - * - * @param consumer Plot consumer - */ - public void forEach(@Nonnull final Consumer consumer) { - Preconditions.checkNotNull(consumer, "Consumer may not be null"); - this.asCollection().forEach(consumer); - } - /** * Get the amount of plots contained in the query result * @@ -429,5 +420,8 @@ public final class PlotQuery { return this; } + @NotNull @Override public Iterator iterator() { + return this.asCollection().iterator(); + } }