Skip to content

Instantly share code, notes, and snippets.

@effective-light
Forked from natemort/UUIDFetcher.java
Last active June 20, 2017 07:12
Show Gist options
  • Select an option

  • Save effective-light/1adb2957851b8e5da7423662ba0b2430 to your computer and use it in GitHub Desktop.

Select an option

Save effective-light/1adb2957851b8e5da7423662ba0b2430 to your computer and use it in GitHub Desktop.
UUIDFetcher v3 Utilizing Mojang's new profiles API.
/*
* Copyright (c) 2015 Nate Mortensen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.evilmidget38;
import com.google.gson.Gson;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
import java.util.concurrent.Callable;
public class ProfileFetcher implements Callable<Map<String, ProfileFetcher.GameProfile>>
{
private static final double PROFILES_PER_REQUEST = 100;
private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft";
private static final Gson gson = new Gson();
private final List<String> names;
private final boolean rateLimiting;
public ProfileFetcher(List<String> names, boolean rateLimiting)
{
this.names = Collections.unmodifiableList( names );
this.rateLimiting = rateLimiting;
}
public ProfileFetcher(List<String> names)
{
this( names, true );
}
@Override
public Map<String, GameProfile> call() throws Exception
{
Map<String, GameProfile> profileMap = new HashMap<>();
int requests = ( int ) Math.ceil( names.size() / PROFILES_PER_REQUEST );
for ( int i = 0; i < requests; i++ )
{
HttpURLConnection connection = createConnection();
String body = gson.toJson( names.subList( i * 100, Math.min( ( i + 1 ) * 100, names.size() ) ) );
writeBody( connection, body );
GameProfile[] array = gson.fromJson( new InputStreamReader( connection.getInputStream() ), GameProfile[].class );
for ( GameProfile profile : array )
{
profileMap.put( profile.getName(), profile );
profile.setUuid( getUUID( profile.getId() ) );
}
if ( rateLimiting && i != requests - 1 )
{
Thread.sleep( 100L );
}
}
return profileMap;
}
private static void writeBody(HttpURLConnection connection, String body) throws Exception
{
OutputStream stream = connection.getOutputStream();
stream.write( body.getBytes() );
stream.flush();
stream.close();
}
private static HttpURLConnection createConnection() throws Exception
{
URL url = new URL( PROFILE_URL );
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;
}
public static byte[] toBytes(UUID uuid)
{
ByteBuffer byteBuffer = ByteBuffer.wrap( new byte[ 16 ] );
byteBuffer.putLong( uuid.getMostSignificantBits() );
byteBuffer.putLong( uuid.getLeastSignificantBits() );
return byteBuffer.array();
}
public static UUID fromBytes(byte[] array)
{
if ( array.length != 16 )
{
throw new IllegalArgumentException( "Illegal byte array length: " + array.length );
}
ByteBuffer byteBuffer = ByteBuffer.wrap( array );
long mostSignificant = byteBuffer.getLong();
long leastSignificant = byteBuffer.getLong();
return new UUID( mostSignificant, leastSignificant );
}
private static UUID getUUID(String id)
{
return UUID.fromString( id.substring( 0, 8 ) + "-" + id.substring( 8, 12 ) + "-" + id.substring( 12, 16 )
+ "-" + id.substring( 16, 20 ) + "-" + id.substring( 20, 32 ) );
}
public static GameProfile getProfileOf(String name) throws Exception
{
return new ProfileFetcher( Collections.singletonList( name ) ).call().get( name );
}
public class GameProfile
{
private UUID uuid;
private String id;
private String name;
boolean legacy;
boolean demo;
private void setUuid(UUID uuid)
{
this.uuid = uuid;
}
private String getId()
{
return id;
}
public UUID getUuid()
{
return uuid;
}
public String getName()
{
return name;
}
public boolean isLegacy()
{
return legacy;
}
public boolean isDemo()
{
return demo;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment