JEFF Media Developer Blog

Java & Spigot development

Menu
  • Blog
  • Main Website
  • Privacy Policy
Menu

Creating custom heads in Spigot 1.18.1+

Posted on August 1, 2022April 7, 2023 by mfnalex

Spigot 1.18.1 added the new PlayerProfiles class, which finally allows us to use custom heads without needing any reflection! You can obtain them as normal items, or actually place them down into the world. I’ll show you how both works:

Creating a new PlayerProfile

First, we gotta create a new PlayerProfile object. To do so, we only need a UUID. We’re just putting this into a new method so we can reuse it later:

private static final UUID RANDOM_UUID = UUID.fromString("92864445-51c5-4c3b-9039-517c9927d1b4"); // We reuse the same "random" UUID all the time

private static PlayerProfile getProfile(String url) {
    PlayerProfile profile = Bukkit.createPlayerProfile(RANDOM_UUID); // Get a new player profile
    PlayerTextures textures = profile.getTextures();
    URL urlObject;
    try {
        urlObject = new URL(url); // The URL to the skin, for example: https://textures.minecraft.net/texture/18813764b2abc94ec3c3bc67b9147c21be850cdf996679703157f4555997ea63a
    } catch (MalformedURLException exception) {
        throw new RuntimeException("Invalid URL", exception);
    }
    textures.setSkin(urlObject); // Set the skin of the player profile to the URL
    profile.setTextures(textures); // Set the textures back to the profile
    return profile;
}

Turning a PlayerProfile into an ItemStack

Let’s just write a tiny command, that gives the player a “gay zombie” head whenever they run the command. Please note that normally, you shouldn’t blindly cast the CommandSender to Player, but you already know that.

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    Player player = (Player) sender;
    PlayerProfile profile = getProfile("https://textures.minecraft.net/texture/18813764b2abc94ec3c3bc67b9147c21be850cdf996679703157f4555997ea63");
    System.out.println(profile.getTextures().getSkin().toString());
    ItemStack head = new ItemStack(Material.PLAYER_HEAD);
    SkullMeta meta = (SkullMeta) head.getItemMeta();
    meta.setOwnerProfile(profile); // Set the owning player of the head to the player profile
    head.setItemMeta(meta);
    player.getInventory().addItem(head);
    return true;
}
It works! Here’s our “gay zombie” head 🙂

Important: Please notice that were are NOT using base64 values for the skin, but the usual URl to the skin file. You can obtain these links for example here. (Just click on any Skin, then copy/paste the part at the bottom where it says Minecraft-URL. You have to copy the full URL, including the http://textures.minecraft.net part.)

If you only have access to the base64 string of your skin, then you’ll have to parse it manually. For example, you can just get your system’s Base64Decoder, then pass your string. It will now be something like this:

{"textures":{"SKIN":{"url":"http://textures.minecraft.net/texture/3453b43182ba4f470f42f237dbc081bc5523beec232989bc6f9d6557926ef480"}}}

You now need to extract the “url” part. You could for sample just parse this a json, or, even simpler – simply substring the stuff you don’t need:

public static URL getUrlFromBase64(String base64) throws MalformedURLException {
    String decoded = new String(Base64.getDecoder().decode(base64));
    // We simply remove the "beginning" and "ending" part of the JSON, so we're left with only the URL. You could use a proper
    // JSON parser for this, but that's not worth it. The String will always start exactly with this stuff anyway
    return new URL(decoded.substring("{\"textures\":{\"SKIN\":{\"url\":\"".length(), decoded.length() - "\"}}}".length()));
}

Placing a custom head in the world

Placing your skull as actual block is equally easy. Just get the block’s “Skull” BlockState, then set the owning profile. Do not forget to update the BlockState afterwards!

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    Player player = (Player) sender;
    PlayerProfile profile = getProfile("https://textures.minecraft.net/texture/b72afb31e8f49ff90d49127d0562c19ff07ddc0bba1f7d82cd150718df233f29");
    System.out.println(profile.getTextures().getSkin().toString());
    Block block = player.getTargetBlockExact(100, FluidCollisionMode.NEVER);
    block.setType(Material.PLAYER_HEAD);
    Skull skull = (Skull) block.getState();
    skull.setOwnerProfile(profile);
    skull.update(false);
    return true;
}

What to do on outdated versions like 1.18 and lower?

If you’re using outdated Spigot versions, then you indeed need reflection to create custom heads. I won’t give any support for ancient versions though, but you can just check out my SkullUtils class from my JeffLib library. It supports 1.16.1+, however it’s using the base64 Strings instead of directly using the proper URL:

Join my Discord Server for feedback or support. Just check out the channel #programming-help 🙂

2 thoughts on “Creating custom heads in Spigot 1.18.1+”

  1. Wihy says:
    July 28, 2023 at 1:11 pm

    Here’s a method combining the two without using deprecated method (Paper API)

    public static ItemStack getHead(String headID) {
    PlayerProfile profile = Bukkit.createProfile(UUID.fromString(“92864445-51c5-4c3b-9039-517c9927d1b4”));
    PlayerTextures textures = profile.getTextures();

    try {
    textures.setSkin(URI.create(“https://textures.minecraft.net/texture/” + headID).toURL());
    } catch (MalformedURLException exception) {
    throw new RuntimeException(“Invalid URL”, exception);
    }

    profile.setTextures(textures);

    ItemStack head = new ItemStack(Material.PLAYER_HEAD);
    SkullMeta meta = (SkullMeta) head.getItemMeta();
    meta.setPlayerProfile(profile);
    head.setItemMeta(meta);

    return head;
    }

    Reply
    1. mfnalex says:
      July 31, 2023 at 4:39 pm

      Hi, I am unsure what you are talking about – my original code also does not use any deprecated methods, does it?

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • Don’t disable dependency-reduced-pom.xml
  • Maven Multi-Module setup for supporting different NMS versions
  • How to read or block Spigot’s console output
  • Why IntelliJ complains about your pom.xml file
  • How to make maven automatically put your plugin’s .jar into your test server’s plugins folder

Recent Comments

  1. mfnalex on Obfuscate plugins using allatori
  2. Mareczek on Obfuscate plugins using allatori
  3. mfnalex on Creating custom heads in Spigot 1.18.1+
  4. Wihy on Creating custom heads in Spigot 1.18.1+
  5. not oliver193 on Use Consumers when spawning custom entities

Archives

  • August 2023
  • July 2023
  • June 2023
  • February 2023
  • January 2023
  • October 2022
  • August 2022
  • March 2022
  • December 2021
  • October 2021
  • August 2021
©2023 JEFF Media Developer Blog
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT