From ecd0664d6648a351b7c581cedcdb27a68dfde333 Mon Sep 17 00:00:00 2001 From: ryanhamshire Date: Thu, 8 Jan 2015 19:40:08 -0800 Subject: [PATCH] UUID conversion reliability improvement. From bug reports, seems the max batch size might have been changed by Mojang. Can't find any docs, so the code now starts with 100 per batch and goes smaller with the first batch until it succeeds. --- .../GriefPrevention/UUIDFetcher.java | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/me/ryanhamshire/GriefPrevention/UUIDFetcher.java b/src/me/ryanhamshire/GriefPrevention/UUIDFetcher.java index 69c881f..dd5f0cc 100644 --- a/src/me/ryanhamshire/GriefPrevention/UUIDFetcher.java +++ b/src/me/ryanhamshire/GriefPrevention/UUIDFetcher.java @@ -16,7 +16,7 @@ import java.nio.ByteBuffer; import java.util.*; class UUIDFetcher { - private static final double PROFILES_PER_REQUEST = 100; + private static int 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 names; @@ -90,15 +90,14 @@ class UUIDFetcher { { GriefPrevention.AddLogEntry("Calling Mojang to get UUIDs for remaining unresolved players (this is the slowest step)..."); - int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST); - for (int i = 0; i < requests; i++) + for (int i = 0; i * PROFILES_PER_REQUEST < names.size(); i++) { boolean retry = false; JSONArray array = null; do { HttpURLConnection connection = createConnection(); - String body = JSONArray.toJSONString(names.subList(i * 100, Math.min((i + 1) * 100, names.size()))); + String body = JSONArray.toJSONString(names.subList(i * PROFILES_PER_REQUEST, Math.min((i + 1) * PROFILES_PER_REQUEST, names.size()))); writeBody(connection, body); retry = false; array = null; @@ -112,8 +111,22 @@ class UUIDFetcher { if(e.getMessage().contains("429")) { retry = true; - GriefPrevention.AddLogEntry("Mojang says we're sending requests too fast. Will retry every 30 seconds until we succeed..."); - Thread.sleep(30000); + + //if this is the first time we're sending anything, the batch size must be too big + //try reducing it + if(i == 0 && PROFILES_PER_REQUEST > 1) + { + GriefPrevention.AddLogEntry("Batch size " + PROFILES_PER_REQUEST + " seems too large. Looking for a workable batch size..."); + PROFILES_PER_REQUEST = Math.max(PROFILES_PER_REQUEST - 5, 1); + } + + //otherwise, keep the batch size which has worked for previous iterations + //but wait a little while before trying again. + else + { + GriefPrevention.AddLogEntry("Mojang says we're sending requests too fast. Will retry every 30 seconds until we succeed..."); + Thread.sleep(30000); + } } else { @@ -131,7 +144,7 @@ class UUIDFetcher { lookupCache.put(name, uuid); lookupCache.put(name.toLowerCase(), uuid); } - if (rateLimiting && i != requests - 1) { + if (rateLimiting) { Thread.sleep(200L); } }