set player head to a texture

Discussion in 'Plugin Development' started by Cat 700, Mar 14, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    Cat 700

    so I want to set a itemstack of a player head to this SkullOwner:{Id:"198765c1-c236-4c7d-8555-d0dbacfcb123",Properties:{textures:[{Value:"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMTc2MmExNWIwNDY5MmEyZTRiM2ZiMzY2M2JkNGI3ODQzNGRjZTE3MzJiOGViMWM3YTlmN2MwZmJmNmYifX19"}]}}

    Code:
    ItemStack bottle = new ItemStack(Material.PLAYER_HEAD);
            net.minecraft.server.v1_15_R1.ItemStack nmsbottle = CraftItemStack.asNMSCopy(bottle);
            NBTTagCompound bottlecompound = (nmsbottle.hasTag()) ? nmsbottle.getTag() : new NBTTagCompound();
            bottlecompound.setString("SkullOwner:{Id:\"198765c1-c236-4c7d-8555-d0dbacfcb123\",Properties:{textures:[{Value:\"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMTc2MmExNWIwNDY5MmEyZTRiM2ZiMzY2M2JkNGI3ODQzNGRjZTE3MzJiOGViMWM3YTlmN2MwZmJmNmYifX19\"}]}}, id", "minecraft:skull");
            //bottlecompound.setString("SkullOwner:{Id:\"198765c1-c236-4c7d-8555-d0dbacfcb123\",Properties:{textures:[{Value:\"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMTc2MmExNWIwNDY5MmEyZTRiM2ZiMzY2M2JkNGI3ODQzNGRjZTE3MzJiOGViMWM3YTlmN2MwZmJmNmYifX19\"}]}}, MythicCrafter", nbt);
            nmsbottle.setTag(bottlecompound);
            ShapedRecipe expBottle = new ShapedRecipe(CraftItemStack.asBukkitCopy(nmsbottle));
            expBottle.shape("   "," B ","   ");
            expBottle.setIngredient('B', Material.GLASS_BOTTLE);
            getServer().addRecipe(expBottle);
    I tried doing nbt with the code above except the code above outputs 'SkullOwner:{Id:"198765c1-c236-4c7d-8555-d0dbacfcb123",Properties:{textures:[{Value:"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMTc2MmExNWIwNDY5MmEyZTRiM2ZiMzY2M2JkNGI3ODQzNGRjZTE3MzJiOGViMWM3YTlmN2MwZmJmNmYifX19"}]}}, id': "minecraft:skull"

    if there is a way to use it without nbt that would be helpful I found some stuff about itemmeta but didn't completely understand it and I think it was only for mob heads
     
  2. Offline

    Tango_

    Have you looked into SkullMeta#setOwningPlayer(OfflinePlayer) ?
     
  3. Offline

    Cat 700

    I don't know the owner of the head I got it from a website
     
  4. Offline

    Tango_

    You can alternatively do this using Base64 skulls, do you know the website you got the skull ID from?

    Here is a method I made for 1.15 to get a Skull ItemStack using the Base64 ID

    Code:
    public ItemStack getCustomSkull(String url) {
    
            ItemStack head = new ItemStack(Material.PLAYER_HEAD);
            if (url.isEmpty()) return head;
    
            SkullMeta skullMeta = (SkullMeta) head.getItemMeta();
            GameProfile profile = new GameProfile(UUID.randomUUID(), null);
    
            profile.getProperties().put("textures", new Property("textures", url));
    
            try {
                Method mtd = skullMeta.getClass().getDeclaredMethod("setProfile", GameProfile.class);
                mtd.setAccessible(true);
                mtd.invoke(skullMeta, profile);
            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
                ex.printStackTrace();
            }
    
            head.setItemMeta(skullMeta);
            return head;
        }
    If you enter the long 'Value' parameter from your NBT string, it should return the head you found.
     
    Last edited: Mar 14, 2020
  5. Offline

    Cat 700

    thank you

    @Tango_ your code isn't working the return says that void can't return. and getCustomSkull says
    Multiple markers at this line
    - Illegal modifier for parameter getCustomSkull; only final is
    permitted
    - Syntax error on token ")", ; expected
    - Syntax error on token "(", ; expected
     
    Last edited: Mar 14, 2020
  6. Offline

    Tango_

    Can you post your whole code?
     
  7. Offline

    Cat 700

    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.UUID;
    import org.bukkit.Material;
    import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.SkullMeta;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    import com.mojang.authlib.GameProfile;
    import com.mojang.authlib.properties.Property;
    import net.minecraft.server.v1_15_R1.NBTTagCompound;
    public class mythics extends JavaPlugin {

    @SuppressWarnings("deprecation")
    public void onEnable() {
    getServer().getPluginManager().registerEvents(new teleportBow(), (Plugin)this);
    getServer().getPluginManager().registerEvents(new mythicCrafter(), (Plugin)this);

    ItemStack bottle = new ItemStack(Material.PLAYER_HEAD);
    net.minecraft.server.v1_15_R1.ItemStack nmsbottle = CraftItemStack.asNMSCopy(bottle);
    NBTTagCompound bottlecompound = (nmsbottle.hasTag()) ? nmsbottle.getTag() : new NBTTagCompound();
    //bottlecompound.setString("SkullOwner:{Id:\"198765c1-c236-4c7d-8555-d0dbacfcb123\",Properties:{textures:[{Value:\"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMTc2MmExNWIwNDY5MmEyZTRiM2ZiMzY2M2JkNGI3ODQzNGRjZTE3MzJiOGViMWM3YTlmN2MwZmJmNmYifX19\"}]}}, id", "minecraft:skull");
    //bottlecompound.setString("SkullOwner:{Id:\"198765c1-c236-4c7d-8555-d0dbacfcb123\",Properties:{textures:[{Value:\"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMTc2MmExNWIwNDY5MmEyZTRiM2ZiMzY2M2JkNGI3ODQzNGRjZTE3MzJiOGViMWM3YTlmN2MwZmJmNmYifX19\"}]}}, MythicCrafter", nbt);
    nmsbottle.setTag(bottlecompound);
    ShapedRecipe expBottle = new ShapedRecipe(CraftItemStack.asBukkitCopy(nmsbottle));
    expBottle.shape(" "," B "," ");
    expBottle.setIngredient('B', Material.GLASS_BOTTLE);
    getServer().addRecipe(expBottle);




    public ItemStack getCustomSkull(String headURL) {
    ItemStack head = new ItemStack(Material.PLAYER_HEAD);
    if (headURL.isEmpty()) return head;
    SkullMeta skullMeta = (SkullMeta) head.getItemMeta();
    GameProfile profile = new GameProfile(UUID.randomUUID(), null);
    profile.getProperties().put("textures", new Property("textures", headURL));
    try {
    Method mtd = skullMeta.getClass().getDeclaredMethod("setProfile", GameProfile.class);
    mtd.setAccessible(true);
    mtd.invoke(skullMeta, profile);
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
    ex.printStackTrace();
    }
    head.setItemMeta(skullMeta);
    return head;
    }
    }
    }
     
  8. Offline

    Tango_

    Put my method outside of the OnEnable method and be sure to use the '@Override' annotation above the onEnable method

    You can then get your head by doing this:
    Example (open)
    Code:
    ItemStack skull = getCustomSkull("eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMTc2MmExNWIwNDY5MmEyZTRiM2ZiMzY2M2JkNGI3ODQzNGRjZTE3MzJiOGViMWM3YTlmN2MwZmJmNmYifX19");
     
  9. Offline

    Cat 700

    ok testing now thanks
     
  10. Offline

    Machine Maker

    @Cat 700 In the future, post code on a website built for handling lines of code, like https://pastebin.com. It makes it way easier to read, and it keeps your indentations.
     
Thread Status:
Not open for further replies.

Share This Page