2015-01-07 14:29:20 +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;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
import java.nio.file.attribute.BasicFileAttributes;
|
|
|
|
import java.util.ArrayList;
|
2015-02-12 08:41:26 +01:00
|
|
|
import java.util.HashSet;
|
2015-01-07 14:29:20 +01:00
|
|
|
|
2015-01-13 17:38:15 +01:00
|
|
|
import org.bukkit.Bukkit;
|
2015-02-12 08:41:26 +01:00
|
|
|
import org.bukkit.Location;
|
2015-01-13 17:38:15 +01:00
|
|
|
import org.bukkit.World;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
2015-02-19 09:51:10 +01:00
|
|
|
import com.intellectualcrafters.plot.PlotSquared;
|
2015-01-07 14:29:20 +01:00
|
|
|
import com.intellectualcrafters.plot.config.C;
|
|
|
|
import com.intellectualcrafters.plot.object.ChunkLoc;
|
|
|
|
import com.intellectualcrafters.plot.object.Plot;
|
|
|
|
import com.intellectualcrafters.plot.object.PlotId;
|
2015-02-20 12:23:48 +01:00
|
|
|
import com.intellectualcrafters.plot.util.MainUtil;
|
2015-02-20 07:34:19 +01:00
|
|
|
import com.intellectualcrafters.plot.util.TaskManager;
|
2015-02-20 07:28:21 +01:00
|
|
|
import com.intellectualcrafters.plot.util.bukkit.ChunkManager;
|
2015-02-20 12:24:58 +01:00
|
|
|
import com.intellectualcrafters.plot.util.bukkit.BukkitPlayerFunctions;
|
2015-01-07 14:29:20 +01:00
|
|
|
|
2015-02-04 05:40:39 +01:00
|
|
|
public class Trim extends SubCommand {
|
2015-01-07 16:18:07 +01:00
|
|
|
public static boolean TASK = false;
|
|
|
|
private static int TASK_ID = 0;
|
|
|
|
|
2015-01-07 14:29:20 +01:00
|
|
|
public Trim() {
|
|
|
|
super("trim", "plots.admin", "Delete unmodified portions of your plotworld", "trim", "", CommandCategory.DEBUG, false);
|
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
|
|
|
|
public PlotId getId(final String id) {
|
2015-01-07 14:29:20 +01:00
|
|
|
try {
|
2015-02-20 07:34:19 +01:00
|
|
|
final String[] split = id.split(";");
|
2015-01-07 14:29:20 +01:00
|
|
|
return new PlotId(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
|
2015-02-20 07:34:19 +01:00
|
|
|
} catch (final Exception e) {
|
2015-01-07 14:29:20 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean execute(final Player plr, final String... args) {
|
|
|
|
if (plr != null) {
|
2015-02-20 12:24:58 +01:00
|
|
|
BukkitPlayerFunctions.sendMessage(plr, (C.NOT_CONSOLE));
|
2015-01-07 14:29:20 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (args.length == 1) {
|
2015-02-20 07:34:19 +01:00
|
|
|
final String arg = args[0].toLowerCase();
|
|
|
|
final PlotId id = getId(arg);
|
2015-01-07 14:29:20 +01:00
|
|
|
if (id != null) {
|
2015-02-20 12:24:58 +01:00
|
|
|
BukkitPlayerFunctions.sendMessage(plr, "/plot trim x;z &l<world>");
|
2015-01-07 14:29:20 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (arg.equals("all")) {
|
2015-02-20 12:24:58 +01:00
|
|
|
BukkitPlayerFunctions.sendMessage(plr, "/plot trim all &l<world>");
|
2015-01-07 14:29:20 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-20 12:24:58 +01:00
|
|
|
BukkitPlayerFunctions.sendMessage(plr, C.TRIM_SYNTAX);
|
2015-01-07 14:29:20 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (args.length != 2) {
|
2015-02-20 12:24:58 +01:00
|
|
|
BukkitPlayerFunctions.sendMessage(plr, C.TRIM_SYNTAX);
|
2015-01-07 14:29:20 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
final String arg = args[0].toLowerCase();
|
2015-01-07 15:10:22 +01:00
|
|
|
if (!arg.equals("all")) {
|
2015-02-20 12:24:58 +01:00
|
|
|
BukkitPlayerFunctions.sendMessage(plr, C.TRIM_SYNTAX);
|
2015-01-07 15:10:22 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-01-07 16:18:07 +01:00
|
|
|
final World world = Bukkit.getWorld(args[1]);
|
2015-02-20 09:55:04 +01:00
|
|
|
if ((world == null) || (PlotSquared.getPlotWorld(world) == null)) {
|
2015-02-20 12:24:58 +01:00
|
|
|
BukkitPlayerFunctions.sendMessage(plr, C.NOT_VALID_WORLD);
|
2015-01-07 15:10:22 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-12 07:59:02 +01:00
|
|
|
if (Trim.TASK) {
|
|
|
|
sendMessage(C.TRIM_IN_PROGRESS.s());
|
|
|
|
return false;
|
2015-01-07 14:29:20 +01:00
|
|
|
}
|
2015-02-12 07:59:02 +01:00
|
|
|
sendMessage(C.TRIM_START.s());
|
|
|
|
final ArrayList<ChunkLoc> empty = new ArrayList<>();
|
|
|
|
getTrimRegions(empty, world, new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
deleteChunks(world, empty);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return true;
|
2015-01-07 14:29:20 +01:00
|
|
|
}
|
|
|
|
|
2015-02-12 07:59:02 +01:00
|
|
|
public static boolean getBulkRegions(final ArrayList<ChunkLoc> empty, final World world, final Runnable whenDone) {
|
2015-01-07 16:18:07 +01:00
|
|
|
if (Trim.TASK) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
TaskManager.runTaskAsync(new Runnable() {
|
2015-01-07 16:18:07 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2015-02-20 07:34:19 +01:00
|
|
|
final String directory = world.getName() + File.separator + "region";
|
|
|
|
final File folder = new File(directory);
|
|
|
|
final File[] regionFiles = folder.listFiles();
|
|
|
|
for (final File file : regionFiles) {
|
|
|
|
final String name = file.getName();
|
2015-01-07 16:18:07 +01:00
|
|
|
if (name.endsWith("mca")) {
|
|
|
|
if (file.getTotalSpace() <= 8192) {
|
2015-02-12 07:59:02 +01:00
|
|
|
try {
|
2015-02-20 07:34:19 +01:00
|
|
|
final String[] split = name.split("\\.");
|
|
|
|
final int x = Integer.parseInt(split[1]);
|
|
|
|
final int z = Integer.parseInt(split[2]);
|
|
|
|
final ChunkLoc loc = new ChunkLoc(x, z);
|
2015-02-12 07:59:02 +01:00
|
|
|
empty.add(loc);
|
2015-02-20 07:34:19 +01:00
|
|
|
} catch (final Exception e) {
|
2015-02-12 07:59:02 +01:00
|
|
|
System.out.print("INVALID MCA: " + name);
|
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
} else {
|
|
|
|
final Path path = Paths.get(file.getPath());
|
2015-01-07 16:18:07 +01:00
|
|
|
try {
|
2015-02-20 07:34:19 +01:00
|
|
|
final BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
|
|
|
|
final long creation = attr.creationTime().toMillis();
|
|
|
|
final long modification = file.lastModified();
|
|
|
|
final long diff = Math.abs(creation - modification);
|
2015-01-07 16:18:07 +01:00
|
|
|
if (diff < 10000) {
|
2015-02-12 07:59:02 +01:00
|
|
|
try {
|
2015-02-20 07:34:19 +01:00
|
|
|
final String[] split = name.split("\\.");
|
|
|
|
final int x = Integer.parseInt(split[1]);
|
|
|
|
final int z = Integer.parseInt(split[2]);
|
|
|
|
final ChunkLoc loc = new ChunkLoc(x, z);
|
2015-02-12 07:59:02 +01:00
|
|
|
empty.add(loc);
|
2015-02-20 07:34:19 +01:00
|
|
|
} catch (final Exception e) {
|
2015-02-12 07:59:02 +01:00
|
|
|
System.out.print("INVALID MCA: " + name);
|
|
|
|
}
|
2015-01-07 16:18:07 +01:00
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
} catch (final Exception e) {
|
2015-01-07 16:18:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-12 07:59:02 +01:00
|
|
|
Trim.TASK = false;
|
2015-02-20 07:34:19 +01:00
|
|
|
TaskManager.runTaskAsync(whenDone);
|
2015-02-12 07:59:02 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
Trim.TASK = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean getTrimRegions(final ArrayList<ChunkLoc> empty, final World world, final Runnable whenDone) {
|
|
|
|
if (Trim.TASK) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-12 08:41:26 +01:00
|
|
|
final long startOld = System.currentTimeMillis();
|
2015-02-12 07:59:02 +01:00
|
|
|
sendMessage("Collecting region data...");
|
2015-02-12 08:41:26 +01:00
|
|
|
final ArrayList<Plot> plots = new ArrayList<>();
|
2015-02-19 09:51:10 +01:00
|
|
|
plots.addAll(PlotSquared.getPlots(world).values());
|
2015-02-12 08:41:26 +01:00
|
|
|
final HashSet<ChunkLoc> chunks = new HashSet<>(ChunkManager.getChunkChunks(world));
|
2015-02-12 07:59:02 +01:00
|
|
|
sendMessage(" - MCA #: " + chunks.size());
|
2015-02-20 07:34:19 +01:00
|
|
|
sendMessage(" - CHUNKS: " + (chunks.size() * 1024) + " (max)");
|
|
|
|
sendMessage(" - TIME ESTIMATE: " + (chunks.size() / 1200) + " minutes");
|
2015-02-19 09:51:10 +01:00
|
|
|
Trim.TASK_ID = Bukkit.getScheduler().scheduleSyncRepeatingTask(PlotSquared.getMain(), new Runnable() {
|
2015-02-12 07:59:02 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2015-02-20 07:34:19 +01:00
|
|
|
final long start = System.currentTimeMillis();
|
|
|
|
while ((System.currentTimeMillis() - start) < 50) {
|
2015-02-12 08:41:26 +01:00
|
|
|
if (plots.size() == 0) {
|
|
|
|
empty.addAll(chunks);
|
|
|
|
System.out.print("DONE!");
|
|
|
|
Trim.TASK = false;
|
2015-02-20 07:34:19 +01:00
|
|
|
TaskManager.runTaskAsync(whenDone);
|
2015-02-12 08:41:26 +01:00
|
|
|
Bukkit.getScheduler().cancelTask(Trim.TASK_ID);
|
|
|
|
return;
|
2015-01-07 16:18:07 +01:00
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
final Plot plot = plots.get(0);
|
2015-02-12 08:41:26 +01:00
|
|
|
plots.remove(0);
|
2015-02-20 12:23:48 +01:00
|
|
|
final Location pos1 = MainUtil.getPlotBottomLoc(world, plot.id);
|
|
|
|
final Location pos2 = MainUtil.getPlotTopLoc(world, plot.id);
|
2015-02-20 07:34:19 +01:00
|
|
|
final Location pos3 = new Location(world, pos1.getBlockX(), 64, pos2.getBlockZ());
|
|
|
|
final Location pos4 = new Location(world, pos2.getBlockX(), 64, pos1.getBlockZ());
|
2015-02-12 08:41:26 +01:00
|
|
|
chunks.remove(ChunkManager.getChunkChunk(pos1));
|
|
|
|
chunks.remove(ChunkManager.getChunkChunk(pos2));
|
|
|
|
chunks.remove(ChunkManager.getChunkChunk(pos3));
|
|
|
|
chunks.remove(ChunkManager.getChunkChunk(pos4));
|
2015-02-12 07:59:02 +01:00
|
|
|
}
|
2015-01-07 16:18:07 +01:00
|
|
|
}
|
2015-02-12 08:41:26 +01:00
|
|
|
}, 20L, 20L);
|
2015-02-12 07:59:02 +01:00
|
|
|
Trim.TASK = true;
|
2015-01-07 16:18:07 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-11 09:41:10 +01:00
|
|
|
public static ArrayList<Plot> expired = null;
|
|
|
|
|
2015-02-20 07:34:19 +01:00
|
|
|
// public static void updateUnmodifiedPlots(final World world) {
|
|
|
|
// final SquarePlotManager manager = (SquarePlotManager) PlotSquared.getPlotManager(world);
|
2015-02-20 09:55:04 +01:00
|
|
|
// final SquarePlotWorld plotworld = (SquarePlotWorld) PlotSquared.getPlotWorld(world);
|
2015-02-20 07:34:19 +01:00
|
|
|
// final ArrayList<Plot> expired = new ArrayList<>();
|
|
|
|
// final Set<Plot> plots = ExpireManager.getOldPlots(world.getName()).keySet();
|
|
|
|
// sendMessage("Checking " + plots.size() +" plots! This may take a long time...");
|
|
|
|
// Trim.TASK_ID = Bukkit.getScheduler().scheduleSyncRepeatingTask(PlotSquared.getMain(), new Runnable() {
|
|
|
|
// @Override
|
|
|
|
// public void run() {
|
|
|
|
// if (manager != null && plots.size() > 0) {
|
|
|
|
// Plot plot = plots.iterator().next();
|
|
|
|
// if (plot.hasOwner()) {
|
|
|
|
// SquarePlotManager.checkModified(plot, 0);
|
|
|
|
// }
|
|
|
|
// if (plot.owner == null || !SquarePlotManager.checkModified(plot, plotworld.REQUIRED_CHANGES)) {
|
|
|
|
// expired.add(plot);
|
|
|
|
// sendMessage("found expired: " + plot);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// else {
|
|
|
|
// Trim.expired = expired;
|
|
|
|
// Trim.TASK = false;
|
|
|
|
// sendMessage("Done!");
|
|
|
|
// Bukkit.getScheduler().cancelTask(Trim.TASK_ID);
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }, 1, 1);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
public static void deleteChunks(final World world, final ArrayList<ChunkLoc> chunks) {
|
|
|
|
final String worldname = world.getName();
|
|
|
|
for (final ChunkLoc loc : chunks) {
|
2015-02-11 09:41:10 +01:00
|
|
|
ChunkManager.deleteRegionFile(worldname, loc);
|
|
|
|
}
|
2015-01-07 16:18:07 +01:00
|
|
|
}
|
|
|
|
|
2015-02-11 09:41:10 +01:00
|
|
|
public static void sendMessage(final String message) {
|
2015-02-19 11:23:36 +01:00
|
|
|
PlotSquared.log("&3PlotSquared -> World trim&8: &7" + message);
|
2015-01-07 14:29:20 +01:00
|
|
|
}
|
|
|
|
}
|