Make PlotQuery iterable

This commit is contained in:
Alexander Söderberg 2020-07-18 16:18:23 +02:00
parent e80ade65c7
commit 39fdaa367c
5 changed files with 12 additions and 18 deletions

View File

@ -454,7 +454,7 @@ public class Cluster extends SubCommand {
cluster.getName()); cluster.getName());
} }
for (final Plot plot : PlotQuery.newQuery().inWorld(player2.getLocation() for (final Plot plot : PlotQuery.newQuery().inWorld(player2.getLocation()
.getWorldName()).ownedBy(uuid).asCollection()) { .getWorldName()).ownedBy(uuid)) {
PlotCluster current = plot.getCluster(); PlotCluster current = plot.getCluster();
if (current != null && current.equals(cluster)) { if (current != null && current.equals(cluster)) {
plot.unclaim(); plot.unclaim();
@ -513,7 +513,7 @@ public class Cluster extends SubCommand {
DBFunc.removeInvited(cluster, uuid); DBFunc.removeInvited(cluster, uuid);
MainUtil.sendMessage(player, Captions.CLUSTER_REMOVED, cluster.getName()); MainUtil.sendMessage(player, Captions.CLUSTER_REMOVED, cluster.getName());
for (final Plot plot : PlotQuery.newQuery().inWorld(player.getLocation().getWorldName()) for (final Plot plot : PlotQuery.newQuery().inWorld(player.getLocation().getWorldName())
.ownedBy(uuid).asCollection()) { .ownedBy(uuid)) {
PlotCluster current = plot.getCluster(); PlotCluster current = plot.getCluster();
if (current != null && current.equals(cluster)) { if (current != null && current.equals(cluster)) {
plot.unclaim(); plot.unclaim();

View File

@ -58,7 +58,7 @@ public class Target extends SubCommand {
Plot target = null; Plot target = null;
if (StringMan.isEqualIgnoreCaseToAny(args[0], "near", "nearest")) { if (StringMan.isEqualIgnoreCaseToAny(args[0], "near", "nearest")) {
int distance = Integer.MAX_VALUE; 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); double current = plot.getCenterSynchronous().getEuclideanDistanceSquared(location);
if (current < distance) { if (current < distance) {
distance = (int) current; distance = (int) current;

View File

@ -173,7 +173,7 @@ public class Trim extends SubCommand {
int bz = cbz << 4; int bz = cbz << 4;
CuboidRegion region = CuboidRegion region =
RegionUtil.createRegion(bx, bx + 511, bz, bz + 511); 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 bot = plot.getBottomAbs();
Location top = plot.getExtendedTopAbs(); Location top = plot.getExtendedTopAbs();
CuboidRegion plotReg = RegionUtil CuboidRegion plotReg = RegionUtil

View File

@ -493,7 +493,7 @@ public class MainUtil {
PlotArea area = null; PlotArea area = null;
String alias = null; String alias = null;
for (Plot plot : PlotQuery.newQuery().allPlots().asList()) { for (Plot plot : PlotQuery.newQuery().allPlots()) {
int count = 0; int count = 0;
if (!uuids.isEmpty()) { if (!uuids.isEmpty()) {
for (UUID uuid : uuids) { for (UUID uuid : uuids) {

View File

@ -34,19 +34,20 @@ import com.plotsquared.core.plot.Rating;
import com.plotsquared.core.plot.flag.implementations.DoneFlag; import com.plotsquared.core.plot.flag.implementations.DoneFlag;
import com.plotsquared.core.plot.world.PlotAreaManager; import com.plotsquared.core.plot.world.PlotAreaManager;
import com.plotsquared.core.util.MathMan; 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.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -57,7 +58,7 @@ import java.util.stream.Stream;
* The queries can be reused as no results are stored * The queries can be reused as no results are stored
* in the query itself * in the query itself
*/ */
public final class PlotQuery { public final class PlotQuery implements Iterable<Plot> {
private final Collection<PlotFilter> filters = new LinkedList<>(); private final Collection<PlotFilter> filters = new LinkedList<>();
private final PlotAreaManager plotAreaManager; private final PlotAreaManager plotAreaManager;
@ -381,16 +382,6 @@ public final class PlotQuery {
return this.asList(); return this.asList();
} }
/**
* Perform an action on each plot returned by the query
*
* @param consumer Plot consumer
*/
public void forEach(@Nonnull final Consumer<Plot> consumer) {
Preconditions.checkNotNull(consumer, "Consumer may not be null");
this.asCollection().forEach(consumer);
}
/** /**
* Get the amount of plots contained in the query result * Get the amount of plots contained in the query result
* *
@ -429,5 +420,8 @@ public final class PlotQuery {
return this; return this;
} }
@NotNull @Override public Iterator<Plot> iterator() {
return this.asCollection().iterator();
}
} }