Ir para conteúdo

[API] UUIDFetcher


Posts Recomendados

Postado

Olá, vim trazer para vocês uma api, ela consiste em pegar a uuid real do jogador, ex: "Quero pegar a uuid de um jogador offline, ó senhor, quem irá me defender?", kk brincadeiras a parte ela acessa a api da mojang e pega esta uuid, só não vai tentar usar isto em servidor pirata né filhão.

 

 

 

  1. public class UUIDUtils implements Callable<Map<String, UUID>> {
  2.         private static final double PROFILES_PER_REQUEST = 100;
  3.         private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft";
  4.         private final JSONParser jsonParser = new JSONParser();
  5.         private final List<String> names;
  6.         private final boolean rateLimiting;
  7.  
  8.         public UUIDUtils(List<String> names, boolean rateLimiting) {
  9.                 this.names = ImmutableList.copyOf(names);
  10.                 this.rateLimiting = rateLimiting;
  11.         }
  12.  
  13.         public UUIDUtils(List<String> names) {
  14.                 this(names, true);
  15.         }
  16.  
  17.         public Map<String, UUID> call() throws Exception {
  18.                 Map<String, UUID> uuidMap = new HashMap<String, UUID>();
  19.                 int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST);
  20.                 for (int i = 0; i < requests; i++) {
  21.                         HttpURLConnection connection = createConnection();
  22.                         String body = JSONArray.toJSONString(names.subList(i * 100,
  23.                                         Math.min((i + 1) * 100, names.size())));
  24.                         writeBody(connection, body);
  25.                         JSONArray array = (JSONArray) jsonParser
  26.                                         .parse(new InputStreamReader(connection.getInputStream()));
  27.                         for (Object profile : array) {
  28.                                 JSONObject jsonProfile = (JSONObject) profile;
  29.                                 String id = (String) jsonProfile.get("id");
  30.                                 String name = (String) jsonProfile.get("name");
  31.                                 UUID uuid = UUIDUtils.getUUID(id);
  32.                                 uuidMap.put(name, uuid);
  33.                         }
  34.                         if (rateLimiting && i != requests - 1) {
  35.                                 Thread.sleep(100L);
  36.                         }
  37.                 }
  38.                 return uuidMap;
  39.         }
  40.  
  41.         private static void writeBody(HttpURLConnection connection, String body)
  42.                         throws Exception {
  43.                 OutputStream stream = connection.getOutputStream();
  44.                 stream.write(body.getBytes());
  45.                 stream.flush();
  46.                 stream.close();
  47.         }
  48.  
  49.         private static HttpURLConnection createConnection() throws Exception {
  50.                 URL url = new URL(PROFILE_URL);
  51.                 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  52.                 connection.setRequestMethod("POST");
  53.                 connection.setRequestProperty("Content-Type", "application/json");
  54.                 connection.setUseCaches(false);
  55.                 connection.setDoInput(true);
  56.                 connection.setDoOutput(true);
  57.                 return connection;
  58.         }
  59.  
  60.         private static UUID getUUID(String id) {
  61.                 return UUID.fromString(id.substring(0, 8) + "-" + id.substring(8, 12)
  62.                                 + "-" + id.substring(12, 16) + "-" + id.substring(16, 20) + "-"
  63.                                 + id.substring(20, 32));
  64.         }
  65.  
  66.         public static byte[] toBytes(UUID uuid) {
  67.                 ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]);
  68.                 byteBuffer.putLong(uuid.getMostSignificantBits());
  69.                 byteBuffer.putLong(uuid.getLeastSignificantBits());
  70.                 return byteBuffer.array();
  71.         }
  72.  
  73.         public static UUID fromBytes(byte[] array) {
  74.                 if (array.length != 16) {
  75.                         throw new IllegalArgumentException("Illegal byte array length: "
  76.                                         + array.length);
  77.                 }
  78.                 ByteBuffer byteBuffer = ByteBuffer.wrap(array);
  79.                 long mostSignificant = byteBuffer.getLong();
  80.                 long leastSignificant = byteBuffer.getLong();
  81.                 return new UUID(mostSignificant, leastSignificant);
  82.         }
  83.  
  84.         public static UUID getUUIDOf(String name) throws Exception {
  85.                 return new UUIDUtils(Arrays.asList(name)).call().get(name);
  86.         }
  87. }

 

 

Ou se quiser Clique Aqui

 

Não vou ensinar a utilizar pois somente os que entendem um pouco mais vão conseguir utilizar, e aposto que já sabem pelo menos entender o código

 

*Lembrando que não fui eu que criei o código, por mais que seja um código fácil envolvendo JSON não fui eu que criei. Aconselho também usa-lo em threads para que o servidor não fique esperando a resposta e possa assim causar algum lag.

Guest Henrique
Postado

O Bukkit tem uma interface chamada OfflinePlayer e ela tem essas funções

Postado

O Bukkit tem uma interface chamada OfflinePlayer e ela tem essas funções

Só que ela não retorna a uuid real do jogador, ela nunca vai retornar algo nulo, mas retorna uma uuid fake vamos assim dizer. Se quiser faça um teste pra ver

Postado
ANÁLISE DE INATIVIDADE
 

Este tópico foi automaticamente arquivado devido à inatividade. Para manter a organização e fluidez das discussões na comunidade, tópicos que permanecem sem novas interações por um período prolongado são fechados automaticamente.
 

Se houver interesse em retomar o conteúdo, sinta-se à vontade para criar um novo tópico ou entre em contato com a equipe da comunidade para reabrir este tópico.


Equipe de Moderação
Gamer's Board
Visitante
Este tópico está impedido de receber novos posts.
×
×
  • Criar Novo...