Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.93 KB | None | 0 0
  1. package org.brutality.model.items;
  2.  
  3. import java.util.NoSuchElementException;
  4.  
  5. /**
  6. * The container that represents an item definition.
  7. *
  8. * @author lare96 <http://github.com/lare96>
  9. */
  10. public final class ItemDefinition {
  11.  
  12. /**
  13. * The array that contains all of the item definitions.
  14. */
  15. public static final ItemDefinition[] DEFINITIONS = new ItemDefinition[22722];
  16.  
  17. /**
  18. * The identifier for the item.
  19. */
  20. private final int id;
  21.  
  22. /**
  23. * The proper name of the item.
  24. */
  25. private final String name;
  26.  
  27. /**
  28. * The description of the item.
  29. */
  30. private final String description;
  31.  
  32. /**
  33. * The equipment slot of this item.
  34. */
  35. private final int equipmentSlot;
  36.  
  37. /**
  38. * The flag that determines if the item is noteable.
  39. */
  40. private final boolean noteable;
  41.  
  42. /**
  43. * The flag that determines if the item is noted.
  44. */
  45. private final boolean noted;
  46.  
  47. /**
  48. * The flag that determines if the item is stackable.
  49. */
  50. private final boolean stackable;
  51.  
  52. /**
  53. * The special store price of this item.
  54. */
  55. private final int specialPrice;
  56.  
  57. /**
  58. * The general store price of this item.
  59. */
  60. private final int generalPrice;
  61.  
  62. /**
  63. * The low alch value of this item.
  64. */
  65. private final int lowAlchValue;
  66.  
  67. /**
  68. * The high alch value of this item.
  69. */
  70. private final int highAlchValue;
  71.  
  72. /**
  73. * The weight value of this item.
  74. */
  75. private final double weight;
  76.  
  77. /**
  78. * The array of bonuses for this item.
  79. */
  80. private final int[] bonus;
  81.  
  82. /**
  83. * The flag that determines if this item is two-handed.
  84. */
  85. private final boolean twoHanded;
  86.  
  87. /**
  88. * The flag that determines if this item is a full helmet.
  89. */
  90. private final boolean fullHelm;
  91.  
  92. /**
  93. * The flag that determines if this item is a platebody.
  94. */
  95. private final boolean platebody;
  96.  
  97. /**
  98. * The flag that determines if this item is tradeable.
  99. */
  100. private final boolean tradeable;
  101.  
  102. /**
  103. * Creates a new {@link ItemDefinition}.
  104. *
  105. * @param id
  106. * the identifier for the item.
  107. * @param name
  108. * the proper name of the item.
  109. * @param description
  110. * the description of the item.
  111. * @param equipmentSlot
  112. * the equipment slot of this item.
  113. * @param noteable
  114. * the flag that determines if the item is noteable.
  115. * @param stackable
  116. * the flag that determines if the item is stackable.
  117. * @param specialPrice
  118. * the special store price of this item.
  119. * @param generalPrice
  120. * the general store price of this item.
  121. * @param lowAlchValue
  122. * the low alch value of this item.
  123. * @param highAlchValue
  124. * the high alch value of this item.
  125. * @param weight
  126. * the weight value of this item.
  127. * @param bonus
  128. * the array of bonuses for this item.
  129. * @param twoHanded
  130. * the flag that determines if this item is two-handed.
  131. * @param fullHelm
  132. * the flag that determines if this item is a full helmet.
  133. * @param platebody
  134. * the flag that determines if this item is a platebody.
  135. * @param tradeable
  136. * the flag that determines if this item is tradeable.
  137. */
  138. public ItemDefinition(int id, String name, String description, int equipmentSlot, boolean noteable, boolean noted,
  139. boolean stackable, int specialPrice, int generalPrice, int lowAlchValue, int highAlchValue, double weight,
  140. int[] bonus, boolean twoHanded, boolean fullHelm, boolean platebody, boolean tradeable) {
  141. this.id = id;
  142. this.name = name;
  143. this.description = description;
  144. this.equipmentSlot = equipmentSlot;
  145. this.noteable = noteable;
  146. this.noted = noted;
  147. this.stackable = stackable;
  148. this.specialPrice = specialPrice;
  149. this.generalPrice = generalPrice;
  150. this.lowAlchValue = lowAlchValue;
  151. this.highAlchValue = highAlchValue;
  152. this.weight = weight;
  153. this.bonus = bonus;
  154. this.twoHanded = twoHanded;
  155. this.fullHelm = fullHelm;
  156. this.platebody = platebody;
  157. this.tradeable = tradeable;
  158. }
  159.  
  160. public ItemDefinition(int id) {
  161. this(id, "Item #" + id, "", 3, false, false, false, 0, 0, 0, 0, 0, new int[12], false, false, false, false);
  162. }
  163.  
  164. public static int getIdForName(String name) {
  165. if (name == null)
  166. return -1;
  167. for (int i = 0; i < DEFINITIONS.length; i++) {
  168. if (DEFINITIONS[i] == null)
  169. continue;
  170. if (DEFINITIONS[i].getName().toLowerCase().equalsIgnoreCase(name.toLowerCase()))
  171. return i;
  172. }
  173. return -1;
  174. }
  175.  
  176. public boolean isWearable() {
  177. return equipmentSlot != -1;
  178. }
  179.  
  180. public static ItemDefinition forId(int id) {
  181. if (id < 0 || id > DEFINITIONS.length || DEFINITIONS[id] == null) {
  182. return new ItemDefinition(id);
  183. }
  184. return DEFINITIONS[id];
  185. }
  186.  
  187. // cached unnoted id
  188. private int unnotedId = -1;
  189.  
  190. // much smarter way to do it, with better performance
  191. public int getUnnotedId() {
  192. if (unnotedId == -1) {
  193.  
  194. // we start with a simple inexpensive lookup, the most common way
  195. // which is +1 of the item id
  196. int unnoted = id + 1;
  197. if (!ItemDefinition.DEFINITIONS[unnoted].isNoted()) {
  198.  
  199. // cache the result so we don't have to do this in the future...
  200. unnotedId = unnoted;
  201. return unnotedId;
  202. }
  203.  
  204. // if it isn't found there, we do an expensive lookup
  205. for (ItemDefinition it : DEFINITIONS) {
  206. if (it.name.equals(name) && !it.noted) {
  207.  
  208. // cache the result so we don't have to do this in the
  209. // future...
  210. unnotedId = it.id;
  211. return unnotedId;
  212. }
  213. }
  214.  
  215. // if it's still not found, throw an exception
  216. throw new NoSuchElementException("unnoted id not found for id: " + id);
  217. }
  218.  
  219. // we already calculated the unnoted item, return the previously
  220. // calculated value (it never changes)
  221. return unnotedId;
  222. }
  223.  
  224. /**
  225. * Gets the identifier for the item.
  226. * @param i
  227. *
  228. * @return the identifier.
  229. */
  230. public int getId() {
  231. return id;
  232. }
  233.  
  234. /**
  235. * Gets the proper name of the item.
  236. *
  237. * @return the proper name.
  238. */
  239. public String getName() {
  240. return name;
  241. }
  242.  
  243. /**
  244. * Gets the description of the item.
  245. *
  246. * @return the description.
  247. */
  248. public String getDescription() {
  249. return description;
  250. }
  251.  
  252. /**
  253. * Gets the equipment slot of this item.
  254. *
  255. * @return the equipment slot.
  256. */
  257. public int getEquipmentSlot() {
  258. return equipmentSlot;
  259. }
  260.  
  261. /**
  262. * Determines if the item is noted or not.
  263. *
  264. * @return {@code true} if the item is noted, {@code false} otherwise.
  265. */
  266. public boolean isNoted() {
  267. return noted;
  268. }
  269.  
  270. /**
  271. * Determines if the item is noteable or not.
  272. *
  273. * @return {@code true} if the item is noteable, {@code false} otherwise.
  274. */
  275. public boolean isNoteable() {
  276. return noteable && DEFINITIONS[id + 1].isNoted();
  277. }
  278.  
  279. /**
  280. * Determines if the item is equipable or not.
  281. *
  282. * @return {@code true} if the item is equipable, {@code false} otherwise.
  283. */
  284. public boolean isEquipable() {
  285. return equipmentSlot != -1;
  286. }
  287.  
  288. /**
  289. * Determines if the item is stackable or not.
  290. *
  291. * @return {@code true} if the item is stackable, {@code false} otherwise.
  292. */
  293. public boolean isStackable() {
  294. return stackable || noted;
  295. }
  296.  
  297. /**
  298. * Gets the special store price of this item.
  299. *
  300. * @param i
  301. *
  302. * @return the special price.
  303. */
  304. public int getSpecialPrice() {
  305. return specialPrice;
  306. }
  307.  
  308. /**
  309. * Gets the general store price of this item.
  310. *
  311. * @return the general price.
  312. */
  313. public int getGeneralPrice() {
  314. return generalPrice;
  315. }
  316.  
  317. /**
  318. * Gets the low alch value of this item.
  319. *
  320. * @return the low alch value.
  321. */
  322. public int getLowAlchValue() {
  323. return lowAlchValue;
  324. }
  325.  
  326. /**
  327. * Gets the high alch value of this item.
  328. *
  329. * @param itemId
  330. *
  331. * @return the high alch value.
  332. */
  333. public int getHighAlchValue() {
  334. return highAlchValue;
  335. }
  336.  
  337. /**
  338. * Gets the weight value of this item.
  339. *
  340. * @return the weight value.
  341. */
  342. public double getWeight() {
  343. return weight;
  344. }
  345.  
  346. /**
  347. * Gets the array of bonuses for this item.
  348. *
  349. * @return the array of bonuses.
  350. */
  351. public int[] getBonus() {
  352. return bonus;
  353. }
  354.  
  355. /**
  356. * Determines if this item is two-handed or not.
  357. *
  358. * @return {@code true} if this item is two-handed, {@code false} otherwise.
  359. */
  360. public boolean isTwoHanded() {
  361. return twoHanded;
  362. }
  363.  
  364. /**
  365. * Determines if this item is a full helmet or not.
  366. *
  367. * @return {@code true} if this item is a full helmet, {@code false}
  368. * otherwise.
  369. */
  370. public boolean isFullHelm() {
  371. return fullHelm;
  372. }
  373.  
  374. /**
  375. * Determines if this item is a platebody or not.
  376. *
  377. * @return {@code true} if this item is a platebody, {@code false}
  378. * otherwise.
  379. */
  380. public boolean isPlatebody() {
  381. return platebody;
  382. }
  383.  
  384. /**
  385. * Determines if this item is tradeable.
  386. *
  387. * @return {@code true} if this item is tradeable, {@code false} otherwise.
  388. */
  389. public boolean isTradeable() {
  390. return tradeable;
  391. }
  392. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement