PlotSquared/src/main/java/com/plotsquared/bukkit/commands/DebugUUID.java

341 lines
16 KiB
Java
Raw Normal View History

2015-04-26 10:51:13 +02: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 /
////////////////////////////////////////////////////////////////////////////////////////////////////
2016-02-10 20:43:47 +01:00
package com.plotsquared.bukkit.commands;
2015-04-26 10:51:13 +02:00
2015-07-30 16:25:16 +02:00
import java.io.File;
import java.io.FilenameFilter;
2015-08-26 06:21:48 +02:00
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
2015-07-30 16:25:16 +02:00
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
2015-08-26 06:21:48 +02:00
import java.util.List;
2015-07-30 16:25:16 +02:00
import java.util.Map.Entry;
import java.util.UUID;
import com.intellectualcrafters.plot.PS;
2016-02-10 20:43:47 +01:00
import com.intellectualcrafters.plot.commands.CommandCategory;
import com.intellectualcrafters.plot.commands.RequiredType;
import com.intellectualcrafters.plot.commands.SubCommand;
2015-04-26 13:38:29 +02:00
import com.intellectualcrafters.plot.config.C;
2015-04-27 11:07:42 +02:00
import com.intellectualcrafters.plot.database.AbstractDB;
import com.intellectualcrafters.plot.database.DBFunc;
import com.intellectualcrafters.plot.object.OfflinePlotPlayer;
import com.intellectualcrafters.plot.object.Plot;
2015-04-26 10:51:13 +02:00
import com.intellectualcrafters.plot.object.PlotPlayer;
2015-04-27 11:07:42 +02:00
import com.intellectualcrafters.plot.object.StringWrapper;
2015-04-26 10:51:13 +02:00
import com.intellectualcrafters.plot.util.MainUtil;
2015-08-26 06:21:48 +02:00
import com.intellectualcrafters.plot.util.StringMan;
2015-04-27 11:07:42 +02:00
import com.intellectualcrafters.plot.util.TaskManager;
2015-07-27 09:26:50 +02:00
import com.intellectualcrafters.plot.util.UUIDHandler;
2016-02-10 19:59:51 +01:00
import com.intellectualcrafters.plot.util.WorldUtil;
2015-07-30 16:25:16 +02:00
import com.intellectualcrafters.plot.uuid.UUIDWrapper;
import com.plotsquared.bukkit.uuid.DefaultUUIDWrapper;
import com.plotsquared.bukkit.uuid.LowerOfflineUUIDWrapper;
import com.plotsquared.bukkit.uuid.OfflineUUIDWrapper;
2015-07-27 19:50:04 +02:00
import com.plotsquared.general.commands.Argument;
import com.plotsquared.general.commands.CommandDeclaration;
2015-04-26 10:51:13 +02:00
@CommandDeclaration(
2015-09-11 12:09:22 +02:00
command = "uuidconvert",
permission = "plots.admin",
description = "Debug UUID conversion",
usage = "/plot uuidconvert <lower|offline|online>",
requiredType = RequiredType.CONSOLE,
category = CommandCategory.DEBUG)
2015-09-13 06:04:31 +02:00
public class DebugUUID extends SubCommand {
public DebugUUID() {
requiredArguments = new Argument[] { Argument.String };
2015-04-26 10:51:13 +02:00
}
2015-09-13 06:04:31 +02:00
2015-04-26 10:51:13 +02:00
@Override
2015-10-19 08:27:51 +02:00
public boolean onCommand(final PlotPlayer player, final String[] args) {
2015-08-26 06:21:48 +02:00
final UUIDWrapper currentUUIDWrapper = UUIDHandler.getUUIDWrapper();
final UUIDWrapper newWrapper;
2015-09-13 06:04:31 +02:00
switch (args[0].toLowerCase()) {
case "lower": {
2015-04-26 15:38:02 +02:00
newWrapper = new LowerOfflineUUIDWrapper();
break;
}
2015-09-13 06:04:31 +02:00
case "offline": {
2015-04-26 15:38:02 +02:00
newWrapper = new OfflineUUIDWrapper();
break;
}
2015-09-13 06:04:31 +02:00
case "online": {
2015-04-26 15:38:02 +02:00
newWrapper = new DefaultUUIDWrapper();
break;
}
2015-09-13 06:04:31 +02:00
default: {
try {
2015-09-11 12:09:22 +02:00
final Class<?> clazz = Class.forName(args[0]);
2015-04-26 15:38:02 +02:00
newWrapper = (UUIDWrapper) clazz.newInstance();
2015-09-13 06:04:31 +02:00
} catch (final Exception e) {
2015-04-26 15:38:02 +02:00
MainUtil.sendMessage(player, C.COMMAND_SYNTAX, "/plot uuidconvert <lower|offline|online>");
return false;
}
}
}
2015-09-13 06:04:31 +02:00
if ((args.length != 2) || !args[1].equals("-o")) {
2015-04-27 11:07:42 +02:00
MainUtil.sendMessage(player, C.COMMAND_SYNTAX, "/plot uuidconvert " + args[0] + " - o");
MainUtil.sendMessage(player, "&cBe aware of the following!");
2015-08-14 00:52:31 +02:00
MainUtil.sendMessage(player, "&8 - &cUse the database command or another method to backup your plots beforehand");
2015-04-27 11:07:42 +02:00
MainUtil.sendMessage(player, "&8 - &cIf the process is interrupted, all plots could be deleted");
MainUtil.sendMessage(player, "&8 - &cIf an error occurs, all plots could be deleted");
MainUtil.sendMessage(player, "&8 - &cPlot settings WILL be lost upon conversion");
2015-08-14 00:52:31 +02:00
MainUtil.sendMessage(player, "&cTO REITERATE: BACK UP YOUR DATABASE BEFORE USING THIS!!!");
MainUtil.sendMessage(player, "&7Retype the command with the override parameter when ready :)");
2015-04-27 11:07:42 +02:00
return false;
}
2015-09-13 06:04:31 +02:00
if (currentUUIDWrapper.getClass().getCanonicalName().equals(newWrapper.getClass().getCanonicalName())) {
2015-04-26 15:38:02 +02:00
MainUtil.sendMessage(player, "&cUUID mode already in use!");
return false;
}
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "&6Beginning UUID mode conversion");
MainUtil.sendMessage(player, "&7 - Disconnecting players");
for (Entry<String, PlotPlayer> entry : UUIDHandler.getPlayers().entrySet()) {
entry.getValue().kick("PlotSquared UUID conversion has been initiated. You may reconnect when finished.");
2015-04-27 11:07:42 +02:00
}
2015-09-13 06:04:31 +02:00
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "&7 - Initializing map");
2015-09-13 06:04:31 +02:00
2015-08-26 06:21:48 +02:00
final HashMap<UUID, UUID> uCMap = new HashMap<UUID, UUID>();
final HashMap<UUID, UUID> uCReverse = new HashMap<UUID, UUID>();
2015-09-13 06:04:31 +02:00
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "&7 - Collecting playerdata");
2015-09-13 06:04:31 +02:00
2015-04-27 11:07:42 +02:00
final HashSet<String> worlds = new HashSet<>();
2016-02-10 19:59:51 +01:00
worlds.add(WorldUtil.IMP.getMainWorld());
2015-04-27 11:07:42 +02:00
worlds.add("world");
final HashSet<UUID> uuids = new HashSet<>();
final HashSet<String> names = new HashSet<>();
2015-09-13 06:04:31 +02:00
for (final String worldname : worlds) {
2015-04-27 11:07:42 +02:00
final File playerdataFolder = new File(worldname + File.separator + "playerdata");
2015-09-13 06:04:31 +02:00
String[] dat = playerdataFolder.list(new FilenameFilter() {
2015-04-27 11:07:42 +02:00
@Override
2015-09-13 06:04:31 +02:00
public boolean accept(final File f, final String s) {
2015-04-27 11:07:42 +02:00
return s.endsWith(".dat");
}
});
2015-09-13 06:04:31 +02:00
if (dat != null) {
for (final String current : dat) {
2015-04-27 11:07:42 +02:00
final String s = current.replaceAll(".dat$", "");
2015-09-13 06:04:31 +02:00
try {
2015-04-27 11:07:42 +02:00
final UUID uuid = UUID.fromString(s);
uuids.add(uuid);
2015-09-13 06:04:31 +02:00
} catch (final Exception e) {
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, C.PREFIX.s() + "Invalid playerdata: " + current);
2015-04-27 11:07:42 +02:00
}
}
}
final File playersFolder = new File(worldname + File.separator + "players");
2015-09-13 06:04:31 +02:00
dat = playersFolder.list(new FilenameFilter() {
2015-04-27 11:07:42 +02:00
@Override
2015-09-13 06:04:31 +02:00
public boolean accept(final File f, final String s) {
2015-04-27 11:07:42 +02:00
return s.endsWith(".dat");
}
});
2015-09-13 06:04:31 +02:00
if (dat != null) {
for (final String current : dat) {
2015-04-27 11:07:42 +02:00
names.add(current.replaceAll(".dat$", ""));
}
}
}
2015-09-13 06:04:31 +02:00
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "&7 - Populating map");
2015-04-27 11:07:42 +02:00
UUID uuid2;
final UUIDWrapper wrapper = new DefaultUUIDWrapper();
2015-09-13 06:04:31 +02:00
for (UUID uuid : uuids) {
try {
2015-04-27 11:07:42 +02:00
final OfflinePlotPlayer op = wrapper.getOfflinePlayer(uuid);
uuid = currentUUIDWrapper.getUUID(op);
uuid2 = newWrapper.getUUID(op);
2015-09-13 06:04:31 +02:00
if (!uuid.equals(uuid2) && !uCMap.containsKey(uuid) && !uCReverse.containsKey(uuid2)) {
2015-04-27 11:07:42 +02:00
uCMap.put(uuid, uuid2);
uCReverse.put(uuid2, uuid);
}
2015-09-13 06:04:31 +02:00
} catch (final Throwable e) {
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, C.PREFIX.s() + "&6Invalid playerdata: " + uuid.toString() + ".dat");
2015-04-27 11:07:42 +02:00
}
}
2015-09-13 06:04:31 +02:00
for (final String name : names) {
2015-04-27 11:07:42 +02:00
final UUID uuid = currentUUIDWrapper.getUUID(name);
uuid2 = newWrapper.getUUID(name);
2015-09-13 06:04:31 +02:00
if (!uuid.equals(uuid2)) {
2015-04-27 11:07:42 +02:00
uCMap.put(uuid, uuid2);
uCReverse.put(uuid2, uuid);
}
}
2015-09-13 06:04:31 +02:00
if (uCMap.size() == 0) {
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "&c - Error! Attempting to repopulate");
2015-09-13 06:04:31 +02:00
for (final OfflinePlotPlayer op : currentUUIDWrapper.getOfflinePlayers()) {
if (op.getLastPlayed() != 0) {
2015-09-11 12:09:22 +02:00
// String name = op.getName();
// StringWrapper wrap = new StringWrapper(name);
final UUID uuid = currentUUIDWrapper.getUUID(op);
2015-04-27 11:07:42 +02:00
uuid2 = newWrapper.getUUID(op);
2015-09-13 06:04:31 +02:00
if (!uuid.equals(uuid2)) {
2015-04-27 11:07:42 +02:00
uCMap.put(uuid, uuid2);
uCReverse.put(uuid2, uuid);
}
}
}
2015-09-13 06:04:31 +02:00
if (uCMap.size() == 0) {
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "&cError. Failed to collect UUIDs!");
2015-04-27 11:07:42 +02:00
return false;
2015-09-13 06:04:31 +02:00
} else {
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "&a - Successfully repopulated");
2015-04-27 11:07:42 +02:00
}
}
2015-09-13 06:04:31 +02:00
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "&7 - Replacing cache");
2015-09-13 06:04:31 +02:00
TaskManager.runTaskAsync(new Runnable() {
2015-08-26 06:21:48 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void run() {
for (final Entry<UUID, UUID> entry : uCMap.entrySet()) {
2015-09-11 12:09:22 +02:00
final String name = UUIDHandler.getName(entry.getKey());
2015-09-13 06:04:31 +02:00
if (name != null) {
2015-08-26 06:21:48 +02:00
UUIDHandler.add(new StringWrapper(name), entry.getValue());
}
}
2015-09-13 06:04:31 +02:00
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "&7 - Scanning for applicable files (uuids.txt)");
2015-09-13 06:04:31 +02:00
2015-09-11 12:09:22 +02:00
final File file = new File(PS.get().IMP.getDirectory(), "uuids.txt");
2015-09-13 06:04:31 +02:00
if (file.exists()) {
try {
2015-09-11 12:09:22 +02:00
final List<String> lines = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8);
2015-09-13 06:04:31 +02:00
for (String line : lines) {
try {
2015-08-26 06:21:48 +02:00
line = line.trim();
2015-09-13 06:04:31 +02:00
if (line.length() == 0) {
2015-08-26 06:21:48 +02:00
continue;
}
line = line.replaceAll("[\\|][0-9]+[\\|][0-9]+[\\|]", "");
2015-09-11 12:09:22 +02:00
final String[] split = line.split("\\|");
final String name = split[0];
2015-09-13 06:04:31 +02:00
if ((name.length() == 0) || (name.length() > 16) || !StringMan.isAlphanumericUnd(name)) {
2015-08-26 06:21:48 +02:00
continue;
}
2015-09-11 12:09:22 +02:00
final UUID old = currentUUIDWrapper.getUUID(name);
2015-09-13 06:04:31 +02:00
if (old == null) {
2015-08-26 06:21:48 +02:00
continue;
}
2015-09-11 12:09:22 +02:00
final UUID now = newWrapper.getUUID(name);
2015-08-26 06:21:48 +02:00
UUIDHandler.add(new StringWrapper(name), now);
uCMap.put(old, now);
uCReverse.put(now, old);
2015-09-13 06:04:31 +02:00
} catch (final Exception e2) {
2015-08-26 06:21:48 +02:00
e2.printStackTrace();
}
}
2015-09-13 06:04:31 +02:00
} catch (final IOException e) {
2015-08-26 06:21:48 +02:00
e.printStackTrace();
}
}
2015-09-13 06:04:31 +02:00
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "&7 - Replacing wrapper");
2015-08-26 06:21:48 +02:00
UUIDHandler.setUUIDWrapper(newWrapper);
2015-09-13 06:04:31 +02:00
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "&7 - Updating plot objects");
2015-09-13 06:04:31 +02:00
2016-02-10 19:59:51 +01:00
for (final Plot plot : PS.get().getPlots()) {
2015-09-11 12:09:22 +02:00
final UUID value = uCMap.get(plot.owner);
2015-09-13 06:04:31 +02:00
if (value != null) {
2015-04-27 11:07:42 +02:00
plot.owner = value;
}
2015-08-26 06:21:48 +02:00
plot.getTrusted().clear();
plot.getMembers().clear();
plot.getDenied().clear();
2015-04-27 11:07:42 +02:00
}
2015-09-13 06:04:31 +02:00
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "&7 - Deleting database");
2015-08-26 06:21:48 +02:00
final AbstractDB database = DBFunc.dbManager;
2015-09-11 12:09:22 +02:00
final boolean result = database.deleteTables();
2015-09-13 06:04:31 +02:00
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "&7 - Creating tables");
2015-09-13 06:04:31 +02:00
try {
2015-08-26 06:21:48 +02:00
database.createTables();
2015-09-13 06:04:31 +02:00
if (!result) {
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "&cConversion failed! Attempting recovery");
2015-09-13 06:04:31 +02:00
for (final Plot plot : PS.get().getPlots()) {
2015-09-11 12:09:22 +02:00
final UUID value = uCReverse.get(plot.owner);
2015-09-13 06:04:31 +02:00
if (value != null) {
2015-08-26 06:21:48 +02:00
plot.owner = value;
}
}
2015-09-13 06:04:31 +02:00
database.createPlotsAndData(new ArrayList<>(PS.get().getPlots()), new Runnable() {
2015-08-26 06:21:48 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void run() {
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "&6Recovery was successful!");
2015-08-26 06:21:48 +02:00
}
});
return;
2015-04-29 14:04:25 +02:00
}
2015-09-13 06:04:31 +02:00
} catch (final Exception e) {
2015-08-26 06:21:48 +02:00
e.printStackTrace();
return;
}
2015-09-13 06:04:31 +02:00
if (newWrapper instanceof OfflineUUIDWrapper) {
2015-08-26 06:21:48 +02:00
PS.get().config.set("UUID.force-lowercase", false);
PS.get().config.set("UUID.offline", true);
2015-09-13 06:04:31 +02:00
} else if (newWrapper instanceof LowerOfflineUUIDWrapper) {
2015-08-26 06:21:48 +02:00
PS.get().config.set("UUID.force-lowercase", true);
PS.get().config.set("UUID.offline", true);
2015-09-13 06:04:31 +02:00
} else if (newWrapper instanceof DefaultUUIDWrapper) {
2015-08-26 06:21:48 +02:00
PS.get().config.set("UUID.force-lowercase", false);
PS.get().config.set("UUID.offline", false);
}
2015-09-13 06:04:31 +02:00
try {
2015-08-26 06:21:48 +02:00
PS.get().config.save(PS.get().configFile);
2015-09-13 06:04:31 +02:00
} catch (final Exception e) {
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "Could not save configuration. It will need to be manuall set!");
2015-08-26 06:21:48 +02:00
}
2015-09-13 06:04:31 +02:00
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "&7 - Populating tables");
2015-09-13 06:04:31 +02:00
TaskManager.runTaskAsync(new Runnable() {
2015-04-29 14:04:25 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void run() {
2015-09-11 12:09:22 +02:00
final ArrayList<Plot> plots = new ArrayList<>(PS.get().getPlots());
2015-09-13 06:04:31 +02:00
database.createPlotsAndData(plots, new Runnable() {
2015-08-26 06:21:48 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void run() {
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "&aConversion complete!");
2015-08-26 06:21:48 +02:00
}
});
2015-04-29 14:04:25 +02:00
}
});
2015-09-13 06:04:31 +02:00
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "&aIt is now safe for players to join");
MainUtil.sendMessage(player, "&cConversion is still in progress, you will be notified when it is complete");
2015-04-27 11:07:42 +02:00
}
});
2015-04-26 13:38:29 +02:00
return true;
2015-04-26 10:51:13 +02:00
}
}