diff --git a/src/uk/co/tggl/pluckerpluck/multiinv/MultiInv.java b/src/uk/co/tggl/pluckerpluck/multiinv/MultiInv.java index 366d650..f06ef5b 100644 --- a/src/uk/co/tggl/pluckerpluck/multiinv/MultiInv.java +++ b/src/uk/co/tggl/pluckerpluck/multiinv/MultiInv.java @@ -1,16 +1,6 @@ package uk.co.tggl.pluckerpluck.multiinv; -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - +import com.tux2mc.debugreport.DebugReport; import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.command.Command; @@ -18,9 +8,6 @@ import org.bukkit.entity.Player; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; - -import com.tux2mc.debugreport.DebugReport; - import uk.co.tggl.pluckerpluck.multiinv.command.MICommand; import uk.co.tggl.pluckerpluck.multiinv.inventory.PlayerRestrictionRemoverThread; import uk.co.tggl.pluckerpluck.multiinv.listener.MIInventoryListener; @@ -28,6 +15,13 @@ import uk.co.tggl.pluckerpluck.multiinv.logger.MILogger; import uk.co.tggl.pluckerpluck.multiinv.util.UUIDFetcher; +import java.io.File; +import java.io.IOException; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + /** * Created by IntelliJ IDEA. User: Pluckerpluck Date: 17/12/11 Time: 11:58 To change this template use File | Settings | File Templates. */ @@ -229,44 +223,34 @@ private synchronized void convertToUUID() { for(File gfolder : groups) { if(gfolder.isDirectory()) { File[] users = gfolder.listFiles(); - LinkedList uncachedplayers = new LinkedList(); + Set uncachedplayers = new HashSet(); for(int i = 0; i < users.length; i++) { File user = users[i]; if(user.isFile()) { String filename = user.getName(); - if(filename.endsWith(".yml")) { - String username = filename.substring(0, filename.lastIndexOf(".")); - if(!cacheduuids.containsKey(username)) { - uncachedplayers.add(username); - } - } + String username; + if (filename.endsWith(".ec.yml")) { + username = filename.substring(0, filename.length() - 7); + } else if(filename.endsWith(".yml")) { + username = filename.substring(0, filename.length() - 4); + } else { + continue; + } + + if(!cacheduuids.containsKey(username) && !uncachedplayers.contains(username)) { + uncachedplayers.add(username); + } } } - boolean first = true; - while(uncachedplayers.size() > 0) { - LinkedList playerlist = new LinkedList(); - for(int i = 0; i < uncachedplayers.size() && i < 100; i++) { - playerlist.add(uncachedplayers.remove()); - } - UUIDFetcher fetcher = new UUIDFetcher(playerlist); - try { - Map result = fetcher.call(); - cacheduuids.putAll(result); - if(first) { - first = false; - }else { - try { - wait(100); - } catch (InterruptedException e1) { - - } - } - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } + UUIDFetcher fetcher = new UUIDFetcher(uncachedplayers); + try { + Map result = fetcher.call(); + cacheduuids.putAll(result); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } for(File user : users) { if(user.isFile()) { String filename = user.getName(); diff --git a/src/uk/co/tggl/pluckerpluck/multiinv/util/UUIDFetcher.java b/src/uk/co/tggl/pluckerpluck/multiinv/util/UUIDFetcher.java index b9f9865..0f35745 100644 --- a/src/uk/co/tggl/pluckerpluck/multiinv/util/UUIDFetcher.java +++ b/src/uk/co/tggl/pluckerpluck/multiinv/util/UUIDFetcher.java @@ -1,56 +1,62 @@ package uk.co.tggl.pluckerpluck.multiinv.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.*; import java.util.concurrent.Callable; public class UUIDFetcher implements Callable> { - 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 static final int MAX_NAMES_PER_REQUEST = 100; + private static final String REQUEST_URL = "https://api.mojang.com/profiles/minecraft"; + private final JSONParser jsonParser = new JSONParser(); private final List names; - public UUIDFetcher(List names) { - this.names = ImmutableList.copyOf(names); + + public UUIDFetcher(Collection names) { + this.names = new ArrayList(names); } public Map call() throws Exception { Map uuidMap = new HashMap(); - 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) { - try { - JSONObject jsonProfile = (JSONObject) profile; + boolean first = true; + + while (this.names.size() > 0) { + String requestBody = this.buildNextRequestBody(); + + HttpURLConnection connection = createConnection(); + writeBody(connection, requestBody); + + try { + JSONArray responseArray = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream())); + + for (Object response : responseArray) { + JSONObject jsonProfile = (JSONObject) response; 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)); + 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); - }catch(Exception e) { - - } + } + } catch (Exception e) { + // Ignore + } + + if (first) { + first = false; + } else { + try { + wait(100); + } catch (InterruptedException e1) { + // Ignore + } } } + return uuidMap; } @@ -61,8 +67,8 @@ private static void writeBody(HttpURLConnection connection, String body) throws writer.close(); } - private static HttpURLConnection createConnection(int page) throws Exception { - URL url = new URL(PROFILE_URL+page); + private static HttpURLConnection createConnection() throws Exception { + URL url = new URL(REQUEST_URL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); @@ -71,16 +77,16 @@ private static HttpURLConnection createConnection(int page) throws Exception { connection.setDoOutput(true); return connection; } - - @SuppressWarnings("unchecked") - private static String buildBody(List names) { - List lookups = new ArrayList(); - for (String name : names) { - JSONObject obj = new JSONObject(); - obj.put("name", name); - obj.put("agent", AGENT); - lookups.add(obj); + + private String buildNextRequestBody() { + List requestNames = new ArrayList(); + int requestSize = Math.min(this.names.size(), MAX_NAMES_PER_REQUEST); + + for (int i = 0; i < requestSize; i++) { + requestNames.add(this.names.get(0)); + this.names.remove(0); } - return JSONValue.toJSONString(lookups); + + return JSONArray.toJSONString(requestNames); } }