Jump to content

Recommended Posts

Posted

Hello, I'm making a messenger mod for the client. The person sending/receiving the message are identified by their Mojang UUID. However, I need to find a way to convert the UUID to the person's username to be shown on the GUI. This is client-side, so I'd like to do it without the MinecraftSessionService in the MinecraftServer. I've tried making a query to https://sessionserver.mojang.com/session/minecraft/profile/ and converting it to a GameProfile using Gson. Doesn't work. I've tried using a HttpProfileRepository to get it. It takes to long to respond, usually fails, and is for converting the username to a UUID. It is really important that I do this. It would be easier to use usernames as IDs, but I'd like to be ready for the name changes :P

 

Here's my user class:

package assets.mcmessenger.client.messenger;

import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.UUID;

import javax.imageio.ImageIO;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.util.ResourceLocation;
import assets.mcmessenger.client.messenger.util.GameProfileCreator;

import com.mojang.authlib.GameProfile;

public class User {

private static final String faceFetchUrl = "http://cravatar.eu/helmavatar/%s";

private String uuid;
private String name;

private ResourceLocation face;

public User(String uuid) {

	this.uuid = uuid;

	if(this.uuid.equals(Minecraft.getMinecraft().getSession().getUsername())) {

		this.setName(Minecraft.getMinecraft().getSession().getUsername());

	} else {

		try {

			GameProfile profile = GameProfileCreator.createGameProfileFromUUID(uuid);
			setName(profile.getName());

		} catch(Exception e) {

			setName(e.toString());

		}

	}

}

public String getName() {

	return this.name;

}

public void setName(String name) {

	this.name = name;

}

public UUID getUUID() {

	return UUID.fromString(uuid);

}

public ResourceLocation getFace() {

	if(this.face == null) {

		BufferedImage image = null;

		try {

			image = ImageIO.read(new URL(String.format(faceFetchUrl, getName())));

		} catch(Exception e) {

			e.printStackTrace();

		}

		DynamicTexture texture = new DynamicTexture(0, 0);

		if(image != null) {

			texture = new DynamicTexture(image);

		}

		this.face = Minecraft.getMinecraft().getTextureManager().getDynamicTextureLocation("face_" + getName(), texture);

	}

	return this.face;

}

public boolean equals(User user) {

	return user.uuid == this.uuid;

}

}

 

Here's my profile creator:

package assets.mcmessenger.client.messenger.util;

import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.UUID;

import org.apache.commons.io.IOUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonReader;
import com.mojang.api.profiles.Profile;
import com.mojang.authlib.GameProfile;

public class GameProfileCreator {

private static final String fetchIdUrl = "https://api.mojang.com/users/profiles/minecraft/%s?at=%s";
private static final String fetchNameUrl = "https://sessionserver.mojang.com/session/minecraft/profile/%s";

public static Profile createGameProfileFromUsername(String username) {

	try {

		Gson gson = new GsonBuilder().setPrettyPrinting().create();
		String url = String.format(fetchIdUrl, username, String.valueOf(System.currentTimeMillis()));
		JsonReader reader = new JsonReader(new InputStreamReader(new URL(url).openStream()));
		reader.setLenient(true);

		return (Profile)gson.fromJson(reader, Profile.class);

	} catch(Exception e) {

		e.printStackTrace();

	}

	Profile profile = new Profile();
	profile.setName(username);
	return profile;

}

public static Profile createGameProfileFromUUID(String uuid) {

	try {

		Gson gson = new GsonBuilder().setPrettyPrinting().create();
		String url = String.format(fetchNameUrl, uuid);
		JsonReader reader = new JsonReader(new InputStreamReader(new URL(url).openStream()));
		reader.setLenient(true);

		return (Profile)gson.fromJson(reader, Profile.class);

	} catch(Exception e) {

		e.printStackTrace();

	}

	Profile profile = new Profile();
	profile.setId(uuid);
	return profile;

}

}

 

If anyone can help, I would be SO grateful! Thank you very much for your time.

- Romejanic

Romejanic

 

Creator of Witch Hats, Explosive Chickens and Battlefield!

Posted

bigteddy98's uuidlib worked pretty well. I used it for a one time, out of game conversion from UUID to name, dunno how fast it would be

it seems to have mostly disappeared, but went like this

 

given uuid as a string, after

 

uuid = uuid.replaceAll("-", ""); // no dashes

 

it went through

	String name = null;
	try {
		URL url = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid);
		URLConnection connection = url.openConnection();
		Scanner jsonScanner = new Scanner(connection.getInputStream(), "UTF-8");
		String json = jsonScanner.next();
		JSONParser parser = new JSONParser();
		Object obj = parser.parse(json);
		name = (String) ((JSONObject) obj).get("name");
		jsonScanner.close();
	} catch (Exception ex) {
		ex.printStackTrace();
	}

 

there's probably some reason this is horrible code or unworkable or something, but it did what I wanted to for converting a bunch of UUIDs to usernames

Posted

bigteddy98's uuidlib worked pretty well. I used it for a one time, out of game conversion from UUID to name, dunno how fast it would be

it seems to have mostly disappeared, but went like this

 

given uuid as a string, after

 

uuid = uuid.replaceAll("-", ""); // no dashes

 

it went through

	String name = null;
	try {
		URL url = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid);
		URLConnection connection = url.openConnection();
		Scanner jsonScanner = new Scanner(connection.getInputStream(), "UTF-8");
		String json = jsonScanner.next();
		JSONParser parser = new JSONParser();
		Object obj = parser.parse(json);
		name = (String) ((JSONObject) obj).get("name");
		jsonScanner.close();
	} catch (Exception ex) {
		ex.printStackTrace();
	}

 

there's probably some reason this is horrible code or unworkable or something, but it did what I wanted to for converting a bunch of UUIDs to usernames

 

Thanks for your response. The initial method didn't work, so I had to change it to this:

 

public static String getName(String uuid) {

	String name = null;

	try {

		URL url = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid);
		BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
		StringBuilder sb = new StringBuilder();
		String line;

		while((line = reader.readLine()) != null) {

			sb.append(line + "\n");

		}

		System.out.println(sb.toString());

		JsonParser parser = new JsonParser();
		JsonElement obj = parser.parse(sb.toString().trim());
		name = obj.getAsJsonObject().get("name").getAsString();
		reader.close();

	} catch (Exception ex) {

		ex.printStackTrace();
		name = ex.toString();

	}

	return name;

}

 

It was saying that I couldn't convert the response to a Json object. I put the print in and the string returning from the server was empty. I pasted the URL in Chrome along with a UUID at the end, and nothing happened. The script doesn't return anything. I just need another source to get the json code :(

 

 

Romejanic

 

Creator of Witch Hats, Explosive Chickens and Battlefield!

Posted

Actually, I misread.  UUID to username, not other way around.

So, use MinecraftServer.getServer().func_152652_a(<uuid>).

If that returns null, use this to request it from the servers.

 

Yeah, but my mod is client-side. I can't use the server. I guess I could make my own instance of the cache, but that might be a little dangerous. I'll try it anyway.

Romejanic

 

Creator of Witch Hats, Explosive Chickens and Battlefield!

Posted

Uhm... why do you need that clientside?! That sounds unusual.

 

I'm making a messaging service, and the message identification is based on UUIDs to support name changes. It has zero involvement with the server, it's all on the client.

Romejanic

 

Creator of Witch Hats, Explosive Chickens and Battlefield!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.