UUID mode conversion (untested)

This commit is contained in:
boy0001 2015-04-27 19:07:42 +10:00
parent 27c2b08c2f
commit 42a34fc44a
10 changed files with 284 additions and 31 deletions

View File

@ -102,10 +102,12 @@ import com.intellectualcrafters.plot.util.ChunkManager;
import com.intellectualcrafters.plot.util.ConsoleColors; import com.intellectualcrafters.plot.util.ConsoleColors;
import com.intellectualcrafters.plot.util.EventUtil; import com.intellectualcrafters.plot.util.EventUtil;
import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.PlayerManager;
import com.intellectualcrafters.plot.util.SetupUtils; import com.intellectualcrafters.plot.util.SetupUtils;
import com.intellectualcrafters.plot.util.TaskManager; import com.intellectualcrafters.plot.util.TaskManager;
import com.intellectualcrafters.plot.util.bukkit.BukkitChunkManager; import com.intellectualcrafters.plot.util.bukkit.BukkitChunkManager;
import com.intellectualcrafters.plot.util.bukkit.BukkitEventUtil; import com.intellectualcrafters.plot.util.bukkit.BukkitEventUtil;
import com.intellectualcrafters.plot.util.bukkit.BukkitPlayerManager;
import com.intellectualcrafters.plot.util.bukkit.BukkitSetBlockManager; import com.intellectualcrafters.plot.util.bukkit.BukkitSetBlockManager;
import com.intellectualcrafters.plot.util.bukkit.BukkitSetupUtils; import com.intellectualcrafters.plot.util.bukkit.BukkitSetupUtils;
import com.intellectualcrafters.plot.util.bukkit.BukkitTaskManager; import com.intellectualcrafters.plot.util.bukkit.BukkitTaskManager;
@ -510,4 +512,9 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain {
public void registerWorldEvents() { public void registerWorldEvents() {
getServer().getPluginManager().registerEvents(new WorldEvents(), this); getServer().getPluginManager().registerEvents(new WorldEvents(), this);
} }
@Override
public PlayerManager initPlayerManager() {
return new BukkitPlayerManager();
}
} }

View File

@ -12,6 +12,7 @@ import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.util.BlockManager; import com.intellectualcrafters.plot.util.BlockManager;
import com.intellectualcrafters.plot.util.ChunkManager; import com.intellectualcrafters.plot.util.ChunkManager;
import com.intellectualcrafters.plot.util.EventUtil; import com.intellectualcrafters.plot.util.EventUtil;
import com.intellectualcrafters.plot.util.PlayerManager;
import com.intellectualcrafters.plot.util.SetupUtils; import com.intellectualcrafters.plot.util.SetupUtils;
import com.intellectualcrafters.plot.util.TaskManager; import com.intellectualcrafters.plot.util.TaskManager;
import com.intellectualcrafters.plot.uuid.UUIDWrapper; import com.intellectualcrafters.plot.uuid.UUIDWrapper;
@ -68,4 +69,6 @@ public interface IPlotMain {
public void registerChunkProcessor(); public void registerChunkProcessor();
public void registerWorldEvents(); public void registerWorldEvents();
public PlayerManager initPlayerManager();
} }

View File

