PlotSquared/Core/src/main/java/com/plotsquared/core/plot/expiration/ExpiryTask.java

166 lines
6.3 KiB
Java
Raw Normal View History

/*
* 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/>.
*/
2020-04-15 21:26:54 +02:00
package com.plotsquared.core.plot.expiration;
import com.plotsquared.core.PlotSquared;
2020-04-16 06:14:33 +02:00
import com.plotsquared.core.configuration.Settings;
2020-04-15 21:26:54 +02:00
import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.plot.PlotArea;
2020-07-10 17:32:07 +02:00
import com.plotsquared.core.plot.world.PlotAreaManager;
2020-07-08 15:09:25 +02:00
import com.plotsquared.core.util.query.PlotQuery;
import org.checkerframework.checker.nullness.qual.NonNull;
2019-02-04 14:59:11 +01:00
2016-06-17 05:33:47 +02:00
import java.util.ArrayList;
2020-07-08 15:09:25 +02:00
import java.util.Collection;
2016-06-17 05:33:47 +02:00
import java.util.Collections;
2020-07-08 15:09:25 +02:00
import java.util.LinkedList;
2016-06-17 05:33:47 +02:00
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
public class ExpiryTask {
private final Settings.Auto_Clear settings;
2020-07-10 17:32:07 +02:00
private final PlotAreaManager plotAreaManager;
2018-08-10 17:01:10 +02:00
private long cutoffThreshold = Long.MIN_VALUE;
public ExpiryTask(final Settings.Auto_Clear settings, final @NonNull PlotAreaManager plotAreaManager) {
this.settings = settings;
2020-07-10 17:32:07 +02:00
this.plotAreaManager = plotAreaManager;
}
2018-06-18 19:29:14 +02:00
public Settings.Auto_Clear getSettings() {
return settings;
}
2016-06-17 05:33:47 +02:00
public boolean allowsArea(PlotArea area) {
2020-04-30 12:01:52 +02:00
return settings.WORLDS.contains(area.toString()) || settings.WORLDS
.contains(area.getWorldName()) || settings.WORLDS.contains("*");
}
2016-06-17 05:33:47 +02:00
public boolean applies(PlotArea area) {
if (allowsArea(area)) {
if (settings.REQUIRED_PLOTS <= 0) {
return true;
}
Set<Plot> plots = null;
2018-08-10 17:01:10 +02:00
if (cutoffThreshold != Long.MAX_VALUE
&& area.getPlots().size() > settings.REQUIRED_PLOTS
|| (plots = getPlotsToCheck()).size() > settings.REQUIRED_PLOTS) {
2016-06-17 05:33:47 +02:00
// calculate cutoff
if (cutoffThreshold == Long.MIN_VALUE) {
plots = plots != null ? plots : getPlotsToCheck();
int diff = settings.REQUIRED_PLOTS;
boolean min = true;
if (plots.size() > settings.REQUIRED_PLOTS) {
2016-06-17 05:33:47 +02:00
min = false;
diff = plots.size() - settings.REQUIRED_PLOTS;
2016-06-17 05:33:47 +02:00
}
ExpireManager expireManager = PlotSquared.platform().expireManager();
List<Long> entireList =
plots.stream().map(plot -> expireManager.getAge(plot, settings.DELETE_IF_OWNER_IS_UNKNOWN))
.collect(Collectors.toList());
2016-06-17 05:33:47 +02:00
List<Long> top = new ArrayList<>(diff + 1);
if (diff > 1000) {
Collections.sort(entireList);
cutoffThreshold = entireList.get(settings.REQUIRED_PLOTS);
} else {
loop:
for (long num : entireList) {
int size = top.size();
if (size == 0) {
top.add(num);
continue;
}
long end = top.get(size - 1);
if (min ? num < end : num > end) {
for (int i = 0; i < size; i++) {
long existing = top.get(i);
if (min ? num < existing : num > existing) {
top.add(i, num);
if (size == diff) {
top.remove(size);
}
continue loop;
}
}
}
if (size < diff) {
top.add(num);
}
}
cutoffThreshold = top.get(top.size() - 1);
}
// Add half a day, as expiry is performed each day
cutoffThreshold += (TimeUnit.DAYS.toMillis(1) / 2);
}
return true;
} else {
cutoffThreshold = Long.MAX_VALUE;
}
}
return false;
}
public Set<Plot> getPlotsToCheck() {
2020-07-08 15:09:25 +02:00
final Collection<PlotArea> areas = new LinkedList<>();
2020-07-10 17:32:07 +02:00
for (final PlotArea plotArea : this.plotAreaManager.getAllPlotAreas()) {
2020-07-08 15:09:25 +02:00
if (this.allowsArea(plotArea)) {
areas.add(plotArea);
2016-06-17 05:33:47 +02:00
}
2020-07-08 15:09:25 +02:00
}
return PlotQuery.newQuery().inAreas(areas).asSet();
2016-06-17 05:33:47 +02:00
}
public boolean applies(long diff) {
2016-06-17 05:33:47 +02:00
return diff > TimeUnit.DAYS.toMillis(settings.DAYS) && diff > cutoffThreshold;
}
2018-06-18 19:29:14 +02:00
public boolean appliesAccountAge(long accountAge) {
if (settings.SKIP_ACCOUNT_AGE_DAYS != -1) {
2018-06-18 19:29:14 +02:00
return accountAge <= TimeUnit.DAYS.toMillis(settings.SKIP_ACCOUNT_AGE_DAYS);
}
2018-06-18 19:29:14 +02:00
return false;
}
public boolean needsAnalysis() {
return settings.THRESHOLD > 0;
}
public boolean applies(PlotAnalysis analysis) {
return analysis.getComplexity(settings) <= settings.THRESHOLD;
}
public boolean requiresConfirmation() {
return settings.CONFIRMATION;
}
2016-06-17 05:33:47 +02:00
/**
* Returns {@code true} if this task respects unknown owners
*
* @return {@code true} if unknown owners should be counted as never online
2022-01-27 13:49:17 +01:00
* @since 6.4.0
*/
public boolean shouldDeleteForUnknownOwner() {
return settings.DELETE_IF_OWNER_IS_UNKNOWN;
}
2016-06-17 05:33:47 +02:00
}