Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow URL argument in setSkullTexture() #213

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Collection;
import java.util.UUID;
import java.util.function.Function;
Expand Down Expand Up @@ -118,17 +120,25 @@ public static String getTexture(@NonNull ItemStack itemStack) {
* Set the Base64 texture of the given skull ItemStack.
*
* @param itemStack The ItemStack.
* @param texture The new skull texture (Base64).
* @param texture The new skull texture (Base64 or URL).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not really "or URL". It can either be a very specific URL: http://textures.minecraft.net/texture/ or a hexidecimal id? of a texture on the same site. Just mentioning URL can be misleading here.

*/
public static void setSkullTexture(@NonNull ItemStack itemStack, @NonNull String texture) {
try {
ItemMeta meta = itemStack.getItemMeta();
if (meta instanceof SkullMeta) {
GameProfile profile = new GameProfile(UUID.randomUUID(), "");
Property property = new Property("textures", texture);

PropertyMap properties = profile.getProperties();
properties.put("textures", property);
String json = Base64.getDecoder().decode(texture);
EvModder marked this conversation as resolved.
Show resolved Hide resolved
if(!json.contains("\"url\":\"")){
// Decoded json does not contain a skin URL, so check if the 'texture' argument is a URL.
if(texture.startsWith("http://")); // We are all set.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is problematic as it ignores the possibility of a https:// url. So either also check if it starts with https:// or maybe just check if it starts with http (Tho that could be a bit to vague)

Copy link
Author

@EvModder EvModder May 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, but Minecraft's skin server only uses http://. They could change in the future, but currently the game won't render skins that aren't hosted under Minecraft's website, so I didn't bother for https:// (since no existing valid skin will have it).

The old HD skins (super-high resolution heads, 640x640 etc) from the Education Edition website used https://, but when Minecraft removed that they did so on their server end, so it applies to ALL versions of Minecraft

To be flexible in case they start re-allowing that in the future, could do "http" as you suggest. It's a bit more vague, but yeah likely not an issue

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I also find a bit problematic is, how this setup could allow any URL to work as a skin. While I doubt it will work, do I feel like it's not a good idea to leave it up to the server to do the cleanup here...

It just feels... odd.

Copy link
Author

@EvModder EvModder May 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, I'll update the check to be more thorough (by checking for the full Minecraft skin server URL)

(Note that the same criticism could be used against the existing Base64 codepath)

// Check if the texture is a hexidecimal (like the skinfiles on Minecraft's servers)
else if(texture.matches("^[0-9a-fA-F]+$")) texture = "http://textures.minecraft.net/texture/"+texture;
else; // TODO: Texture invalid, print a warning or something?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't log anything. The method is called frequently and we don't want it to spam the console.

json = "{\"textures\":{\"SKIN\":{\"url\":\""+texture+"\"}}}"; // (Stable for all MC versions with custom skins.)
texture = Base64.getEncoder().encodeToString(json.getBytes(StandardCharsets.ISO_8859_1));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After you move the if block into the try block, the entirety of this "texture-modification" logic should be moved into a private method. By moving it out, we simplify this method and make everything easier to read. The private method could be, for example:

private String normalizeTextureURL(String texture) {
    // ...
}

profile.getProperties().put("textures", new Property("textures", texture));

if (SET_PROFILE_METHOD == null && !INITIALIZED) {
try {
Expand Down