Skip to content

Instantly share code, notes, and snippets.

@Swedz
Created May 18, 2020 00:52
Show Gist options
  • Save Swedz/bcc1045c4263200a35f37006cbf92300 to your computer and use it in GitHub Desktop.
Save Swedz/bcc1045c4263200a35f37006cbf92300 to your computer and use it in GitHub Desktop.
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import net.minecraft.server.v1_15_R1.EntityPlayer;
import org.bukkit.Material;
import org.bukkit.craftbukkit.libs.org.apache.commons.codec.binary.Base64;
import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.Iterator;
import java.util.UUID;
public class PlayerSkin {
private final String texture;
/**
* Get the player skin data.
*
* @param url the URL to get the data from
*/
public PlayerSkin(URL url) {
this.texture = new String(Base64.encodeBase64(("{textures:{SKIN:{url:\"" + url.toString() + "\"}}}").getBytes()));
}
/**
* Get the player skin data.
*
* @param texture the texture property
*/
public PlayerSkin(String texture) {
this.texture = texture;
}
/**
* Get the player skin data for the player.
*
* @param player the {@link Player}
*/
public PlayerSkin(Player player) {
// Get the profile data
EntityPlayer entityPlayer = ((CraftPlayer) player).getHandle();
GameProfile profile = entityPlayer.getProfile();
// Get the properties
Iterator<Property> properties = profile.getProperties().get("textures").iterator();
if(properties.hasNext()) {
Property property = properties.next();
this.texture = property.getValue();
}
else
this.texture = null;
}
/**
* Get the base64 encoded json object that contains the skin data.
*
* @return the texture
*/
public String getTexture() {
return this.texture;
}
/**
* Get the item for the skin instance.
*
* @param uuid the UUID for the game profile
* @return the item
*/
public ItemStack getItem(UUID uuid) {
ItemStack item = new ItemStack(Material.PLAYER_HEAD);
// Make sure they have a skin
if(this.getTexture() != null) {
// Start loading in skull metadata
SkullMeta sm = (SkullMeta) item.getItemMeta();
// Make a profile with the skin texture
GameProfile profile = new GameProfile(uuid, null);
byte[] encoded = this.getTexture().getBytes();
profile.getProperties().put("textures", new Property("textures", new String(encoded)));
// Update the skull metadata
try {
Field profileField = sm.getClass().getDeclaredField("profile");
profileField.setAccessible(true);
profileField.set(sm, profile);
} catch (Exception ex) {
ex.printStackTrace();
}
// Update the item meta
item.setItemMeta(sm);
}
return item;
}
/**
* Get the item for the skin instance.
*
* @return the item
*/
public ItemStack getItem() {
return this.getItem(UUID.randomUUID());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment