Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.13 KB | None | 0 0
  1. package com.arlania.world.content.combat.prayer;
  2.  
  3. import java.util.HashMap;
  4.  
  5. import com.arlania.engine.task.Task;
  6. import com.arlania.engine.task.TaskManager;
  7. import com.arlania.model.PlayerRights;
  8. import com.arlania.model.Prayerbook;
  9. import com.arlania.model.Skill;
  10. import com.arlania.model.Locations.Location;
  11. import com.arlania.util.NameUtils;
  12. import com.arlania.world.content.Sounds;
  13. import com.arlania.world.content.Sounds.Sound;
  14. import com.arlania.world.content.combat.CombatType;
  15. import com.arlania.world.content.minigames.impl.Dueling;
  16. import com.arlania.world.content.minigames.impl.Dueling.DuelRule;
  17. import com.arlania.world.entity.impl.player.Player;
  18.  
  19.  
  20. /**
  21. * All of the prayers that can be activated and deactivated. This currently only
  22. * has support for prayers present in the <b>317 protocol</b>.
  23. *
  24. * @author Gabriel
  25. */
  26. public class PrayerHandler {
  27.  
  28. /**
  29. * Represents a prayer's configurations, such as their
  30. * level requirement, buttonId, configId and drain rate.
  31. *
  32. * @author relex lawl
  33. */
  34.  
  35.  
  36. private enum PrayerData {
  37.  
  38. THICK_SKIN(1, .1, 25000, 83),
  39. BURST_OF_STRENGTH(4, .1, 25002, 84),
  40. CLARITY_OF_THOUGHT(7, .1, 25004, 85),
  41. SHARP_EYE(8, .1, 25006, 601),
  42. MYSTIC_WILL(9, .1, 25008, 602),
  43. ROCK_SKIN(10, .1, 25010, 86),
  44. SUPERHUMAN_STRENGTH(13, .2, 25012, 87),
  45. IMPROVED_REFLEXES(16, .2, 25014, 88),
  46. RAPID_RESTORE(19, .4, 25016, 89),
  47. RAPID_HEAL(22, .1, 25018, 90),
  48. PROTECT_ITEM(25, .1, 25020, 91),
  49. HAWK_EYE(26, .1, 25022, 603),
  50. MYSTIC_LORE(27, .3, 25024, 604),
  51. STEEL_SKIN(28, .3, 25026, 92),
  52. ULTIMATE_STRENGTH(31, .4, 25028, 93),
  53. INCREDIBLE_REFLEXES(34, .4, 25030, 94),
  54. PROTECT_FROM_MAGIC(37, .5, 25032, 95, 2),
  55. PROTECT_FROM_MISSILES(40, .5, 25034, 96, 1),
  56. PROTECT_FROM_MELEE(43, .5, 25036, 97, 0),
  57. EAGLE_EYE(44, .5, 25038, 605),
  58. MYSTIC_MIGHT(45, .5, 25040, 606),
  59. RETRIBUTION(46, .5, 25042, 98, 4),
  60. REDEMPTION(49, .5, 25044, 99, 5),
  61. SMITE(52, 1, 25046, 100, 685, 6),
  62. CHIVALRY(60, 1, 25048, 607),
  63. PIETY(70, 1, 25050, 608),
  64. RIGOUR(80, 2, 18021, 708),
  65. AUGURY(80, 2, 18027, 709);
  66.  
  67.  
  68. private PrayerData(int requirement, double drainRate, int buttonId, int configId, int... hint) {
  69. this.requirement = requirement;
  70. this.drainRate = drainRate;
  71. this.buttonId = buttonId;
  72. this.configId = configId;
  73. if (hint.length > 0)
  74. this.hint = hint[0];
  75. }
  76.  
  77. /**
  78. * The prayer's level requirement for player to be able
  79. * to activate it.
  80. */
  81. private int requirement;
  82.  
  83. /**
  84. * The prayer's action button id in prayer tab.
  85. */
  86. private int buttonId;
  87.  
  88. /**
  89. * The prayer's config id to switch their glow on/off by
  90. * sending the sendConfig packet.
  91. */
  92. private int configId;
  93.  
  94. /**
  95. * The prayer's drain rate as which it will drain
  96. * the associated player's prayer points.
  97. */
  98. private double drainRate;
  99.  
  100. /**
  101. * The prayer's head icon hint index.
  102. */
  103. private int hint = -1;
  104.  
  105. /**
  106. * The prayer's formatted name.
  107. */
  108. private String name;
  109.  
  110. /**
  111. * Gets the prayer's formatted name.
  112. * @return The prayer's name
  113. */
  114. private final String getPrayerName() {
  115. if (name == null)
  116. return NameUtils.capitalizeWords(toString().toLowerCase().replaceAll("_", " "));
  117. return name;
  118. }
  119.  
  120. /**
  121. * Contains the PrayerData with their corresponding prayerId.
  122. */
  123. private static HashMap <Integer, PrayerData> prayerData = new HashMap <Integer, PrayerData> ();
  124.  
  125. /**
  126. * Contains the PrayerData with their corresponding buttonId.
  127. */
  128. private static HashMap <Integer, PrayerData> actionButton = new HashMap <Integer, PrayerData> ();
  129.  
  130. /**
  131. * Populates the prayerId and buttonId maps.
  132. */
  133. static {
  134. for (PrayerData pd : PrayerData.values()) {
  135. prayerData.put(pd.ordinal(), pd);
  136. actionButton.put(pd.buttonId, pd);
  137. }
  138. }
  139. }
  140.  
  141. /**
  142. * Gets the protecting prayer based on the argued combat type.
  143. *
  144. * @param type
  145. * the combat type.
  146. * @return the protecting prayer.
  147. */
  148. public static int getProtectingPrayer(CombatType type) {
  149. switch (type) {
  150. case MELEE:
  151. return PROTECT_FROM_MELEE;
  152. case MAGIC:
  153. case DRAGON_FIRE:
  154. return PROTECT_FROM_MAGIC;
  155. case RANGED:
  156. return PROTECT_FROM_MISSILES;
  157. default:
  158. throw new IllegalArgumentException("Invalid combat type: " + type);
  159. }
  160. }
  161.  
  162. public static boolean isActivated(Player player, int prayer) {
  163. return player.getPrayerActive()[prayer];
  164. }
  165.  
  166. /**
  167. * Activates a prayer with specified <code>buttonId</code>.
  168. * @param player The player clicking on prayer button.
  169. * @param buttonId The button the player is clicking.
  170. */
  171. public static void togglePrayerWithActionButton(Player player, final int buttonId) {
  172. for (PrayerData pd : PrayerData.values()) {
  173. if (buttonId == pd.buttonId) {
  174. if (!player.getPrayerActive()[pd.ordinal()])
  175. activatePrayer(player, pd.ordinal());
  176. else
  177. deactivatePrayer(player, pd.ordinal());
  178. }
  179. }
  180. }
  181.  
  182. /**
  183. * Activates said prayer with specified <code>prayerId</code> and de-activates
  184. * all non-stackable prayers.
  185. * @param player The player activating prayer.
  186. * @param prayerId The id of the prayer being turned on, also known as the ordinal in the respective enum.
  187. */
  188. public static void activatePrayer(Player player, final int prayerId) {
  189. if(player.getPrayerbook() == Prayerbook.CURSES) {
  190. deactivatePrayers(player);
  191. return;
  192. }
  193. if (player.getPrayerActive()[prayerId])
  194. return;
  195. if(Dueling.checkRule(player, DuelRule.NO_PRAYER)) {
  196. player.getPacketSender().sendMessage("Prayer has been disabled in this duel.");
  197. CurseHandler.deactivateAll(player);
  198. PrayerHandler.deactivateAll(player);
  199. return;
  200. }
  201. if(player.getLocation() == Location.RECIPE_FOR_DISASTER) {
  202. player.getPacketSender().sendMessage("For some reason, your prayers do not have any effect in here.");
  203. CurseHandler.deactivateAll(player);
  204. PrayerHandler.deactivateAll(player);
  205. return;
  206. }
  207. PrayerData pd = PrayerData.prayerData.get(prayerId);
  208. if (player.getSkillManager().getCurrentLevel(Skill.PRAYER) <= 0) {
  209. player.getPacketSender().sendConfig(pd.configId, 0);
  210. player.getPacketSender().sendMessage("You do not have enough Prayer points. You can recharge your points at an altar.");
  211. return;
  212. }
  213. if (player.getSkillManager().getMaxLevel(Skill.PRAYER) < (pd.requirement * 10)) {
  214. player.getPacketSender().sendConfig(pd.configId, 0);
  215. player.getPacketSender().sendMessage("You need a Prayer level of at least " + pd.requirement + " to use " + pd.getPrayerName() + ".");
  216. return;
  217. }
  218. if (prayerId == CHIVALRY && player.getSkillManager().getMaxLevel(Skill.DEFENCE) < 60) {
  219. player.getPacketSender().sendConfig(pd.configId, 0);
  220. player.getPacketSender().sendMessage("You need a Defence level of at least 60 to use Chivalry.");
  221. return;
  222. }
  223. if (prayerId == PIETY && player.getSkillManager().getMaxLevel(Skill.DEFENCE) < 70) {
  224. player.getPacketSender().sendConfig(pd.configId, 0);
  225. player.getPacketSender().sendMessage("You need a Defence level of at least 70 to use Piety.");
  226. return;
  227. }
  228. if (prayerId == RIGOUR && player.getSkillManager().getMaxLevel(Skill.DEFENCE) < 80) {
  229. player.getPacketSender().sendConfig(pd.configId, 0);
  230. player.getPacketSender().sendMessage("You need a Defence level of at least 80 to use Rigour.");
  231. return;
  232. }
  233. if (prayerId == AUGURY && player.getSkillManager().getMaxLevel(Skill.DEFENCE) < 80) {
  234. player.getPacketSender().sendConfig(pd.configId, 0);
  235. player.getPacketSender().sendMessage("You need a Defence level of at least 80 to use Augury.");
  236. return;
  237. }
  238. switch (prayerId) {
  239. case THICK_SKIN:
  240. case ROCK_SKIN:
  241. case STEEL_SKIN:
  242. resetPrayers(player, DEFENCE_PRAYERS, prayerId);
  243. break;
  244. case BURST_OF_STRENGTH:
  245. case SUPERHUMAN_STRENGTH:
  246. case ULTIMATE_STRENGTH:
  247. resetPrayers(player, STRENGTH_PRAYERS, prayerId);
  248. resetPrayers(player, RANGED_PRAYERS, prayerId);
  249. resetPrayers(player, MAGIC_PRAYERS, prayerId);
  250. break;
  251. case CLARITY_OF_THOUGHT:
  252. case IMPROVED_REFLEXES:
  253. case INCREDIBLE_REFLEXES:
  254. resetPrayers(player, ATTACK_PRAYERS, prayerId);
  255. resetPrayers(player, RANGED_PRAYERS, prayerId);
  256. resetPrayers(player, MAGIC_PRAYERS, prayerId);
  257. break;
  258. case SHARP_EYE:
  259. case HAWK_EYE:
  260. case EAGLE_EYE:
  261. case MYSTIC_WILL:
  262. case MYSTIC_LORE:
  263. case MYSTIC_MIGHT:
  264. resetPrayers(player, STRENGTH_PRAYERS, prayerId);
  265. resetPrayers(player, ATTACK_PRAYERS, prayerId);
  266. resetPrayers(player, RANGED_PRAYERS, prayerId);
  267. resetPrayers(player, MAGIC_PRAYERS, prayerId);
  268. break;
  269. case CHIVALRY:
  270. case PIETY:
  271. resetPrayers(player, DEFENCE_PRAYERS, prayerId);
  272. resetPrayers(player, STRENGTH_PRAYERS, prayerId);
  273. resetPrayers(player, ATTACK_PRAYERS, prayerId);
  274. resetPrayers(player, RANGED_PRAYERS, prayerId);
  275. resetPrayers(player, MAGIC_PRAYERS, prayerId);
  276. break;
  277. case PROTECT_FROM_MAGIC:
  278. case PROTECT_FROM_MISSILES:
  279. case PROTECT_FROM_MELEE:
  280. resetPrayers(player, OVERHEAD_PRAYERS, prayerId);
  281. break;
  282. case RIGOUR:
  283. case AUGURY:
  284. resetPrayers(player, DEFENCE_PRAYERS, prayerId);
  285. resetPrayers(player, STRENGTH_PRAYERS, prayerId);
  286. resetPrayers(player, ATTACK_PRAYERS, prayerId);
  287. resetPrayers(player, RANGED_PRAYERS, prayerId);
  288. resetPrayers(player, MAGIC_PRAYERS, prayerId);
  289. break;
  290. case RETRIBUTION:
  291. case REDEMPTION:
  292. case SMITE:
  293. resetPrayers(player, OVERHEAD_PRAYERS, prayerId);
  294. break;
  295. }
  296. /*if (player.isPrayerInjured()) {
  297. if (prayerId == PROTECT_FROM_MAGIC ||
  298. prayerId == PROTECT_FROM_MISSILES || prayerId == PROTECT_FROM_MELEE) {
  299. player.getPacketSender().sendMessage("You have been injured and cannot use this prayer!");
  300. player.getPacketSender().sendConfig(pd.configId, 0);
  301. return;
  302. }
  303. }
  304. SoundEffects.sendSoundEffect(player, SoundEffects.SoundData.ACTIVATE_PRAYER_OR_CURSE, 10, 0);*/
  305. player.setPrayerActive(prayerId, true);
  306. player.getPacketSender().sendConfig(pd.configId, 1);
  307. if (hasAnyPrayerOn(player) || (hasNoPrayerOn(player, prayerId) && !player.isDrainingPrayer()))
  308. startDrain(player);
  309. if (pd.hint != -1) {
  310. int hintId = getHeadHint(player);
  311. player.getAppearance().setHeadHint(hintId);
  312. }
  313. Sounds.sendSound(player, Sound.ACTIVATE_PRAYER_OR_CURSE);
  314. }
  315.  
  316. /**
  317. * Deactivates said prayer with specified <code>prayerId</code>.
  318. * @param player The player deactivating prayer.
  319. * @param prayerId The id of the prayer being deactivated.
  320. */
  321. public static void deactivatePrayer(Player player, int prayerId) {
  322. if (!player.getPrayerActive()[prayerId])
  323. return;
  324. PrayerData pd = PrayerData.prayerData.get(prayerId);
  325. player.getPrayerActive()[prayerId] = false;
  326. player.getPacketSender().sendConfig(pd.configId, 0);
  327. if (pd.hint != -1) {
  328. int hintId = getHeadHint(player);
  329. player.getAppearance().setHeadHint(hintId);
  330. }
  331. Sounds.sendSound(player, Sound.DEACTIVATE_PRAYER_OR_CURSE);
  332. }
  333.  
  334. /**
  335. * Deactivates every prayer in the player's prayer book.
  336. * @param player The player to deactivate prayers for.
  337. */
  338. public static void deactivatePrayers(Player player) {
  339. for (int i = 0; i < player.getPrayerActive().length; i++) {
  340. if (player.getPrayerActive()[i]) {
  341. deactivatePrayer(player, i);
  342. }
  343. }
  344. }
  345.  
  346. public static void deactivateAll(Player player) {
  347. for (int i = 0; i < player.getPrayerActive().length; i++) {
  348. PrayerData pd = PrayerData.prayerData.get(i);
  349. if(pd == null)
  350. continue;
  351. player.getPrayerActive()[i] = false;
  352. player.getPacketSender().sendConfig(pd.configId, 0);
  353. if (pd.hint != -1) {
  354. int hintId = getHeadHint(player);
  355. player.getAppearance().setHeadHint(hintId);
  356. }
  357. }
  358. player.setDrainingPrayer(false);
  359. }
  360.  
  361. /**
  362. * Gets the player's current head hint if they activate or deactivate
  363. * a head prayer.
  364. * @param player The player to fetch head hint index for.
  365. * @return The player's current head hint index.
  366. */
  367. private static int getHeadHint(Player player) {
  368. boolean[] prayers = player.getPrayerActive();
  369. if (prayers[PROTECT_FROM_MELEE])
  370. return 0;
  371. if (prayers[PROTECT_FROM_MISSILES])
  372. return 1;
  373. if (prayers[PROTECT_FROM_MAGIC])
  374. return 2;
  375. if (prayers[RETRIBUTION])
  376. return 3;
  377. if (prayers[SMITE])
  378. return 4;
  379. if (prayers[REDEMPTION])
  380. return 5;
  381. return -1;
  382. }
  383.  
  384. /**
  385. * Initializes the player's prayer drain once a first prayer
  386. * has been selected.
  387. * @param player The player to start prayer drain for.
  388. */
  389. private static void startDrain(final Player player) {
  390. if (getDrain(player) <= 0 && !player.isDrainingPrayer())
  391. return;
  392. player.setDrainingPrayer(true);
  393. TaskManager.submit(new Task(1, player, true) {
  394. @Override
  395. public void execute() {
  396. if (!player.isDrainingPrayer()) {
  397. this.stop();
  398. return;
  399. }
  400. //if((player.getRights() == PlayerRights.EXTREME_DONATOR || player.getRights() == PlayerRights.ADMINISTRATOR || player.getRights() == PlayerRights.MODERATOR|| player.getRights() == PlayerRights.SUPPORT|| player.getRights() == PlayerRights.LEGENDARY_DONATOR || player.getRights() == PlayerRights.UBER_DONATOR) && player.getLocation() != Location.WILDERNESS)
  401. //return;
  402. if (player.getSkillManager().getCurrentLevel(Skill.PRAYER) <= 0) {
  403. for (int i = 0; i < player.getPrayerActive().length; i++) {
  404. if (player.getPrayerActive()[i])
  405. deactivatePrayer(player, i);
  406. }
  407. Sounds.sendSound(player, Sound.RUN_OUT_OF_PRAYER_POINTS);
  408. player.getPacketSender().sendMessage("You have run out of Prayer points!");
  409. this.stop();
  410. return;
  411. }
  412. double drainAmount = getDrain(player);
  413. if(player.getLocation() != Location.WILDERNESS) {
  414. //if(player.getRights() == PlayerRights.EXTREME_DONATOR || player.getRights() == PlayerRights.ADMINISTRATOR || player.getRights() == PlayerRights.MODERATOR|| player.getRights() == PlayerRights.SUPPORT|| player.getRights() == PlayerRights.LEGENDARY_DONATOR || player.getRights() == PlayerRights.UBER_DONATOR) {
  415. // drainAmount = 0.5;
  416. //}
  417. }
  418. //if (drainAmount <= 0) {
  419. // this.stop();
  420. // return;
  421. //}
  422. int total = (int) (player.getSkillManager().getCurrentLevel(Skill.PRAYER) - drainAmount);
  423. player.getSkillManager().setCurrentLevel(Skill.PRAYER, total, true);
  424. }
  425. @Override
  426. public void stop() {
  427. setEventRunning(false);
  428. player.setDrainingPrayer(false);
  429. }
  430. });
  431. }
  432.  
  433. /**
  434. * Gets the amount of prayer to drain for <code>player</code>.
  435. * @param player The player to get drain amount for.
  436. * @return The amount of prayer that will be drained from the player.
  437. */
  438. private static final double getDrain(Player player) {
  439. double toRemove = 0.0;
  440. for (int i = 0; i < player.getPrayerActive().length; i++) {
  441. if (player.getPrayerActive()[i]) {
  442. PrayerData prayerData = PrayerData.prayerData.get(i);
  443. toRemove += prayerData.drainRate / 1;
  444. }
  445. }
  446. if (toRemove > 0) {
  447. toRemove /= (1 + (0.05 * player.getBonusManager().getOtherBonus()[2]));
  448. }
  449. return toRemove;
  450. }
  451.  
  452. /**
  453. * Checks if a player has no prayer on.
  454. * @param player The player to check prayer status for.
  455. * @param exceptionId The prayer id currently being turned on/activated.
  456. * @return if <code>true</code>, it means player has no prayer on besides <code>exceptionId</code>.
  457. */
  458. public static final boolean hasNoPrayerOn(Player player, int exceptionId) {
  459. int prayersOn = 0;
  460. for (int i = 0; i < player.getPrayerActive().length; i++) {
  461. if (player.getPrayerActive()[i] && i != exceptionId)
  462. prayersOn++;
  463. }
  464. return prayersOn == 0;
  465. }
  466.  
  467.  
  468. public static final boolean hasAnyPrayerOn(Player player) {
  469. for (int i = 0; i < player.getPrayerActive().length; i++) {
  470. if (player.getPrayerActive()[i])
  471. return true;
  472. }
  473. return false;
  474. }
  475. /**
  476. * Resets <code> prayers </code> with an exception for <code> prayerID </code>
  477. *
  478. * @param prayers The array of prayers to reset
  479. * @param prayerID The prayer ID to not turn off (exception)
  480. */
  481. public static void resetPrayers(Player player, int[] prayers, int prayerID) {
  482. for (int i = 0; i < prayers.length; i++) {
  483. if (prayers[i] != prayerID)
  484. deactivatePrayer(player, prayers[i]);
  485. }
  486. }
  487.  
  488. /**
  489. * Checks if action button ID is a prayer button.
  490. *
  491. * @param buttonId
  492. * action button being hit.
  493. */
  494. public static final boolean isButton(final int actionButtonID) {
  495. return PrayerData.actionButton.containsKey(actionButtonID);
  496. }
  497.  
  498. public static final int THICK_SKIN = 0, BURST_OF_STRENGTH = 1, CLARITY_OF_THOUGHT = 2, SHARP_EYE = 3, MYSTIC_WILL = 4,
  499. ROCK_SKIN = 5, SUPERHUMAN_STRENGTH = 6, IMPROVED_REFLEXES = 7, RAPID_RESTORE = 8, RAPID_HEAL = 9,
  500. PROTECT_ITEM = 10, HAWK_EYE = 11, MYSTIC_LORE = 12, STEEL_SKIN = 13, ULTIMATE_STRENGTH = 14,
  501. INCREDIBLE_REFLEXES = 15, PROTECT_FROM_MAGIC = 16, PROTECT_FROM_MISSILES = 17,
  502. PROTECT_FROM_MELEE = 18, EAGLE_EYE = 19, MYSTIC_MIGHT = 20, RETRIBUTION = 21, REDEMPTION = 22, SMITE = 23, CHIVALRY = 24,
  503. PIETY = 25, RIGOUR = 26, AUGURY = 27;
  504.  
  505. /**
  506. * Contains every prayer that counts as a defense prayer.
  507. */
  508. private static final int[] DEFENCE_PRAYERS = {THICK_SKIN, ROCK_SKIN, STEEL_SKIN, CHIVALRY, PIETY, RIGOUR, AUGURY};
  509.  
  510. /**
  511. * Contains every prayer that counts as a strength prayer.
  512. */
  513. private static final int[] STRENGTH_PRAYERS = {BURST_OF_STRENGTH, SUPERHUMAN_STRENGTH, ULTIMATE_STRENGTH, CHIVALRY, PIETY};
  514.  
  515. /**
  516. * Contains every prayer that counts as an attack prayer.
  517. */
  518. private static final int[] ATTACK_PRAYERS = {CLARITY_OF_THOUGHT, IMPROVED_REFLEXES, INCREDIBLE_REFLEXES, CHIVALRY, PIETY};
  519.  
  520. /**
  521. * Contains every prayer that counts as a ranged prayer.
  522. */
  523. private static final int[] RANGED_PRAYERS = {SHARP_EYE, HAWK_EYE, EAGLE_EYE, RIGOUR};
  524.  
  525. /**
  526. * Contains every prayer that counts as a magic prayer.
  527. */
  528. private static final int[] MAGIC_PRAYERS = {MYSTIC_WILL, MYSTIC_LORE, MYSTIC_MIGHT, AUGURY};
  529.  
  530. /**
  531. * Contains every prayer that counts as an overhead prayer, excluding protect from summoning.
  532. */
  533. public static final int[] OVERHEAD_PRAYERS = {PROTECT_FROM_MAGIC, PROTECT_FROM_MISSILES, PROTECT_FROM_MELEE, RETRIBUTION, REDEMPTION, SMITE};
  534.  
  535. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement