PlotSquared/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Condense.java

217 lines
9.8 KiB
Java
Raw Normal View History

package com.github.intellectualsites.plotsquared.plot.commands;
2015-02-15 11:52:51 +01:00
import com.github.intellectualsites.plotsquared.commands.CommandDeclaration;
import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.object.Plot;
import com.github.intellectualsites.plotsquared.plot.object.PlotArea;
import com.github.intellectualsites.plotsquared.plot.object.PlotId;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.util.MainUtil;
import com.github.intellectualsites.plotsquared.plot.util.MathMan;
import com.github.intellectualsites.plotsquared.plot.util.TaskManager;
import com.github.intellectualsites.plotsquared.plot.util.WorldUtil;
2016-03-29 21:47:59 +02:00
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
Pull/2693 (#2694) * Commit WIP flag work. * More ported flag types, and additions to the flag API. * Make PlotFlag more generic to allow generic flag creation * Pull Captions methods into a Caption interface. * Port MusicFlag * Port flight flag * Port UntrustedVisitFlag * Port DenyExitFlag * Remove paper suggestion * Make ListFlag lists immutable * Update Flag containers. Add javadocs. Add missing methods. * Port description flag * Port greeting and farewell flags * Port weather flag * Move getExample implementation to BooleanFlag * Port reserved flags * Port all boolean flags. * Remove unused flag types * Invert liquid-flow flag * Find the real (legacy) flag name * Change NOITFY -> NOTIFY in Captions * Make IntegerFlag extendable * Port integer flags * Update Flag command to current API state * Begin remaking flag command * Create DoubleFlag + extract common parsing stuff * Supply arguments in flag parse exceptions * Implement missing flag subcommands * Update Flag command to current API state * Implement PriceFlag * Port deny-teleport * Port gamemode flags * Port BreakFlag * Port PlaceFlag * Port UseFlag * Remove old unused flag constants * Port blocked-cmds flag * Fix entity util * Port TimeFlag * Use CaptionUtility for formatting * Port keep flag * Fix imports * Reformat code * Remove unused classes * Fix MainUtil.java * Remove FlagCmd * Add flag info header and footer * Comment out flag related stuff in SchematicHandler * Remove FlagManager * Finalize Plot.java * Finalize PlotArea.java * Finalize PlotListener * Fix API issues * Fix a bunch of compile errors * Fix `/plot flag remove` * Fix initialization of GlobalFlagContainer * Apply API changes to events * Update SQLManager to new API * Invert default value for DenyExitFlag * Replace flag.getValue().toString() with flag.toString() * Make FlagContainer instance in Plot final * Fix various command issues * Clean up PlotSettings * Don't show internal flags in flag list * Fix `/plot flag add` * Remove the info inventory as it's 100% broken * Add plot info entries and fix up the default format * Fix default flag state in Captions * 781c200 part 2 * Fix odd grammar in captions * Fix odd grammar in captions v2 * Add create table statements for plot_flags * Remove old flag references in SQLManager * Use the new plot_flags table * Add tab completion to `/plot flag` * Improve parse error handling * Make flag permission check recognize parse exceptions * Initial progress towards flag conversion * Fix minor issues * Don't validate flags during flag conversion * Allow unrecognized flags to be parsed * Filter out internal flags from command sugguestions * Use the wrong caption when there's no plot description set * Limit command suggestions for boolean flags * Make blocktypelistflags accept blockcategories * Require categories to be prefixed with '#' and fix some minor display issues * Fix plot database conversion * Update PlotFlagEvent.java Updated return description * Fix command annotation wrapping * Add FLAG_UPDATE event for FlagContainer listeners * Make getParentContainer nullable * Document castUnsafe in FlagContainer * Document FlagContainer constructors * Add missing documentation to FlagContainer * Document FlagParseException * Fix wording in FlagContainer javadoc * Document InternalFlag * Document PlotFlag * Minor changes * Remove revisit comments Co-authored-by: Hannes Greule <SirYwell@users.noreply.github.com> Co-authored-by: NotMyFault <mc.cache@web.de> Co-authored-by: Matt <4009945+MattBDev@users.noreply.github.com>
2020-02-24 18:42:02 +01:00
@CommandDeclaration(command = "condense",
permission = "plots.admin",
2019-09-04 03:01:25 +02:00
usage = "/plot condense <area> <start|stop|info> [radius]",
Pull/2693 (#2694) * Commit WIP flag work. * More ported flag types, and additions to the flag API. * Make PlotFlag more generic to allow generic flag creation * Pull Captions methods into a Caption interface. * Port MusicFlag * Port flight flag * Port UntrustedVisitFlag * Port DenyExitFlag * Remove paper suggestion * Make ListFlag lists immutable * Update Flag containers. Add javadocs. Add missing methods. * Port description flag * Port greeting and farewell flags * Port weather flag * Move getExample implementation to BooleanFlag * Port reserved flags * Port all boolean flags. * Remove unused flag types * Invert liquid-flow flag * Find the real (legacy) flag name * Change NOITFY -> NOTIFY in Captions * Make IntegerFlag extendable * Port integer flags * Update Flag command to current API state * Begin remaking flag command * Create DoubleFlag + extract common parsing stuff * Supply arguments in flag parse exceptions * Implement missing flag subcommands * Update Flag command to current API state * Implement PriceFlag * Port deny-teleport * Port gamemode flags * Port BreakFlag * Port PlaceFlag * Port UseFlag * Remove old unused flag constants * Port blocked-cmds flag * Fix entity util * Port TimeFlag * Use CaptionUtility for formatting * Port keep flag * Fix imports * Reformat code * Remove unused classes * Fix MainUtil.java * Remove FlagCmd * Add flag info header and footer * Comment out flag related stuff in SchematicHandler * Remove FlagManager * Finalize Plot.java * Finalize PlotArea.java * Finalize PlotListener * Fix API issues * Fix a bunch of compile errors * Fix `/plot flag remove` * Fix initialization of GlobalFlagContainer * Apply API changes to events * Update SQLManager to new API * Invert default value for DenyExitFlag * Replace flag.getValue().toString() with flag.toString() * Make FlagContainer instance in Plot final * Fix various command issues * Clean up PlotSettings * Don't show internal flags in flag list * Fix `/plot flag add` * Remove the info inventory as it's 100% broken * Add plot info entries and fix up the default format * Fix default flag state in Captions * 781c200 part 2 * Fix odd grammar in captions * Fix odd grammar in captions v2 * Add create table statements for plot_flags * Remove old flag references in SQLManager * Use the new plot_flags table * Add tab completion to `/plot flag` * Improve parse error handling * Make flag permission check recognize parse exceptions * Initial progress towards flag conversion * Fix minor issues * Don't validate flags during flag conversion * Allow unrecognized flags to be parsed * Filter out internal flags from command sugguestions * Use the wrong caption when there's no plot description set * Limit command suggestions for boolean flags * Make blocktypelistflags accept blockcategories * Require categories to be prefixed with '#' and fix some minor display issues * Fix plot database conversion * Update PlotFlagEvent.java Updated return description * Fix command annotation wrapping * Add FLAG_UPDATE event for FlagContainer listeners * Make getParentContainer nullable * Document castUnsafe in FlagContainer * Document FlagContainer constructors * Add missing documentation to FlagContainer * Document FlagParseException * Fix wording in FlagContainer javadoc * Document InternalFlag * Document PlotFlag * Minor changes * Remove revisit comments Co-authored-by: Hannes Greule <SirYwell@users.noreply.github.com> Co-authored-by: NotMyFault <mc.cache@web.de> Co-authored-by: Matt <4009945+MattBDev@users.noreply.github.com>
2020-02-24 18:42:02 +01:00
description = "Condense a plotworld",
category = CommandCategory.ADMINISTRATION,
requiredType = RequiredType.CONSOLE)
public class Condense extends SubCommand {
2016-03-22 18:53:17 +01:00
public static boolean TASK = false;
2016-03-22 18:53:17 +01:00
2018-08-10 17:01:10 +02:00
@Override public boolean onCommand(final PlotPlayer player, String[] args) {
if (args.length != 2 && args.length != 3) {
2019-09-04 03:01:25 +02:00
MainUtil.sendMessage(player, getUsage());
2015-02-16 04:46:57 +01:00
return false;
}
PlotArea area = PlotSquared.get().getPlotAreaByString(args[0]);
2016-02-10 19:59:51 +01:00
if (area == null || !WorldUtil.IMP.isWorld(area.worldname)) {
2016-06-02 17:38:47 +02:00
MainUtil.sendMessage(player, "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()) {
case "start": {
if (args.length == 2) {
2018-08-10 17:01:10 +02:00
MainUtil.sendMessage(player,
"/plot condense " + area.toString() + " start <radius>");
2015-02-16 04:46:57 +01:00
return false;
}
if (Condense.TASK) {
2016-06-02 17:38:47 +02:00
MainUtil.sendMessage(player, "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])) {
2016-06-02 17:38:47 +02:00
MainUtil.sendMessage(player, "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]);
ArrayList<Plot> plots = new ArrayList<>(PlotSquared.get().getPlots(area));
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();
int minimumRadius = (int) Math.ceil(Math.sqrt(size) / 2 + 1);
2016-03-23 02:41:37 +01:00
if (radius < minimumRadius) {
2016-06-02 17:38:47 +02:00
MainUtil.sendMessage(player, "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<>();
2015-02-20 07:34:19 +01:00
PlotId start = new PlotId(0, 0);
while (start.x <= minimumRadius && start.y <= minimumRadius) {
2016-02-10 19:59:51 +01:00
Plot plot = area.getPlotAbs(start);
if (plot != null && !plot.hasOwner()) {
free.add(plot.getId());
2015-02-16 06:36:47 +01:00
}
2018-12-27 17:29:35 +01:00
start = Auto.getNextPlotId(start, 1);
2015-02-16 06:36:47 +01:00
}
2016-03-23 02:41:37 +01:00
if (free.isEmpty() || toMove.isEmpty()) {
2016-06-02 17:38:47 +02:00
MainUtil.sendMessage(player, "NO FREE PLOTS FOUND");
2015-03-13 14:29:58 +01:00
return false;
}
2016-06-02 17:38:47 +02:00
MainUtil.sendMessage(player, "TASK STARTED...");
Condense.TASK = true;
2015-09-22 15:23:28 +02:00
Runnable run = new Runnable() {
2018-08-10 17:01:10 +02:00
@Override public void run() {
if (!Condense.TASK) {
2016-06-02 17:38:47 +02:00
MainUtil.sendMessage(player, "TASK CANCELLED.");
2015-09-22 15:23:28 +02:00
}
if (allPlots.isEmpty()) {
Condense.TASK = false;
2018-08-10 17:01:10 +02:00
MainUtil.sendMessage(player,
"TASK COMPLETE. PLEASE VERIFY THAT NO NEW PLOTS HAVE BEEN CLAIMED DURING TASK.");
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));
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);
result.set(origin.move(possible, () -> {
if (result.get()) {
MainUtil.sendMessage(player,
"Moving: " + origin + " -> " + possible);
TaskManager.runTaskLater(task, 1);
2015-09-22 15:23:28 +02:00
}
}, false));
if (result.get()) {
2015-02-16 06:36:47 +01:00
break;
}
}
if (free.isEmpty()) {
Condense.TASK = false;
2016-06-02 17:38:47 +02:00
MainUtil.sendMessage(player, "TASK FAILED. NO FREE PLOTS FOUND!");
2015-02-16 06:36:47 +01:00
return;
}
2015-09-22 15:23:28 +02:00
if (i >= free.size()) {
2016-06-02 17:38:47 +02:00
MainUtil.sendMessage(player, "SKIPPING COMPLEX PLOT: " + origin);
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;
}
case "stop":
if (!Condense.TASK) {
2016-06-02 17:38:47 +02:00
MainUtil.sendMessage(player, "TASK ALREADY STOPPED");
2015-02-16 06:36:47 +01:00
return false;
}
Condense.TASK = false;
2016-06-02 17:38:47 +02:00
MainUtil.sendMessage(player, "TASK STOPPED");
2015-02-16 04:46:57 +01:00
return true;
case "info":
2015-09-13 06:04:31 +02:00
if (args.length == 2) {
2018-08-10 17:01:10 +02:00
MainUtil.sendMessage(player,
"/plot condense " + area.toString() + " info <radius>");
2015-02-16 04:46:57 +01:00
return false;
}
2015-09-13 06:04:31 +02:00
if (!MathMan.isInteger(args[2])) {
2016-06-02 17:38:47 +02:00
MainUtil.sendMessage(player, "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();
int minimumRadius = (int) Math.ceil(Math.sqrt(size) / 2 + 1);
2016-03-23 02:41:37 +01:00
if (radius < minimumRadius) {
2016-06-02 17:38:47 +02:00
MainUtil.sendMessage(player, "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();
2016-06-02 17:38:47 +02:00
MainUtil.sendMessage(player, "=== DEFAULT EVAL ===");
MainUtil.sendMessage(player, "MINIMUM RADIUS: " + minimumRadius);
MainUtil.sendMessage(player, "MAXIMUM MOVES: " + maxMove);
MainUtil.sendMessage(player, "=== INPUT EVAL ===");
MainUtil.sendMessage(player, "INPUT RADIUS: " + radius);
MainUtil.sendMessage(player, "ESTIMATED MOVES: " + userMove);
2018-08-10 17:01:10 +02:00
MainUtil.sendMessage(player,
"ESTIMATED TIME: No idea, times will drastically change based on the system performance and load");
2016-06-02 17:38:47 +02:00
MainUtil.sendMessage(player, "&e - Radius is measured in plot width");
2015-02-16 04:46:57 +01:00
return true;
2015-02-15 11:52:51 +01:00
}
2018-08-10 17:01:10 +02:00
MainUtil.sendMessage(player,
"/plot condense " + area.worldname + " <start|stop|info> [radius]");
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) {
2018-08-10 17:01:10 +02:00
if (plot.getId().x > radius || plot.getId().x < -radius || plot.getId().y > radius
|| plot.getId().y < -radius) {
outside.add(plot.getId());
2015-02-16 04:46:57 +01:00
}
}
return outside;
2015-02-15 11:52:51 +01:00
}
}