Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.dori99xd.tials.test;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.Random;
- import java.util.UUID;
- import net.minecraft.util.com.mojang.authlib.GameProfile;
- import net.minecraft.util.com.mojang.authlib.properties.Property;
- import org.bukkit.Bukkit;
- import org.bukkit.Material;
- import org.bukkit.block.Block;
- import org.bukkit.block.Skull;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.block.Action;
- import org.bukkit.event.player.PlayerInteractEvent;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
- /**
- * @author BigTeddy98
- * @author dori99xd
- * Refletions by BigTeddy98
- */
- public class Example extends JavaPlugin implements Listener {
- private static final Random random = new Random();
- private static final String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- private static Method getWorldHandle;
- private static Method getWorldTileEntity;
- private static Method setGameProfile;
- // Example
- @Override
- public void onEnable() {
- getServer().getPluginManager().registerEvents(this, this);
- if (getWorldHandle == null || getWorldTileEntity == null || setGameProfile == null) {
- try {
- getWorldHandle = getCraftClass("CraftWorld").getMethod("getHandle");
- getWorldTileEntity = getMCClass("WorldServer").getMethod("getTileEntity", int.class, int.class, int.class);
- setGameProfile = getMCClass("TileEntitySkull").getMethod("setGameProfile", GameProfile.class);
- } catch (NoSuchMethodException | SecurityException e) {
- e.printStackTrace();
- }
- }
- }
- // Example
- @EventHandler(ignoreCancelled=true)
- public void on(PlayerInteractEvent event) {
- if(event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock().getType() == Material.SKULL)
- setSkullWithNonPlayerProfile("http://213.136.94.170/downloads/img/skin.png", true, event.getClickedBlock());
- }
- // Method
- public static void setSkullWithNonPlayerProfile(String skinURL, boolean randomName, Block skull) {
- if(skull.getType() != Material.SKULL)
- throw new IllegalArgumentException("Block must be a skull.");
- Skull s = (Skull) skull.getState();
- try {
- setSkullProfile(s, getNonPlayerProfile(skinURL, randomName));
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- } catch (SecurityException e) {
- e.printStackTrace();
- }
- skull.getWorld().refreshChunk(skull.getChunk().getX(), skull.getChunk().getZ());
- }
- // Method
- private static void setSkullProfile(Skull skull, GameProfile someGameprofile) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
- Object world = getWorldHandle.invoke(skull.getWorld());
- Object tileSkull = getWorldTileEntity.invoke(world, skull.getX(), skull.getY(), skull.getZ());
- setGameProfile.invoke(tileSkull, someGameprofile);
- }
- // Method
- public static GameProfile getNonPlayerProfile(String skinURL, boolean randomName) {
- GameProfile newSkinProfile = new GameProfile(UUID.randomUUID(), randomName ? getRandomString(16) : null);
- newSkinProfile.getProperties().put("textures", new Property("textures", Base64Coder.encodeString("{textures:{SKIN:{url:\"" + skinURL + "\"}}}")));
- return newSkinProfile;
- }
- // Example
- public static String getRandomString(int length) {
- StringBuilder b = new StringBuilder(length);
- for(int j = 0; j < length; j++)
- b.append(chars.charAt(random.nextInt(chars.length())));
- return b.toString();
- }
- // Refletion
- private static Class<?> getMCClass(String name) {
- String version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3] + ".";
- String className = "net.minecraft.server." + version + name;
- Class<?> clazz = null;
- try {
- clazz = Class.forName(className);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- return clazz;
- }
- // Refletion
- private static Class<?> getCraftClass(String name) {
- String version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3] + ".";
- String className = "org.bukkit.craftbukkit." + version + name;
- Class<?> clazz = null;
- try {
- clazz = Class.forName(className);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- return clazz;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement