mirror of
https://github.com/ChestShop-authors/ChestShop-3.git
synced 2025-02-17 10:41:26 +01:00
Start preparing for 1.7.6
This commit is contained in:
parent
bc218746ae
commit
4c65fbf9ef
@ -1,7 +1,51 @@
|
||||
package com.Acrobot.Breeze.Utils.MojangAPI;
|
||||
|
||||
/**
|
||||
* @author Andrzej Pomirski (Acrobot)
|
||||
*/
|
||||
public class NameFetcher {
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
public class NameFetcher implements Callable<Map<UUID, String>> {
|
||||
private static final String PROFILE_URL = "https://sessionserver.mojang.com/session/minecraft/profile/";
|
||||
|
||||
private final JSONParser jsonParser = new JSONParser();
|
||||
private final List<UUID> uuids;
|
||||
|
||||
public NameFetcher(UUID... uuids) {
|
||||
this.uuids = ImmutableList.copyOf(uuids);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<UUID, String> call() throws Exception {
|
||||
Map<UUID, String> uuidStringMap = new HashMap<UUID, String>();
|
||||
|
||||
for (UUID uuid: uuids) {
|
||||
HttpURLConnection connection = (HttpURLConnection) new URL(PROFILE_URL+uuid.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");
|
||||
String errorMessage = (String) response.get("errorMessage");
|
||||
|
||||
if (cause != null && !cause.isEmpty()) {
|
||||
throw new IllegalStateException(errorMessage);
|
||||
}
|
||||
|
||||
uuidStringMap.put(uuid, name);
|
||||
}
|
||||
return uuidStringMap;
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,105 @@
|
||||
package com.Acrobot.Breeze.Utils.MojangAPI;
|
||||
|
||||
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.*;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* @author Andrzej Pomirski (Acrobot)
|
||||
* Class adapted from evilmidget38's UUIDFetcher
|
||||
* https://gist.github.com/evilmidget38/a5c971d2f2b2c3b3fb37
|
||||
*/
|
||||
public class UUIDFetcher {
|
||||
|
||||
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(String... names) {
|
||||
this.names = ImmutableList.copyOf(names);
|
||||
}
|
||||
|
||||
public UUIDFetcher(String name) {
|
||||
this.names = ImmutableList.of(name);
|
||||
}
|
||||
|
||||
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 jsonObject = new JSONObject();
|
||||
|
||||
jsonObject.put("name", name);
|
||||
jsonObject.put("agent", AGENT);
|
||||
|
||||
lookups.add(jsonObject);
|
||||
}
|
||||
return JSONValue.toJSONString(lookups);
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,75 @@
|
||||
package com.Acrobot.Breeze.Utils;
|
||||
|
||||
import com.Acrobot.Breeze.Utils.MojangAPI.NameFetcher;
|
||||
import com.Acrobot.Breeze.Utils.MojangAPI.UUIDFetcher;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Created by Andrzej Pomirski (Acrobot)
|
||||
* Provides methods to handle usernames/UUIDs
|
||||
* @author Andrzej Pomirski (Acrobot)
|
||||
*/
|
||||
public class NameUtil {
|
||||
private static final UUID INVALID_UUID = UUID.fromString("00000000-0000-0000-0000-000000000000");
|
||||
private static final String PROFILE_AGENT = "minecraft";
|
||||
|
||||
public static UUID getUUID(Player player) {
|
||||
return player.getUniqueId();
|
||||
}
|
||||
|
||||
public static UUID getUUID(String username) {
|
||||
UUIDFetcher fetcher = new UUIDFetcher(username);
|
||||
|
||||
try {
|
||||
Map<String, UUID> uuidMap = fetcher.call();
|
||||
|
||||
if (uuidMap.size() < 1) {
|
||||
return INVALID_UUID;
|
||||
}
|
||||
|
||||
return uuidMap.get(username);
|
||||
} catch (Exception exception){
|
||||
return INVALID_UUID;
|
||||
}
|
||||
}
|
||||
|
||||
public static Map<String, UUID> getUUID(String... usernames) {
|
||||
UUIDFetcher fetcher = new UUIDFetcher(usernames);
|
||||
|
||||
try {
|
||||
return fetcher.call();
|
||||
} catch (Exception exception){
|
||||
return ImmutableMap.of();
|
||||
}
|
||||
}
|
||||
|
||||
public static String getName(UUID uuid) {
|
||||
NameFetcher fetcher = new NameFetcher(uuid);
|
||||
|
||||
try {
|
||||
Map<UUID, String> uuidMap = fetcher.call();
|
||||
|
||||
return uuidMap.get(uuid);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Map<UUID, String> getName(UUID... uuids) {
|
||||
NameFetcher fetcher = new NameFetcher(uuids);
|
||||
|
||||
try {
|
||||
return fetcher.call();
|
||||
} catch (Exception e) {
|
||||
return ImmutableMap.of();
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isInvalid(UUID uuid) {
|
||||
return uuid.equals(INVALID_UUID);
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,73 @@
|
||||
package com.Acrobot.ChestShop.UUIDs;
|
||||
|
||||
import com.Acrobot.Breeze.Utils.NameUtil;
|
||||
import com.Acrobot.ChestShop.ChestShop;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.Configuration;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Lets you save/cache username and UUID relations
|
||||
* @author Andrzej Pomirski (Acrobot)
|
||||
*/
|
||||
public class UUIDSaver {
|
||||
private static FileConfiguration uuidStorage;
|
||||
|
||||
public static UUID getUUID(Player player) {
|
||||
if (uuidStorage.getString(player.getUniqueId().toString()) == null) {
|
||||
uuidStorage.set(player.getUniqueId().toString(), player.getPlayer());
|
||||
}
|
||||
|
||||
return player.getUniqueId();
|
||||
}
|
||||
|
||||
public static String getUsername(Player player) {
|
||||
if (uuidStorage.getString(player.getUniqueId().toString()) != null) {
|
||||
uuidStorage.set(player.getUniqueId().toString(), player.getName());
|
||||
}
|
||||
|
||||
return uuidStorage.getString(player.getUniqueId().toString());
|
||||
}
|
||||
|
||||
public static String getUsername(final UUID uuid) {
|
||||
String username = uuidStorage.getString(uuid.toString());
|
||||
|
||||
if (username != null) {
|
||||
return username;
|
||||
}
|
||||
|
||||
Bukkit.getScheduler().runTaskAsynchronously(ChestShop.getPlugin(), new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
String name = NameUtil.getName(uuid);
|
||||
|
||||
if (name != null) {
|
||||
uuidStorage.set(uuid.toString(), name);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return uuidStorage.getString(uuid.toString());
|
||||
}
|
||||
|
||||
public static void load() {
|
||||
File uuidStorageFile = ChestShop.loadFile("uuid.storage");
|
||||
uuidStorage = YamlConfiguration.loadConfiguration(uuidStorageFile);
|
||||
}
|
||||
|
||||
public static void save() {
|
||||
File uuidStorageFile = ChestShop.loadFile("uuid.storage");
|
||||
|
||||
try {
|
||||
uuidStorage.save(uuidStorageFile);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user