mrkirby153

Untitled

Jan 3rd, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.61 KB | None | 0 0
  1. package me.mrkirby153.swc.api.nms;
  2.  
  3. import me.mrkirby153.swc.api.player.SWCPlayers;
  4. import me.mrkirby153.swc.hub.player.SWCPlayer;
  5. import net.minecraft.server.v1_7_R4.*;
  6. import org.bukkit.ChatColor;
  7. import org.bukkit.Location;
  8. import org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer;
  9. import org.bukkit.entity.EntityType;
  10. import org.bukkit.entity.Player;
  11. import org.bukkit.event.EventHandler;
  12. import org.bukkit.event.Listener;
  13. import org.bukkit.event.player.PlayerMoveEvent;
  14. import org.bukkit.plugin.java.JavaPlugin;
  15. import org.bukkit.scheduler.BukkitRunnable;
  16.  
  17. import java.lang.reflect.Field;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20.  
  21.  
  22. public class BarAPI implements Listener{
  23.  
  24.  
  25. public static final int ENTITY_ID = 1234;
  26. private static JavaPlugin plugin;
  27. private static HashMap<String, Boolean> hasHealthBar = new HashMap<String, Boolean>();
  28. private static Map<Player, Integer> percentageMap = new HashMap<Player, Integer>();
  29. private static Map<Player, Boolean> stopPercentage = new HashMap<Player, Boolean>();
  30.  
  31. private static Map<String, Integer> direction = new HashMap<String, Integer>();
  32. private static Map<String, String> displayText = new HashMap<String, String>();
  33.  
  34. public static void sendPacket(Player player, Packet packet) {
  35. EntityPlayer entityPlayer = ((CraftPlayer) player).getHandle();
  36.  
  37. entityPlayer.playerConnection.sendPacket(packet);
  38. }
  39.  
  40. /**
  41. * CALL THIS BEFORE DOING ANYTHING. PASS IN AN INSTANCE TO YOUR PLUGIN
  42. *
  43. * @param plugin
  44. */
  45. public static void init(JavaPlugin plugin) {
  46. BarAPI.plugin = plugin;
  47. plugin.getServer().getPluginManager().registerEvents(new BarAPI(), plugin);
  48. }
  49.  
  50. public static Field getField(Class<?> cl, String field_name) {
  51. try {
  52. Field field = cl.getDeclaredField(field_name);
  53. return field;
  54. } catch (SecurityException e) {
  55. e.printStackTrace();
  56. } catch (NoSuchFieldException e) {
  57. e.printStackTrace();
  58. }
  59. return null;
  60. }
  61.  
  62. //Accessing packets
  63. @SuppressWarnings("deprecation")
  64. public static PacketPlayOutSpawnEntityLiving getMobPacket(String text, Location loc, Player player) {
  65. PacketPlayOutSpawnEntityLiving mobPacket = new PacketPlayOutSpawnEntityLiving();
  66. boolean is18 = (((CraftPlayer) player).getHandle().playerConnection.networkManager.getVersion() == 47);
  67. try {
  68.  
  69. Field a = getField(mobPacket.getClass(), "a");
  70. a.setAccessible(true);
  71. a.set(mobPacket, ENTITY_ID);
  72.  
  73. Field b = getField(mobPacket.getClass(), "b");
  74. b.setAccessible(true);
  75. if (is18) {
  76. b.set(mobPacket, (byte) EntityType.WITHER.getTypeId());
  77. } else {
  78. b.set(mobPacket, EntityType.ENDER_DRAGON.getTypeId());
  79. }
  80. // Calculate 30 blocks in fromt
  81. if (!is18) {
  82. SWCPlayer swcPlayer = SWCPlayers.findPlayer(player);
  83. if (swcPlayer != null) {
  84. Location hubSpawn = swcPlayer.getCurrentHub().getSpawnLocation();
  85. loc = new Location(hubSpawn.getWorld(), hubSpawn.getX(), 1, hubSpawn.getZ());
  86. } else {
  87. loc = new Location(player.getLocation().getWorld(), player.getLocation().getX(), 1, player.getLocation().getZ());
  88. }
  89. Field c = getField(mobPacket.getClass(), "c");
  90. c.setAccessible(true);
  91. c.set(mobPacket, (int) Math.floor(loc.getBlockX() * 32.0D));
  92.  
  93. Field d = getField(mobPacket.getClass(), "d");
  94. d.setAccessible(true);
  95. d.set(mobPacket, (int) Math.floor(loc.getBlockY() * 32.0D));
  96.  
  97. Field e = getField(mobPacket.getClass(), "e");
  98. e.setAccessible(true);
  99. e.set(mobPacket, (int) Math.floor(loc.getBlockZ() * 32.0D));
  100. } else {
  101. Location newLoc;
  102. newLoc = player.getLocation().add(player.getLocation().getDirection().setY(0).normalize().multiply(30));
  103. loc = newLoc;
  104. int x = (int) Math.floor(loc.getBlockX() * 32.0D);
  105. int y = (int) Math.floor(loc.getBlockY() * 32.0D);
  106. int z = (int) Math.floor(loc.getBlockZ() * 32.0D);
  107. // System.out.println("Spawned at " + loc.getBlockX() + ", " + loc.getBlockY() + ", " + loc.getBlockZ());
  108. Field c = getField(mobPacket.getClass(), "c");
  109. c.setAccessible(true);
  110. c.set(mobPacket, x);
  111.  
  112. Field d = getField(mobPacket.getClass(), "d");
  113. d.setAccessible(true);
  114. d.set(mobPacket, y);
  115.  
  116. Field e = getField(mobPacket.getClass(), "e");
  117. e.setAccessible(true);
  118. e.set(mobPacket, z);
  119. }
  120.  
  121. Field f = getField(mobPacket.getClass(), "f");
  122. f.setAccessible(true);
  123. f.set(mobPacket, (byte) 0);
  124.  
  125. Field g = getField(mobPacket.getClass(), "g");
  126. g.setAccessible(true);
  127. g.set(mobPacket, (byte) 0);
  128.  
  129. Field h = getField(mobPacket.getClass(), "h");
  130. h.setAccessible(true);
  131. h.set(mobPacket, (byte) 0);
  132.  
  133. Field i = getField(mobPacket.getClass(), "i");
  134. i.setAccessible(true);
  135. i.set(mobPacket, (byte) 0);
  136.  
  137. Field j = getField(mobPacket.getClass(), "j");
  138. j.setAccessible(true);
  139. j.set(mobPacket, (byte) 0);
  140.  
  141. Field k = getField(mobPacket.getClass(), "k");
  142. k.setAccessible(true);
  143. k.set(mobPacket, (byte) 0);
  144.  
  145. } catch (IllegalArgumentException | IllegalAccessException e1) {
  146. e1.printStackTrace();
  147. }
  148.  
  149. DataWatcher watcher = getWatcher(text, 300, ((CraftPlayer) player).getHandle().playerConnection.networkManager.getVersion());
  150.  
  151. try {
  152. Field t = PacketPlayOutSpawnEntityLiving.class.getDeclaredField("l");
  153. t.setAccessible(true);
  154. t.set(mobPacket, watcher);
  155. } catch (Exception ex) {
  156. ex.printStackTrace();
  157. }
  158.  
  159. return mobPacket;
  160. }
  161.  
  162. public static PacketPlayOutEntityDestroy getDestroyEntityPacket() {
  163. PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy();
  164.  
  165. Field a = getField(packet.getClass(), "a");
  166. a.setAccessible(true);
  167. try {
  168. a.set(packet, new int[]{ENTITY_ID});
  169. } catch (IllegalArgumentException | IllegalAccessException e) {
  170. e.printStackTrace();
  171. }
  172.  
  173. return packet;
  174. }
  175.  
  176. public static PacketPlayOutEntityMetadata getMetadataPacket(DataWatcher watcher) {
  177. PacketPlayOutEntityMetadata metaPacket = new PacketPlayOutEntityMetadata();
  178.  
  179. Field a = getField(metaPacket.getClass(), "a");
  180. a.setAccessible(true);
  181. try {
  182. a.set(metaPacket, ENTITY_ID);
  183. } catch (IllegalArgumentException | IllegalAccessException e1) {
  184. e1.printStackTrace();
  185. }
  186.  
  187. try {
  188. Field b = PacketPlayOutEntityMetadata.class.getDeclaredField("b");
  189. b.setAccessible(true);
  190. b.set(metaPacket, watcher.c());
  191. } catch (Exception e) {
  192. e.printStackTrace();
  193. }
  194.  
  195. return metaPacket;
  196. }
  197.  
  198. public static PacketPlayInClientCommand getRespawnPacket() {
  199. PacketPlayInClientCommand packet = new PacketPlayInClientCommand();
  200.  
  201. Field a = getField(packet.getClass(), "a");
  202. a.setAccessible(true);
  203. try {
  204. a.set(packet, 1);
  205. } catch (IllegalArgumentException | IllegalAccessException e) {
  206. e.printStackTrace();
  207. }
  208.  
  209. return packet;
  210. }
  211.  
  212. public static DataWatcher getWatcher(String text, int health, int protocolVersion) {
  213. DataWatcher watcher = new DataWatcher(null);
  214. boolean is18 = (protocolVersion == 47);
  215. // watcher.a(0, (byte) 0x20); //Flags, 0x20 = invisible
  216. if (!is18)
  217. health -= 100;
  218. watcher.a(6, (float) health);
  219. watcher.a((is18) ? 2 : 10, text); //Entity name
  220. watcher.a((is18) ? 3 : 11, (byte) 1); //Show name, 1 = show, 0 = don't show
  221. if (is18) {
  222. watcher.a(20, 0);
  223. }
  224. //watcher.a(16, health); //Wither health, 300 = full health
  225.  
  226. return watcher;
  227. }
  228.  
  229. //Other methods
  230.  
  231. /**
  232. * use the bar as a simple text display.
  233. *
  234. * @param text
  235. * @param player
  236. */
  237. public static void displayTextBar(String text, final Player player) {
  238. if (plugin == null) return;
  239. createBar(player, text);
  240.  
  241. /*new BukkitRunnable() {
  242. @Override
  243. public void run() {
  244. destroyBar(player);
  245. }
  246. }.runTaskLater(plugin, 120L);*/
  247. }
  248.  
  249. /**
  250. * after we're finished with the bar (either from our countdown
  251. * or the percentage handler reached 100 (or was stopped) )
  252. * call this to remove the bar from the players HUD
  253. *
  254. * @param player
  255. */
  256. public static void destroyBar(Player player) {
  257. PacketPlayOutEntityDestroy destroyEntityPacket = getDestroyEntityPacket();
  258.  
  259. sendPacket(player, destroyEntityPacket);
  260. hasHealthBar.put(player.getName(), false);
  261. }
  262.  
  263. /**
  264. * creates a bar with the text given. Use this before
  265. * starting to send data about the bar (like a percentage)
  266. *
  267. * @param player
  268. * @param text
  269. */
  270. public static void createBar(Player player, String text) {
  271. PacketPlayOutSpawnEntityLiving mobPacket = getMobPacket(text, player.getLocation(), player);
  272.  
  273. sendPacket(player, mobPacket);
  274. hasHealthBar.put(player.getName(), true);
  275. displayText.put(player.getName(), text);
  276.  
  277. }
  278.  
  279. static ChatColor percentageColor = ChatColor.GREEN;
  280.  
  281. /**
  282. * A dev-controlled percentage bar. The only automation is the
  283. * update procedure. The dev must use setPercentage() to keep the
  284. * display updated and, once the percentage is 100, the bar will destroy
  285. * itself and briefly display the endText
  286. * This should be used in the case of the bar being a "percentage complete"
  287. * bar or the like
  288. *
  289. * @param text text to display above the bar
  290. * @param player
  291. * @param percentage
  292. * @param appendPercentage if true the percentage will be appended to the end of the text
  293. * @param appendEndPercentage if true the percentage will be appended when the bar is finished, whether it
  294. * finished through automation or through being cancelled
  295. */
  296. public static void displayPercentageBar(String text, String endText, final Player player, int percentage, boolean appendPercentage, boolean appendEndPercentage) {
  297. if (plugin == null) return;
  298. stopPercentage.put(player, false);
  299. setPercentage(player, percentage);
  300. final String dText = appendPercentage ? appendPercentage(text, percentage) : text;
  301. createBar(player, text);
  302. final String dEndText = appendEndPercentage ? appendPercentage(endText, percentage) : endText;
  303.  
  304. new BukkitRunnable() {
  305.  
  306. @Override
  307. public void run() {
  308. if (getPercentage(player) >= 100 || getStop(player)) {
  309. destroyBar(player);
  310.  
  311. createBar(player, dEndText);
  312.  
  313. new BukkitRunnable() {
  314. @Override
  315. public void run() {
  316. destroyBar(player);
  317. }
  318. }.runTaskLater(plugin, 60L);
  319. this.cancel();
  320. }
  321. setBarHealth(player, getPercentage(player) * 3, dText);
  322. }
  323. }.runTaskTimer(plugin, 10L, 10L);
  324. }
  325.  
  326. public static void stopPercentage(Player player) {
  327. stopPercentage.put(player, true);
  328. }
  329.  
  330. public static boolean getStop(Player player) {
  331. return stopPercentage.get(player);
  332. }
  333.  
  334. public static void setPercentage(Player player, int percentage) {
  335. percentageMap.put(player, percentage);
  336. }
  337.  
  338. public static int getPercentage(Player player) {
  339. return percentageMap.get(player);
  340. }
  341.  
  342. /**
  343. * set the color of the percentage appended to the bar if appendPercentage is true in displayPercentageBar()
  344. * default color is Green.
  345. *
  346. * @param pColor
  347. */
  348. public static void setPercentageColor(ChatColor pColor) {
  349. percentageColor = pColor;
  350. }
  351.  
  352. private static String appendPercentage(String message, int percentage) {
  353. return message + percentageColor + " - " + percentage + "%";
  354. }
  355.  
  356. /**
  357. * This sets the bars "health fill" amount. 300 is full, 0 is empty.
  358. *
  359. * @param player
  360. * @param health
  361. * @param text text that is on the bar to set health on
  362. */
  363. public static void setBarHealth(Player player, int health, String text) {
  364. DataWatcher watcher = getWatcher(text, health, ((CraftPlayer) player).getHandle().playerConnection.networkManager.getVersion());
  365. PacketPlayOutEntityMetadata metaPacket = getMetadataPacket(watcher);
  366.  
  367. sendPacket(player, metaPacket);
  368. }
  369.  
  370. /**
  371. * used internally, the extra parameter healthAdd determines the amount of health
  372. * to add or subtract each second and the delay is for the update speed in ticks.
  373. *
  374. * @param text
  375. * @param completeText
  376. * @param player
  377. * @param healthAdd
  378. * @param delay
  379. * @param loadUp
  380. */
  381. public static void displayLoadingBar(final String text, final String completeText, final Player player, final int healthAdd, final long delay, final boolean loadUp) {
  382. if (plugin == null) return;
  383.  
  384. createBar(player, text);
  385.  
  386. new BukkitRunnable() {
  387. int health = (loadUp ? 0 : 300);
  388.  
  389. @Override
  390. public void run() {
  391. //fires if time is left on the "loading bar" loop
  392. if ((loadUp ? health < 300 : health > 0)) {
  393. setBarHealth(player, health, text);
  394. if (loadUp) {
  395. health += healthAdd;
  396. } else {
  397. health -= healthAdd;
  398. }
  399. //fires once the bar is done loading, cleans up the bar and
  400. //puts the "complete text" on the bar for the player to see
  401. } else {
  402. //this code will remain uncleaned until I find out
  403. //whether the order of these lines is important-backspace119
  404. DataWatcher watcher = getWatcher(text, (loadUp ? 300 : 0), ((CraftPlayer) player).getHandle().playerConnection.networkManager.getVersion());
  405. PacketPlayOutEntityMetadata metaPacket = getMetadataPacket(watcher);
  406. PacketPlayOutEntityDestroy destroyEntityPacket = getDestroyEntityPacket();
  407.  
  408. sendPacket(player, metaPacket);
  409. sendPacket(player, destroyEntityPacket);
  410. hasHealthBar.put(player.getName(), false);
  411.  
  412. //Complete text
  413. createBar(player, completeText);
  414.  
  415. setBarHealth(player, 300, completeText);
  416.  
  417. new BukkitRunnable() {
  418. @Override
  419. public void run() {
  420. destroyBar(player);
  421. }
  422. }.runTaskLater(plugin, 40L);
  423.  
  424. this.cancel();
  425. }
  426. }
  427. }.runTaskTimer(plugin, delay, delay);
  428. }
  429.  
  430. /**
  431. * Use this to display a loading bar to the player with the given delay.
  432. *
  433. * @param text text to show before load is complete
  434. * @param completeText text to show after load is complete
  435. * @param player
  436. * @param secondsDelay
  437. * @param loadUp load towards left ("up") if true, right if false
  438. */
  439. public static void displayLoadingBar(final String text, final String completeText, final Player player, final int secondsDelay, final boolean loadUp) {
  440.  
  441. final int healthChangePerSecond = 300 / secondsDelay;
  442.  
  443. displayLoadingBar(text, completeText, player, healthChangePerSecond, 20L, loadUp);
  444. }
  445.  
  446.  
  447. @EventHandler
  448. public void onMove(PlayerMoveEvent event){
  449. Player player = event.getPlayer();
  450. if(event.isCancelled())
  451. return;
  452. if(((CraftPlayer) event.getPlayer()).getHandle().playerConnection.networkManager.getVersion() != 47)
  453. return;
  454. if(hasHealthBar.get(event.getPlayer().getName())){
  455. // Teleport the entity
  456. Location newLoc = player.getLocation().add(player.getLocation().getDirection()/*.setY(0)*/.normalize().multiply(30));
  457. // System.out.println("Teleporting to "+newLoc.getBlockX()+","+newLoc.getBlockY()+","+newLoc.getBlockZ());
  458. /* PacketPlayOutEntityTeleport packet = new PacketPlayOutEntityTeleport(ENTITY_ID, newLoc.getBlockX(), newLoc.getBlockY(), newLoc.getBlockZ(), (byte) 0, (byte) 0, false, false);
  459. ((CraftPlayer) event.getPlayer()).getHandle().playerConnection.sendPacket(packet);*/
  460. displayTextBar(displayText.get(event.getPlayer().getName()), event.getPlayer());
  461. } else {
  462. hasHealthBar.remove(event.getPlayer().getName());
  463. }
  464. }
  465. }
Advertisement
Add Comment
Please, Sign In to add comment