Performance optimization for the UUID Updater

This commit is contained in:
GeorgH93 2015-03-01 18:15:12 +01:00
parent ef51db0e0b
commit c09b291f65
4 changed files with 45 additions and 91 deletions

View File

@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>at.pcgamingfreaks</groupId> <groupId>at.pcgamingfreaks</groupId>
<artifactId>MinePacks</artifactId> <artifactId>MinePacks</artifactId>
<version>1.1.2</version> <version>1.1.3</version>
<repositories> <repositories>
<repository> <repository>
<id>spigot-repo</id> <id>spigot-repo</id>

View File

@ -71,7 +71,7 @@ private void CheckUUIDs()
{ {
plugin.log.info(plugin.lang.Get("Console.UpdateUUIDs")); plugin.log.info(plugin.lang.Get("Console.UpdateUUIDs"));
} }
converter.add("UPDATE `" + Table_Players + "` SET `uuid`='" + UUIDConverter.getUUIDFromName(res.getString(1)) + "' WHERE `name`='" + res.getString(1).replace("\\", "\\\\").replace("'", "\\'") + "'"); converter.add("UPDATE `" + Table_Players + "` SET `uuid`='" + UUIDConverter.getUUIDFromName(res.getString(1), true) + "' WHERE `name`='" + res.getString(1).replace("\\", "\\\\").replace("'", "\\'") + "'");
} }
if(converter.size() > 0) if(converter.size() > 0)
{ {

View File

@ -69,7 +69,7 @@ private void CheckUUIDs()
{ {
plugin.log.info(plugin.lang.Get("Console.UpdateUUIDs")); plugin.log.info(plugin.lang.Get("Console.UpdateUUIDs"));
} }
converter.add("UPDATE `" + Table_Players + "` SET `uuid`='" + UUIDConverter.getUUIDFromName(res.getString(1)) + "' WHERE `name`='" + res.getString(1).replace("\\", "\\\\").replace("'", "\\'") + "'"); converter.add("UPDATE `" + Table_Players + "` SET `uuid`='" + UUIDConverter.getUUIDFromName(res.getString(1), plugin.getServer().getOnlineMode()) + "' WHERE `name`='" + res.getString(1).replace("\\", "\\\\").replace("'", "\\'") + "'");
} }
if(converter.size() > 0) if(converter.size() > 0)
{ {

View File

@ -18,121 +18,75 @@
package at.pcgamingfreaks.georgh.MinePacks.Database; package at.pcgamingfreaks.georgh.MinePacks.Database;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL; import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner; import java.util.Scanner;
import java.util.UUID;
import com.google.gson.Gson; import com.google.common.base.Charsets;
import com.google.gson.JsonParser;
import org.json.simple.JSONObject; import com.google.gson.JsonObject;
import org.json.simple.parser.JSONParser;
public class UUIDConverter public class UUIDConverter
{ {
private static Gson gson = new Gson(); public static String getNameFromUUID(String uuid)
protected static String getNameFromUUID(String uuid)
{ {
String name = null; String name = null;
try try
{ {
URL url = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid); URL url = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid.replaceAll("-", ""));
URLConnection connection = url.openConnection(); Scanner jsonScanner = new Scanner(url.openConnection().getInputStream(), "UTF-8");
Scanner jsonScanner = new Scanner(connection.getInputStream(), "UTF-8");
String json = jsonScanner.next(); String json = jsonScanner.next();
JSONParser parser = new JSONParser(); name = (((JsonObject)new JsonParser().parse(json)).get("name")).toString();
Object obj = parser.parse(json);
name = (String) ((JSONObject) obj).get("name");
jsonScanner.close(); jsonScanner.close();
} }
catch (Exception ex)
{
ex.printStackTrace();
}
return name;
}
protected static String getUUIDFromName(String name)
{
try
{
ProfileData profC = new ProfileData(name);
String UUID = null;
for (int i = 1; i <= 100; i++)
{
PlayerProfile[] result = post(new URL("https://api.mojang.com/profiles/page/" + i), Proxy.NO_PROXY, gson.toJson(profC).getBytes());
if (result.length == 0)
{
break;
}
UUID = result[0].getId();
}
return UUID;
}
catch (Exception e) catch (Exception e)
{ {
e.printStackTrace(); e.printStackTrace();
} }
return null; return name;
} }
private static PlayerProfile[] post(URL url, Proxy proxy, byte[] bytes) throws IOException public static String getUUIDFromName(String name, boolean onlinemode)
{ {
HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy); return getUUIDFromName(name, onlinemode, false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.write(bytes);
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuffer response = new StringBuffer();
String line;
while ((line = reader.readLine()) != null)
{
response.append(line);
response.append('\r');
}
reader.close();
return gson.fromJson(response.toString(), SearchResult.class).getProfiles();
} }
private static class PlayerProfile public static String getUUIDFromName(String name, boolean onlinemode, boolean withSeperators)
{ {
private String id; String uuid = null;
public String getId() if(onlinemode)
{ {
return id; try
{
BufferedReader in = new BufferedReader(new InputStreamReader(new URL("https://api.mojang.com/users/profiles/minecraft/" + name).openStream()));
uuid = (((JsonObject)new JsonParser().parse(in)).get("id")).toString().replaceAll("\"", "");
in.close();
}
catch (Exception e)
{
e.printStackTrace();
uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(Charsets.UTF_8)).toString();
} }
} }
else
private static class SearchResult
{ {
private PlayerProfile[] profiles; uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(Charsets.UTF_8)).toString();
public PlayerProfile[] getProfiles() }
if(uuid != null)
{ {
return profiles; if(withSeperators)
{
if(!uuid.contains("-"))
{
return uuid = uuid.replaceAll("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})", "$1-$2-$3-$4-$5");
} }
} }
else
private static class ProfileData
{ {
@SuppressWarnings("unused") uuid = uuid.replaceAll("-", "");
private String name;
@SuppressWarnings("unused")
private String agent = "minecraft";
public ProfileData(String name)
{
this.name = name;
} }
} }
return uuid;
}
} }