Advertisement
Guest User

Untitled

a guest
Feb 25th, 2015
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.88 KB | None | 0 0
  1. package me.iamguus.sendserver.nms;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.InvocationTargetException;
  5. import java.lang.reflect.Method;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8.  
  9. import org.bukkit.Location;
  10. import org.bukkit.entity.Player;
  11.  
  12. public class MobBarAPI
  13. {
  14. private static MobBarAPI instance;
  15. private Map< String , Dragon > dragonMap = new HashMap< String , Dragon >();
  16.  
  17. private MobBarAPI()
  18. {
  19. }
  20.  
  21. public static MobBarAPI getInstance()
  22. {
  23. if ( MobBarAPI.instance == null )
  24. MobBarAPI.instance = new MobBarAPI();
  25.  
  26. return MobBarAPI.instance;
  27. }
  28.  
  29. public void setStatus(Player player, String text, int percent, boolean reset) throws Exception
  30. {
  31. if ( percent <= 0 )
  32. {
  33. percent = 1;
  34. }
  35. else
  36. if ( percent > 100 )
  37. {
  38. throw new IllegalArgumentException("percent cannot be greater than 100, percent = " + percent);
  39. }
  40.  
  41. Dragon dragon = null;
  42.  
  43. if ( dragonMap.containsKey(player.getName()) && !reset )
  44. {
  45. dragon = dragonMap.get(player.getName());
  46. }
  47. else
  48. {
  49. dragon = new Dragon(text, player.getLocation().add(0 , -200 , 0), percent);
  50. Object mobPacket = dragon.getSpawnPacket();
  51. sendPacket(player , mobPacket);
  52. dragonMap.put(player.getName() , dragon);
  53. }
  54.  
  55. if ( text == "" )
  56. {
  57. Object destroyPacket = dragon.getDestroyPacket();
  58. sendPacket(player , destroyPacket);
  59. dragonMap.remove(player.getName());
  60. }
  61. else
  62. {
  63. dragon.setName(text);
  64. dragon.setHealth(percent);
  65. Object metaPacket = dragon.getMetaPacket(dragon.getWatcher());
  66. Object teleportPacket = dragon.getTeleportPacket(player.getLocation().add(0 , -200 , 0));
  67. sendPacket(player , metaPacket);
  68. sendPacket(player , teleportPacket);
  69. }
  70. }
  71.  
  72. private void sendPacket(Player player, Object packet)
  73. {
  74. try
  75. {
  76. Object nmsPlayer = ReflectionUtils.getHandle(player);
  77. Field con_field = nmsPlayer.getClass().getField("playerConnection");
  78. Object con = con_field.get(nmsPlayer);
  79. Method packet_method = ReflectionUtils.getMethod(con.getClass() , "sendPacket");
  80. packet_method.invoke(con , packet);
  81. }
  82. catch (SecurityException e)
  83. {
  84. e.printStackTrace();
  85. }
  86. catch (IllegalArgumentException e)
  87. {
  88. e.printStackTrace();
  89. }
  90. catch (IllegalAccessException e)
  91. {
  92. e.printStackTrace();
  93. }
  94. catch (InvocationTargetException e)
  95. {
  96. e.printStackTrace();
  97. }
  98. catch (NoSuchFieldException e)
  99. {
  100. e.printStackTrace();
  101. }
  102. }
  103.  
  104. private class Dragon
  105. {
  106.  
  107. private static final int MAX_HEALTH = 200;
  108. private int id;
  109. private int x;
  110. private int y;
  111. private int z;
  112. private int pitch = 0;
  113. private int yaw = 0;
  114. private byte xvel = 0;
  115. private byte yvel = 0;
  116. private byte zvel = 0;
  117. private float health;
  118. private boolean visible = false;
  119. private String name;
  120. private Object world;
  121.  
  122. private Object dragon;
  123.  
  124. public Dragon( String name , Location loc , int percent )
  125. {
  126. this.name = name;
  127. this.x = loc.getBlockX();
  128. this.y = loc.getBlockY();
  129. this.z = loc.getBlockZ();
  130. this.health = percent / 100F * MAX_HEALTH;
  131. this.world = ReflectionUtils.getHandle(loc.getWorld());
  132. }
  133.  
  134. public void setHealth(int percent)
  135. {
  136. this.health = percent / 100F * MAX_HEALTH;
  137. }
  138.  
  139. public void setName(String name)
  140. {
  141. this.name = name;
  142. }
  143.  
  144. public Object getSpawnPacket() throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException,
  145. InvocationTargetException, NoSuchMethodException
  146. {
  147. Class< ? > Entity = ReflectionUtils.getCraftClass("Entity");
  148. Class< ? > EntityLiving = ReflectionUtils.getCraftClass("EntityLiving");
  149. Class< ? > EntityEnderDragon = ReflectionUtils.getCraftClass("EntityEnderDragon");
  150. dragon = EntityEnderDragon.getConstructor(ReflectionUtils.getCraftClass("World")).newInstance(world);
  151.  
  152. Method setLocation = ReflectionUtils.getMethod(EntityEnderDragon , "setLocation" , new Class< ? >[]
  153. { double.class , double.class , double.class , float.class , float.class });
  154. setLocation.invoke(dragon , x , y , z , pitch , yaw);
  155.  
  156. Method setInvisible = ReflectionUtils.getMethod(EntityEnderDragon , "setInvisible" , new Class< ? >[]
  157. { boolean.class });
  158. setInvisible.invoke(dragon , visible);
  159.  
  160. Method setCustomName = ReflectionUtils.getMethod(EntityEnderDragon , "setCustomName" , new Class< ? >[]
  161. { String.class });
  162. setCustomName.invoke(dragon , name);
  163.  
  164. Method setHealth = ReflectionUtils.getMethod(EntityEnderDragon , "setHealth" , new Class< ? >[]
  165. { float.class });
  166. setHealth.invoke(dragon , health);
  167.  
  168. Field motX = ReflectionUtils.getField(Entity , "motX");
  169. motX.set(dragon , xvel);
  170.  
  171. Field motY = ReflectionUtils.getField(Entity , "motX");
  172. motY.set(dragon , yvel);
  173.  
  174. Field motZ = ReflectionUtils.getField(Entity , "motX");
  175. motZ.set(dragon , zvel);
  176.  
  177. Method getId = ReflectionUtils.getMethod(EntityEnderDragon , "getId" , new Class< ? >[] { });
  178. this.id = (Integer) getId.invoke(dragon);
  179.  
  180. Class< ? > PacketPlayOutSpawnEntityLiving = ReflectionUtils.getCraftClass("PacketPlayOutSpawnEntityLiving");
  181.  
  182. Object packet = PacketPlayOutSpawnEntityLiving.getConstructor(new Class< ? >[]
  183. { EntityLiving }).newInstance(dragon);
  184.  
  185. return packet;
  186. }
  187.  
  188. public Object getDestroyPacket() throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException,
  189. InvocationTargetException, NoSuchMethodException, NoSuchFieldException
  190. {
  191. Class< ? > PacketPlayOutEntityDestroy = ReflectionUtils.getCraftClass("PacketPlayOutEntityDestroy");
  192.  
  193. Object packet = PacketPlayOutEntityDestroy.getConstructor().newInstance();
  194.  
  195. Field a = PacketPlayOutEntityDestroy.getDeclaredField("a");
  196. a.setAccessible(true);
  197. a.set(packet , new int[]
  198. { id });
  199.  
  200. return packet;
  201. }
  202.  
  203. public Object getMetaPacket(Object watcher) throws IllegalArgumentException, SecurityException, InstantiationException,
  204. IllegalAccessException, InvocationTargetException, NoSuchMethodException
  205. {
  206. Class< ? > DataWatcher = ReflectionUtils.getCraftClass("DataWatcher");
  207.  
  208. Class< ? > PacketPlayOutEntityMetadata = ReflectionUtils.getCraftClass("PacketPlayOutEntityMetadata");
  209.  
  210. Object packet = PacketPlayOutEntityMetadata.getConstructor(new Class< ? >[]
  211. { int.class , DataWatcher , boolean.class }).newInstance(id , watcher , true);
  212.  
  213. return packet;
  214. }
  215.  
  216. public Object getTeleportPacket(Location loc) throws IllegalArgumentException, SecurityException, InstantiationException,
  217. IllegalAccessException, InvocationTargetException, NoSuchMethodException
  218. {
  219. Class< ? > PacketPlayOutEntityTeleport = ReflectionUtils.getCraftClass("PacketPlayOutEntityTeleport");
  220.  
  221. Object packet = PacketPlayOutEntityTeleport.getConstructor().newInstance(this.id , loc.getBlockX() * 32 ,
  222. loc.getBlockY() * 32 , loc.getBlockZ() * 32 , (byte) ( (int) loc.getYaw() * 256 / 360 ) ,
  223. (byte) ( (int) loc.getPitch() * 256 / 360 ));
  224.  
  225. return packet;
  226. }
  227.  
  228. public Object getWatcher() throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException,
  229. InvocationTargetException, NoSuchMethodException
  230. {
  231. Class< ? > Entity = ReflectionUtils.getCraftClass("Entity");
  232. Class< ? > DataWatcher = ReflectionUtils.getCraftClass("DataWatcher");
  233.  
  234. Object watcher = DataWatcher.getConstructor(new Class< ? >[]
  235. { Entity }).newInstance(dragon);
  236.  
  237. Method a = ReflectionUtils.getMethod(DataWatcher , "a" , new Class< ? >[]
  238. { int.class , Object.class });
  239.  
  240. a.invoke(watcher , 0 , visible ? (byte) 0 : (byte) 0x20);
  241. a.invoke(watcher , 6 , (Float) health);
  242. a.invoke(watcher , 7 , (Integer) 0);
  243. a.invoke(watcher , 8 , (Byte) (byte) 0);
  244. a.invoke(watcher , 10 , name);
  245. a.invoke(watcher , 11 , (Byte) (byte) 1);
  246. return watcher;
  247. }
  248.  
  249. }
  250.  
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement