Skip to content
2 changes: 0 additions & 2 deletions Core/src/main/java/com/plotsquared/core/PlotSquared.java
Original file line number Diff line number Diff line change
Expand Up @@ -713,8 +713,6 @@ private void sortPlotsByHash(final @NonNull Plot @NonNull [] input) {
case CREATION_DATE_TIMESTAMP -> toReturn.addAll(sortPlotsByTimestamp(map.get(area)));
case DISTANCE_FROM_ORIGIN -> toReturn.addAll(sortPlotsByHash(map.get(area)));
case LAST_MODIFIED -> toReturn.addAll(sortPlotsByModified(map.get(area)));
default -> {
}
}
}
return toReturn;
Expand Down
47 changes: 21 additions & 26 deletions Core/src/main/java/com/plotsquared/core/command/Visit.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,10 @@ public Visit(final @NonNull PlotAreaManager plotAreaManager) {

private void visit(
final @NonNull PlotPlayer<?> player, final @NonNull PlotQuery query, final PlotArea sortByArea,
final RunnableVal3<Command, Runnable, Runnable> confirm, final RunnableVal2<Command, CommandResult> whenDone, int page
final RunnableVal3<Command, Runnable, Runnable> confirm, final RunnableVal2<Command, CommandResult> whenDone,
int page, final boolean isQueryingBasePlot
) {
// We get the query once,
// then we get it another time further on
final List<Plot> unsorted = query.asList();

if (unsorted.size() > 1) {
if (!isQueryingBasePlot && query.hasMinimumMatches(2)) {
query.whereBasePlot();
}

Expand Down Expand Up @@ -237,7 +234,8 @@ public CompletableFuture<Boolean> execute(
finalSortByArea,
confirm,
whenDone,
finalPage1
finalPage1,
true
);
}
});
Expand All @@ -261,12 +259,9 @@ public CompletableFuture<Boolean> execute(
if (throwable instanceof TimeoutException) {
// The request timed out
player.sendMessage(TranslatableCaption.of("players.fetching_players_timeout"));
} else if (uuid != null && (Settings.Teleport.VISIT_MERGED_OWNERS
? !PlotQuery.newQuery().ownersInclude(uuid).anyMatch()
: !PlotQuery.newQuery().ownedBy(uuid).anyMatch())) {
// It was a valid UUID but the player has no plots
player.sendMessage(TranslatableCaption.of("errors.player_no_plots"));
} else if (uuid == null) {
return;
}
if (uuid == null){
// player not found, so we assume it's an alias if no page was provided
if (finalPage == Integer.MIN_VALUE) {
this.visit(
Expand All @@ -275,32 +270,32 @@ public CompletableFuture<Boolean> execute(
player.getApplicablePlotArea(),
confirm,
whenDone,
1
1,
false
);
} else {
player.sendMessage(
TranslatableCaption.of("errors.invalid_player"),
TagResolver.resolver("value", Tag.inserting(Component.text(finalArgs[0])))
);
}
} else {
this.visit(
player,
Settings.Teleport.VISIT_MERGED_OWNERS
? PlotQuery.newQuery().ownersInclude(uuid).whereBasePlot()
: PlotQuery.newQuery().ownedBy(uuid).whereBasePlot(),
null,
confirm,
whenDone,
finalPage
);
return;
}
final PlotQuery query = Settings.Teleport.VISIT_MERGED_OWNERS
? PlotQuery.newQuery().ownersInclude(uuid)
: PlotQuery.newQuery().ownedBy(uuid);
if (!query.anyMatch()) {
// It was a valid UUID but the player has no plots
player.sendMessage(TranslatableCaption.of("errors.player_no_plots"));
return;
}
this.visit(player, query.whereBasePlot(), null, confirm, whenDone, finalPage, true);
});
} else {
// Try to parse a plot
final Plot plot = Plot.getPlotFromString(player, finalArgs[0], true);
if (plot != null) {
this.visit(player, PlotQuery.newQuery().withPlot(plot), null, confirm, whenDone, 1);
this.visit(player, PlotQuery.newQuery().withPlot(plot), null, confirm, whenDone, 1, false);
}
}
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* PlotSquared, a land and world management plugin for Minecraft.
* Copyright (C) IntellectualSites <https://intellectualsites.com>
* Copyright (C) IntellectualSites team and contributors
*
* 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 <https://www.gnu.org/licenses/>.
*/
package com.plotsquared.core.util.comparator;

import com.plotsquared.core.plot.Plot;

import java.util.Comparator;

