Started UUID Conversion

This commit is contained in:
Blackvein 2014-07-11 15:50:51 -07:00
parent 79ddad8b9e
commit 9df95187fb
3 changed files with 223 additions and 1 deletions

View File

@ -0,0 +1,96 @@
package com.evilmidget38;
import com.google.common.collect.ImmutableList;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.concurrent.Callable;
public class UUIDFetcher implements Callable<Map<String, UUID>> {
private static final double PROFILES_PER_REQUEST = 100;
private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft";
private final JSONParser jsonParser = new JSONParser();
private final List<String> names;
private final boolean rateLimiting;
public UUIDFetcher(List<String> names, boolean rateLimiting) {
this.names = ImmutableList.copyOf(names);
this.rateLimiting = rateLimiting;
}
public UUIDFetcher(List<String> names) {
this(names, true);
}
public Map<String, UUID> call() throws Exception {
Map<String, UUID> uuidMap = new HashMap<String, UUID>();
int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST);
for (int i = 0; i < requests; i++) {
HttpURLConnection connection = createConnection();
String body = JSONArray.toJSONString(names.subList(i * 100, Math.min((i + 1) * 100, names.size())));
writeBody(connection, body);
JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
for (Object profile : array) {
JSONObject jsonProfile = (JSONObject) profile;
String id = (String) jsonProfile.get("id");
String name = (String) jsonProfile.get("name");
UUID uuid = UUIDFetcher.getUUID(id);
uuidMap.put(name, uuid);
}
if (rateLimiting && i != requests - 1) {
Thread.sleep(100L);
}
}
return uuidMap;
}
private static void writeBody(HttpURLConnection connection, String body) throws Exception {
OutputStream stream = connection.getOutputStream();
stream.write(body.getBytes());
stream.flush();
stream.close();
}
private static HttpURLConnection createConnection() throws Exception {
URL url = new URL(PROFILE_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
return connection;
}
private static UUID getUUID(String id) {
return UUID.fromString(id.substring(0, 8) + "-" + id.substring(8, 12) + "-" + id.substring(12, 16) + "-" + id.substring(16, 20) + "-" +id.substring(20, 32));
}
public static byte[] toBytes(UUID uuid) {
ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]);
byteBuffer.putLong(uuid.getMostSignificantBits());
byteBuffer.putLong(uuid.getLeastSignificantBits());
return byteBuffer.array();
}
public static UUID fromBytes(byte[] array) {
if (array.length != 16) {
throw new IllegalArgumentException("Illegal byte array length: " + array.length);
}
ByteBuffer byteBuffer = ByteBuffer.wrap(array);
long mostSignificant = byteBuffer.getLong();
long leastSignificant = byteBuffer.getLong();
return new UUID(mostSignificant, leastSignificant);
}
public static UUID getUUIDOf(String name) throws Exception {
return new UUIDFetcher(Arrays.asList(name)).call().get(name);
}
}

View File

@ -1,6 +1,7 @@
package me.blackvein.quests;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Collections;
@ -65,6 +66,7 @@ import org.bukkit.potion.PotionEffectType;
import com.codisimus.plugins.phatloots.PhatLoots;
import com.codisimus.plugins.phatloots.PhatLootsAPI;
import com.evilmidget38.UUIDFetcher;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.skills.SkillType;
@ -83,8 +85,10 @@ import java.net.URLClassLoader;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.UUID;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
@ -5001,6 +5005,124 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener,
return;
}
}
public void convertQuesters() {
int numQuesters = 0;
int succeeded = 0;
int failed = 0;
final File dataFolder = new File(this.getDataFolder(), "data/");
final File oldDataFolder = new File(this.getDataFolder(), "data/old/");
if(oldDataFolder.exists() == false || oldDataFolder.exists() && oldDataFolder.isDirectory() == false)
oldDataFolder.mkdir();
if(dataFolder.exists() && dataFolder.isDirectory()) {
final File[] files = dataFolder.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return dir.getPath().equals(dataFolder.getPath()) && name.endsWith(".yml");
}
});
numQuesters = files.length;
if(numQuesters > 0) {
final ArrayList<String> names = new ArrayList<String>();
Quests.printInfo("Gathering Quester information...");
for(int i = 0; i < numQuesters; i++) {
final File file = files[i];
final File old = new File(oldDataFolder, file.getName());
final String name = file.getName().substring(0, file.getName().length() - 4);
final FileConfiguration config = new YamlConfiguration();
try{
config.load(file);
config.save(old);
config.set("lastKnownName", name);
config.save(file);
}catch (Exception e) {
failed++;
}
names.add(name);
succeeded++;
}
Quests.printInfo("Completed: " + succeeded + "Success(es). " + failed + " Failure(s). " + numQuesters + " Total.");
Quests.printInfo("Preparing to convert data.");
Bukkit.getScheduler().runTaskAsynchronously(this, new Runnable() {
@Override
public void run() {
Quests.printInfo("Done. Converting data...");
int converted = 0;
int failed = 0;
final UUIDFetcher fetcher = new UUIDFetcher(names);
final Map<String, UUID> idMap;
try {
idMap = fetcher.call();
} catch (Exception ex) {
Quests.printSevere("Error retrieving data from Mojang account database. Error log:");
Logger.getLogger(Quests.class.getName()).log(Level.SEVERE, null, ex);
return;
}
for(Entry<String, UUID> entry : idMap.entrySet()) {
try {
final File found = new File(dataFolder, entry.getKey() + ".yml");
final File copy = new File(dataFolder, entry.getValue() + ".yml");
final FileConfiguration config = new YamlConfiguration();
config.load(found);
config.save(copy);
found.delete();
converted++;
} catch (Exception ex) {
failed++;
}
}
Quests.printInfo("Conversion completed: " + converted + " Converted. " + failed + " Failed.");
Quests.printInfo("Old data files stored in /Quests/data/old");
}
});
} else {
Quests.printInfo("No Questers to convert!");
}
} else {
Quests.printInfo("Data folder does not exist!");
}
}
}

View File

@ -98,11 +98,15 @@ public class MiscUtil {
private static String unfixUnderscores(String s) {
for(int i = 1; i < s.length(); i++) {
int max = s.length();
for(int i = 1; i < max; i++) {
if(Character.isUpperCase(s.charAt(i))) {
s = s.substring(0, i) + "_" + s.substring(i);
i++;
max++;
}