Convert old player configs

This commit is contained in:
gravitylow 2014-04-15 15:08:25 -04:00
parent e3c0f6376f
commit 8453ab3e76
2 changed files with 116 additions and 3 deletions

View File

@ -1,15 +1,15 @@
package com.massivecraft.factions.zcore.persist; package com.massivecraft.factions.zcore.persist;
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.zcore.util.DiscUtil; import com.massivecraft.factions.zcore.util.DiscUtil;
import com.massivecraft.factions.zcore.util.TextUtil; import com.massivecraft.factions.zcore.util.TextUtil;
import com.massivecraft.factions.zcore.util.UUIDFetcher;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.libs.com.google.gson.Gson; import org.bukkit.craftbukkit.libs.com.google.gson.Gson;
import java.io.File; import java.io.File;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.Collection; import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.logging.Level; import java.util.logging.Level;
@ -221,6 +221,34 @@ public abstract class EntityCollection<E extends Entity> {
} }
Type type = this.getMapType(); Type type = this.getMapType();
if (type.toString().contains("FPlayer")) {
Map<String, FPlayer> data = this.gson.fromJson(content, type);
// Convert any leftover player names in this file
ArrayList<String> list = new ArrayList<String>();
for (String value : data.keySet()) {
if (value.matches("[a-z0-9_]{2,16}")) {
list.add(value);
}
}
if (list.size() > 0) {
UUIDFetcher fetcher = new UUIDFetcher(list);
try {
Map<String, UUID> response = fetcher.call();
for (String value : response.keySet()) {
String id = response.get(value).toString();
FPlayer player = data.get(value);
data.remove(value); // Out with the old...
data.put(response.get(value).toString(), player); // And in with the new
player.setId(id); // Update the object so it knows
}
} catch (Exception e) {
e.printStackTrace();
}
Bukkit.getLogger().log(Level.INFO, "Converted " + list.size() + " old player names to UUID");
}
return (Map<String, E>) data;
}
try { try {
return this.gson.fromJson(content, type); return this.gson.fromJson(content, type);
} catch (Exception ex) { } catch (Exception ex) {

View File

@ -0,0 +1,85 @@
package com.massivecraft.factions.zcore.util;
import com.google.common.collect.ImmutableList;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.JSONParser;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Callable;
/**
* @author evilmidget38
*/
public class UUIDFetcher implements Callable<Map<String, UUID>> {
private static final int MAX_SEARCH = 100;
private static final String PROFILE_URL = "https://api.mojang.com/profiles/page/";
private static final String AGENT = "minecraft";
private final JSONParser jsonParser = new JSONParser();
private final List<String> names;
public UUIDFetcher(List<String> names) {
this.names = ImmutableList.copyOf(names);
}
public Map<String, UUID> call() throws Exception {
Map<String, UUID> uuidMap = new HashMap<String, UUID>();
String body = buildBody(names);
for (int i = 1; i < MAX_SEARCH; i++) {
HttpURLConnection connection = createConnection(i);
writeBody(connection, body);
JSONObject jsonObject = (JSONObject) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
JSONArray array = (JSONArray) jsonObject.get("profiles");
Number count = (Number) jsonObject.get("size");
if (count.intValue() == 0) {
break;
}
for (Object profile : array) {
JSONObject jsonProfile = (JSONObject) profile;
String id = (String) jsonProfile.get("id");
String name = (String) jsonProfile.get("name");
UUID uuid = UUID.fromString(id.substring(0, 8) + "-" + id.substring(8, 12) + "-" + id.substring(12, 16) + "-" + id.substring(16, 20) + "-" +id.substring(20, 32));
uuidMap.put(name, uuid);
}
}
return uuidMap;
}
private static void writeBody(HttpURLConnection connection, String body) throws Exception {
DataOutputStream writer = new DataOutputStream(connection.getOutputStream());
writer.write(body.getBytes());
writer.flush();
writer.close();
}
private static HttpURLConnection createConnection(int page) throws Exception {
URL url = new URL(PROFILE_URL+page);
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;
}
@SuppressWarnings("unchecked")
private static String buildBody(List<String> names) {
List<JSONObject> lookups = new ArrayList<JSONObject>();
for (String name : names) {
JSONObject obj = new JSONObject();
obj.put("name", name);
obj.put("agent", AGENT);
lookups.add(obj);
}
return JSONValue.toJSONString(lookups);
}
}