initial commit
This commit is contained in:
59
src/main/java/cc/fascinated/mojang/MojangAPIService.java
Normal file
59
src/main/java/cc/fascinated/mojang/MojangAPIService.java
Normal file
@ -0,0 +1,59 @@
|
||||
package cc.fascinated.mojang;
|
||||
|
||||
import cc.fascinated.Main;
|
||||
import cc.fascinated.mojang.types.MojangApiProfile;
|
||||
import cc.fascinated.mojang.types.MojangSessionServerProfile;
|
||||
import lombok.SneakyThrows;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
|
||||
@Service
|
||||
public class MojangAPIService {
|
||||
|
||||
private static final HttpClient CLIENT = HttpClient.newHttpClient();
|
||||
|
||||
@Value("${mojang.session-server}")
|
||||
private String mojangSessionServerUrl;
|
||||
|
||||
@Value("${mojang.api}")
|
||||
private String mojangApiUrl;
|
||||
|
||||
/**
|
||||
* Gets the Session Server profile of the player with the given UUID.
|
||||
*
|
||||
* @param id the uuid or name of the player
|
||||
* @return the profile
|
||||
*/
|
||||
@SneakyThrows
|
||||
public MojangSessionServerProfile getSessionServerProfile(String id) {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI(mojangSessionServerUrl + "/session/minecraft/profile/" + id))
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = CLIENT.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
return Main.getGSON().fromJson(response.body(), MojangSessionServerProfile.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Mojang API profile of the player with the given UUID.
|
||||
*
|
||||
* @param id the name of the player
|
||||
* @return the profile
|
||||
*/
|
||||
@SneakyThrows
|
||||
public MojangApiProfile getApiProfile(String id) {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI(mojangApiUrl + "/users/profiles/minecraft/" + id))
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = CLIENT.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
return Main.getGSON().fromJson(response.body(), MojangApiProfile.class);
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package cc.fascinated.mojang.types;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class MojangApiProfile {
|
||||
|
||||
private String id;
|
||||
private String name;
|
||||
|
||||
public MojangApiProfile() {}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package cc.fascinated.mojang.types;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Getter @ToString
|
||||
public class MojangSessionServerProfile {
|
||||
|
||||
/**
|
||||
* The UUID of the player.
|
||||
*/
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* The name of the player.
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* The properties for the player.
|
||||
*/
|
||||
private final List<MojangSessionServerProfileProperties> properties = new ArrayList<>();
|
||||
|
||||
public MojangSessionServerProfile() {}
|
||||
|
||||
/**
|
||||
* Get the texture property for the player.
|
||||
*
|
||||
* @return the texture property
|
||||
*/
|
||||
public MojangSessionServerProfileProperties getTextureProperty() {
|
||||
for (MojangSessionServerProfileProperties property : properties) {
|
||||
if (property.getName().equals("textures")) {
|
||||
return property;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package cc.fascinated.mojang.types;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
|
||||
@Getter @ToString
|
||||
public class MojangSessionServerProfileProperties {
|
||||
private String name;
|
||||
private String value;
|
||||
|
||||
public MojangSessionServerProfileProperties() {}
|
||||
}
|
Reference in New Issue
Block a user