@ -67,6 +67,7 @@ import com.intellectualcrafters.plot.util.ExpireManager;
import com.intellectualcrafters.plot.util.Logger; import com.intellectualcrafters.plot.util.Logger;
import com.intellectualcrafters.plot.util.Logger.LogLevel; import com.intellectualcrafters.plot.util.Logger.LogLevel;
import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.PlayerManager;
import com.intellectualcrafters.plot.util.SetupUtils; import com.intellectualcrafters.plot.util.SetupUtils;
import com.intellectualcrafters.plot.util.TaskManager; import com.intellectualcrafters.plot.util.TaskManager;
import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; import com.intellectualcrafters.plot.util.bukkit.UUIDHandler;
@ -547,6 +548,8 @@ public class PlotSquared {
ChunkManager.manager = IMP.initChunkManager(); ChunkManager.manager = IMP.initChunkManager();
// Plot listener // Plot listener
APlotListener.manager = IMP.initPlotListener(); APlotListener.manager = IMP.initPlotListener();
// Player manager
PlayerManager.manager = IMP.initPlayerManager();
// PlotMe // PlotMe
if (Settings.CONVERT_PLOTME || Settings.CACHE_PLOTME) { if (Settings.CONVERT_PLOTME || Settings.CACHE_PLOTME) {

View File

@ -20,9 +20,28 @@
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
package com.intellectualcrafters.plot.commands; package com.intellectualcrafters.plot.commands;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.UUID;
import org.bukkit.Bukkit;
import com.intellectualcrafters.plot.PlotSquared;
import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.database.AbstractDB;
import com.intellectualcrafters.plot.database.DBFunc;
import com.intellectualcrafters.plot.database.SQLManager;
import com.intellectualcrafters.plot.object.OfflinePlotPlayer;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.StringWrapper;
import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.PlayerManager;
import com.intellectualcrafters.plot.util.TaskManager;
import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; import com.intellectualcrafters.plot.util.bukkit.UUIDHandler;
import com.intellectualcrafters.plot.uuid.DefaultUUIDWrapper; import com.intellectualcrafters.plot.uuid.DefaultUUIDWrapper;
import com.intellectualcrafters.plot.uuid.LowerOfflineUUIDWrapper; import com.intellectualcrafters.plot.uuid.LowerOfflineUUIDWrapper;
@ -73,11 +92,184 @@ public class DebugUUID extends SubCommand {
} }
} }
if (args.length != 2 || !args[1].equals("-o")) {
MainUtil.sendMessage(player, C.COMMAND_SYNTAX, "/plot uuidconvert " + args[0] + " - o");
MainUtil.sendMessage(player, "&cBe aware of the following!");
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");
MainUtil.sendMessage(player, "&cBACK UP YOUR DATABASE BEFORE USING THIS!!!");
MainUtil.sendMessage(player, "&7Retype the command with the override parameter when ready");
return false;
}
if (currentUUIDWrapper.getClass().getCanonicalName().equals(newWrapper.getClass().getCanonicalName())) { if (currentUUIDWrapper.getClass().getCanonicalName().equals(newWrapper.getClass().getCanonicalName())) {
MainUtil.sendMessage(player, "&cUUID mode already in use!"); MainUtil.sendMessage(player, "&cUUID mode already in use!");
return false; return false;
} }
MainUtil.sendConsoleMessage("&6Beginning UUID mode conversion");
MainUtil.sendConsoleMessage("&7 - Disconnecting players");
for (PlotPlayer user : UUIDHandler.players.values()) {
PlayerManager.manager.kickPlayer(user, "PlotSquared UUID conversion has been initiated. You may reconnect when finished.");
}
MainUtil.sendConsoleMessage("&7 - Initializing map");
HashMap<UUID, UUID> uCMap = new HashMap<UUID, UUID>();
HashMap<UUID, UUID> uCReverse = new HashMap<UUID, UUID>();
MainUtil.sendConsoleMessage("&7 - Collecting playerdata");
final HashSet<String> worlds = new HashSet<>();
worlds.add(Bukkit.getWorlds().get(0).getName());
worlds.add("world");
final HashSet<UUID> uuids = new HashSet<>();
final HashSet<String> names = new HashSet<>();
for (final String worldname : worlds) {
final File playerdataFolder = new File(worldname + File.separator + "playerdata");
String[] dat = playerdataFolder.list(new FilenameFilter() {
@Override
public boolean accept(final File f, final String s) {
return s.endsWith(".dat");
}
});
if (dat != null) {
for (final String current : dat) {
final String s = current.replaceAll(".dat$", "");
try {
final UUID uuid = UUID.fromString(s);
uuids.add(uuid);
} catch (final Exception e) {
PlotSquared.log(C.PREFIX.s() + "Invalid playerdata: " + current);
}
}
}
final File playersFolder = new File(worldname + File.separator + "players");
dat = playersFolder.list(new FilenameFilter() {
@Override
public boolean accept(final File f, final String s) {
return s.endsWith(".dat");
}
});
if (dat != null) {
for (final String current : dat) {
names.add(current.replaceAll(".dat$", ""));
}
}
}
MainUtil.sendConsoleMessage("&7 - Populating map");
UUID uuid2;
final UUIDWrapper wrapper = new DefaultUUIDWrapper();
for (UUID uuid : uuids) {
try {
final OfflinePlotPlayer op = wrapper.getOfflinePlayer(uuid);
uuid = currentUUIDWrapper.getUUID(op);
uuid2 = newWrapper.getUUID(op);
if (!uuid.equals(uuid2)) {
uCMap.put(uuid, uuid2);
uCReverse.put(uuid2, uuid);
}
} catch (final Throwable e) {
PlotSquared.log(C.PREFIX.s() + "&6Invalid playerdata: " + uuid.toString() + ".dat");
}
}
for (final String name : names) {
final UUID uuid = currentUUIDWrapper.getUUID(name);
uuid2 = newWrapper.getUUID(name);
if (!uuid.equals(uuid2)) {
uCMap.put(uuid, uuid2);
uCReverse.put(uuid2, uuid);
}
}
if (uCMap.size() == 0) {
MainUtil.sendConsoleMessage("&c - Error! Attempting to repopulate");
for (OfflinePlotPlayer op : currentUUIDWrapper.getOfflinePlayers()) {
if (op.getLastPlayed() != 0) {
String name = op.getName();
StringWrapper wrap = new StringWrapper(name);
UUID uuid = currentUUIDWrapper.getUUID(op);
uuid2 = newWrapper.getUUID(op);
if (!uuid.equals(uuid2)) {
uCMap.put(uuid, uuid2);
uCReverse.put(uuid2, uuid);
}
}
}
if (uCMap.size() == 0) {
MainUtil.sendConsoleMessage("&cError. Failed to collect UUIDs!");
return false;
}
else {
MainUtil.sendConsoleMessage("&a - Successfully repopulated");
}
}
MainUtil.sendConsoleMessage("&7 - Replacing cache");
for (Entry<UUID, UUID> entry : uCMap.entrySet()) {
String name = UUIDHandler.getName(entry.getKey());
UUIDHandler.add(new StringWrapper(name), entry.getValue());
}
MainUtil.sendConsoleMessage("&7 - Replacing wrapper");
UUIDHandler.uuidWrapper = newWrapper;
MainUtil.sendConsoleMessage("&7 - Updating plot objects");
for (Plot plot : PlotSquared.getPlots()) {
UUID value = uCMap.get(plot.owner);
if (value != null) {
plot.owner = value;
}
plot.helpers = new ArrayList<>();
plot.trusted = new ArrayList<>();
plot.denied = new ArrayList<>();
}
MainUtil.sendConsoleMessage("&7 - Deleting database");
final AbstractDB database = DBFunc.dbManager;
boolean result = database.deleteTables();
MainUtil.sendConsoleMessage("&7 - Creating tables");
try {
database.createTables(PlotSquared.getMySQL() != null ? "mysql" : "sqlite", true);
if (!result) {
MainUtil.sendConsoleMessage("&cConversion failed! Attempting recovery");
for (Plot plot : PlotSquared.getPlots()) {
UUID value = uCReverse.get(plot.owner);
if (value != null) {
plot.owner = value;
}
}
database.createPlots(new ArrayList<>(PlotSquared.getPlots()));
return false;
}
}
catch (Exception e) {
e.printStackTrace();
return false;
}
MainUtil.sendConsoleMessage("&7 - Populating tables");
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
ArrayList<Plot> plots = new ArrayList<>(PlotSquared.getPlots());
database.createPlots(plots);
int size = plots.size();
ArrayList<Integer> ids = new ArrayList<Integer>();
for (int i = 1; i <= size; i++) {
ids.add(i);
}
database.createSettings(ids);
MainUtil.sendConsoleMessage("&aConversion complete!");
}
});
MainUtil.sendConsoleMessage("&aIt is now safe for players to join");
MainUtil.sendConsoleMessage("&cConversion is still in progress, you will be notified when it is complete");
return true; return true;
} }
} }

