Add a MojangApiUtil based on the work of @evilmidget38. Thanks man.

This commit is contained in:
Olof Larsson 2014-04-09 01:57:48 +02:00
parent 33d1de2e13
commit ebb5cbe74a
2 changed files with 133 additions and 39 deletions

View File

@ -2,13 +2,10 @@ package com.massivecraft.mcore;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
@ -33,8 +30,6 @@ import com.massivecraft.mcore.store.Coll;
import com.massivecraft.mcore.store.ExamineThread;
import com.massivecraft.mcore.teleport.EngineScheduledTeleport;
import com.massivecraft.mcore.util.PlayerUtil;
import com.massivecraft.mcore.util.TimeDiffUtil;
import com.massivecraft.mcore.util.TimeUnit;
import com.massivecraft.mcore.xlib.bson.types.ObjectId;
import com.massivecraft.mcore.xlib.gson.Gson;
import com.massivecraft.mcore.xlib.gson.GsonBuilder;
@ -177,19 +172,7 @@ public class MCore extends MPlugin
VaultFeatures.get()
);
/*
test("");
test("+1day");
test("1day");
test("1 day");
test("-1day");
test("1week4d");
test("+1week-4d");
test("day");
test("1month");
test("1months");
test("1months2ms");
*/
// test();
// Delete Files (at once and additionally after all plugins loaded)
TaskDeleteFiles.get().run();
@ -198,37 +181,24 @@ public class MCore extends MPlugin
this.postEnable();
}
public void test(String diffString)
public void test()
{
log("===========================");
log("Testing Diff String \""+diffString+"\":");
log("===========================");
log("===========================");
try
{
Map<TimeUnit, Long> unitcounts = TimeDiffUtil.unitcounts(diffString);
for (Entry<TimeUnit, Long> entry : unitcounts.entrySet())
{
System.out.println(entry.getValue()+": "+entry.getKey());
}
System.out.println("---");
long millis = TimeDiffUtil.millis(unitcounts);
log("millis: "+millis);
String verboose = ChatColor.stripColor(TimeDiffUtil.formatedVerboose(unitcounts));
String minimal = ChatColor.stripColor(TimeDiffUtil.formatedMinimal(unitcounts));
log("verboose: "+verboose);
log("minimal: "+minimal);
long millisRec = TimeDiffUtil.millis(minimal);
log("millisRec: "+millisRec);
log("matches: "+(millis == millisRec));
// whatever you fee like
}
catch (Exception e)
{
e.printStackTrace();
}
log("===========================");
log("===========================");
log("===========================");
}
@Override

View File

@ -0,0 +1,124 @@
package com.massivecraft.mcore.util;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.JSONParser;
/**
* Many thanks to evilmidget38!
* This utility class is based on his work.
* http://forums.bukkit.org/threads/player-name-uuid-fetcher.250926/
*/
public class MojangApiUtil
{
// -------------------------------------------- //
// NAME --> ID
// -------------------------------------------- //
// The player names you supply does not have to use correct capitalization.
// In the map returned however, the names will have correction capitalization.
public static Map<String, UUID> getPlayerIds(Collection<String> playerNames) throws Exception
{
Map<String, UUID> ret = new HashMap<String, UUID>();
JSONParser jsonParser = new JSONParser();
String body = createBody(playerNames);
for (int i = 1; i < 100; i++)
{
HttpURLConnection connection = createConnection(i);
writeBody(connection, body);
JSONObject jsonObject = (JSONObject) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
JSONArray profiles = (JSONArray) jsonObject.get("profiles");
Number count = (Number) jsonObject.get("size");
if (count.intValue() == 0)
{
break;
}
for (Object profile : profiles)
{
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));
ret.put(name, uuid);
}
}
return ret;
}
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("https://api.mojang.com/profiles/page/" + 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 createBody(Collection<String> playerNames)
{
List<JSONObject> lookups = new ArrayList<JSONObject>();
for (String playerName : playerNames)
{
JSONObject obj = new JSONObject();
obj.put("name", playerName);
obj.put("agent", "minecraft");
lookups.add(obj);
}
return JSONValue.toJSONString(lookups);
}
// -------------------------------------------- //
// ID --> NAME
// -------------------------------------------- //
public static Map<UUID, String> getPlayerNames(Collection<UUID> playerIds) throws Exception
{
Map<UUID, String> ret = new HashMap<UUID, String>();
JSONParser jsonParser = new JSONParser();
for (UUID playerId: playerIds)
{
HttpURLConnection connection = (HttpURLConnection) new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + playerId.toString().replace("-", "")).openConnection();
JSONObject response = (JSONObject) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
String name = (String) response.get("name");
if (name == null)
{
continue;
}
String cause = (String) response.get("cause");
if (cause != null && cause.length() > 0)
{
String errorMessage = (String) response.get("errorMessage");
throw new IllegalStateException(errorMessage);
}
ret.put(playerId, name);
}
return ret;
}
}