Guest User

Untitled

a guest
Nov 14th, 2014
614
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package net.dori99xd.tials.test;
  2.  
  3. import java.lang.reflect.InvocationTargetException;
  4. import java.lang.reflect.Method;
  5. import java.util.Random;
  6. import java.util.UUID;
  7.  
  8. import net.minecraft.util.com.mojang.authlib.GameProfile;
  9. import net.minecraft.util.com.mojang.authlib.properties.Property;
  10.  
  11. import org.bukkit.Bukkit;
  12. import org.bukkit.Material;
  13. import org.bukkit.block.Block;
  14. import org.bukkit.block.Skull;
  15. import org.bukkit.event.EventHandler;
  16. import org.bukkit.event.Listener;
  17. import org.bukkit.event.block.Action;
  18. import org.bukkit.event.player.PlayerInteractEvent;
  19. import org.bukkit.plugin.java.JavaPlugin;
  20. import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
  21.  
  22. /**
  23.  * @author BigTeddy98
  24.  * @author dori99xd
  25.  * Refletions by BigTeddy98
  26.  */
  27. public class Example extends JavaPlugin implements Listener {
  28.  
  29.     private static final Random random = new Random();
  30.     private static final String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  31.     private static Method getWorldHandle;
  32.     private static Method getWorldTileEntity;
  33.     private static Method setGameProfile;
  34.  
  35.     // Example
  36.     @Override
  37.     public void onEnable() {
  38.         getServer().getPluginManager().registerEvents(this, this);
  39.         if (getWorldHandle == null || getWorldTileEntity == null || setGameProfile == null) {
  40.             try {
  41.                 getWorldHandle = getCraftClass("CraftWorld").getMethod("getHandle");
  42.                 getWorldTileEntity = getMCClass("WorldServer").getMethod("getTileEntity", int.class, int.class, int.class);
  43.                 setGameProfile = getMCClass("TileEntitySkull").getMethod("setGameProfile", GameProfile.class);
  44.             } catch (NoSuchMethodException | SecurityException e) {
  45.                 e.printStackTrace();
  46.             }
  47.         }
  48.     }
  49.    
  50.     // Example
  51.     @EventHandler(ignoreCancelled=true)
  52.     public void on(PlayerInteractEvent event) {
  53.         if(event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock().getType() == Material.SKULL)
  54.             setSkullWithNonPlayerProfile("http://213.136.94.170/downloads/img/skin.png", true, event.getClickedBlock());
  55.     }
  56.  
  57.     // Method
  58.     public static void setSkullWithNonPlayerProfile(String skinURL, boolean randomName, Block skull) {
  59.         if(skull.getType() != Material.SKULL)
  60.             throw new IllegalArgumentException("Block must be a skull.");
  61.         Skull s = (Skull) skull.getState();
  62.         try {
  63.             setSkullProfile(s, getNonPlayerProfile(skinURL, randomName));
  64.         } catch (IllegalAccessException e) {
  65.             e.printStackTrace();
  66.         } catch (IllegalArgumentException e) {
  67.             e.printStackTrace();
  68.         } catch (InvocationTargetException e) {
  69.             e.printStackTrace();
  70.         } catch (SecurityException e) {
  71.             e.printStackTrace();
  72.         }
  73.         skull.getWorld().refreshChunk(skull.getChunk().getX(), skull.getChunk().getZ());
  74.     }
  75.    
  76.     // Method
  77.     private static void setSkullProfile(Skull skull, GameProfile someGameprofile) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  78.         Object world = getWorldHandle.invoke(skull.getWorld());
  79.         Object tileSkull = getWorldTileEntity.invoke(world, skull.getX(), skull.getY(), skull.getZ());
  80.         setGameProfile.invoke(tileSkull, someGameprofile);
  81.     }
  82.  
  83.     // Method
  84.     public static GameProfile getNonPlayerProfile(String skinURL, boolean randomName) {
  85.         GameProfile newSkinProfile = new GameProfile(UUID.randomUUID(), randomName ? getRandomString(16) : null);
  86.         newSkinProfile.getProperties().put("textures", new Property("textures", Base64Coder.encodeString("{textures:{SKIN:{url:\"" + skinURL + "\"}}}")));
  87.         return newSkinProfile;
  88.     }
  89.  
  90.     // Example
  91.     public static String getRandomString(int length) {
  92.         StringBuilder b = new StringBuilder(length);
  93.         for(int j = 0; j < length; j++)
  94.             b.append(chars.charAt(random.nextInt(chars.length())));
  95.         return b.toString();
  96.     }
  97.  
  98.     // Refletion
  99.     private static Class<?> getMCClass(String name) {
  100.         String version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3] + ".";
  101.         String className = "net.minecraft.server." + version + name;
  102.         Class<?> clazz = null;
  103.         try {
  104.             clazz = Class.forName(className);
  105.         } catch (ClassNotFoundException e) {
  106.             e.printStackTrace();
  107.         }
  108.         return clazz;
  109.     }
  110.  
  111.     // Refletion
  112.     private static Class<?> getCraftClass(String name) {
  113.         String version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3] + ".";
  114.         String className = "org.bukkit.craftbukkit." + version + name;
  115.         Class<?> clazz = null;
  116.         try {
  117.             clazz = Class.forName(className);
  118.         } catch (ClassNotFoundException e) {
  119.             e.printStackTrace();
  120.         }
  121.         return clazz;
  122.     }
  123. }
RAW Paste Data