TF-PlotSquared/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/DebugClaimTest.java

180 lines
9.1 KiB
Java
Raw Normal View History

2014-11-08 20:27:09 +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 /
////////////////////////////////////////////////////////////////////////////////////////////////////
2014-10-22 23:22:00 +11:00
package com.intellectualcrafters.plot.commands;
2014-11-08 20:27:09 +01:00
import com.google.common.collect.BiMap;
2014-11-16 10:48:18 +01:00
import com.intellectualcrafters.plot.PlotMain;
import com.intellectualcrafters.plot.config.C;
2014-11-08 20:27:09 +01:00
import com.intellectualcrafters.plot.database.DBFunc;
import com.intellectualcrafters.plot.events.PlayerClaimPlotEvent;
2014-11-16 10:48:18 +01:00
import com.intellectualcrafters.plot.flag.FlagManager;
import com.intellectualcrafters.plot.object.*;
import com.intellectualcrafters.plot.util.PlayerFunctions;
import com.intellectualcrafters.plot.util.PlotHelper;
import com.intellectualcrafters.plot.util.UUIDHandler;
2014-10-22 23:22:00 +11:00
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
2014-11-08 20:27:09 +01:00
import java.util.ArrayList;
import java.util.UUID;
2014-10-22 23:22:00 +11:00
/**
* @author Citymonstret
*/
public class DebugClaimTest extends SubCommand {
2014-11-05 14:42:08 +11:00
public DebugClaimTest() {
2014-11-10 21:54:36 +11:00
super(Command.DEBUGCLAIMTEST, "If you accidentally delete your database, this command will attempt to restore all plots based on the data from the plot signs. Execution time may vary", "debugclaimtest", CommandCategory.DEBUG, false);
2014-11-05 14:42:08 +11:00
}
2014-11-16 10:48:18 +01:00
@SuppressWarnings("unused")
public static boolean claimPlot(final Player player, final Plot plot, final boolean teleport) {
return claimPlot(player, plot, teleport, "");
}
public static boolean claimPlot(final Player player, final Plot plot, final boolean teleport, @SuppressWarnings("unused") final String schematic) {
final PlayerClaimPlotEvent event = new PlayerClaimPlotEvent(player, plot);
Bukkit.getPluginManager().callEvent(event);
if (!event.isCancelled()) {
PlotHelper.createPlot(player, plot);
PlotHelper.setSign(player, plot);
PlayerFunctions.sendMessage(player, C.CLAIMED);
if (teleport) {
PlotMain.teleportPlayer(player, player.getLocation(), plot);
}
plot.settings.setFlags(FlagManager.parseFlags(PlotMain.getWorldSettings(player.getWorld()).DEFAULT_FLAGS));
}
return event.isCancelled();
}
2014-11-05 14:42:08 +11:00
@Override
public boolean execute(final Player plr, final String... args) {
if (plr == null) {
if (args.length < 3) {
2014-11-08 20:27:09 +01:00
return !PlayerFunctions.sendMessage(null, "If you accidentally delete your database, this command will attempt to restore all plots based on the data from the plot signs. \n\n&cMissing world arg /plot debugclaimtest {world} {PlotId min} {PlotId max}");
2014-11-05 14:42:08 +11:00
}
final World world = Bukkit.getWorld(args[0]);
if ((world == null) || !PlotMain.isPlotWorld(world)) {
2014-11-08 20:27:09 +01:00
return !PlayerFunctions.sendMessage(null, "&cInvalid plot world!");
2014-11-05 14:42:08 +11:00
}
PlotId min, max;
try {
final String[] split1 = args[1].split(";");
final String[] split2 = args[2].split(";");
min = new PlotId(Integer.parseInt(split1[0]), Integer.parseInt(split1[1]));
max = new PlotId(Integer.parseInt(split2[0]), Integer.parseInt(split2[1]));
2014-11-08 20:27:09 +01:00
} catch (final Exception e) {
return !PlayerFunctions.sendMessage(null, "&cInvalid min/max values. &7The values are to Plot IDs in the format &cX;Y &7where X,Y are the plot coords\nThe conversion will only check the plots in the selected area.");
2014-11-05 14:42:08 +11:00
}
2014-11-08 20:27:09 +01:00
PlayerFunctions.sendMessage(null, "&3Sign Block&8->&3PlotSquared&8: &7Beginning sign to plot conversion. This may take a while...");
PlayerFunctions.sendMessage(null, "&3Sign Block&8->&3PlotSquared&8: Found an excess of 250,000 chunks. Limiting search radius... (~3.8 min)");
2014-11-05 14:42:08 +11:00
final PlotManager manager = PlotMain.getPlotManager(world);
final PlotWorld plotworld = PlotMain.getWorldSettings(world);
2014-11-08 20:27:09 +01:00
final ArrayList<Plot> plots = new ArrayList<>();
2014-11-05 14:42:08 +11:00
for (final PlotId id : PlayerFunctions.getPlotSelectionIds(world, min, max)) {
final Plot plot = PlotHelper.getPlot(world, id);
final boolean contains = PlotMain.getPlots(world).containsKey(plot.id);
2014-10-22 23:22:00 +11:00
if (contains) {
2014-11-08 20:27:09 +01:00
PlayerFunctions.sendMessage(null, " - &cDB Already contains: " + plot.id);
2014-10-22 23:22:00 +11:00
continue;
}
2014-11-05 14:42:08 +11:00
final Location loc = manager.getSignLoc(world, plotworld, plot);
final Chunk chunk = world.getChunkAt(loc);
2014-10-22 23:22:00 +11:00
if (!chunk.isLoaded()) {
2014-11-05 14:42:08 +11:00
final boolean result = chunk.load(false);
2014-10-22 23:22:00 +11:00
if (!result) {
continue;
}
}
2014-11-05 14:42:08 +11:00
final Block block = world.getBlockAt(loc);
if (block != null) {
2014-10-22 23:22:00 +11:00
if (block.getState() instanceof Sign) {
2014-11-05 14:42:08 +11:00
final Sign sign = (Sign) block.getState();
2014-11-08 20:27:09 +01:00
String line = sign.getLine(2);
if ((line != null) && (line.length() > 2)) {
line = line.substring(2);
2014-11-05 14:42:08 +11:00
2014-11-08 20:27:09 +01:00
final BiMap<StringWrapper, UUID> map = UUIDHandler.getUuidMap();
2014-11-05 14:42:08 +11:00
2014-11-08 20:27:09 +01:00
UUID uuid = (map.get(new StringWrapper(line)));
2014-11-05 14:42:08 +11:00
2014-11-08 20:27:09 +01:00
if (uuid == null) {
for (final StringWrapper string : map.keySet()) {
if (string.value.toLowerCase().startsWith(line.toLowerCase())) {
uuid = map.get(string);
break;
2014-10-22 23:22:00 +11:00
}
}
2014-11-08 20:27:09 +01:00
}
if (uuid == null) {
uuid = UUIDHandler.getUUID(line);
}
if (uuid != null) {
PlayerFunctions.sendMessage(null, " - &aFound plot: " + plot.id + " : " + line);
plot.owner = uuid;
plot.hasChanged = true;
plots.add(plot);
} else {
PlayerFunctions.sendMessage(null, " - &cInvalid playername: " + plot.id + " : " + line);
2014-10-22 23:22:00 +11:00
}
}
}
}
2014-11-05 14:42:08 +11:00
}
if (plots.size() > 0) {
2014-11-08 20:27:09 +01:00
PlayerFunctions.sendMessage(null, "&3Sign Block&8->&3PlotSquared&8: &7Updating '" + plots.size() + "' plots!");
2014-11-05 14:42:08 +11:00
DBFunc.createPlots(plots);
DBFunc.createAllSettingsAndHelpers(plots);
for (final Plot plot : plots) {
PlotMain.updatePlot(plot);
}
2014-11-08 20:27:09 +01:00
PlayerFunctions.sendMessage(null, "&3Sign Block&8->&3PlotSquared&8: &7Complete!");
2014-11-05 14:42:08 +11:00
2014-11-08 20:27:09 +01:00
} else {
PlayerFunctions.sendMessage(null, "No plots were found for the given search.");
2014-11-05 14:42:08 +11:00
}
2014-11-08 20:27:09 +01:00
} else {
2014-11-05 14:42:08 +11:00
PlayerFunctions.sendMessage(plr, "This debug command can only be executed by console as it has been deemed unsafe if abused.");
}
2014-10-22 23:22:00 +11:00
return true;
2014-11-05 14:42:08 +11:00
}
2014-10-22 23:22:00 +11:00
}