/**
* Sort plots by {@link Plot#temp} (being the auto increment id in database) in natural order for {@code temp > 0}.
* For {@code temp < 1} sort by {@link Plot#hashCode()}
*/
public class PlotByCreationDateComparator implements Comparator<Plot> {

public static final Comparator<Plot> INSTANCE = new PlotByCreationDateComparator();

private PlotByCreationDateComparator() {
}

@Override
@SuppressWarnings("deprecation") // Plot#temp
public int compare(final Plot first, final Plot second) {
if (first.temp > 0 && second.temp > 0) {
return Integer.compare(first.temp, second.temp);
}
// second is implicitly `< 1` (due to previous condition)
if (first.temp > 0) {
return 1;
}
// sort dangling plots (temp < 1) by their hashcode
return Integer.compare(first.hashCode(), second.hashCode());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* PlotSquared, a land and world management plugin for Minecraft.
* Copyright (C) IntellectualSites <https://intellectualsites.com>
* Copyright (C) IntellectualSites team and contributors
*
* 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 <https://www.gnu.org/licenses/>.
*/
package com.plotsquared.core.util.comparator;

import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.plot.flag.implementations.DoneFlag;
import com.plotsquared.core.util.MathMan;

import java.util.Comparator;

/**
* Sort plots by their {@link DoneFlag} in reverse numeric natural order. (more recent "finished plots" first)
* <br>
* Non-finished plots last, unsorted.
*/
public class PlotByDoneComparator implements Comparator<Plot> {

public static final PlotByDoneComparator INSTANCE = new PlotByDoneComparator();

private PlotByDoneComparator() {
}

@Override
public int compare(final Plot first, final Plot second) {
String firstDone = first.getFlag(DoneFlag.class);
String lastDone = second.getFlag(DoneFlag.class);
if (MathMan.isInteger(firstDone)) {
if (MathMan.isInteger(lastDone)) {
return Integer.parseInt(lastDone) - Integer.parseInt(firstDone);
}
return -1; // only "first" is finished, so sort "second" after "first"
}
return 0; // neither is finished
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* PlotSquared, a land and world management plugin for Minecraft.
* Copyright (C) IntellectualSites <https://intellectualsites.com>
* Copyright (C) IntellectualSites team and contributors
*
* 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 <https://www.gnu.org/licenses/>.
*/
package com.plotsquared.core.util.comparator;

import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.plot.Rating;

import java.util.Comparator;
import java.util.Map;
import java.util.UUID;

public class PlotByRatingComparator implements Comparator<Plot> {

public static final PlotByRatingComparator INSTANCE = new PlotByRatingComparator();

PlotByRatingComparator() {
}

@Override
public int compare(final Plot p1, final Plot p2) {
double v1 = 0;
int p1s = p1.getSettings().getRatings().size();
int p2s = p2.getRatings().size();
if (!p1.getSettings().getRatings().isEmpty()) {
v1 = p1.getRatings().values().stream().mapToDouble(Rating::getAverageRating)
.map(av -> av * av).sum();
v1 /= p1s;
v1 += p1s;
}
double v2 = 0;
if (!p2.getSettings().getRatings().isEmpty()) {
for (Map.Entry<UUID, Rating> entry : p2.getRatings().entrySet()) {
double av = entry.getValue().getAverageRating();
v2 += av * av;
}
v2 /= p2s;
v2 += p2s;
}
if (v2 == v1 && v2 != 0) {
return p2s - p1s;
}
return (int) Math.signum(v2 - v1);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* PlotSquared, a land and world management plugin for Minecraft.
* Copyright (C) IntellectualSites <https://intellectualsites.com>
* Copyright (C) IntellectualSites team and contributors
*
* 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 <https://www.gnu.org/licenses/>.
*/
package com.plotsquared.core.util.comparator;

import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.plot.PlotArea;

import javax.annotation.Nullable;
import java.util.Comparator;

public class PlotInPrioritizedAreaComparator implements Comparator<Plot> {

private final PlotArea priorityArea;

public PlotInPrioritizedAreaComparator(@Nullable final PlotArea area) {
this.priorityArea = area;
}

@Override
public int compare(final Plot first, final Plot second) {
if (this.priorityArea == null) {
return 0; // no defined priority? don't sort
}
if (this.priorityArea.equals(first.getArea())) {
return -1;
}
if (this.priorityArea.equals(second.getArea())) {
return 1;
}
return 0; // same area, don't sort
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Stream;

class AreaLimitedPlotProvider implements PlotProvider {

Expand All @@ -42,4 +43,9 @@ public Collection<Plot> getPlots() {
return plots;
}

@Override
public Stream<Plot> streamPlots() {
return streamPlotsInPlotAreas(this.areas.toArray(PlotArea[]::new));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.plotsquared.core.plot.Plot;

import java.util.Collection;
import java.util.stream.Stream;

class ExpiredPlotProvider implements PlotProvider {

Expand All @@ -30,4 +31,9 @@ public Collection<Plot> getPlots() {
return PlotSquared.platform().expireManager().getPendingExpired();
}

@Override
public Stream<Plot> streamPlots() {
return getPlots().stream();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.util.Collection;
import java.util.Collections;
import java.util.stream.Stream;

class FixedPlotProvider implements PlotProvider {

Expand All @@ -37,4 +38,9 @@ public Collection<Plot> getPlots() {
return Collections.singleton(plot);
}

@Override
public Stream<Plot> streamPlots() {
return Stream.of(plot);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;

class GlobalPlotProvider implements PlotProvider {

Expand All @@ -44,4 +45,9 @@ public Collection<Plot> getPlots() {
return plots;
}

@Override
public Stream<Plot> streamPlots() {
return streamPlotsInPlotAreas(this.plotAreaManager.getAllPlotAreas());
}

}
Loading