RuneHive-Game
Loading...
Searching...
No Matches
Item.java
Go to the documentation of this file.
1package com.runehive.game.world.items;
2
3import com.google.common.collect.Iterables;
4import com.runehive.game.world.entity.combat.attack.FightType;
5import com.runehive.game.world.entity.combat.ranged.RangedWeaponDefinition;
6import com.runehive.game.world.entity.combat.weapon.WeaponInterface;
7import com.runehive.game.world.items.containers.equipment.EquipmentType;
8import com.runehive.game.world.items.containers.pricechecker.PriceType;
9import com.runehive.util.Utility;
10
11import java.util.ArrayList;
12import java.util.List;
13import java.util.Optional;
14import java.util.OptionalInt;
15
16/**
17 * The container class that represents an item that can be interacted with.
18 *
19 * @author lare96 <http://github.com/lare96>
20 */
21public class Item implements Cloneable {
22
23 /**
24 * The identification of this item.
25 */
26 private int id;
27
28 /**
29 * The quantity of this item.
30 */
31 private int amount;
32
33 /**
34 * Creates a new {@link Item}.
35 */
36 public Item(int id, int amount) {
37 if (amount < 0) amount = 0;
38 this.id = id;
39 this.amount = amount;
40 }
41
42 /**
43 * Creates a new {@link Item} with a random amount between {@code minAmt} and {@code maxAmt}.
44 */
45 public Item(int id, int minAmt, int maxAmt) {
46 if (minAmt < 0 || maxAmt < 0) amount = 0;
47 this.id = id;
48 this.amount = Utility.random(minAmt, maxAmt, true);
49 }
50
51 public Item(int id, long amount) {
52 int amount2;
53 if (amount < 0) {
54 amount2 = 0;
55 } else if (amount > Integer.MAX_VALUE) {
56 amount2 = Integer.MAX_VALUE;
57 } else {
58 amount2 = (int) amount;
59 }
60 this.id = id;
61 this.amount = amount2;
62 }
63
64 public Item clone() {
65 try {
66 return (Item) super.clone();
67 } catch (CloneNotSupportedException e) {
68 e.printStackTrace();
69 }
70 return this;
71 }
72
73 /**
74 * Gets the unnoted item.
75 *
76 * @return the unnoted item.
77 */
78 public Item unnoted() {
79 return new Item(getDefinition().getUnnotedId(), amount);
80 }
81
82 /**
83 * Gets the item note item.
84 *
85 * @return The item note id, or the original item item if it cannot be
86 * noted.
87 */
88 public Item noted() {
89 return new Item(getDefinition().getNotedId(), amount);
90 }
91
92 /**
93 * Gets the value for this item.
94 *
95 * @param type the type to derive the value from.
96 * @return the value of this item.
97 */
98 public int getValue(PriceType type) {
100 if (def == null) {
101 return 0;
102 }
103
104 switch (type) {
105 case VALUE:
106 return def.getValue();
107 case HIGH_ALCH_VALUE:
108 return def.getHighAlch();
109 case LOW_ALCH_VALUE:
110 return def.getLowAlch();
111 }
112
113 return 0;
114 }
115
116 public int getSellValue() {
117 return (int) Math.floor(getValue() / 2);
118 }
119
120 /**
121 * Gets the value for this item.
122 *
123 * @return the value of this item.
124 */
125 public int getValue() {
127 if (def == null) {
128 return 0;
129 }
130 return def.getValue();
131 }
132
133 /**
134 * Creates a new item with {@code newId} and the same amount as this
135 * instance. The returned {@code Item} <strong>does not</strong> hold any
136 * references to this one unless {@code id == newId}. It will throw an
137 * exception on an invalid id.
138 *
139 * @param newId The new id to set.
140 * @return The newly id set {@code Item}.
141 */
142 public Item createWithId(int newId) {
143 if (id == newId) {
144 return this;
145 }
146 return new Item(newId, amount);
147 }
148
149 /**
150 * Creates a new item with {@code newAmount} and the same identifier as this
151 * instance. The returned {@code Item} <strong>does not</strong> hold any
152 * references to this one unless {@code amount == newAmount}. It will throw
153 * an exception on overflows and negative values.
154 *
155 * @param newAmount The new amount to set.
156 * @return The newly amount set {@code Item}.
157 */
158 public Item createWithAmount(int newAmount) {
159 if (amount == newAmount) {
160 return this;
161 }
162 return new Item(id, newAmount);
163 }
164
165 /**
166 * Creates a new item with {@code amount + addAmount} and the same
167 * identifier. The returned {@code Item} <strong>does not</strong> hold any
168 * references to this one. It will also have a maximum amount of {@code
169 * Integer.MAX_VALUE}.
170 *
171 * @param addAmount The amount to deposit.
172 * @return The newly incremented {@code Item}.
173 */
174 public Item createAndIncrement(int addAmount) {
175 if (addAmount < 0) { // Same effect as decrementing.
176 return createAndDecrement(Math.abs(addAmount));
177 }
178
179 int newAmount = amount + addAmount;
180
181 if (newAmount < amount) { // An overflow.
182 newAmount = Integer.MAX_VALUE;
183 }
184
185 Item item = clone();
186 item.setAmount(newAmount);
187 return item;
188 }
189
190 /**
191 * Creates a new item with {@code amount - removeAmount} and the same
192 * identifier. The returned {@code Item} <strong>does not</strong> hold any
193 * references to this one. It will also have a minimum amount of {@code 1}.
194 *
195 * @param removeAmount The amount to withdraw.
196 * @return The newly incremented {@code Item}.
197 */
198 public Item createAndDecrement(int removeAmount) {
199 if (removeAmount < 0) { // Same effect as incrementing.
200 return createAndIncrement(-removeAmount);
201 }
202
203 int newAmount = amount - removeAmount;
204
205 // Value too low, or an overflow.
206 if (newAmount < 1 || newAmount > amount) {
207 newAmount = 1;
208 }
209
210 Item clone = clone();
211 clone.setAmount(newAmount);
212 return clone;
213 }
214
215 /**
216 * Creates a new {@link Item} with an quantity of {@code 1}.
217 *
218 * @param id the identification of this item.
219 */
220 public Item(int id) {
221 this(id, 1);
222 }
223
224 /**
225 * Converts an {@link Item} array into an Integer array.
226 *
227 * @param ids the array to convert into an Integer array.
228 * @return the Integer array containing the values from the item array.
229 */
230 public static final int[] convert(Item... ids) {
231 List<Integer> values = new ArrayList<>();
232 for (Item identifier : ids) {
233 values.add(identifier.getId());
234 }
235 return values.stream().mapToInt(Integer::intValue).toArray();
236 }
237
238 /**
239 * Converts an int array into an {@link Item} array.
240 *
241 * @param id the array to convert into an item array.
242 * @return the item array containing the values from the int array.
243 */
244 public static final Item[] convert(int... id) {
245 List<Item> items = new ArrayList<>();
246 for (int identifier : id) {
247 items.add(new Item(identifier));
248 }
249 return Iterables.toArray(items, Item.class);
250 }
251
252 /**
253 * Determines if {@code item} is valid. In other words, determines if
254 * {@code item} is not {@code null} and the {@link Item#id}.
255 *
256 * @param item the item to determine if valid.
257 * @return {@code true} if the item is valid, {@code false} otherwise.
258 */
259 public static boolean valid(Item item) {
260 return item != null && item.id > 0 && item.id < ItemDefinition.DEFINITIONS.length && item.getDefinition() != null;
261 }
262
263 /**
264 * A substitute for {@link Object#clone()} that creates another 'copy' of
265 * this instance. The created copy <i>safe</i> meaning it does not hold
266 * <b>any</b> references to the original instance.
267 *
268 * @return the copy of this instance that does not hold any references.
269 */
270 public Item copy() {
271 return new Item(id, amount);
272 }
273
274 /**
275 * Increments the amount by {@code 1}.
276 */
277 public final void incrementAmount() {
279 }
280
281 /**
282 * Decrements the amount by {@code 1}.
283 */
284 public final void decrementAmount() {
286 }
287
288 /**
289 * Increments the amount by {@code amount}.
290 *
291 * @param amount the amount to increment by.
292 */
293 public final void incrementAmountBy(int amount) {
294 this.amount += amount;
295 }
296
297 /**
298 * Decrements the amount by {@code amount}
299 *
300 * @param amount the amount to decrement by.
301 */
302 public final void decrementAmountBy(int amount) {
303 if ((this.amount - amount) < 1) {
304 this.amount = 0;
305 } else {
306 this.amount -= amount;
307 }
308 }
309
310 /**
311 * Gets the item definition for the item identifier.
312 *
313 * @return the item definition.
314 */
316 return ItemDefinition.get(id);
317 }
318
319 /**
320 * Gets the identification of this item.
321 *
322 * @return the identification.
323 */
324 public final int getId() {
325 return id;
326 }
327
328 /**
329 * Sets the identification of this item.
330 *
331 * @param id the new identification of this item.
332 */
333 public final void setId(int id) {
334 this.id = id;
335 }
336
337 /**
338 * Gets the quantity of this item.
339 *
340 * @return the quantity.
341 */
342 public final int getAmount() {
343 return amount;
344 }
345
346 /**
347 * Sets the quantity of this item.
348 *
349 * @param amount the new quantity of this item.
350 */
351 public final void setAmount(int amount) {
352 if (amount < 0) amount = 0;
353 this.amount = amount;
354 }
355
356 public String getName() {
357 return getDefinition().getName();
358 }
359
360 public String getDestroyMessage() {
362 }
363
364 public boolean isStackable() {
365 return getDefinition().isStackable();
366 }
367
368 public boolean isNoteable() {
369 return getDefinition().isNoteable();
370 }
371
372 public boolean isNoted() {
373 return getDefinition().isNoted();
374 }
375
376 public boolean isEquipable() {
377 return getDefinition().getEquipmentType() != EquipmentType.NOT_WIELDABLE || getDefinition().isEquipable();
378 }
379
380 public boolean isTwoHanded() {
381 return getDefinition().isTwoHanded();
382 }
383
384 public boolean isTradeable() {
385 return getDefinition().isTradeable();
386 }
387
388 public boolean isDestroyable() {
389 return getDefinition().isDestroyable();
390 }
391
392 public int getStandAnimation() {
393 return getDefinition().getStandAnimation();
394 }
395
396 public int getWalkAnimation() {
397 return getDefinition().getWalkAnimation();
398 }
399
400 public int getRunAnimation() {
401 return getDefinition().getRunAnimation();
402 }
403
404
405 public OptionalInt getAttackAnimation(FightType type) {
406 return getDefinition().getAttackAnimation(type);
407 }
408
409 public OptionalInt getBlockAnimation() {
410 return getDefinition().getBlockAnimation();
411 }
412
413 public int getNotedId() {
414 return getDefinition().getNotedId();
415 }
416
417 public int getUnnotedId() {
418 return getDefinition().getUnnotedId();
419 }
420
421 public int getStreetValue() {
422 return getDefinition().getStreetValue();
423 }
424
425 public int getBaseValue() {
426 return getDefinition().getBaseValue();
427 }
428
429 public int getHighAlch() {
430 return getDefinition().getHighAlch();
431 }
432
433 public int getLowAlch() {
434 return getDefinition().getLowAlch();
435 }
436
437 public double getWeight() {
438 return getDefinition().getWeight();
439 }
440
442 return getDefinition().getEquipmentType();
443 }
444
445 public Optional<RangedWeaponDefinition> getRangedDefinition() {
446 return getDefinition().getRangedDefinition();
447 }
448
449 public int[] getRequirements() {
450 return getDefinition().getRequirements();
451 }
452
453 public int[] getBonuses() {
454 return getDefinition().getBonuses();
455 }
456
457 public int getBonus(int index) {
458 return getDefinition().getBonuses()[index];
459 }
460
461 public boolean equalIds(Item other) {
462 return other != null && id == other.id;
463 }
464
465 public boolean matchesId(int id) {
466 return this.id == id;
467 }
468
469 @Override
470 public final String toString() {
471 return String.format("item[id=%d amount=%d]", id, amount);
472 }
473
474 @Override
475 public boolean equals(Object obj) {
476 if (obj instanceof Item) {
477 Item other = (Item) obj;
478 return other.id == id && other.amount == amount;
479 }
480 return false;
481 }
482
483 @Override
484 public int hashCode() {
485 return amount << 16 | id & 0xFFFF;
486 }
487
489 return getDefinition().getWeaponInterface();
490 }
491}
Represents all of an in-game Item's attributes.
boolean isNoted()
Gets the item note state.
static ItemDefinition get(int id)
Gets an item definition.
String getDestroyMessage()
Gets the item destroy message.
final void setAmount(int amount)
Sets the quantity of this item.
Definition Item.java:351
static final int[] convert(Item... ids)
Converts an Item array into an Integer array.
Definition Item.java:230
Optional< RangedWeaponDefinition > getRangedDefinition()
Definition Item.java:445
final int getId()
Gets the identification of this item.
Definition Item.java:324
final void decrementAmount()
Decrements the amount by 1.
Definition Item.java:284
int getValue()
Gets the value for this item.
Definition Item.java:125
Item(int id, long amount)
Definition Item.java:51
static boolean valid(Item item)
Determines if item is valid.
Definition Item.java:259
Item createWithId(int newId)
Creates a new item with newId and the same amount as this instance.
Definition Item.java:142
final int getAmount()
Gets the quantity of this item.
Definition Item.java:342
int id
The identification of this item.
Definition Item.java:26
Item noted()
Gets the item note item.
Definition Item.java:88
Item copy()
A substitute for Object#clone() that creates another 'copy' of this instance.
Definition Item.java:270
ItemDefinition getDefinition()
Gets the item definition for the item identifier.
Definition Item.java:315
Item createAndIncrement(int addAmount)
Creates a new item with amount + addAmount and the same identifier.
Definition Item.java:174
int getValue(PriceType type)
Gets the value for this item.
Definition Item.java:98
final void decrementAmountBy(int amount)
Decrements the amount by amount @endiliteral.
Definition Item.java:302
Item(int id, int minAmt, int maxAmt)
Creates a new Item with a random amount between minAmt and maxAmt.
Definition Item.java:45
Item createWithAmount(int newAmount)
Creates a new item with newAmount and the same identifier as this instance.
Definition Item.java:158
boolean equals(Object obj)
Definition Item.java:475
WeaponInterface getWeaponInterface()
Definition Item.java:488
OptionalInt getAttackAnimation(FightType type)
Definition Item.java:405
final void incrementAmount()
Increments the amount by 1.
Definition Item.java:277
int amount
The quantity of this item.
Definition Item.java:31
Item(int id)
Creates a new Item with an quantity of 1.
Definition Item.java:220
boolean equalIds(Item other)
Definition Item.java:461
Item unnoted()
Gets the unnoted item.
Definition Item.java:78
final void setId(int id)
Sets the identification of this item.
Definition Item.java:333
static final Item[] convert(int... id)
Converts an int array into an Item array.
Definition Item.java:244
final void incrementAmountBy(int amount)
Increments the amount by amount.
Definition Item.java:293
EquipmentType getEquipmentType()
Definition Item.java:441
Item createAndDecrement(int removeAmount)
Creates a new item with amount - removeAmount and the same identifier.
Definition Item.java:198
Item(int id, int amount)
Creates a new Item.
Definition Item.java:36
Handles miscellaneous methods.
Definition Utility.java:27
static int random(int bound)
Definition Utility.java:239
The enumerated type whose elements represent the fighting types.
The enumerated type whose elements represent the weapon interfaces.
The enumerated types of a players equipped item slots.