mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2024-11-22 13:16:45 +01:00
Add plot query system
This commit is contained in:
parent
6090c7ccac
commit
e912909aad
@ -82,6 +82,7 @@ import com.plotsquared.core.util.StringMan;
|
||||
import com.plotsquared.core.util.StringWrapper;
|
||||
import com.plotsquared.core.util.WorldUtil;
|
||||
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.uuid.UUIDHandler;
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
@ -116,6 +117,7 @@ import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
@ -640,17 +642,7 @@ public class PlotSquared {
|
||||
* @return Set of base Plots
|
||||
*/
|
||||
public Set<Plot> getBasePlots() {
|
||||
int size = getPlotCount();
|
||||
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);
|
||||
return PlotQuery.newQuery().whereBasePlot().asSet();
|
||||
}
|
||||
|
||||
public List<Plot> sortPlotsByTemp(Collection<Plot> plots) {
|
||||
@ -905,25 +897,22 @@ public class PlotSquared {
|
||||
* @return a filtered set of plots
|
||||
*/
|
||||
public Set<Plot> getPlots(final PlotFilter... filters) {
|
||||
final HashSet<Plot> set = new HashSet<>();
|
||||
forEachPlotArea(value -> {
|
||||
for (PlotFilter filter : filters) {
|
||||
if (!filter.allowsArea(value)) {
|
||||
return;
|
||||
final List<PlotArea> areas = new LinkedList<>();
|
||||
for (final PlotArea plotArea : this.getPlotAreas()) {
|
||||
for (final PlotFilter filter : filters) {
|
||||
if (filter.allowsArea(plotArea)) {
|
||||
areas.add(plotArea);
|
||||
}
|
||||
}
|
||||
loop:
|
||||
for (Entry<PlotId, Plot> entry2 : value.getPlotEntries()) {
|
||||
Plot plot = entry2.getValue();
|
||||
for (PlotFilter filter : filters) {
|
||||
}
|
||||
return PlotQuery.newQuery().inAreas(areas).thatPasses(plot -> {
|
||||
for (final PlotFilter filter : filters) {
|
||||
if (!filter.allowsPlot(plot)) {
|
||||
continue loop;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
set.add(plot);
|
||||
}
|
||||
});
|
||||
return set;
|
||||
return true;
|
||||
}).asSet();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -990,7 +979,7 @@ public class PlotSquared {
|
||||
* @return Set of plot
|
||||
*/
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
public Set<Plot> getPlots(String world, UUID uuid) {
|
||||
final Set<Plot> plots =
|
||||
getPlots(world).stream().filter(plot -> plot.hasOwner() && plot.isOwnerAbs(uuid))
|
||||
.collect(Collectors.toSet());
|
||||
return Collections.unmodifiableSet(plots);
|
||||
return PlotQuery.newQuery().inWorld(world).ownedBy(uuid).asSet();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1026,13 +1012,7 @@ public class PlotSquared {
|
||||
* @return Set of plots
|
||||
*/
|
||||
public Set<Plot> getPlots(PlotArea area, UUID uuid) {
|
||||
final Set<Plot> plots = new HashSet<>();
|
||||
for (Plot plot : getPlots(area)) {
|
||||
if (plot.hasOwner() && plot.isOwnerAbs(uuid)) {
|
||||
plots.add(plot);
|
||||
}
|
||||
}
|
||||
return Collections.unmodifiableSet(plots);
|
||||
return PlotQuery.newQuery().inArea(area).ownedBy(uuid).asSet();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1047,9 +1027,7 @@ public class PlotSquared {
|
||||
}
|
||||
|
||||
public Collection<Plot> getPlots(String world) {
|
||||
final Set<Plot> set = new HashSet<>();
|
||||
forEachPlotArea(world, value -> set.addAll(value.getPlots()));
|
||||
return set;
|
||||
return PlotQuery.newQuery().inWorld(world).asCollection();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1059,7 +1037,7 @@ public class PlotSquared {
|
||||
* @return Set of Plot
|
||||
*/
|
||||
public Set<Plot> getPlots(PlotPlayer player) {
|
||||
return getPlots(player.getUUID());
|
||||
return PlotQuery.newQuery().ownedBy(player).asSet();
|
||||
}
|
||||
|
||||
public Collection<Plot> getPlots(PlotArea area) {
|
||||
@ -1081,13 +1059,7 @@ public class PlotSquared {
|
||||
* @return Set of Plot's owned by the player
|
||||
*/
|
||||
public Set<Plot> getPlots(final UUID uuid) {
|
||||
final Set<Plot> plots = new HashSet<>();
|
||||
forEachPlot(value -> {
|
||||
if (value.isOwnerAbs(uuid)) {
|
||||
plots.add(value);
|
||||
}
|
||||
});
|
||||
return Collections.unmodifiableSet(plots);
|
||||
return PlotQuery.newQuery().ownedBy(uuid).asSet();
|
||||
}
|
||||
|
||||
public boolean hasPlot(final UUID uuid) {
|
||||
@ -1096,13 +1068,7 @@ public class PlotSquared {
|
||||
}
|
||||
|
||||
public Set<Plot> getBasePlots(final UUID uuid) {
|
||||
final Set<Plot> plots = new HashSet<>();
|
||||
forEachBasePlot(value -> {
|
||||
if (value.isOwner(uuid)) {
|
||||
plots.add(value);
|
||||
}
|
||||
});
|
||||
return Collections.unmodifiableSet(plots);
|
||||
return PlotQuery.newQuery().ownedBy(uuid).whereBasePlot().asSet();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1112,13 +1078,7 @@ public class PlotSquared {
|
||||
* @return Set of Plot
|
||||
*/
|
||||
public Set<Plot> getPlotsAbs(final UUID uuid) {
|
||||
final Set<Plot> plots = new HashSet<>();
|
||||
forEachPlot(value -> {
|
||||
if (value.isOwnerAbs(uuid)) {
|
||||
plots.add(value);
|
||||
}
|
||||
});
|
||||
return Collections.unmodifiableSet(plots);
|
||||
return PlotQuery.newQuery().ownedBy(uuid).asSet();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2103,16 +2063,7 @@ public class PlotSquared {
|
||||
*/
|
||||
public Set<Plot> getPlotsByAlias(@Nullable final String alias,
|
||||
@NonNull final String worldname) {
|
||||
final Set<Plot> result = new HashSet<>();
|
||||
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);
|
||||
return PlotQuery.newQuery().inWorld(worldname).withAlias(alias).asSet();
|
||||
}
|
||||
|
||||
public Set<PlotArea> getPlotAreas(final String world, final CuboidRegion region) {
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
|
||||
}
|
@ -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();
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user