View File

@ -337,4 +337,11 @@ public interface AbstractDB {
public void resizeCluster(PlotCluster current, PlotClusterId resize); public void resizeCluster(PlotCluster current, PlotClusterId resize);
public void movePlot(Plot originalPlot, Plot newPlot); public void movePlot(Plot originalPlot, Plot newPlot);
public void createSettings(final ArrayList<Integer> mylist);
/**
* Don't fuck with this one, unless you enjoy it rough
*/
public boolean deleteTables();
} }

View File

@ -1888,4 +1888,33 @@ public class SQLManager implements AbstractDB {
} }
}); });
} }
@Override
public boolean deleteTables() {
try {
SQLManager.this.connection = PlotSquared.getMySQL().forceConnection();
final Statement stmt = this.connection.createStatement();
stmt.addBatch("DROP TABLE `" + prefix + "cluster_invited`");
stmt.addBatch("DROP TABLE `" + prefix + "cluster_helpers`");
stmt.addBatch("DROP TABLE `" + prefix + "cluster`");
stmt.addBatch("DROP TABLE `" + prefix + "plot_ratings`");
stmt.addBatch("DROP TABLE `" + prefix + "plot_settings`");
stmt.addBatch("DROP TABLE `" + prefix + "plot_comments`");
stmt.addBatch("DROP TABLE `" + prefix + "plot_trusted`");
stmt.addBatch("DROP TABLE `" + prefix + "plot_helpers`");
stmt.addBatch("DROP TABLE `" + prefix + "plot_denied`");
stmt.executeBatch();
stmt.clearBatch();
stmt.close();
PreparedStatement statement = connection.prepareStatement("DROP TABLE `" + prefix + "plot`");
statement.executeUpdate();
statement.close();
return true;
}
catch (Exception e) {
e.printStackTrace();
return false;
}
}
} }

