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

303 lines
13 KiB
Java
Raw Normal View History

2016-02-10 20:43:47 +01:00
package com.plotsquared.bukkit.commands;
2015-04-26 10:51:13 +02:00
2016-06-02 19:42:32 +02:00
import com.google.common.collect.Sets;
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;
2016-06-02 19:42:32 +02:00
import com.plotsquared.bukkit.uuid.DatFileFilter;
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;
2016-03-23 02:41:37 +01:00
2016-02-11 02:37:21 +01:00
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.UUID;
@CommandDeclaration(
2016-03-23 02:41:37 +01: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 {
2016-03-20 23:19:37 +01:00
2015-09-13 06:04:31 +02:00
public DebugUUID() {
super(Argument.String);
2015-04-26 10:51:13 +02:00
}
2016-03-20 23:19:37 +01:00
2015-04-26 10:51:13 +02:00
@Override
2016-03-23 02:41:37 +01:00
public boolean onCommand(final PlotPlayer player, String[] args) {
2015-08-26 06:21:48 +02:00
final UUIDWrapper currentUUIDWrapper = UUIDHandler.getUUIDWrapper();
final UUIDWrapper newWrapper;
2016-03-20 23:19:37 +01:00
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;
case "offline":
2015-04-26 15:38:02 +02:00
newWrapper = new OfflineUUIDWrapper();
break;
case "online":
2015-04-26 15:38:02 +02:00
newWrapper = new DefaultUUIDWrapper();
break;
default:
2015-09-13 06:04:31 +02:00
try {
2016-03-23 02:41:37 +01:00
Class<?> clazz = Class.forName(args[0]);
2015-04-26 15:38:02 +02:00
newWrapper = (UUIDWrapper) clazz.newInstance();
2016-05-12 23:09:35 +02:00
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException ignored) {
2015-04-26 15:38:02 +02:00
MainUtil.sendMessage(player, C.COMMAND_SYNTAX, "/plot uuidconvert <lower|offline|online>");
return false;
}
}
if (args.length != 2 || !"-o".equals(args[1])) {
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;
}
2016-03-20 23:19:37 +01:00
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
}
2016-03-20 23:19:37 +01:00
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "&7 - Initializing map");
final HashMap<UUID, UUID> uCMap = new HashMap<>();
final HashMap<UUID, UUID> uCReverse = new HashMap<>();
2016-03-20 23:19:37 +01:00
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "&7 - Collecting playerdata");
2016-03-20 23:19:37 +01:00
2016-06-02 19:42:32 +02:00
HashSet<String> worlds = Sets.newHashSet(WorldUtil.IMP.getMainWorld(), "world");
2016-03-23 02:41:37 +01:00
HashSet<UUID> uuids = new HashSet<>();
HashSet<String> names = new HashSet<>();
2016-03-29 21:47:59 +02:00
for (String worldName : worlds) {
File playerDataFolder = new File(worldName + File.separator + "playerdata");
2016-06-02 19:42:32 +02:00
String[] dat = playerDataFolder.list(new DatFileFilter());
2016-03-23 02:41:37 +01:00
if (dat != null) {
for (String current : dat) {
String s = current.replaceAll(".dat$", "");
2015-09-13 06:04:31 +02:00
try {
2016-03-23 02:41:37 +01:00
UUID uuid = UUID.fromString(s);
2015-04-27 11:07:42 +02:00
uuids.add(uuid);
2016-04-26 16:14:22 +02:00
} catch (Exception ignored) {
2016-03-20 23:19:37 +01:00
MainUtil.sendMessage(player, C.PREFIX + "Invalid playerdata: " + current);
2015-04-27 11:07:42 +02:00
}
}
2016-03-23 02:41:37 +01:00
}
2016-03-29 21:47:59 +02:00
File playersFolder = new File(worldName + File.separator + "players");
2016-06-02 19:42:32 +02:00
dat = playersFolder.list(new DatFileFilter());
2015-09-13 06:04:31 +02:00
if (dat != null) {
2016-03-23 02:41:37 +01:00
for (String current : dat) {
2015-04-27 11:07:42 +02:00
names.add(current.replaceAll(".dat$", ""));
}
}
}
2016-03-20 23:19:37 +01: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;
2016-03-23 02:41:37 +01:00
UUIDWrapper wrapper = new DefaultUUIDWrapper();
2015-09-13 06:04:31 +02:00
for (UUID uuid : uuids) {
try {
2016-03-23 02:41:37 +01:00
OfflinePlotPlayer op = wrapper.getOfflinePlayer(uuid);
2015-04-27 11:07:42 +02:00
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);
}
2016-04-26 16:14:22 +02:00
} catch (Throwable ignored) {
2016-03-20 23:19:37 +01:00
MainUtil.sendMessage(player, C.PREFIX + "&6Invalid playerdata: " + uuid.toString() + ".dat");
2015-04-27 11:07:42 +02:00
}
}
2016-03-23 02:41:37 +01:00
for (String name : names) {
UUID uuid = currentUUIDWrapper.getUUID(name);
2015-04-27 11:07:42 +02:00
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);
}
}
if (uCMap.isEmpty()) {
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "&c - Error! Attempting to repopulate");
2016-03-23 02:41:37 +01:00
for (OfflinePlotPlayer op : currentUUIDWrapper.getOfflinePlayers()) {
2015-09-13 06:04:31 +02:00
if (op.getLastPlayed() != 0) {
2015-09-11 12:09:22 +02:00
// String name = op.getName();
// StringWrapper wrap = new StringWrapper(name);
2016-03-23 02:41:37 +01:00
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);
}
}
}
if (uCMap.isEmpty()) {
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
}
}
2016-03-20 23:19:37 +01: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() {
2016-03-23 02:41:37 +01:00
for (Entry<UUID, UUID> entry : uCMap.entrySet()) {
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());
}
}
2016-03-20 23:19:37 +01:00
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "&7 - Scanning for applicable files (uuids.txt)");
2016-03-20 23:19:37 +01:00
2016-03-23 02:41:37 +01:00
File file = new File(PS.get().IMP.getDirectory(), "uuids.txt");
2015-09-13 06:04:31 +02:00
if (file.exists()) {
try {
2016-03-23 02:41:37 +01:00
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();
if (line.isEmpty()) {
2015-08-26 06:21:48 +02:00
continue;
}
line = line.replaceAll("[\\|][0-9]+[\\|][0-9]+[\\|]", "");
2016-03-23 02:41:37 +01:00
String[] split = line.split("\\|");
String name = split[0];
if (name.isEmpty() || name.length() > 16 || !StringMan.isAlphanumericUnd(name)) {
2015-08-26 06:21:48 +02:00
continue;
}
2016-03-23 02:41:37 +01:00
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;
}
2016-03-23 02:41:37 +01:00
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);
2016-03-23 02:41:37 +01:00
} catch (Exception e2) {
2015-08-26 06:21:48 +02:00
e2.printStackTrace();
}
}
2016-03-23 02:41:37 +01:00
} catch (IOException e) {
2015-08-26 06:21:48 +02:00
e.printStackTrace();
}
}
2016-03-20 23:19:37 +01: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);
2016-03-20 23:19:37 +01:00
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "&7 - Updating plot objects");
2016-03-20 23:19:37 +01:00
2016-03-23 02:41:37 +01:00
for (Plot plot : PS.get().getPlots()) {
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
}
2016-03-20 23:19:37 +01: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;
2016-03-23 02:41:37 +01:00
boolean result = database.deleteTables();
2016-03-20 23:19:37 +01:00
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "&7 - Creating tables");
2016-03-20 23:19:37 +01:00
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");
2016-03-23 02:41:37 +01:00
for (Plot plot : PS.get().getPlots()) {
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
}
2016-03-23 02:41:37 +01:00
} catch (Exception e) {
2015-08-26 06:21:48 +02:00
e.printStackTrace();
return;
}
2016-03-20 23:19:37 +01:00
2015-09-13 06:04:31 +02:00
if (newWrapper instanceof OfflineUUIDWrapper) {
PS.get().worlds.set("UUID.force-lowercase", false);
PS.get().worlds.set("UUID.offline", true);
2015-09-13 06:04:31 +02:00
} else if (newWrapper instanceof DefaultUUIDWrapper) {
PS.get().worlds.set("UUID.force-lowercase", false);
PS.get().worlds.set("UUID.offline", false);
2015-08-26 06:21:48 +02:00
}
2015-09-13 06:04:31 +02:00
try {
PS.get().worlds.save(PS.get().worldsFile);
2016-04-30 00:14:12 +02:00
} catch (IOException ignored) {
2016-03-29 21:47:59 +02:00
MainUtil.sendMessage(player, "Could not save configuration. It will need to be manual set!");
2015-08-26 06:21:48 +02:00
}
2016-03-20 23:19:37 +01:00
2015-10-19 08:27:51 +02:00
MainUtil.sendMessage(player, "&7 - Populating tables");
2016-03-20 23:19:37 +01:00
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() {
2016-03-23 02:41:37 +01:00
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
}
});
2016-03-20 23:19:37 +01: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
}
}