Add plot query system

This commit is contained in:
Alexander Söderberg 2020-05-23 22:20:57 +02:00 committed by Alexander Söderberg
parent 6090c7ccac
commit e912909aad
11 changed files with 667 additions and 74 deletions

View File

@ -82,6 +82,7 @@ import com.plotsquared.core.util.StringMan;
import com.plotsquared.core.util.StringWrapper; import com.plotsquared.core.util.StringWrapper;
import com.plotsquared.core.util.WorldUtil; import com.plotsquared.core.util.WorldUtil;
import com.plotsquared.core.util.logger.ILogger; import com.plotsquared.core.util.logger.ILogger;
import com.plotsquared.core.util.query.PlotQuery;
import com.plotsquared.core.util.task.TaskManager; import com.plotsquared.core.util.task.TaskManager;
import com.plotsquared.core.util.uuid.UUIDHandler; import com.plotsquared.core.util.uuid.UUIDHandler;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
@ -116,6 +117,7 @@ import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
@ -640,17 +642,7 @@ public class PlotSquared {
* @return Set of base Plots * @return Set of base Plots
*/ */
public Set<Plot> getBasePlots() { public Set<Plot> getBasePlots() {
int size = getPlotCount(); return PlotQuery.newQuery().whereBasePlot().asSet();
final Set<Plot> result = new HashSet<>(size);
forEachPlotArea(value -> {
for (Plot plot : value.getPlots()) {
if (!plot.isBasePlot()) {
continue;
}
result.add(plot);
}
});
return Collections.unmodifiableSet(result);
} }
public List<Plot> sortPlotsByTemp(Collection<Plot> plots) { public List<Plot> sortPlotsByTemp(Collection<Plot> plots) {
@ -905,25 +897,22 @@ public class PlotSquared {
* @return a filtered set of plots * @return a filtered set of plots
*/ */
public Set<Plot> getPlots(final PlotFilter... filters) { public Set<Plot> getPlots(final PlotFilter... filters) {
final HashSet<Plot> set = new HashSet<>(); final List<PlotArea> areas = new LinkedList<>();
forEachPlotArea(value -> { for (final PlotArea plotArea : this.getPlotAreas()) {
for (PlotFilter filter : filters) { for (final PlotFilter filter : filters) {
if (!filter.allowsArea(value)) { if (filter.allowsArea(plotArea)) {
return; areas.add(plotArea);
} }
} }
loop: }
for (Entry<PlotId, Plot> entry2 : value.getPlotEntries()) { return PlotQuery.newQuery().inAreas(areas).thatPasses(plot -> {
Plot plot = entry2.getValue(); for (final PlotFilter filter : filters) {
for (PlotFilter filter : filters) {
if (!filter.allowsPlot(plot)) { if (!filter.allowsPlot(plot)) {
continue loop; return false;
} }
} }
set.add(plot); return true;
} }).asSet();
});
return set;
} }
/** /**
@ -990,7 +979,7 @@ public class PlotSquared {
* @return Set of plot * @return Set of plot
*/ */
public Set<Plot> getPlots(String world, PlotPlayer player) { public Set<Plot> getPlots(String world, PlotPlayer player) {
return getPlots(world, player.getUUID()); return PlotQuery.newQuery().inWorld(world).ownedBy(player).asSet();
} }
/** /**
@ -1001,7 +990,7 @@ public class PlotSquared {
* @return Set of plot * @return Set of plot
*/ */
public Set<Plot> getPlots(PlotArea area, PlotPlayer player) { public Set<Plot> getPlots(PlotArea area, PlotPlayer player) {
return getPlots(area, player.getUUID()); return PlotQuery.newQuery().inArea(area).ownedBy(player).asSet();
} }
/** /**
@ -1012,10 +1001,7 @@ public class PlotSquared {
* @return Set of plot * @return Set of plot
*/ */
public Set<Plot> getPlots(String world, UUID uuid) { public Set<Plot> getPlots(String world, UUID uuid) {
final Set<Plot> plots = return PlotQuery.newQuery().inWorld(world).ownedBy(uuid).asSet();
getPlots(world).stream().filter(plot -> plot.hasOwner() && plot.isOwnerAbs(uuid))
.collect(Collectors.toSet());
return Collections.unmodifiableSet(plots);
} }
/** /**
@ -1026,13 +1012,7 @@ public class PlotSquared {
* @return Set of plots * @return Set of plots
*/ */
public Set<Plot> getPlots(PlotArea area, UUID uuid) { public Set<Plot> getPlots(PlotArea area, UUID uuid) {
final Set<Plot> plots = new HashSet<>(); return PlotQuery.newQuery().inArea(area).ownedBy(uuid).asSet();
for (Plot plot : getPlots(area)) {
if (plot.hasOwner() && plot.isOwnerAbs(uuid)) {
plots.add(plot);
}
}
return Collections.unmodifiableSet(plots);
} }
/** /**
@ -1047,9 +1027,7 @@ public class PlotSquared {
} }
public Collection<Plot> getPlots(String world) { public Collection<Plot> getPlots(String world) {
final Set<Plot> set = new HashSet<>(); return PlotQuery.newQuery().inWorld(world).asCollection();
forEachPlotArea(world, value -> set.addAll(value.getPlots()));
return set;
} }
/** /**
@ -1059,7 +1037,7 @@ public class PlotSquared {
* @return Set of Plot * @return Set of Plot
*/ */
public Set<Plot> getPlots(PlotPlayer player) { public Set<Plot> getPlots(PlotPlayer player) {
return getPlots(player.getUUID()); return PlotQuery.newQuery().ownedBy(player).asSet();
} }
public Collection<Plot> getPlots(PlotArea area) { public Collection<Plot> getPlots(PlotArea area) {
@ -1081,13 +1059,7 @@ public class PlotSquared {
* @return Set of Plot's owned by the player * @return Set of Plot's owned by the player
*/ */
public Set<Plot> getPlots(final UUID uuid) { public Set<Plot> getPlots(final UUID uuid) {
final Set<Plot> plots = new HashSet<>(); return PlotQuery.newQuery().ownedBy(uuid).asSet();
forEachPlot(value -> {
if (value.isOwnerAbs(uuid)) {
plots.add(value);
}
});
return Collections.unmodifiableSet(plots);
} }
public boolean hasPlot(final UUID uuid) { public boolean hasPlot(final UUID uuid) {
@ -1096,13 +1068,7 @@ public class PlotSquared {
} }
public Set<Plot> getBasePlots(final UUID uuid) { public Set<Plot> getBasePlots(final UUID uuid) {
final Set<Plot> plots = new HashSet<>(); return PlotQuery.newQuery().ownedBy(uuid).whereBasePlot().asSet();
forEachBasePlot(value -> {
if (value.isOwner(uuid)) {
plots.add(value);
}
});
return Collections.unmodifiableSet(plots);
} }
/** /**
@ -1112,13 +1078,7 @@ public class PlotSquared {
* @return Set of Plot * @return Set of Plot
*/ */
public Set<Plot> getPlotsAbs(final UUID uuid) { public Set<Plot> getPlotsAbs(final UUID uuid) {
final Set<Plot> plots = new HashSet<>(); return PlotQuery.newQuery().ownedBy(uuid).asSet();
forEachPlot(value -> {
if (value.isOwnerAbs(uuid)) {
plots.add(value);
}
});
return Collections.unmodifiableSet(plots);
} }
/** /**
@ -2103,16 +2063,7 @@ public class PlotSquared {
*/ */
public Set<Plot> getPlotsByAlias(@Nullable final String alias, public Set<Plot> getPlotsByAlias(@Nullable final String alias,
@NonNull final String worldname) { @NonNull final String worldname) {
final Set<Plot> result = new HashSet<>(); return PlotQuery.newQuery().inWorld(worldname).withAlias(alias).asSet();
if (alias != null) {
for (final Plot plot : getPlots()) {
if (alias.equals(plot.getAlias()) && (worldname == null || worldname
.equals(plot.getWorldName()))) {
result.add(plot);
}
}
}
return Collections.unmodifiableSet(result);
} }
public Set<PlotArea> getPlotAreas(final String world, final CuboidRegion region) { public Set<PlotArea> getPlotAreas(final String world, final CuboidRegion region) {

View File

@ -0,0 +1,43 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* 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;
class AliasFilter implements PlotFilter {
private final String alias;
AliasFilter(@NotNull final String alias) {
this.alias = alias;
}
@Override public boolean accepts(@NotNull final Plot plot) {
return this.alias.equalsIgnoreCase(plot.getAlias());
}
}

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 com.plotsquared.core.plot.PlotArea;
import java.util.Collection;
import java.util.stream.Stream;
class AreaLimitedPlotProvider implements PlotProvider {
private final Collection<PlotArea> areas;
AreaLimitedPlotProvider(Collection<PlotArea> areas) {
this.areas = areas;
}
@Override public Stream<Plot> getPlots() {
return areas.stream().flatMap(area -> area.getPlots().stream());
}
}

View File

@ -0,0 +1,39 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* 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.PlotSquared;
import com.plotsquared.core.plot.Plot;
import java.util.stream.Stream;
class GlobalPlotProvider implements PlotProvider {
@Override public Stream<Plot> getPlots() {
return PlotSquared.get().getPlots().stream();
}
}

View File

@ -0,0 +1,45 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* 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.UUID;
class MemberFilter implements PlotFilter {
@NotNull private final UUID uuid;
MemberFilter(@NotNull final UUID uuid) {
this.uuid = uuid;
}
@Override public boolean accepts(@NotNull final Plot plot) {
return plot.isAdded(uuid);
}
}

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.Objects;
import java.util.UUID;
class OwnerFilter implements PlotFilter {
private final UUID owner;
OwnerFilter(@NotNull final UUID owner) {
this.owner = owner;
}
@Override public boolean accepts(@NotNull final Plot plot) {
return plot.hasOwner() && Objects.equals(plot.getOwnerAbs(), this.owner);
}
}

View File

@ -0,0 +1,73 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* 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.google.common.base.Preconditions;
import com.plotsquared.core.plot.Plot;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List;
/**
* Paginated collection of plots as a result of a {@link PlotQuery query}
*/
public final class PaginatedPlotResult {
private final List<Plot> plots;
private final int pageSize;
PaginatedPlotResult(@NotNull final List<Plot> plots, final int pageSize) {
this.plots = plots;
this.pageSize = pageSize;
}
/**
* Get the plots belonging to a certain page.
*
* @param page Positive page number. Indexed from 1
* @return Plots that belong to the specified page
*/
public List<Plot> getPage(final int page) {
Preconditions.checkState(page >= 0, "Page must be positive");
final int from = (page - 1) * this.pageSize;
if (this.plots.size() < from) {
return Collections.emptyList();
}
final int to = Math.max(from + pageSize, this.plots.size());
return this.plots.subList(from, to);
}
/**
* Get the number of available pages
*
* @return Available pages
*/
public int getPages() {
return (int) Math.ceil((double) plots.size() / (double) pageSize);
}
}

View File

@ -0,0 +1,41 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* 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.function.Predicate;
@FunctionalInterface interface PlotFilter extends Predicate<Plot> {
@Override default boolean test(@NotNull final Plot plot) {
return this.accepts(plot);
}
boolean accepts(@NotNull final Plot plot);
}

View File

@ -0,0 +1,36 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* 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 java.util.stream.Stream;
@FunctionalInterface interface PlotProvider {
Stream<Plot> getPlots();
}

View File

@ -0,0 +1,228 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* 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.google.common.base.Preconditions;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.plot.PlotArea;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* This represents a plot query, and can be used to
* search for plots matching certain criteria.
* <p>
* The queries can be reused as no results are stored
* in the query itself
*/
public final class PlotQuery {
private PlotProvider plotProvider = new GlobalPlotProvider();
private final Collection<PlotFilter> filters = new LinkedList<>();
private PlotQuery() {
}
/**
* Create a new plot query instance
*
* @return New query
*/
public static PlotQuery newQuery() {
return new PlotQuery();
}
/**
* Query for plots in a single area
*
* @param area Area
* @return The query instance
*/
@NotNull public PlotQuery inArea(@NotNull final PlotArea area) {
Preconditions.checkNotNull(area, "Area may not be null");
this.plotProvider = new AreaLimitedPlotProvider(Collections.singletonList(area));
return this;
}
/**
* Query for plots in all areas in a world
*
* @param world World name
* @return The query instance
*/
@NotNull public PlotQuery inWorld(@NotNull final String world) {
Preconditions.checkNotNull(world, "World may not be null");
this.plotProvider = new AreaLimitedPlotProvider(PlotSquared.get().getPlotAreas(world));
return this;
}
/**
* Query for plots in specific areas
*
* @param areas Plot areas
* @return The query instance
*/
@NotNull public PlotQuery inAreas(@NotNull final Collection<PlotArea> areas) {
Preconditions.checkNotNull(areas, "Areas may not be null");
Preconditions.checkState(!areas.isEmpty(), "At least one area must be provided");
this.plotProvider = new AreaLimitedPlotProvider(Collections.unmodifiableCollection(areas));
return this;
}
/**
* Query for base plots only
*
* @return The query instance
*/
@NotNull public PlotQuery whereBasePlot() {
return this.addFilter(new PredicateFilter(Plot::isBasePlot));
}
/**
* Query for plots owned by a specific player
*
* @param owner Owner UUID
* @return The query instance
*/
@NotNull public PlotQuery ownedBy(@NotNull final UUID owner) {
Preconditions.checkNotNull(owner, "Owner may not be null");
return this.addFilter(new OwnerFilter(owner));
}
/**
* Query for plots owned by a specific player
*
* @param owner Owner
* @return The query instance
*/
@NotNull public PlotQuery ownedBy(@NotNull final PlotPlayer owner) {
Preconditions.checkNotNull(owner, "Owner may not be null");
return this.addFilter(new OwnerFilter(owner.getUUID()));
}
/**
* Query for plots with a specific alias
*
* @param alias Plot alias
* @return The query instance
*/
@NotNull public PlotQuery withAlias(@NotNull final String alias) {
Preconditions.checkNotNull(alias, "Alias may not be null");
return this.addFilter(new AliasFilter(alias));
}
/**
* Query for plots with a specific member (added/trusted/owner)
*
* @param member Member UUID
* @return The query instance
*/
@NotNull public PlotQuery withMember(@NotNull final UUID member) {
Preconditions.checkNotNull(member, "Member may not be null");
return this.addFilter(new MemberFilter(member));
}
/**
* Query for plots that passes a given predicate
*
* @param predicate Predicate
* @return The query instance
*/
@NotNull public PlotQuery thatPasses(@NotNull final Predicate<Plot> predicate) {
Preconditions.checkNotNull(predicate, "Predicate may not be null");
return this.addFilter(new PredicateFilter(predicate));
}
/**
* Get all plots that match the given criteria
*
* @return Matching plots
*/
@NotNull public Stream<Plot> asStream() {
Stream<Plot> plots = this.plotProvider.getPlots();
for (final PlotFilter filter : this.filters) {
plots = plots.filter(filter);
}
return plots;
}
/**
* Get all plots that match the given criteria
*
* @return Matching plots as an immutable list
*/
@NotNull public List<Plot> asList() {
return Collections.unmodifiableList(this.asStream().collect(Collectors.toList()));
}
/**
* Get all plots that match the given criteria
*
* @return Matching plots as an immutable set
*/
@NotNull public Set<Plot> asSet() {
return Collections.unmodifiableSet(this.asStream().collect(Collectors.toSet()));
}
/**
* Get all plots that match the given criteria
* in the form of a {@link PaginatedPlotResult}
*
* @param pageSize The size of the pages. Must be positive.
* @return Paginated plot result
*/
@NotNull public PaginatedPlotResult getPaginated(final int pageSize) {
Preconditions.checkState(pageSize > 0, "Page size must be greater than 0");
return new PaginatedPlotResult(this.asList(), pageSize);
}
/**
* Get all plots that match the given criteria
*
* @return Matching plots as an immutable collection
*/
@NotNull public Collection<Plot> asCollection() {
return this.asList();
}
@NotNull private PlotQuery addFilter(@NotNull final PlotFilter filter) {
this.filters.add(filter);
return this;
}
}

View File

@ -0,0 +1,45 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* 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.function.Predicate;
class PredicateFilter implements PlotFilter {
private final Predicate<Plot> predicate;
PredicateFilter(@NotNull final Predicate<Plot> predicate) {
this.predicate = predicate;
}
@Override public boolean accepts(@NotNull final Plot plot) {
return predicate.test(plot);
}
}