View File

@ -275,6 +275,7 @@ public class Plot implements Cloneable {
return ((this.id.x.equals(other.id.x)) && (this.id.y.equals(other.id.y)) && (this.world.equals(other.world))); return ((this.id.x.equals(other.id.x)) && (this.id.y.equals(other.id.y)) && (this.world.equals(other.world)));
} }
/** /**
* Get the plot hashcode * Get the plot hashcode
* *
@ -283,23 +284,6 @@ public class Plot implements Cloneable {
*/ */
@Override @Override
public int hashCode() { public int hashCode() {
final int x = this.id.x; return this.id.hashCode();
final int y = this.id.y;
if (x >= 0) {
if (y >= 0) {
return (x * x) + (3 * x) + (2 * x * y) + y + (y * y);
} else {
final int y1 = -y;
return (x * x) + (3 * x) + (2 * x * y1) + y1 + (y1 * y1) + 1;
}
} else {
final int x1 = -x;
if (y >= 0) {
return -((x1 * x1) + (3 * x1) + (2 * x1 * y) + y + (y * y));
} else {
final int y1 = -y;
return -((x1 * x1) + (3 * x1) + (2 * x1 * y1) + y1 + (y1 * y1) + 1);
}
}
} }
} }

View File

@ -124,23 +124,28 @@ public class PlotId {
} }
} }
private int hash;
@Override @Override
public int hashCode() { public int hashCode() {
if (this.x >= 0) { if (hash == 0) {
if (this.y >= 0) { if (x >= 0) {
return (this.x * this.x) + (3 * this.x) + (2 * this.x * this.y) + this.y + (this.y * this.y); if (y >= 0) {
hash = (x * x) + (3 * x) + (2 * x * y) + y + (y * y);
} else { } else {
final int y1 = -this.y; final int y1 = -y;
return (this.x * this.x) + (3 * this.x) + (2 * this.x * y1) + y1 + (y1 * y1) + 1; hash = (x * x) + (3 * x) + (2 * x * y1) + y1 + (y1 * y1) + 1;
} }
} else { } else {
final int x1 = -this.x; final int x1 = -x;
if (this.y >= 0) { if (y >= 0) {
return -((x1 * x1) + (3 * x1) + (2 * x1 * this.y) + this.y + (this.y * this.y)); hash = -((x1 * x1) + (3 * x1) + (2 * x1 * y) + y + (y * y));
} else { } else {
final int y1 = -this.y; final int y1 = -y;
return -((x1 * x1) + (3 * x1) + (2 * x1 * y1) + y1 + (y1 * y1) + 1); hash = -((x1 * x1) + (3 * x1) + (2 * x1 * y1) + y1 + (y1 * y1) + 1);
} }
} }
} }
return hash;
}
} }

View File

@ -0,0 +1,9 @@
package com.intellectualcrafters.plot.util;
import com.intellectualcrafters.plot.object.PlotPlayer;
public abstract class PlayerManager {
public static PlayerManager manager;
public abstract void kickPlayer(PlotPlayer player, String reason);
}

View File

@ -0,0 +1,14 @@
package com.intellectualcrafters.plot.util.bukkit;
import com.intellectualcrafters.plot.object.BukkitPlayer;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.util.PlayerManager;
public class BukkitPlayerManager extends PlayerManager {
@Override
public void kickPlayer(PlotPlayer player, String reason) {
((BukkitPlayer) player).player.kickPlayer(reason);
}
}