Offline uuid from flat file.

This commit is contained in:
boy0001 2015-08-26 14:21:48 +10:00
parent 1872e147cb
commit 3229705012
9 changed files with 225 additions and 125 deletions

View file

@ -8,7 +8,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<artifactId>PlotSquared</artifactId>
<version>3.1.6</version>
<version>3.1.7</version>
<name>PlotSquared</name>
<packaging>jar</packaging>
<build>

View file

@ -22,9 +22,13 @@ package com.intellectualcrafters.plot.commands;
import java.io.File;
import java.io.FilenameFilter;
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;
@ -40,6 +44,7 @@ import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.StringWrapper;
import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.StringMan;
import com.intellectualcrafters.plot.util.TaskManager;
import com.intellectualcrafters.plot.util.UUIDHandler;
import com.intellectualcrafters.plot.uuid.UUIDWrapper;
@ -69,8 +74,8 @@ public class DebugUUID extends SubCommand {
public boolean onCommand(final PlotPlayer plr, final String[] args) {
PlotPlayer player = null;
UUIDWrapper currentUUIDWrapper = UUIDHandler.getUUIDWrapper();
UUIDWrapper newWrapper = null;
final UUIDWrapper currentUUIDWrapper = UUIDHandler.getUUIDWrapper();
final UUIDWrapper newWrapper;
switch (args[0].toLowerCase()) {
case "lower": {
@ -121,8 +126,8 @@ public class DebugUUID extends SubCommand {
MainUtil.sendConsoleMessage("&7 - Initializing map");
HashMap<UUID, UUID> uCMap = new HashMap<UUID, UUID>();
HashMap<UUID, UUID> uCReverse = new HashMap<UUID, UUID>();
final HashMap<UUID, UUID> uCMap = new HashMap<UUID, UUID>();
final HashMap<UUID, UUID> uCReverse = new HashMap<UUID, UUID>();
MainUtil.sendConsoleMessage("&7 - Collecting playerdata");
@ -172,7 +177,7 @@ public class DebugUUID extends SubCommand {
final OfflinePlotPlayer op = wrapper.getOfflinePlayer(uuid);
uuid = currentUUIDWrapper.getUUID(op);
uuid2 = newWrapper.getUUID(op);
if (!uuid.equals(uuid2)) {
if (!uuid.equals(uuid2) && !uCMap.containsKey(uuid) && !uCReverse.containsKey(uuid2)) {
uCMap.put(uuid, uuid2);
uCReverse.put(uuid2, uuid);
}
@ -192,8 +197,8 @@ public class DebugUUID extends SubCommand {
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);
// String name = op.getName();
// StringWrapper wrap = new StringWrapper(name);
UUID uuid = currentUUIDWrapper.getUUID(op);
uuid2 = newWrapper.getUUID(op);
if (!uuid.equals(uuid2)) {
@ -212,92 +217,135 @@ public class DebugUUID extends SubCommand {
}
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.setUUIDWrapper(newWrapper);
MainUtil.sendConsoleMessage("&7 - Updating plot objects");
for (Plot plot : PS.get().getPlotsRaw()) {
UUID value = uCMap.get(plot.owner);
if (value != null) {
plot.owner = value;
}
plot.getTrusted().clear();
plot.getMembers().clear();
plot.getDenied().clear();
}
MainUtil.sendConsoleMessage("&7 - Deleting database");
final AbstractDB database = DBFunc.dbManager;
boolean result = database.deleteTables();
MainUtil.sendConsoleMessage("&7 - Creating tables");
try {
database.createTables();
if (!result) {
MainUtil.sendConsoleMessage("&cConversion failed! Attempting recovery");
for (Plot plot : PS.get().getPlots()) {
UUID value = uCReverse.get(plot.owner);
if (value != null) {
plot.owner = value;
}
}
database.createPlotsAndData(new ArrayList<>(PS.get().getPlots()), new Runnable() {
@Override
public void run() {
MainUtil.sendMessage(null, "&6Recovery was successful!");
}
});
return false;
}
}
catch (Exception e) {
e.printStackTrace();
return false;
}
if (newWrapper instanceof OfflineUUIDWrapper) {
PS.get().config.set("UUID.force-lowercase", false);
PS.get().config.set("UUID.offline", true);
}
else if (newWrapper instanceof LowerOfflineUUIDWrapper) {
PS.get().config.set("UUID.force-lowercase", true);
PS.get().config.set("UUID.offline", true);
}
else if (newWrapper instanceof DefaultUUIDWrapper) {
PS.get().config.set("UUID.force-lowercase", false);
PS.get().config.set("UUID.offline", false);
}
try {
PS.get().config.save(PS.get().configFile);
}
catch (Exception e) {
MainUtil.sendConsoleMessage("Could not save configuration. It will need to be manuall set!");
}
MainUtil.sendConsoleMessage("&7 - Populating tables");
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
ArrayList<Plot> plots = new ArrayList<>(PS.get().getPlots());
database.createPlotsAndData(plots, new Runnable() {
for (Entry<UUID, UUID> entry : uCMap.entrySet()) {
String name = UUIDHandler.getName(entry.getKey());
if (name != null) {
UUIDHandler.add(new StringWrapper(name), entry.getValue());
}
}
MainUtil.sendConsoleMessage("&7 - Scanning for applicable files (uuids.txt)");
File file = new File(PS.get().IMP.getDirectory(), "uuids.txt");
if (file.exists()) {
try {
List<String> lines = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8);
for (String line : lines) {
try {
line = line.trim();
if (line.length() == 0) {
continue;
}
line = line.replaceAll("[\\|][0-9]+[\\|][0-9]+[\\|]", "");
String[] split = line.split("\\|");
String name = split[0];
if (name.length() == 0 || name.length() > 16 || !StringMan.isAlphanumericUnd(name)) {
continue;
}
UUID old = currentUUIDWrapper.getUUID(name);
if (old == null) {
continue;
}
UUID now = newWrapper.getUUID(name);
UUIDHandler.add(new StringWrapper(name), now);
uCMap.put(old, now);
uCReverse.put(now, old);
}
catch (Exception e2) {
e2.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
MainUtil.sendConsoleMessage("&7 - Replacing wrapper");
UUIDHandler.setUUIDWrapper(newWrapper);
MainUtil.sendConsoleMessage("&7 - Updating plot objects");
for (Plot plot : PS.get().getPlotsRaw()) {
UUID value = uCMap.get(plot.owner);
if (value != null) {
plot.owner = value;
}
plot.getTrusted().clear();
plot.getMembers().clear();
plot.getDenied().clear();
}
MainUtil.sendConsoleMessage("&7 - Deleting database");
final AbstractDB database = DBFunc.dbManager;
boolean result = database.deleteTables();
MainUtil.sendConsoleMessage("&7 - Creating tables");
try {
database.createTables();
if (!result) {
MainUtil.sendConsoleMessage("&cConversion failed! Attempting recovery");
for (Plot plot : PS.get().getPlots()) {
UUID value = uCReverse.get(plot.owner);
if (value != null) {
plot.owner = value;
}
}
database.createPlotsAndData(new ArrayList<>(PS.get().getPlots()), new Runnable() {
@Override
public void run() {
MainUtil.sendMessage(null, "&6Recovery was successful!");
}
});
return;
}
}
catch (Exception e) {
e.printStackTrace();
return;
}
if (newWrapper instanceof OfflineUUIDWrapper) {
PS.get().config.set("UUID.force-lowercase", false);
PS.get().config.set("UUID.offline", true);
}
else if (newWrapper instanceof LowerOfflineUUIDWrapper) {
PS.get().config.set("UUID.force-lowercase", true);
PS.get().config.set("UUID.offline", true);
}
else if (newWrapper instanceof DefaultUUIDWrapper) {
PS.get().config.set("UUID.force-lowercase", false);
PS.get().config.set("UUID.offline", false);
}
try {
PS.get().config.save(PS.get().configFile);
}
catch (Exception e) {
MainUtil.sendConsoleMessage("Could not save configuration. It will need to be manuall set!");
}
MainUtil.sendConsoleMessage("&7 - Populating tables");
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
MainUtil.sendConsoleMessage("&aConversion complete!");
ArrayList<Plot> plots = new ArrayList<>(PS.get().getPlots());
database.createPlotsAndData(plots, new Runnable() {
@Override
public void run() {
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");
}
});
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;
}
}

View file

@ -439,7 +439,7 @@ public class SQLManager implements AbstractDB {
}
}
}
}
}
}
createSettings(settings, new Runnable() {
@Override
@ -977,10 +977,13 @@ public class SQLManager implements AbstractDB {
final DatabaseMetaData meta = connection.getMetaData();
int create = 0;
for (final String s : tables) {
ResultSet set = meta.getTables(null, null, prefix + s, null);
ResultSet set = meta.getTables(null, null, prefix + s, new String[] {"TABLE"});
// ResultSet set = meta.getTables(null, null, prefix + s, null);
if (!set.next()) {
create++;
}
else {
}
set.close();
}
if (create == 0) {
@ -2390,34 +2393,30 @@ public class SQLManager implements AbstractDB {
@Override
public boolean deleteTables() {
addGlobalTask(new Runnable() {
@Override
public void run() {
try {
close();
SQLManager.this.connection = database.forceConnection();
final Statement stmt = 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_rating`");
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();
} catch (Exception e) {
e.printStackTrace();
}
}
});
try {
close();
CLOSED = false;
SQLManager.this.connection = database.forceConnection();
final Statement stmt = 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_rating`");
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();
} catch (Exception e) {
e.printStackTrace();
}
return true;
}

View file

@ -2,6 +2,7 @@ package com.intellectualcrafters.plot.util;
import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import com.intellectualcrafters.plot.PS;
@ -89,7 +90,11 @@ public class SetBlockQueue {
if (locked) {
return;
}
Entry<ChunkWrapper, PlotBlock[][]> n = blocks.entrySet().iterator().next();
Iterator<Entry<ChunkWrapper, PlotBlock[][]>> iter = blocks.entrySet().iterator();
if (!iter.hasNext()) {
return;
}
Entry<ChunkWrapper, PlotBlock[][]> n = iter.next();
ChunkWrapper chunk = n.getKey();
PlotBlock[][] blocks = n.getValue();
int X = chunk.x << 4;

View file

@ -113,6 +113,16 @@ public class StringMan {
return true;
}
public static boolean isAlphanumericUnd(String str) {
for (int i=0; i<str.length(); i++) {
char c = str.charAt(i);
if (c < 0x30 || (c >= 0x3a && c <= 0x40) || (c > 0x5a && c <= 0x60) || c > 0x7a || c == '_') {
return false;
}
}
return true;
}
public static boolean isAlpha(String str) {
for (int i=0; i<str.length(); i++) {
char c = str.charAt(i);

View file

@ -90,15 +90,20 @@ public abstract class UUIDHandlerImplementation {
if ((uuid == null) || (name == null)) {
return false;
}
BiMap<UUID, StringWrapper> inverse = uuidMap.inverse();
if (inverse.containsKey(uuid)) {
if (uuidMap.containsKey(name)) {
try {
uuidMap.put(name, uuid);
}
catch (Exception e) {
BiMap<UUID, StringWrapper> inverse = uuidMap.inverse();
if (inverse.containsKey(uuid)) {
if (uuidMap.containsKey(name)) {
return false;
}
rename(uuid, name);
return false;
}
rename(uuid, name);
return false;
uuidMap.put(name, uuid);
}
uuidMap.put(name, uuid);
return true;
}

View file

@ -3,6 +3,9 @@ package com.plotsquared.bukkit.uuid;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@ -12,7 +15,6 @@ import org.bukkit.Bukkit;
import org.bukkit.World;
import com.google.common.collect.HashBiMap;
import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.config.C;
@ -23,6 +25,7 @@ import com.intellectualcrafters.plot.object.RunnableVal;
import com.intellectualcrafters.plot.object.StringWrapper;
import com.intellectualcrafters.plot.util.ExpireManager;
import com.intellectualcrafters.plot.util.NbtFactory;
import com.intellectualcrafters.plot.util.StringMan;
import com.intellectualcrafters.plot.util.TaskManager;
import com.intellectualcrafters.plot.util.UUIDHandler;
import com.intellectualcrafters.plot.util.UUIDHandlerImplementation;
@ -56,6 +59,36 @@ public class FileUUIDHandler extends UUIDHandlerImplementation {
@Override
public void run() {
PS.debug(C.PREFIX.s() + "&6Starting player data caching for: " + world);
File uuidfile = new File(PS.get().IMP.getDirectory(), "uuids.txt");
if (uuidfile.exists()) {
try {
List<String> lines = Files.readAllLines(uuidfile.toPath(), StandardCharsets.UTF_8);
for (String line : lines) {
try {
line = line.trim();
if (line.length() == 0) {
continue;
}
line = line.replaceAll("[\\|][0-9]+[\\|][0-9]+[\\|]", "");
String[] split = line.split("\\|");
String name = split[0];
if (name.length() == 0 || name.length() > 16 || !StringMan.isAlphanumericUnd(name)) {
continue;
}
UUID uuid = uuidWrapper.getUUID(name);
if (uuid == null) {
continue;
}
UUIDHandler.add(new StringWrapper(name), uuid);
}
catch (Exception e2) {
e2.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
if (Settings.TWIN_MODE_UUID) {
final HashBiMap<StringWrapper, UUID> toAdd = HashBiMap.create(new HashMap<StringWrapper, UUID>());
toAdd.put(new StringWrapper("*"), DBFunc.everyone);
@ -76,7 +109,7 @@ public class FileUUIDHandler extends UUIDHandlerImplementation {
UUID uuid = UUID.fromString(s);
if (check || all.remove(uuid)) {
File file = new File(playerdataFolder + File.separator + current);
InputSupplier<FileInputStream> is = Files.newInputStreamSupplier(file);
InputSupplier<FileInputStream> is = com.google.common.io.Files.newInputStreamSupplier(file);
NbtFactory.NbtCompound compound = NbtFactory.fromStream(is, NbtFactory.StreamOptions.GZIP_COMPRESSION);
NbtFactory.NbtCompound bukkit = (NbtFactory.NbtCompound) compound.get("bukkit");
String name = (String) bukkit.get("lastKnownName");
@ -146,7 +179,7 @@ public class FileUUIDHandler extends UUIDHandlerImplementation {
for (UUID uuid : uuids) {
try {
File file = new File(playerdataFolder + File.separator + uuid.toString() + ".dat");
InputSupplier<FileInputStream> is = Files.newInputStreamSupplier(file);
InputSupplier<FileInputStream> is = com.google.common.io.Files.newInputStreamSupplier(file);
NbtFactory.NbtCompound compound = NbtFactory.fromStream(is, NbtFactory.StreamOptions.GZIP_COMPRESSION);
NbtFactory.NbtCompound bukkit = (NbtFactory.NbtCompound) compound.get("bukkit");
String name = (String) bukkit.get("lastKnownName");

Binary file not shown.

Binary file not shown.