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>
<groupId>at.pcgamingfreaks</groupId>
<artifactId>MinePacks</artifactId>
<version>1.1.2</version>
<version>1.1.3</version>
<repositories>
<repository>
<id>spigot-repo</id>

View File

@ -71,7 +71,7 @@ private void CheckUUIDs()
{
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)
{

View File

@ -69,7 +69,7 @@ private void CheckUUIDs()
{
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)
{

View File

@ -18,121 +18,75 @@
package at.pcgamingfreaks.georgh.MinePacks.Database;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
import java.util.UUID;
import com.google.gson.Gson;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import com.google.common.base.Charsets;
import com.google.gson.JsonParser;
import com.google.gson.JsonObject;
public class UUIDConverter
{
private static Gson gson = new Gson();
protected static String getNameFromUUID(String uuid)
public static String getNameFromUUID(String uuid)
{
String name = null;
try
{
URL url = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid);
URLConnection connection = url.openConnection();
Scanner jsonScanner = new Scanner(connection.getInputStream(), "UTF-8");
URL url = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid.replaceAll("-", ""));
Scanner jsonScanner = new Scanner(url.openConnection().getInputStream(), "UTF-8");
String json = jsonScanner.next();
JSONParser parser = new JSONParser();
Object obj = parser.parse(json);
name = (String) ((JSONObject) obj).get("name");
name = (((JsonObject)new JsonParser().parse(json)).get("name")).toString();
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)
{
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);
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();
return getUUIDFromName(name, onlinemode, false);
}
private static class PlayerProfile
public static String getUUIDFromName(String name, boolean onlinemode, boolean withSeperators)
{
private String id;
public String getId()
String uuid = null;
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();
}
}
}
private static class SearchResult
{
private PlayerProfile[] profiles;
public PlayerProfile[] getProfiles()
else
{
return profiles;
uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(Charsets.UTF_8)).toString();
}
}
private static class ProfileData
{
@SuppressWarnings("unused")
private String name;
@SuppressWarnings("unused")
private String agent = "minecraft";
public ProfileData(String name)
if(uuid != null)
{
this.name = name;
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
{
uuid = uuid.replaceAll("-", "");
}
}
return uuid;
}
}