2015-02-15 11:52:51 +01:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// PlotSquared - A plot manager and world generator for the Bukkit API /
|
|
|
|
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
|
|
|
|
// /
|
|
|
|
// 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, write to the Free Software Foundation, /
|
|
|
|
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
|
|
|
|
// /
|
|
|
|
// You can contact us via: support@intellectualsites.com /
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
package com.intellectualcrafters.plot.commands;
|
|
|
|
|
2015-07-03 14:15:20 +02:00
|
|
|
import com.intellectualcrafters.plot.PS;
|
2015-02-15 11:52:51 +01:00
|
|
|
import com.intellectualcrafters.plot.object.Plot;
|
|
|
|
import com.intellectualcrafters.plot.object.PlotId;
|
2015-02-21 12:38:44 +01:00
|
|
|
import com.intellectualcrafters.plot.object.PlotPlayer;
|
2015-02-22 07:30:58 +01:00
|
|
|
import com.intellectualcrafters.plot.util.BlockManager;
|
2015-02-20 12:23:48 +01:00
|
|
|
import com.intellectualcrafters.plot.util.MainUtil;
|
2015-07-27 19:50:04 +02:00
|
|
|
import com.plotsquared.general.commands.CommandDeclaration;
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.Set;
|
2015-02-15 11:52:51 +01:00
|
|
|
|
2015-07-27 00:26:19 +02:00
|
|
|
@CommandDeclaration(
|
|
|
|
command = "condense",
|
|
|
|
permission = "plots.admin",
|
|
|
|
description = "Condense a plotworld",
|
|
|
|
category = CommandCategory.DEBUG,
|
2015-07-27 15:10:14 +02:00
|
|
|
requiredType = RequiredType.CONSOLE
|
2015-07-27 00:26:19 +02:00
|
|
|
)
|
2015-02-15 11:52:51 +01:00
|
|
|
public class Condense extends SubCommand {
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2015-07-27 00:26:19 +02:00
|
|
|
public static boolean TASK = false;
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2015-07-03 11:30:26 +02:00
|
|
|
public static void sendMessage(final String message) {
|
2015-07-03 14:15:20 +02:00
|
|
|
PS.log("&3PlotSquared -> Plot condense&8: &7" + message);
|
2015-07-03 11:30:26 +02:00
|
|
|
}
|
|
|
|
|
2015-02-16 04:46:57 +01:00
|
|
|
@Override
|
2015-07-27 15:10:14 +02:00
|
|
|
public boolean onCommand(final PlotPlayer plr, String ... args) {
|
2015-02-20 07:34:19 +01:00
|
|
|
if ((args.length != 2) && (args.length != 3)) {
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, "/plot condense <world> <start|stop|info> [radius]");
|
2015-02-16 04:46:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
final String worldname = args[0];
|
2015-07-03 14:15:20 +02:00
|
|
|
if (!BlockManager.manager.isWorld(worldname) || !PS.get().isPlotWorld(worldname)) {
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, "INVALID WORLD");
|
2015-02-16 04:46:57 +01:00
|
|
|
return false;
|
2015-02-15 11:52:51 +01:00
|
|
|
}
|
2015-02-16 04:46:57 +01:00
|
|
|
switch (args[1].toLowerCase()) {
|
|
|
|
case "start": {
|
|
|
|
if (args.length == 2) {
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, "/plot condense " + worldname + " start <radius>");
|
2015-02-16 04:46:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-16 06:36:47 +01:00
|
|
|
if (TASK) {
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, "TASK ALREADY STARTED");
|
2015-02-16 06:36:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (args.length == 2) {
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, "/plot condense " + worldname + " start <radius>");
|
2015-02-16 06:36:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!StringUtils.isNumeric(args[2])) {
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, "INVALID RADIUS");
|
2015-02-16 06:36:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
final int radius = Integer.parseInt(args[2]);
|
2015-07-03 14:15:20 +02:00
|
|
|
final Collection<Plot> plots = PS.get().getPlots(worldname).values();
|
2015-02-20 07:34:19 +01:00
|
|
|
final int size = plots.size();
|
|
|
|
final int minimum_radius = (int) Math.ceil((Math.sqrt(size) / 2) + 1);
|
2015-02-16 06:36:47 +01:00
|
|
|
if (radius < minimum_radius) {
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, "RADIUS TOO SMALL");
|
2015-02-16 06:36:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
final List<PlotId> to_move = new ArrayList<>(getPlots(plots, radius));
|
|
|
|
final List<PlotId> free = new ArrayList<>();
|
2015-02-20 07:34:19 +01:00
|
|
|
PlotId start = new PlotId(0, 0);
|
|
|
|
while ((start.x <= minimum_radius) && (start.y <= minimum_radius)) {
|
2015-02-22 07:09:20 +01:00
|
|
|
final Plot plot = MainUtil.getPlot(worldname, start);
|
2015-02-16 06:36:47 +01:00
|
|
|
if (!plot.hasOwner()) {
|
|
|
|
free.add(plot.id);
|
|
|
|
}
|
|
|
|
start = Auto.getNextPlot(start, 1);
|
|
|
|
}
|
2015-03-13 14:29:58 +01:00
|
|
|
if (free.size() == 0 || to_move.size() == 0) {
|
2015-07-13 21:31:22 +02:00
|
|
|
MainUtil.sendMessage(plr, "NO FREE PLOTS FOUND");
|
2015-03-13 14:29:58 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-03-24 04:11:28 +01:00
|
|
|
MainUtil.move(MainUtil.getPlot(worldname, to_move.get(0)), MainUtil.getPlot(worldname, free.get(0)), new Runnable() {
|
2015-02-16 06:36:47 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (!TASK) {
|
|
|
|
sendMessage("CONDENSE TASK CANCELLED");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
to_move.remove(0);
|
|
|
|
free.remove(0);
|
|
|
|
int index = 0;
|
2015-02-20 07:34:19 +01:00
|
|
|
for (final PlotId id : to_move) {
|
2015-02-22 07:09:20 +01:00
|
|
|
final Plot plot = MainUtil.getPlot(worldname, id);
|
2015-02-16 06:36:47 +01:00
|
|
|
if (plot.hasOwner()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
for (int i = 0; i < index; i++) {
|
2015-02-16 06:36:47 +01:00
|
|
|
to_move.remove(0);
|
|
|
|
}
|
|
|
|
index = 0;
|
2015-02-20 07:34:19 +01:00
|
|
|
for (final PlotId id : free) {
|
2015-02-22 07:09:20 +01:00
|
|
|
final Plot plot = MainUtil.getPlot(worldname, id);
|
2015-02-16 06:36:47 +01:00
|
|
|
if (!plot.hasOwner()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
for (int i = 0; i < index; i++) {
|
2015-02-16 06:36:47 +01:00
|
|
|
free.remove(0);
|
|
|
|
}
|
|
|
|
if (to_move.size() == 0) {
|
|
|
|
sendMessage("TASK COMPLETE. PLEASE VERIFY THAT NO NEW PLOTS HAVE BEEN CLAIMED DURING TASK.");
|
|
|
|
TASK = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (free.size() == 0) {
|
|
|
|
sendMessage("TASK FAILED. NO FREE PLOTS FOUND!");
|
|
|
|
TASK = false;
|
|
|
|
return;
|
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
sendMessage("MOVING " + to_move.get(0) + " to " + free.get(0));
|
2015-03-24 04:11:28 +01:00
|
|
|
MainUtil.move(MainUtil.getPlot(worldname, to_move.get(0)), MainUtil.getPlot(worldname, free.get(0)), this);
|
2015-02-16 06:36:47 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
TASK = true;
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, "TASK STARTED...");
|
2015-02-16 04:46:57 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case "stop": {
|
2015-02-16 06:36:47 +01:00
|
|
|
if (!TASK) {
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, "TASK ALREADY STOPPED");
|
2015-02-16 06:36:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
TASK = false;
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, "TASK STOPPED");
|
2015-02-16 04:46:57 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case "info": {
|
|
|
|
if (args.length == 2) {
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, "/plot condense " + worldname + " info <radius>");
|
2015-02-16 04:46:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!StringUtils.isNumeric(args[2])) {
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, "INVALID RADIUS");
|
2015-02-16 04:46:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
final int radius = Integer.parseInt(args[2]);
|
2015-07-03 14:15:20 +02:00
|
|
|
final Collection<Plot> plots = PS.get().getPlots(worldname).values();
|
2015-02-20 07:34:19 +01:00
|
|
|
final int size = plots.size();
|
|
|
|
final int minimum_radius = (int) Math.ceil((Math.sqrt(size) / 2) + 1);
|
2015-02-16 06:36:47 +01:00
|
|
|
if (radius < minimum_radius) {
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, "RADIUS TOO SMALL");
|
2015-02-16 06:36:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
final int max_move = getPlots(plots, minimum_radius).size();
|
|
|
|
final int user_move = getPlots(plots, radius).size();
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, "=== DEFAULT EVAL ===");
|
|
|
|
MainUtil.sendMessage(plr, "MINIMUM RADIUS: " + minimum_radius);
|
|
|
|
MainUtil.sendMessage(plr, "MAXIMUM MOVES: " + max_move);
|
|
|
|
MainUtil.sendMessage(plr, "=== INPUT EVAL ===");
|
|
|
|
MainUtil.sendMessage(plr, "INPUT RADIUS: " + radius);
|
|
|
|
MainUtil.sendMessage(plr, "ESTIMATED MOVES: " + user_move);
|
|
|
|
MainUtil.sendMessage(plr, "ESTIMATED TIME: " + "No idea, times will drastically change based on the system performance and load");
|
|
|
|
MainUtil.sendMessage(plr, "&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
|
|
|
}
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, "/plot condense " + worldname + " <start|stop|info> [radius]");
|
2015-02-16 04:46:57 +01:00
|
|
|
return false;
|
2015-02-15 11:52:51 +01:00
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2015-02-20 07:34:19 +01:00
|
|
|
public Set<PlotId> getPlots(final Collection<Plot> plots, final int radius) {
|
|
|
|
final HashSet<PlotId> outside = new HashSet<>();
|
|
|
|
for (final Plot plot : plots) {
|
|
|
|
if ((plot.id.x > radius) || (plot.id.x < -radius) || (plot.id.y > radius) || (plot.id.y < -radius)) {
|
2015-02-16 06:36:47 +01:00
|
|
|
outside.add(plot.id);
|
2015-02-16 04:46:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return outside;
|
2015-02-15 11:52:51 +01:00
|
|
|
}
|
|
|
|
}
|