Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2015
2,696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 33.02 KB | None | 0 0
  1. package utils;
  2. import java.lang.reflect.Constructor;
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.Method;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Map.Entry;
  9.  
  10. import org.bukkit.Bukkit;
  11. import org.bukkit.Location;
  12. import org.bukkit.Material;
  13. import org.bukkit.entity.Player;
  14.  
  15. import utils.ReflectionUtils.PackageType;
  16. import utils.ReflectionUtils.PacketType;
  17. /**
  18. * <b>ParticleEffect Library</b>
  19. * <p>
  20. * This library was created by @DarkBlade12 based on content related to particles of @microgeek (names and packet parameters), it allows you to display all Minecraft particle effects on a Bukkit server
  21. * <p>
  22. * You are welcome to use it, modify it and redistribute it under the following conditions:
  23. * <ul>
  24. * <li>Don't claim this class as your own
  25. * <li>Don't remove this disclaimer
  26. * </ul>
  27. * <p>
  28. * <i>It would be nice if you provide credit to me if you use this class in a published project</i>
  29. *
  30. * @author DarkBlade12
  31. * @version 1.5
  32. */
  33. public enum ParticleEffect {
  34. /**
  35. * A particle effect which is displayed by exploding tnt and creepers:
  36. * <ul>
  37. * <li>It looks like a crowd of gray balls which are fading away
  38. * <li>The speed value has no influence on this particle effect
  39. * </ul>
  40. */
  41. HUGE_EXPLOSION("hugeexplosion"),
  42. /**
  43. * A particle effect which is displayed by exploding ghast fireballs and wither skulls:
  44. * <ul>
  45. * <li>It looks like a gray ball which is fading away
  46. * <li>The speed value slightly influences the size of this particle effect
  47. * </ul>
  48. */
  49. LARGE_EXPLODE("largeexplode"),
  50. /**
  51. * A particle effect which is displayed by launching fireworks:
  52. * <ul>
  53. * <li>It looks like a white star which is sparkling
  54. * <li>The speed value influences the velocity at which the particle flies off
  55. * </ul>
  56. */
  57. FIREWORKS_SPARK("fireworksSpark"),
  58. /**
  59. * A particle effect which is displayed by swimming entities and arrows in water:
  60. * <ul>
  61. * <li>It looks like a bubble
  62. * <li>The speed value influences the velocity at which the particle flies off
  63. * </ul>
  64. */
  65. BUBBLE("bubble", true),
  66. /**
  67. * A particle effect which is displayed by water:
  68. * <ul>
  69. * <li>It looks like a tiny blue square
  70. * <li>The speed value has no influence on this particle effect
  71. * </ul>
  72. */
  73. SUSPEND("suspend", true),
  74. /**
  75. * A particle effect which is displayed by air when close to bedrock and the in the void:
  76. * <ul>
  77. * <li>It looks like a tiny gray square
  78. * <li>The speed value has no influence on this particle effect
  79. * </ul>
  80. */
  81. DEPTH_SUSPEND("depthSuspend"),
  82. /**
  83. * A particle effect which is displayed by mycelium:
  84. * <ul>
  85. * <li>It looks like a tiny gray square
  86. * <li>The speed value has no influence on this particle effect
  87. * </ul>
  88. */
  89. TOWN_AURA("townaura"),
  90. /**
  91. * A particle effect which is displayed when landing a critical hit and by arrows:
  92. * <ul>
  93. * <li>It looks like a light brown cross
  94. * <li>The speed value influences the velocity at which the particle flies off
  95. * </ul>
  96. */
  97. CRIT("crit"),
  98. /**
  99. * A particle effect which is displayed when landing a hit with an enchanted weapon:
  100. * <ul>
  101. * <li>It looks like a cyan star
  102. * <li>The speed value influences the velocity at which the particle flies off
  103. * </ul>
  104. */
  105. MAGIC_CRIT("magicCrit"),
  106. /**
  107. * A particle effect which is displayed by primed tnt, torches, droppers, dispensers, end portals, brewing stands and monster spawners:
  108. * <ul>
  109. * <li>It looks like a little gray cloud
  110. * <li>The speed value influences the velocity at which the particle flies off
  111. * </ul>
  112. */
  113. SMOKE("smoke"),
  114. /**
  115. * A particle effect which is displayed by entities with active potion effects:
  116. * <ul>
  117. * <li>It looks like a colored swirl
  118. * <li>The speed value causes the particle to be colored black when set to 0
  119. * </ul>
  120. */
  121. MOB_SPELL("mobSpell"),
  122. /**
  123. * A particle effect which is displayed by entities with active potion effects applied through a beacon:
  124. * <ul>
  125. * <li>It looks like a transparent colored swirl
  126. * <li>The speed value causes the particle to be always colored black when set to 0
  127. * </ul>
  128. */
  129. MOB_SPELL_AMBIENT("mobSpellAmbient"),
  130. /**
  131. * A particle effect which is displayed when splash potions or bottles o' enchanting hit something:
  132. * <ul>
  133. * <li>It looks like a white swirl
  134. * <li>The speed value causes the particle to only move upwards when set to 0
  135. * </ul>
  136. */
  137. SPELL("spell"),
  138. /**
  139. * A particle effect which is displayed when instant splash potions hit something:
  140. * <ul>
  141. * <li>It looks like a white cross
  142. * <li>The speed value causes the particle to only move upwards when set to 0
  143. * </ul>
  144. */
  145. INSTANT_SPELL("instantSpell"),
  146. /**
  147. * A particle effect which is displayed by witches:
  148. * <ul>
  149. * <li>It looks like a purple cross
  150. * <li>The speed value causes the particle to only move upwards when set to 0
  151. * </ul>
  152. */
  153. WITCH_MAGIC("witchMagic"),
  154. /**
  155. * A particle effect which is displayed by note blocks:
  156. * <ul>
  157. * <li>It looks like a colored note
  158. * <li>The speed value causes the particle to be colored green when set to 0
  159. * </ul>
  160. */
  161. NOTE("note"),
  162. /**
  163. * A particle effect which is displayed by nether portals, endermen, ender pearls, eyes of ender, ender chests and dragon eggs:
  164. * <ul>
  165. * <li>It looks like a purple cloud
  166. * <li>The speed value influences the spread of this particle effect
  167. * </ul>
  168. */
  169. PORTAL("portal"),
  170. /**
  171. * A particle effect which is displayed by enchantment tables which are nearby bookshelves:
  172. * <ul>
  173. * <li>It looks like a cryptic white letter
  174. * <li>The speed value influences the spread of this particle effect
  175. * </ul>
  176. */
  177. ENCHANTMENT_TABLE("enchantmenttable"),
  178. /**
  179. * A particle effect which is displayed by exploding tnt and creepers:
  180. * <ul>
  181. * <li>It looks like a white cloud
  182. * <li>The speed value influences the velocity at which the particle flies off
  183. * </ul>
  184. */
  185. EXPLODE("explode"),
  186. /**
  187. * A particle effect which is displayed by torches, active furnaces, magma cubes and monster spawners:
  188. * <ul>
  189. * <li>It looks like a tiny flame
  190. * <li>The speed value influences the velocity at which the particle flies off
  191. * </ul>
  192. */
  193. FLAME("flame"),
  194. /**
  195. * A particle effect which is displayed by lava:
  196. * <ul>
  197. * <li>It looks like a spark
  198. * <li>The speed value has no influence on this particle effect
  199. * </ul>
  200. */
  201. LAVA("lava"),
  202. /**
  203. * A particle effect which is currently unused:
  204. * <ul>
  205. * <li>It looks like a transparent gray square
  206. * <li>The speed value has no influence on this particle effect
  207. * </ul>
  208. */
  209. FOOTSTEP("footstep"),
  210. /**
  211. * A particle effect which is displayed by swimming entities, rain dropping on the ground and shaking wolves:
  212. * <ul>
  213. * <li>It looks like a blue drop
  214. * <li>The speed value has no influence on this particle effect
  215. * </ul>
  216. */
  217. SPLASH("splash"),
  218. /**
  219. * A particle effect which is displayed on water when fishing:
  220. * <ul>
  221. * <li>It looks like a blue droplet
  222. * <li>The speed value influences the velocity at which the particle flies off
  223. * </ul>
  224. */
  225. WAKE("wake"),
  226. /**
  227. * A particle effect which is displayed by fire, minecarts with furnace and blazes:
  228. * <ul>
  229. * <li>It looks like a large gray cloud
  230. * <li>The speed value influences the velocity at which the particle flies off
  231. * </ul>
  232. */
  233. LARGE_SMOKE("largesmoke"),
  234. /**
  235. * A particle effect which is displayed when a mob dies:
  236. * <ul>
  237. * <li>It looks like a large white cloud
  238. * <li>The speed value influences the velocity at which the particle flies off
  239. * </ul>
  240. */
  241. CLOUD("cloud"),
  242. /**
  243. * A particle effect which is displayed by redstone ore, powered redstone, redstone torches and redstone repeaters:
  244. * <ul>
  245. * <li>It looks like a tiny colored cloud
  246. * <li>The speed value causes the particle to be colored red when set to 0
  247. * </ul>
  248. */
  249. RED_DUST("reddust"),
  250. /**
  251. * A particle effect which is displayed when snowballs or eggs hit something:
  252. * <ul>
  253. * <li>It looks like a tiny part of the snowball icon
  254. * <li>The speed value has no influence on this particle effect
  255. * </ul>
  256. */
  257. SNOWBALL_POOF("snowballpoof"),
  258. /**
  259. * A particle effect which is displayed by blocks beneath a water source:
  260. * <ul>
  261. * <li>It looks like a blue drip
  262. * <li>The speed value has no influence on this particle effect
  263. * </ul>
  264. */
  265. DRIP_WATER("dripWater"),
  266. /**
  267. * A particle effect which is displayed by blocks beneath a lava source:
  268. * <ul>
  269. * <li>It looks like an orange drip
  270. * <li>The speed value has no influence on this particle effect
  271. * </ul>
  272. */
  273. DRIP_LAVA("dripLava"),
  274. /**
  275. * A particle effect which is currently unused:
  276. * <ul>
  277. * <li>It looks like a tiny white cloud
  278. * <li>The speed value influences the velocity at which the particle flies off
  279. * </ul>
  280. */
  281. SNOW_SHOVEL("snowshovel"),
  282. /**
  283. * A particle effect which is displayed by slimes:
  284. * <ul>
  285. * <li>It looks like a tiny part of the slimeball icon
  286. * <li>The speed value has no influence on this particle effect
  287. * </ul>
  288. */
  289. SLIME("slime"),
  290. /**
  291. * A particle effect which is displayed when breeding and taming animals:
  292. * <ul>
  293. * <li>It looks like a red heart
  294. * <li>The speed value has no influence on this particle effect
  295. * </ul>
  296. */
  297. HEART("heart"),
  298. /**
  299. * A particle effect which is displayed when attacking a villager in a village:
  300. * <ul>
  301. * <li>It looks like a cracked gray heart
  302. * <li>The speed value has no influence on this particle effect
  303. * </ul>
  304. */
  305. ANGRY_VILLAGER("angryVillager"),
  306. /**
  307. * A particle effect which is displayed when using bone meal and trading with a villager in a village:
  308. * <ul>
  309. * <li>It looks like a green star
  310. * <li>The speed value has no influence on this particle effect
  311. * </ul>
  312. */
  313. HAPPY_VILLAGER("happyVillager");
  314. private static final Map<String, ParticleEffect> NAME_MAP = new HashMap<String, ParticleEffect>();
  315. private final String name;
  316. private final boolean requiresWater;
  317. // Initialize map for quick name lookup
  318. static {
  319. for (ParticleEffect effect : values()) {
  320. NAME_MAP.put(effect.name, effect);
  321. }
  322. }
  323. /**
  324. * Construct a new particle effect
  325. *
  326. * @param name Name of this particle effect
  327. * @param requiresWater Indicates whether water is required for this particle effect to display properly
  328. */
  329. private ParticleEffect(String name, boolean requiresWater) {
  330. this.name = name;
  331. this.requiresWater = requiresWater;
  332. }
  333. /**
  334. * Construct a new particle effect with {@link #requiresWater} set to <code>false</code>
  335. *
  336. * @param name Name of this particle effect
  337. * @see #ParticleEffect(String, boolean)
  338. */
  339. private ParticleEffect(String name) {
  340. this(name, false);
  341. }
  342. /**
  343. * Returns the name of this particle effect
  344. *
  345. * @return The name
  346. */
  347. public String getName() {
  348. return name;
  349. }
  350. /**
  351. * Determine if water is required for this particle effect to display properly
  352. *
  353. * @return Whether water is required or not
  354. */
  355. public boolean getRequiresWater() {
  356. return requiresWater;
  357. }
  358. /**
  359. * Returns the particle effect with the given name
  360. *
  361. * @param name Name of the particle effect
  362. * @return The particle effect
  363. */
  364. public static ParticleEffect fromName(String name) {
  365. for (Entry<String, ParticleEffect> entry : NAME_MAP.entrySet()) {
  366. if (!entry.getKey().equalsIgnoreCase(name)) {
  367. continue;
  368. }
  369. return entry.getValue();
  370. }
  371. return null;
  372. }
  373. /**
  374. * Determine if water is at a certain location
  375. *
  376. * @param location Location to check
  377. * @return Whether water is at this location or not
  378. */
  379. private static boolean isWater(Location location) {
  380. Material material = location.getBlock().getType();
  381. return material == Material.WATER || material == Material.STATIONARY_WATER;
  382. }
  383. /**
  384. * Determine if an id is a block id
  385. *
  386. * @param id Id to check
  387. * @return Whether id is a block or not
  388. */
  389. @SuppressWarnings("deprecation")
  390. private static boolean isBlock(int id) {
  391. Material material = Material.getMaterial(id);
  392. return material != null && material.isBlock();
  393. }
  394. /**
  395. * Displays a particle effect which is only visible for all players within a certain range in the world of @param center
  396. *
  397. * @param offsetX Maximum distance particles can fly away from the center on the x-axis
  398. * @param offsetY Maximum distance particles can fly away from the center on the y-axis
  399. * @param offsetZ Maximum distance particles can fly away from the center on the z-axis
  400. * @param speed Display speed of the particles
  401. * @param amount Amount of particles
  402. * @param center Center location of the effect
  403. * @param range Range of the visibility (Maximum range for particles is usually 16, but it can differ for some types)
  404. * @throws IllegalArgumentException If the particle effect requires water and none is at the center location
  405. * @see ParticleEffectPacket
  406. * @see ParticleEffectPacket#sendTo(Location, double)
  407. */
  408. public void display(float offsetX, float offsetY, float offsetZ, float speed, int amount, Location center, double range) throws IllegalArgumentException {
  409. if (requiresWater && !isWater(center)) {
  410. throw new IllegalArgumentException("There is no water at the center location");
  411. }
  412. new ParticleEffectPacket(name, offsetX, offsetY, offsetZ, speed, amount).sendTo(center, range);
  413. }
  414. /**
  415. * Displays a particle effect which is only visible for the specified players
  416. *
  417. * @param offsetX Maximum distance particles can fly away from the center on the x-axis
  418. * @param offsetY Maximum distance particles can fly away from the center on the y-axis
  419. * @param offsetZ Maximum distance particles can fly away from the center on the z-axis
  420. * @param speed Display speed of the particles
  421. * @param amount Amount of particles
  422. * @param center Center location of the effect
  423. * @param players Receivers of the effect
  424. * @throws IllegalArgumentException If the particle effect requires water and none is at the center location
  425. * @see ParticleEffectPacket
  426. * @see ParticleEffectPacket#sendTo(Location, List)
  427. */
  428. public void display(float offsetX, float offsetY, float offsetZ, float speed, int amount, Location center, List<Player> players) throws IllegalArgumentException {
  429. if (requiresWater && !isWater(center)) {
  430. throw new IllegalArgumentException("There is no water at the center location");
  431. }
  432. new ParticleEffectPacket(name, offsetX, offsetY, offsetZ, speed, amount).sendTo(center, players);
  433. }
  434. /**
  435. * Displays an icon crack (item break) particle effect which is only visible for all players within a certain range in the world of @param center
  436. *
  437. * @param id Id of the icon
  438. * @param data Data value
  439. * @param offsetX Maximum distance particles can fly away from the center on the x-axis
  440. * @param offsetY Maximum distance particles can fly away from the center on the y-axis
  441. * @param offsetZ Maximum distance particles can fly away from the center on the z-axis
  442. * @param speed Display speed of the particles
  443. * @param amount Amount of particles
  444. * @param center Center location of the effect
  445. * @param range Range of the visibility (Maximum range for particles is usually 16, but it can differ for some types)
  446. * @see ParticleEffectPacket
  447. * @see ParticleEffectPacket#sendTo(Location, double)
  448. */
  449. public static void displayIconCrack(int id, byte data, float offsetX, float offsetY, float offsetZ, float speed, int amount, Location center, double range) {
  450. new ParticleEffectPacket("iconcrack_" + id + "_" + data, offsetX, offsetY, offsetZ, speed, amount).sendTo(center, range);
  451. }
  452. /**
  453. * Displays an icon crack (item break) particle effect which is only visible for the specified players
  454. *
  455. * @param id Id of the icon
  456. * @param data Data value
  457. * @param offsetX Maximum distance particles can fly away from the center on the x-axis
  458. * @param offsetY Maximum distance particles can fly away from the center on the y-axis
  459. * @param offsetZ Maximum distance particles can fly away from the center on the z-axis
  460. * @param speed Display speed of the particles
  461. * @param amount Amount of particles
  462. * @param center Center location of the effect
  463. * @param players Receivers of the effect
  464. * @see ParticleEffectPacket
  465. * @see ParticleEffectPacket#sendTo(Location, List)
  466. */
  467. public static void displayIconCrack(int id, byte data, float offsetX, float offsetY, float offsetZ, float speed, int amount, Location center, List<Player> players) {
  468. new ParticleEffectPacket("iconcrack_" + id + "_" + data, offsetX, offsetY, offsetZ, speed, amount).sendTo(center, players);
  469. }
  470. /**
  471. * Displays a block crack (block break) particle effect which is only visible for all players within a certain range in the world of @param center
  472. *
  473. * @param id Id of the block
  474. * @param data Data value
  475. * @param offsetX Maximum distance particles can fly away from the center on the x-axis
  476. * @param offsetY Maximum distance particles can fly away from the center on the y-axis
  477. * @param offsetZ Maximum distance particles can fly away from the center on the z-axis
  478. * @param amount Amount of particles
  479. * @param center Center location of the effect
  480. * @param range Range of the visibility (Maximum range for particles is usually 16, but it can differ for some types)
  481. * @throws IllegalArgumentException If the specified id is not a block id
  482. * @see ParticleEffectPacket
  483. * @see ParticleEffectPacket#sendTo(Location, double)
  484. */
  485. public static void displayBlockCrack(int id, byte data, float offsetX, float offsetY, float offsetZ, int amount, Location center, double range) throws IllegalArgumentException {
  486. if (!isBlock(id)) {
  487. throw new IllegalArgumentException("Invalid block id");
  488. }
  489. new ParticleEffectPacket("blockcrack_" + id + "_" + data, offsetX, offsetY, offsetZ, 0, amount).sendTo(center, range);
  490. }
  491. /**
  492. * Displays a block crack (block break) particle effect which is only visible for the specified players
  493. *
  494. * @param id Id of the block
  495. * @param data Data value
  496. * @param offsetX Maximum distance particles can fly away from the center on the x-axis
  497. * @param offsetY Maximum distance particles can fly away from the center on the y-axis
  498. * @param offsetZ Maximum distance particles can fly away from the center on the z-axis
  499. * @param amount Amount of particles
  500. * @param center Center location of the effect
  501. * @param players Receivers of the effect
  502. * @throws IllegalArgumentException If the specified id is not a block id
  503. * @see ParticleEffectPacket
  504. * @see ParticleEffectPacket#sendTo(Location, List)
  505. */
  506. public static void displayBlockCrack(int id, byte data, float offsetX, float offsetY, float offsetZ, int amount, Location center, List<Player> players) throws IllegalArgumentException {
  507. if (!isBlock(id)) {
  508. throw new IllegalArgumentException("Invalid block id");
  509. }
  510. new ParticleEffectPacket("blockcrack_" + id + "_" + data, offsetX, offsetY, offsetZ, 0, amount).sendTo(center, players);
  511. }
  512. /**
  513. * Displays a block dust particle effect which is only visible for all players within a certain range in the world of @param center
  514. *
  515. * @param id Id of the block
  516. * @param data Data value
  517. * @param offsetX Maximum distance particles can fly away from the center on the x-axis
  518. * @param offsetY Maximum distance particles can fly away from the center on the y-axis
  519. * @param offsetZ Maximum distance particles can fly away from the center on the z-axis
  520. * @param speed Display speed of the particles
  521. * @param amount Amount of particles
  522. * @param center Center location of the effect
  523. * @param range Range of the visibility (Maximum range for particles is usually 16, but it can differ for some types)
  524. * @throws IllegalArgumentException If the specified id is not a block id
  525. * @see ParticleEffectPacket
  526. * @see ParticleEffectPacket#sendTo(Location, double)
  527. */
  528. public static void displayBlockDust(int id, byte data, float offsetX, float offsetY, float offsetZ, float speed, int amount, Location center, double range) throws IllegalArgumentException {
  529. if (!isBlock(id)) {
  530. throw new IllegalArgumentException("Invalid block id");
  531. }
  532. new ParticleEffectPacket("blockdust_" + id + "_" + data, offsetX, offsetY, offsetZ, speed, amount).sendTo(center, range);
  533. }
  534. /**
  535. * Displays a block dust particle effect which is only visible for the specified players
  536. *
  537. * @param id Id of the block
  538. * @param data Data value
  539. * @param offsetX Maximum distance particles can fly away from the center on the x-axis
  540. * @param offsetY Maximum distance particles can fly away from the center on the y-axis
  541. * @param offsetZ Maximum distance particles can fly away from the center on the z-axis
  542. * @param speed Display speed of the particles
  543. * @param amount Amount of particles
  544. * @param center Center location of the effect
  545. * @param players Receivers of the effect
  546. * @throws IllegalArgumentException If the specified id is not a block id
  547. * @see ParticleEffectPacket
  548. * @see ParticleEffectPacket#sendTo(Location, List)
  549. */
  550. public static void displayBlockDust(int id, byte data, float offsetX, float offsetY, float offsetZ, float speed, int amount, Location center, List<Player> players) throws IllegalArgumentException {
  551. if (!isBlock(id)) {
  552. throw new IllegalArgumentException("Invalid block id");
  553. }
  554. new ParticleEffectPacket("blockdust_" + id + "_" + data, offsetX, offsetY, offsetZ, speed, amount).sendTo(center, players);
  555. }
  556. /**
  557. * Represents a particle effect packet with all attributes which is used for sending packets to the players
  558. * <p>
  559. * This class is part of the <b>ParticleEffect Library</b> and follows the same usage conditions
  560. *
  561. * @author DarkBlade12
  562. * @since 1.5
  563. */
  564. public static final class ParticleEffectPacket {
  565. private static Constructor<?> packetConstructor;
  566. private static Method getHandle;
  567. private static Field playerConnection;
  568. private static Method sendPacket;
  569. private static boolean initialized;
  570. private final String name;
  571. private final float offsetX;
  572. private final float offsetY;
  573. private final float offsetZ;
  574. private final float speed;
  575. private final int amount;
  576. private Object packet;
  577. /**
  578. * Construct a new particle effect packet
  579. *
  580. * @param name Name of the effect
  581. * @param offsetX Maximum distance particles can fly away from the center on the x-axis
  582. * @param offsetY Maximum distance particles can fly away from the center on the y-axis
  583. * @param offsetZ Maximum distance particles can fly away from the center on the z-axis
  584. * @param speed Display speed of the particles
  585. * @param amount Amount of particles
  586. * @throws IllegalArgumentException If the speed is lower than 0 or the amount is lower than 1
  587. * @see #initialize()
  588. */
  589. public ParticleEffectPacket(String name, float offsetX, float offsetY, float offsetZ, float speed, int amount) throws IllegalArgumentException {
  590. initialize();
  591. if (speed < 0) {
  592. throw new IllegalArgumentException("The speed is lower than 0");
  593. }
  594. this.name = name;
  595. this.offsetX = offsetX;
  596. this.offsetY = offsetY;
  597. this.offsetZ = offsetZ;
  598. this.speed = speed;
  599. this.amount = amount;
  600. }
  601. /**
  602. * Initializes {@link #packetConstructor}, {@link #getHandle}, {@link #playerConnection} and {@link #sendPacket} and sets {@link #initialized} to <code>true</code> if it succeeds
  603. * <p>
  604. * <b>Note:</b> These fields only have to be initialized once, so it will return if {@link #initialized} is already set to <code>true</code>
  605. *
  606. * @throws VersionIncompatibleException if accessed packets, fields or methods differ in your bukkit version
  607. */
  608. public static void initialize() throws VersionIncompatibleException {
  609. if (initialized) {
  610. return;
  611. }
  612. try {
  613. int version = Integer.parseInt(Character.toString(PackageType.getServerVersion().charAt(3)));
  614. Class<?> packetClass = PackageType.MINECRAFT_SERVER.getClass(version < 7 ? "Packet63WorldParticles" : PacketType.PLAY_OUT_WORLD_PARTICLES.getName());
  615. packetConstructor = ReflectionUtils.getConstructor(packetClass);
  616. getHandle = ReflectionUtils.getMethod("CraftPlayer", PackageType.CRAFTBUKKIT_ENTITY, "getHandle");
  617. playerConnection = ReflectionUtils.getField("EntityPlayer", PackageType.MINECRAFT_SERVER, false, "playerConnection");
  618. sendPacket = ReflectionUtils.getMethod(playerConnection.getType(), "sendPacket", PackageType.MINECRAFT_SERVER.getClass("Packet"));
  619. } catch (Exception exception) {
  620. throw new VersionIncompatibleException("Your current bukkit version seems to be incompatible with this library", exception);
  621. }
  622. initialized = true;
  623. }
  624. /**
  625. * Determine if {@link #packetConstructor}, {@link #getHandle}, {@link #playerConnection} and {@link #sendPacket} are initialized
  626. *
  627. * @return Whether these fields are initialized or not
  628. * @see #initialize()
  629. */
  630. public static boolean isInitialized() {
  631. return initialized;
  632. }
  633. /**
  634. * Sends the packet to a single player and caches it
  635. *
  636. * @param center Center location of the effect
  637. * @param player Receiver of the packet
  638. * @throws PacketInstantiationException if instantion fails due to an unknown error
  639. * @throws PacketSendingException if sending fails due to an unknown error
  640. */
  641. public void sendTo(Location center, Player player) throws PacketInstantiationException, PacketSendingException {
  642. if (packet == null) {
  643. try {
  644. packet = packetConstructor.newInstance();
  645. ReflectionUtils.setValue(packet, true, "a", name);
  646. ReflectionUtils.setValue(packet, true, "b", (float) center.getX());
  647. ReflectionUtils.setValue(packet, true, "c", (float) center.getY());
  648. ReflectionUtils.setValue(packet, true, "d", (float) center.getZ());
  649. ReflectionUtils.setValue(packet, true, "e", offsetX);
  650. ReflectionUtils.setValue(packet, true, "f", offsetY);
  651. ReflectionUtils.setValue(packet, true, "g", offsetZ);
  652. ReflectionUtils.setValue(packet, true, "h", speed);
  653. ReflectionUtils.setValue(packet, true, "i", amount);
  654. } catch (Exception exception) {
  655. throw new PacketInstantiationException("Packet instantiation failed", exception);
  656. }
  657. }
  658. try {
  659. sendPacket.invoke(playerConnection.get(getHandle.invoke(player)), packet);
  660. } catch (Exception exception) {
  661. throw new PacketSendingException("Failed to send the packet to player '" + player.getName() + "'", exception);
  662. }
  663. }
  664. /**
  665. * Sends the packet to all players in the list
  666. *
  667. * @param center Center location of the effect
  668. * @param players Receivers of the packet
  669. * @throws IllegalArgumentException If the player list is empty
  670. * @see #sendTo(Location center, Player player)
  671. */
  672. public void sendTo(Location center, List<Player> players) throws IllegalArgumentException {
  673. if (players.isEmpty()) {
  674. throw new IllegalArgumentException("The player list is empty");
  675. }
  676. for (Player player : players) {
  677. sendTo(center, player);
  678. }
  679. }
  680. /**
  681. * Sends the packet to all players in a certain range
  682. *
  683. * @param center Center location of the effect
  684. * @param range Range in which players will receive the packet (Maximum range for particles is usually 16, but it can differ for some types)
  685. * @throws IllegalArgumentException If the range is lower than 1
  686. * @see #sendTo(Location center, Player player)
  687. */
  688. @SuppressWarnings("deprecation")
  689. public void sendTo(Location center, double range) throws IllegalArgumentException {
  690. if (range < 1) {
  691. throw new IllegalArgumentException("The range is lower than 1");
  692. }
  693. String worldName = center.getWorld().getName();
  694. double squared = range * range;
  695. for (Player player : Bukkit.getOnlinePlayers()) {
  696. if (!player.getWorld().getName().equals(worldName) || player.getLocation().distanceSquared(center) > squared) {
  697. continue;
  698. }
  699. sendTo(center, player);
  700. }
  701. }
  702. /**
  703. * Represents a runtime exception that is thrown if a bukkit version is not compatible with this library
  704. * <p>
  705. * This class is part of the <b>ParticleEffect Library</b> and follows the same usage conditions
  706. *
  707. * @author DarkBlade12
  708. * @since 1.5
  709. */
  710. private static final class VersionIncompatibleException extends RuntimeException {
  711. private static final long serialVersionUID = 3203085387160737484L;
  712. /**
  713. * Construct a new version incompatible exception
  714. *
  715. * @param message Message that will be logged
  716. * @param cause Cause of the exception
  717. */
  718. public VersionIncompatibleException(String message, Throwable cause) {
  719. super(message, cause);
  720. }
  721. }
  722. /**
  723. * Represents a runtime exception that is thrown if packet instantiation fails
  724. * <p>
  725. * This class is part of the <b>ParticleEffect Library</b> and follows the same usage conditions
  726. *
  727. * @author DarkBlade12
  728. * @since 1.4
  729. */
  730. private static final class PacketInstantiationException extends RuntimeException {
  731. private static final long serialVersionUID = 3203085387160737484L;
  732. /**
  733. * Construct a new packet instantiation exception
  734. *
  735. * @param message Message that will be logged
  736. * @param cause Cause of the exception
  737. */
  738. public PacketInstantiationException(String message, Throwable cause) {
  739. super(message, cause);
  740. }
  741. }
  742. /**
  743. * Represents a runtime exception that is thrown if packet sending fails
  744. * <p>
  745. * This class is part of the <b>ParticleEffect Library</b> and follows the same usage conditions
  746. *
  747. * @author DarkBlade12
  748. * @since 1.4
  749. */
  750. private static final class PacketSendingException extends RuntimeException {
  751. private static final long serialVersionUID = 3203085387160737484L;
  752. /**
  753. * Construct a new packet sending exception
  754. *
  755. * @param message Message that will be logged
  756. * @param cause Cause of the exception
  757. */
  758. public PacketSendingException(String message, Throwable cause) {
  759. super(message, cause);
  760. }
  761. }
  762. }
  763. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement