Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.88 KB | None | 0 0
  1. package com.rs.game.player.content;
  2.  
  3. import java.io.Serializable;
  4. import java.util.HashMap;
  5.  
  6. import com.rs.game.item.Item;
  7. import com.rs.game.player.Bank;
  8. import com.rs.game.player.Player;
  9.  
  10. public class PresetSetups implements Serializable {
  11.  
  12. private static final long serialVersionUID = -8227328154529000367L;
  13.  
  14. private String name;
  15. private Item[] equipment, inventory;
  16. private int spellBook, prayers;
  17.  
  18. // TODO
  19. // - Level checks before equipping items [DONE]
  20. // - Bank worn equipment and inventory items prior to loading the setup, to
  21. // prevent deleting items. [DONE]
  22. // - If unable to bank, return and notify the user. [DONE (partially, not
  23. // properly; items will still be safe)]
  24. // - If item id noted, add item in non-noted form [DONE (in Bank class,
  25. // addItem method)]
  26.  
  27. /**
  28. * @param name
  29. * - The name of the preset; presets are called via the name.
  30. * @param equipment
  31. * - The players current equipment setup; sets the players
  32. * equipment
  33. * @param inventory
  34. * - The players current inventory setup; sets the players
  35. * inventory
  36. * @param spellBook
  37. * - The players current spellbook; sets the players spellbook
  38. * @param prayers
  39. * - The players current prayer book; sets the players prayer
  40. * book
  41. */
  42. public PresetSetups(String name, Item[] equipment, Item[] inventory, int spellBook, int prayers) {
  43. this.name = name;
  44. this.equipment = equipment;
  45. this.inventory = inventory;
  46. this.spellBook = spellBook;
  47. this.prayers = prayers;
  48. }
  49.  
  50. public static void giveSet(Player player, PresetSetups set) {
  51. /**
  52. * Ensures that both the player and the set are not null to prevent
  53. * deadlocks
  54. */
  55. if (set == null || player == null) {
  56. return;
  57. }
  58. /**
  59. * Only allows for sets to be loaded whilst banking unless the player is
  60. * an owner
  61. */
  62. if (!player.getInterfaceManager().containsInterface(762) && player.getRights() != 4) {
  63. player.getPackets().sendGameMessage("You can only load a preset setup while banking.");
  64. return;
  65. }
  66. /**
  67. * The players current inventory (copy to prevent editing the items)
  68. */
  69. Item inventory[] = player.getInventory().getItems().getItemsCopy();
  70. if (inventory != null) {
  71. if (Bank.MAX_BANK_SIZE - player.getBank().getBankSize() < player.getInventory().getItems().getSize()) {
  72. player.getPackets().sendGameMessage(
  73. "Preset cancelled as there was not enough space in your bank to load the configuration.");
  74. return;
  75. }
  76. for (int i = 0; i < inventory.length; i++) {
  77. if (inventory[i] == null || inventory[i].getAmount() < 1
  78. || !player.getInventory().containsItem(inventory[i].getId(), inventory[i].getAmount())) {
  79. continue;
  80. }
  81. player.getBank().addItem(inventory[i].getId(), inventory[i].getAmount(), true);
  82. player.getInventory().deleteItem(inventory[i].getId(), inventory[i].getAmount());
  83. }
  84. player.getPackets().sendGameMessage("All items in your inventory have been added to your bank.", true);
  85. }
  86. /**
  87. * The players current equipment (copy to prevent editing the items)
  88. */
  89. Item equipment[] = player.getEquipment().getItems().getItemsCopy();
  90. if (equipment != null) {
  91. if (Bank.MAX_BANK_SIZE - player.getBank().getBankSize() < player.getEquipment().getItems().getSize()) {
  92. player.getPackets().sendGameMessage(
  93. "Preset cancelled as there was not enough space in your bank to load the configuration.");
  94. return;
  95. }
  96. for (int i = 0; i < equipment.length; i++) {
  97. if (equipment[i] == null || equipment[i].getAmount() < 1) {
  98. continue;
  99. }
  100. player.getBank().addItem(equipment[i].getId(), equipment[i].getAmount(), true);
  101. player.getEquipment().deleteItem(equipment[i].getId(), equipment[i].getAmount());
  102. player.getEquipment().refresh(i);
  103. }
  104. player.getPackets().sendGameMessage("All of your equipment has been added to your bank.", true);
  105. }
  106. /**
  107. * Sets the players spellbook to their saved spellbook (ancient /
  108. * standard / lunar) - Utilises an int
  109. */
  110. player.getCombatDefinitions().setSpellBook(set.getSpellBook());
  111. /**
  112. * Sets the players prayer book to their saved prayer book (curses /
  113. * standard) - Utilises a boolean
  114. */
  115. player.getPrayer().setPrayerBook(set.getPrayers() == 1);
  116. /**
  117. * Checks that the player's saved equipment isn't equal to null (null
  118. * check and speed optimisation) Then, if it's not null, proceeds to
  119. * loop through the size of the array, with null checks for items and
  120. * set the players equipment to the alloted slots
  121. */
  122. if (set.getEquipment() != null) {
  123. for (int i = 0; i < set.getEquipment().length; i++) {
  124. if (set.getEquipment()[i] == null || player.getBank().getItem(set.getEquipment()[i].getId()) == null) {
  125. continue;
  126. }
  127. int amount = set.getEquipment()[i].getAmount();
  128. if (amount > player.getBank().getItem(set.getEquipment()[i].getId()).getAmount()) {
  129. amount = player.getBank().getItem(set.getEquipment()[i].getId()).getAmount();
  130. }
  131. if (amount <= 0) {
  132. continue;
  133. }
  134. /**
  135. * The requirements to equip the selected item (will include
  136. * comp cape etc, etc)
  137. */
  138. HashMap<Integer, Integer> requirements = set.getEquipment()[i].getDefinitions()
  139. .getWearingSkillRequiriments();
  140. boolean hasRequirements = true;
  141. if (requirements != null) {
  142. for (int skillId : requirements.keySet()) {
  143. /**
  144. * If the skill is an invalid id; continue
  145. */
  146. if (skillId > 24 || skillId < 0) {
  147. continue;
  148. }
  149. int level = requirements.get(skillId);
  150. /**
  151. * If the level is an invalid level; continue
  152. */
  153. if (level < 0 || level > 120) {
  154. continue;
  155. }
  156. /**
  157. * If the player doesn't meet the wearing requirements
  158. * of the item; continue
  159. */
  160. if (player.getSkills().getLevelForXp(skillId) < level) {
  161. player.getPackets()
  162. .sendGameMessage("You were unable to equip your "
  163. + set.getEquipment()[i].getName().toLowerCase()
  164. + ", as you don't meet the requirements to wear them.", true);
  165. hasRequirements = false;
  166. continue;
  167. }
  168. }
  169. }
  170. /**
  171. * If the player doesn't meet the requirements; continue
  172. */
  173. if (!hasRequirements) {
  174. continue;
  175. }
  176. player.getBank().removeItem(player.getBank().getItemSlot(set.getEquipment()[i].getId()), amount, true,
  177. false);
  178. player.getEquipment().getItems().set(i, new Item(set.getEquipment()[i].getId(), amount));
  179. player.getEquipment().refresh(i);
  180. }
  181. }
  182. /**
  183. * Checks that the player's saved inventory isn't equal to null (null
  184. * check and speed optimisation) Then, if it's not null, proceeds to
  185. * loop through the size of the array, with null checks for items and
  186. * set the players inventory to the alloted slots
  187. *
  188. * Item requirements do not need to be checked for the inventory, as
  189. * there are no requirements to place something in your inventory.
  190. */
  191. if (set.getInventory() != null) {
  192. for (int i = 0; i < set.getInventory().length; i++) {
  193. if (set.getInventory()[i] == null || player.getBank().getItem(set.getInventory()[i].getId()) == null) {
  194. continue;
  195. }
  196. int amount = set.getInventory()[i].getAmount();
  197. if (amount > player.getBank().getItem(set.getInventory()[i].getId()).getAmount()) {
  198. amount = player.getBank().getItem(set.getInventory()[i].getId()).getAmount();
  199. }
  200. if (amount <= 0) {
  201. continue;
  202. }
  203. player.getBank().removeItem(player.getBank().getItemSlot(set.getInventory()[i].getId()), amount, true,
  204. false);
  205. player.getInventory().getItems().set(i, new Item(set.getInventory()[i].getId(), amount));
  206. }
  207. }
  208. /**
  209. * Refreshes the players inventory (all slots at once, rather than doing
  210. * it each loop - used for optimisation and speed)
  211. */
  212. player.getInventory().refresh();
  213. /**
  214. * Generates the players appearance data block, so that they and all
  215. * others around them see the change in gear / appearance
  216. */
  217. player.getAppearence().generateAppearenceData();
  218. /**
  219. * Notifies the player that they were successful in loading their setup
  220. */
  221. player.getPackets().sendGameMessage("Loaded setup: " + set.name + ".");
  222. }
  223.  
  224. /**
  225. * @return - Returns the name of the preset
  226. */
  227. public String getName() {
  228. return name;
  229. }
  230.  
  231. /**
  232. * @return - Returns the equipment saved in the preset
  233. */
  234. public Item[] getEquipment() {
  235. return equipment;
  236. }
  237.  
  238. /**
  239. * @return - Returns the inventory saved in the preset
  240. */
  241. public Item[] getInventory() {
  242. return inventory;
  243. }
  244.  
  245. /**
  246. * @return - Returns the prayer book saved in the preset
  247. */
  248. public int getPrayers() {
  249. return prayers;
  250. }
  251.  
  252. /**
  253. * @return - Returns the spellbook saved in the preset
  254. */
  255. public int getSpellBook() {
  256. return spellBook;
  257. }
  258.  
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement