2020-04-16 04:52:39 +02:00
|
|
|
/*
|
2022-06-15 20:17:17 +02:00
|
|
|
* PlotSquared, a land and world management plugin for Minecraft.
|
|
|
|
* Copyright (C) IntellectualSites <https://intellectualsites.com>
|
|
|
|
* Copyright (C) IntellectualSites team and contributors
|
2020-04-16 04:52:39 +02:00
|
|
|
*
|
2022-06-15 20:17:17 +02:00
|
|
|
* 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.
|
2020-04-16 04:52:39 +02:00
|
|
|
*
|
2022-06-15 20:17:17 +02:00
|
|
|
* 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.
|
2020-04-16 04:52:39 +02:00
|
|
|
*
|
2022-06-15 20:17:17 +02:00
|
|
|
* 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-16 04:52:39 +02:00
|
|
|
*/
|
2020-04-15 21:26:54 +02:00
|
|
|
package com.plotsquared.core.command;
|
2015-02-15 11:52:51 +01:00
|
|
|
|
2020-07-14 18:49:40 +02:00
|
|
|
import com.google.inject.Inject;
|
2020-07-21 21:39:52 +02:00
|
|
|
import com.plotsquared.core.configuration.caption.TranslatableCaption;
|
2020-04-30 12:33:59 +02:00
|
|
|
import com.plotsquared.core.player.PlotPlayer;
|
2020-04-15 21:26:54 +02:00
|
|
|
import com.plotsquared.core.plot.Plot;
|
|
|
|
import com.plotsquared.core.plot.PlotArea;
|
|
|
|
import com.plotsquared.core.plot.PlotId;
|
2020-07-10 17:32:07 +02:00
|
|
|
import com.plotsquared.core.plot.world.PlotAreaManager;
|
2020-04-15 21:26:54 +02:00
|
|
|
import com.plotsquared.core.util.MathMan;
|
|
|
|
import com.plotsquared.core.util.WorldUtil;
|
2020-04-30 12:33:59 +02:00
|
|
|
import com.plotsquared.core.util.task.TaskManager;
|
2020-07-15 13:18:09 +02:00
|
|
|
import com.plotsquared.core.util.task.TaskTime;
|
2020-07-21 21:39:52 +02:00
|
|
|
import net.kyori.adventure.text.minimessage.Template;
|
2021-01-09 22:28:41 +01:00
|
|
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
2016-03-29 21:47:59 +02:00
|
|
|
|
2019-05-14 03:57:41 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
2016-02-14 02:01:18 +01:00
|
|
|
import java.util.Set;
|
2020-02-28 22:15:04 +01:00
|
|
|
import java.util.concurrent.ExecutionException;
|
2016-02-14 02:01:18 +01:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
|
2020-02-24 18:42:02 +01:00
|
|
|
@CommandDeclaration(command = "condense",
|
2021-01-09 22:28:41 +01:00
|
|
|
permission = "plots.admin",
|
|
|
|
usage = "/plot condense <area> <start|stop|info> [radius]",
|
|
|
|
category = CommandCategory.ADMINISTRATION,
|
|
|
|
requiredType = RequiredType.CONSOLE)
|
2020-02-24 18:42:02 +01:00
|
|
|
public class Condense extends SubCommand {
|
2016-03-22 18:53:17 +01:00
|
|
|
|
2015-07-27 00:26:19 +02:00
|
|
|
public static boolean TASK = false;
|
2016-03-22 18:53:17 +01:00
|
|
|
|
2020-07-10 17:32:07 +02:00
|
|
|
private final PlotAreaManager plotAreaManager;
|
2020-07-14 18:49:40 +02:00
|
|
|
private final WorldUtil worldUtil;
|
2020-07-10 17:32:07 +02:00
|
|
|
|
2021-01-09 22:28:41 +01:00
|
|
|
@Inject
|
|
|
|
public Condense(
|
|
|
|
final @NonNull PlotAreaManager plotAreaManager,
|
|
|
|
final @NonNull WorldUtil worldUtil
|
|
|
|
) {
|
2020-07-10 17:32:07 +02:00
|
|
|
this.plotAreaManager = plotAreaManager;
|
2020-07-14 18:49:40 +02:00
|
|
|
this.worldUtil = worldUtil;
|
2020-07-10 17:32:07 +02:00
|
|
|
}
|
|
|
|
|
2022-01-20 21:01:38 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
2021-01-09 22:28:41 +01:00
|
|
|
@Override
|
|
|
|
public boolean onCommand(final PlotPlayer<?> player, String[] args) {
|
2016-03-29 23:00:07 +02:00
|
|
|
if (args.length != 2 && args.length != 3) {
|
2020-07-21 21:39:52 +02:00
|
|
|
player.sendMessage(
|
|
|
|
TranslatableCaption.of("commandconfig.command_syntax"),
|
2021-12-14 23:04:36 +01:00
|
|
|
Template.of("value", "/plot condense <area> <start | stop | info> [radius]")
|
2020-07-21 21:39:52 +02:00
|
|
|
);
|
2015-02-16 04:46:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-10 17:32:07 +02:00
|
|
|
PlotArea area = this.plotAreaManager.getPlotAreaByString(args[0]);
|
2020-07-14 18:49:40 +02:00
|
|
|
if (area == null || !this.worldUtil.isWorld(area.getWorldName())) {
|
2020-07-21 21:39:52 +02:00
|
|
|
player.sendMessage(TranslatableCaption.of("invalid.invalid_area"));
|
2015-02-16 04:46:57 +01:00
|
|
|
return false;
|
2015-02-15 11:52:51 +01:00
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
switch (args[1].toLowerCase()) {
|
2021-05-15 20:39:16 +02:00
|
|
|
case "start" -> {
|
2015-09-13 06:04:31 +02:00
|
|
|
if (args.length == 2) {
|
2020-07-21 21:39:52 +02:00
|
|
|
player.sendMessage(
|
|
|
|
TranslatableCaption.of("commandconfig.command_syntax"),
|
2021-12-14 23:04:36 +01:00
|
|
|
Template.of("value", "/plot condense" + area + " start <radius>")
|
2020-07-21 21:39:52 +02:00
|
|
|
);
|
2015-02-16 04:46:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-03-29 23:00:07 +02:00
|
|
|
if (Condense.TASK) {
|
2020-07-21 21:39:52 +02:00
|
|
|
player.sendMessage(TranslatableCaption.of("condense.task_already_started"));
|
2015-02-16 06:36:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
if (!MathMan.isInteger(args[2])) {
|
2020-07-21 21:39:52 +02:00
|
|
|
player.sendMessage(TranslatableCaption.of("condense.invalid_radius"));
|
2015-02-16 06:36:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
int radius = Integer.parseInt(args[2]);
|
2020-07-08 15:09:25 +02:00
|
|
|
|
|
|
|
final List<Plot> plots = new ArrayList<>(area.getPlots());
|
2015-09-22 15:23:28 +02:00
|
|
|
// remove non base plots
|
2016-03-29 21:47:59 +02:00
|
|
|
Iterator<Plot> iterator = plots.iterator();
|
2015-09-22 15:23:28 +02:00
|
|
|
int maxSize = 0;
|
|
|
|
ArrayList<Integer> sizes = new ArrayList<>();
|
2016-03-29 21:47:59 +02:00
|
|
|
while (iterator.hasNext()) {
|
|
|
|
Plot plot = iterator.next();
|
2015-09-22 15:23:28 +02:00
|
|
|
if (!plot.isBasePlot()) {
|
2016-03-29 21:47:59 +02:00
|
|
|
iterator.remove();
|
2015-09-22 15:23:28 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int size = plot.getConnectedPlots().size();
|
|
|
|
if (size > maxSize) {
|
|
|
|
maxSize = size;
|
|
|
|
}
|
|
|
|
sizes.add(size - 1);
|
|
|
|
}
|
|
|
|
// Sort plots by size (buckets?)]
|
|
|
|
ArrayList<Plot>[] buckets = new ArrayList[maxSize];
|
|
|
|
for (int i = 0; i < plots.size(); i++) {
|
|
|
|
Plot plot = plots.get(i);
|
|
|
|
int size = sizes.get(i);
|
|
|
|
ArrayList<Plot> array = buckets[size];
|
|
|
|
if (array == null) {
|
2016-03-14 07:18:04 +01:00
|
|
|
array = new ArrayList<>();
|
2015-09-22 15:23:28 +02:00
|
|
|
buckets[size] = array;
|
|
|
|
}
|
|
|
|
array.add(plot);
|
|
|
|
}
|
2016-03-14 07:18:04 +01:00
|
|
|
final ArrayList<Plot> allPlots = new ArrayList<>(plots.size());
|
2015-09-22 15:23:28 +02:00
|
|
|
for (int i = buckets.length - 1; i >= 0; i--) {
|
|
|
|
ArrayList<Plot> array = buckets[i];
|
|
|
|
if (array != null) {
|
|
|
|
allPlots.addAll(array);
|
|
|
|
}
|
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
int size = allPlots.size();
|
2016-03-29 23:00:07 +02:00
|
|
|
int minimumRadius = (int) Math.ceil(Math.sqrt(size) / 2 + 1);
|
2016-03-23 02:41:37 +01:00
|
|
|
if (radius < minimumRadius) {
|
2020-07-21 21:39:52 +02:00
|
|
|
player.sendMessage(TranslatableCaption.of("condense.radius_too_small"));
|
2015-02-16 06:36:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
List<PlotId> toMove = new ArrayList<>(getPlots(allPlots, radius));
|
2015-02-16 06:36:47 +01:00
|
|
|
final List<PlotId> free = new ArrayList<>();
|
2020-07-18 11:05:16 +02:00
|
|
|
PlotId start = PlotId.of(0, 0);
|
|
|
|
while (start.getX() <= minimumRadius && start.getY() <= minimumRadius) {
|
2016-02-10 19:59:51 +01:00
|
|
|
Plot plot = area.getPlotAbs(start);
|
|
|
|
if (plot != null && !plot.hasOwner()) {
|
2016-01-10 21:00:56 +01:00
|
|
|
free.add(plot.getId());
|
2015-02-16 06:36:47 +01:00
|
|
|
}
|
2020-07-18 11:05:16 +02:00
|
|
|
start = start.getNextId();
|
2015-02-16 06:36:47 +01:00
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
if (free.isEmpty() || toMove.isEmpty()) {
|
2020-07-21 21:39:52 +02:00
|
|
|
player.sendMessage(TranslatableCaption.of("condense.no_free_plots_found"));
|
2015-03-13 14:29:58 +01:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-21 21:39:52 +02:00
|
|
|
player.sendMessage(TranslatableCaption.of("condense.task_started"));
|
2019-05-14 03:57:41 +02:00
|
|
|
Condense.TASK = true;
|
2015-09-22 15:23:28 +02:00
|
|
|
Runnable run = new Runnable() {
|
2021-01-09 22:28:41 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2016-03-29 23:00:07 +02:00
|
|
|
if (!Condense.TASK) {
|
2021-02-02 16:48:01 +01:00
|
|
|
player.sendMessage(TranslatableCaption.of("debugexec.task_cancelled"));
|
2015-09-22 15:23:28 +02:00
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
if (allPlots.isEmpty()) {
|
2016-03-29 23:00:07 +02:00
|
|
|
Condense.TASK = false;
|
2020-07-21 21:39:52 +02:00
|
|
|
player.sendMessage(TranslatableCaption.of("condense.task_complete"));
|
2015-02-16 06:36:47 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-09-22 15:23:28 +02:00
|
|
|
final Runnable task = this;
|
|
|
|
final Plot origin = allPlots.remove(0);
|
|
|
|
int i = 0;
|
|
|
|
while (free.size() > i) {
|
2016-02-11 02:37:21 +01:00
|
|
|
final Plot possible = origin.getArea().getPlotAbs(free.get(i));
|
2016-02-14 02:01:18 +01:00
|
|
|
if (possible.hasOwner()) {
|
2015-09-22 15:23:28 +02:00
|
|
|
free.remove(i);
|
|
|
|
continue;
|
2015-02-16 06:36:47 +01:00
|
|
|
}
|
2015-09-22 15:23:28 +02:00
|
|
|
i++;
|
|
|
|
final AtomicBoolean result = new AtomicBoolean(false);
|
2020-02-28 22:15:04 +01:00
|
|
|
try {
|
2020-09-11 13:59:40 +02:00
|
|
|
result.set(origin.getPlotModificationManager().move(possible, player, () -> {
|
2020-02-28 22:15:04 +01:00
|
|
|
if (result.get()) {
|
2020-07-21 21:39:52 +02:00
|
|
|
player.sendMessage(
|
|
|
|
TranslatableCaption.of("condense.moving"),
|
2021-12-14 23:04:36 +01:00
|
|
|
Template.of("origin", String.valueOf(origin)),
|
|
|
|
Template.of("possible", String.valueOf(possible))
|
2020-07-21 21:39:52 +02:00
|
|
|
);
|
2020-07-15 13:18:09 +02:00
|
|
|
TaskManager.runTaskLater(task, TaskTime.ticks(1L));
|
2020-02-28 22:15:04 +01:00
|
|
|
}
|
|
|
|
}, false).get());
|
|
|
|
} catch (InterruptedException | ExecutionException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2015-09-22 15:23:28 +02:00
|
|
|
if (result.get()) {
|
2015-02-16 06:36:47 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
if (free.isEmpty()) {
|
2016-03-29 23:00:07 +02:00
|
|
|
Condense.TASK = false;
|
2020-07-21 21:39:52 +02:00
|
|
|
player.sendMessage(TranslatableCaption.of("condense.task_failed"));
|
2015-02-16 06:36:47 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-09-22 15:23:28 +02:00
|
|
|
if (i >= free.size()) {
|
2020-07-21 21:39:52 +02:00
|
|
|
player.sendMessage(
|
|
|
|
TranslatableCaption.of("condense.skipping"),
|
2021-12-14 23:04:36 +01:00
|
|
|
Template.of("plot", String.valueOf(origin))
|
2020-07-21 21:39:52 +02:00
|
|
|
);
|
2015-09-22 15:23:28 +02:00
|
|
|
}
|
2015-02-16 06:36:47 +01:00
|
|
|
}
|
2015-09-22 15:23:28 +02:00
|
|
|
};
|
|
|
|
TaskManager.runTaskAsync(run);
|
2015-02-16 04:46:57 +01:00
|
|
|
return true;
|
|
|
|
}
|
2021-05-15 20:39:16 +02:00
|
|
|
case "stop" -> {
|
2016-03-29 23:00:07 +02:00
|
|
|
if (!Condense.TASK) {
|
2021-02-02 16:48:01 +01:00
|
|
|
player.sendMessage(TranslatableCaption.of("condense.task_stopped"));
|
2015-02-16 06:36:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-03-29 23:00:07 +02:00
|
|
|
Condense.TASK = false;
|
2020-07-21 21:39:52 +02:00
|
|
|
player.sendMessage(TranslatableCaption.of("condense.task_stopped"));
|
2015-02-16 04:46:57 +01:00
|
|
|
return true;
|
2021-05-15 20:39:16 +02:00
|
|
|
}
|
|
|
|
case "info" -> {
|
2015-09-13 06:04:31 +02:00
|
|
|
if (args.length == 2) {
|
2020-07-21 21:39:52 +02:00
|
|
|
player.sendMessage(
|
|
|
|
TranslatableCaption.of("commandconfig.command_syntax"),
|
2021-12-14 23:04:36 +01:00
|
|
|
Template.of("value", "/plot condense " + area + " info <radius>")
|
2020-07-21 21:39:52 +02:00
|
|
|
);
|
2015-02-16 04:46:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
if (!MathMan.isInteger(args[2])) {
|
2020-07-21 21:39:52 +02:00
|
|
|
player.sendMessage(TranslatableCaption.of("condense.invalid_radius"));
|
2015-02-16 04:46:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
int radius = Integer.parseInt(args[2]);
|
|
|
|
Collection<Plot> plots = area.getPlots();
|
|
|
|
int size = plots.size();
|
2016-03-29 23:00:07 +02:00
|
|
|
int minimumRadius = (int) Math.ceil(Math.sqrt(size) / 2 + 1);
|
2016-03-23 02:41:37 +01:00
|
|
|
if (radius < minimumRadius) {
|
2020-07-21 21:39:52 +02:00
|
|
|
player.sendMessage(TranslatableCaption.of("condense.radius_too_small"));
|
2015-02-16 06:36:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
int maxMove = getPlots(plots, minimumRadius).size();
|
|
|
|
int userMove = getPlots(plots, radius).size();
|
2020-07-21 21:39:52 +02:00
|
|
|
player.sendMessage(TranslatableCaption.of("condense.default_eval"));
|
|
|
|
player.sendMessage(
|
|
|
|
TranslatableCaption.of("condense.minimum_radius"),
|
2021-12-14 23:04:36 +01:00
|
|
|
Template.of("minimumRadius", String.valueOf(minimumRadius))
|
2020-07-21 21:39:52 +02:00
|
|
|
);
|
|
|
|
player.sendMessage(
|
|
|
|
TranslatableCaption.of("condense.minimum_radius"),
|
2021-12-14 23:04:36 +01:00
|
|
|
Template.of("maxMove", String.valueOf(maxMove))
|
2020-07-21 21:39:52 +02:00
|
|
|
);
|
|
|
|
player.sendMessage(TranslatableCaption.of("condense.input_eval"));
|
|
|
|
player.sendMessage(
|
|
|
|
TranslatableCaption.of("condense.input_radius"),
|
2021-12-14 23:04:36 +01:00
|
|
|
Template.of("radius", String.valueOf(radius))
|
2020-07-21 21:39:52 +02:00
|
|
|
);
|
|
|
|
player.sendMessage(
|
|
|
|
TranslatableCaption.of("condense.estimated_moves"),
|
2021-12-14 23:04:36 +01:00
|
|
|
Template.of("userMove", String.valueOf(userMove))
|
2020-07-21 21:39:52 +02:00
|
|
|
);
|
|
|
|
player.sendMessage(TranslatableCaption.of("condense.eta"));
|
|
|
|
player.sendMessage(TranslatableCaption.of("condense.radius_measured"));
|
2015-02-16 04:46:57 +01:00
|
|
|
return true;
|
2021-05-15 20:39:16 +02:00
|
|
|
}
|
2015-02-15 11:52:51 +01:00
|
|
|
}
|
2020-07-21 21:39:52 +02:00
|
|
|
player.sendMessage(
|
|
|
|
TranslatableCaption.of("commandconfig.command_syntax"),
|
2021-12-14 23:04:36 +01:00
|
|
|
Template.of("value", "/plot condense " + area.getWorldName() + " <start | stop | info> [radius]")
|
2020-07-21 21:39:52 +02:00
|
|
|
);
|
2015-02-16 04:46:57 +01:00
|
|
|
return false;
|
2015-02-15 11:52:51 +01:00
|
|
|
}
|
2016-03-22 18:53:17 +01:00
|
|
|
|
2016-03-23 02:41:37 +01:00
|
|
|
public Set<PlotId> getPlots(Collection<Plot> plots, int radius) {
|
|
|
|
HashSet<PlotId> outside = new HashSet<>();
|
|
|
|
for (Plot plot : plots) {
|
2020-07-18 11:05:16 +02:00
|
|
|
if (plot.getId().getX() > radius || plot.getId().getX() < -radius || plot.getId().getY() > radius
|
2021-01-09 22:28:41 +01:00
|
|
|
|| plot.getId().getY() < -radius) {
|
2016-01-10 21:00:56 +01:00
|
|
|
outside.add(plot.getId());
|
2015-02-16 04:46:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return outside;
|
2015-02-15 11:52:51 +01:00
|
|
|
}
|
2021-01-09 22:28:41 +01:00
|
|
|
|
2015-02-15 11:52:51 +01:00
|
|
|
}
|