RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
Item.java
1package com.osroyale.game.world.items;
2
3import com.google.common.collect.Iterables;
4import com.osroyale.game.world.entity.combat.attack.FightType;
5import com.osroyale.game.world.entity.combat.ranged.RangedWeaponDefinition;
6import com.osroyale.game.world.entity.combat.weapon.WeaponInterface;
7import com.osroyale.game.world.items.containers.equipment.EquipmentType;
8import com.osroyale.game.world.items.containers.pricechecker.PriceType;
9import com.osroyale.util.Utility;
10
11import java.util.ArrayList;
12import java.util.List;
13import java.util.Optional;
14import java.util.OptionalInt;
15
53
54* The container class that represents an item that can be interacted with.
55 *
56 * @author lare96 <http://github.com/lare96>
57 */
58public class Item implements Cloneable {
59
63 private int id;
64
68 private int amount;
69
73 public Item(int id, int amount) {
74 if (amount < 0) amount = 0;
75 this.id = id;
76 this.amount = amount;
77 }
78
82 public Item(int id, int minAmt, int maxAmt) {
83 if (minAmt < 0 || maxAmt < 0) amount = 0;
84 this.id = id;
85 this.amount = Utility.random(minAmt, maxAmt, true);
86 }
87
88 public Item(int id, long amount) {
89 int amount2;
90 if (amount < 0) {
91 amount2 = 0;
92 } else if (amount > Integer.MAX_VALUE) {
93 amount2 = Integer.MAX_VALUE;
94 } else {
95 amount2 = (int) amount;
96 }
97 this.id = id;
98 this.amount = amount2;
99 }
100
101 public Item clone() {
102 try {
103 return (Item) super.clone();
104 } catch (CloneNotSupportedException e) {
105 e.printStackTrace();
106 }
107 return this;
108 }
109
115 public Item unnoted() {
116 return new Item(getDefinition().getUnnotedId(), amount);
117 }
118
125 public Item noted() {
126 return new Item(getDefinition().getNotedId(), amount);
127 }
128
135 public int getValue(PriceType type) {
136 ItemDefinition def = getDefinition();
137 if (def == null) {
138 return 0;
139 }
140
141 switch (type) {
142 case VALUE:
143 return def.getValue();
144 case HIGH_ALCH_VALUE:
145 return def.getHighAlch();
146 case LOW_ALCH_VALUE:
147 return def.getLowAlch();
148 }
149
150 return 0;
151 }
152
153 public int getSellValue() {
154 return (int) Math.floor(getValue() / 2);
155 }
156
162 public int getValue() {
163 ItemDefinition def = getDefinition();
164 if (def == null) {
165 return 0;
166 }
167 return def.getValue();
168 }
169
179 public Item createWithId(int newId) {
180 if (id == newId) {
181 return this;
182 }
183 return new Item(newId, amount);
184 }
185
195 public Item createWithAmount(int newAmount) {
196 if (amount == newAmount) {
197 return this;
198 }
199 return new Item(id, newAmount);
200 }
201
211 public Item createAndIncrement(int addAmount) {
212 if (addAmount < 0) { // Same effect as decrementing.
213 return createAndDecrement(Math.abs(addAmount));
214 }
215
216 int newAmount = amount + addAmount;
217
218 if (newAmount < amount) { // An overflow.
219 newAmount = Integer.MAX_VALUE;
220 }
221
222 Item item = clone();
223 item.setAmount(newAmount);
224 return item;
225 }
226
235 public Item createAndDecrement(int removeAmount) {
236 if (removeAmount < 0) { // Same effect as incrementing.
237 return createAndIncrement(-removeAmount);
238 }
239
240 int newAmount = amount - removeAmount;
241
242 // Value too low, or an overflow.
243 if (newAmount < 1 || newAmount > amount) {
244 newAmount = 1;
245 }
246
247 Item clone = clone();
248 clone.setAmount(newAmount);
249 return clone;
250 }
251
257 public Item(int id) {
258 this(id, 1);
259 }
260
267 public static final int[] convert(Item... ids) {
268 List<Integer> values = new ArrayList<>();
269 for (Item identifier : ids) {
270 values.add(identifier.getId());
271 }
272 return values.stream().mapToInt(Integer::intValue).toArray();
273 }
274
281 public static final Item[] convert(int... id) {
282 List<Item> items = new ArrayList<>();
283 for (int identifier : id) {
284 items.add(new Item(identifier));
285 }
286 return Iterables.toArray(items, Item.class);
287 }
288
296 public static boolean valid(Item item) {
297 return item != null && item.id > 0 && item.id < ItemDefinition.DEFINITIONS.length && item.getDefinition() != null;
298 }
299
307 public Item copy() {
308 return new Item(id, amount);
309 }
310
314 public final void incrementAmount() {
315 incrementAmountBy(1);
316 }
317
321 public final void decrementAmount() {
322 decrementAmountBy(1);
323 }
324
330 public final void incrementAmountBy(int amount) {
331 this.amount += amount;
332 }
333
339 public final void decrementAmountBy(int amount) {
340 if ((this.amount - amount) < 1) {
341 this.amount = 0;
342 } else {
343 this.amount -= amount;
344 }
345 }
346
352 public ItemDefinition getDefinition() {
353 return ItemDefinition.get(id);
354 }
355
361 public final int getId() {
362 return id;
363 }
364
370 public final void setId(int id) {
371 this.id = id;
372 }
373
379 public final int getAmount() {
380 return amount;
381 }
382
388 public final void setAmount(int amount) {
389 if (amount < 0) amount = 0;
390 this.amount = amount;
391 }
392
393 public String getName() {
394 return getDefinition().getName();
395 }
396
397 public String getDestroyMessage() {
398 return getDefinition().getDestroyMessage();
399 }
400
401 public boolean isStackable() {
402 return getDefinition().isStackable();
403 }
404
405 public boolean isNoteable() {
406 return getDefinition().isNoteable();
407 }
408
409 public boolean isNoted() {
410 return getDefinition().isNoted();
411 }
412
413 public boolean isEquipable() {
414 return getDefinition().getEquipmentType() != EquipmentType.NOT_WIELDABLE || getDefinition().isEquipable();
415 }
416
417 public boolean isTwoHanded() {
418 return getDefinition().isTwoHanded();
419 }
420
421 public boolean isTradeable() {
422 return getDefinition().isTradeable();
423 }
424
425 public boolean isDestroyable() {
426 return getDefinition().isDestroyable();
427 }
428
429 public int getStandAnimation() {
430 return getDefinition().getStandAnimation();
431 }
432
433 public int getWalkAnimation() {
434 return getDefinition().getWalkAnimation();
435 }
436
437 public int getRunAnimation() {
438 return getDefinition().getRunAnimation();
439 }
440
441
442 public OptionalInt getAttackAnimation(FightType type) {
443 return getDefinition().getAttackAnimation(type);
444 }
445
446 public OptionalInt getBlockAnimation() {
447 return getDefinition().getBlockAnimation();
448 }
449
450 public int getNotedId() {
451 return getDefinition().getNotedId();
452 }
453
454 public int getUnnotedId() {
455 return getDefinition().getUnnotedId();
456 }
457
458 public int getStreetValue() {
459 return getDefinition().getStreetValue();
460 }
461
462 public int getBaseValue() {
463 return getDefinition().getBaseValue();
464 }
465
466 public int getHighAlch() {
467 return getDefinition().getHighAlch();
468 }
469
470 public int getLowAlch() {
471 return getDefinition().getLowAlch();
472 }
473
474 public double getWeight() {
475 return getDefinition().getWeight();
476 }
477
478 public EquipmentType getEquipmentType() {
479 return getDefinition().getEquipmentType();
480 }
481
482 public Optional<RangedWeaponDefinition> getRangedDefinition() {
483 return getDefinition().getRangedDefinition();
484 }
485
486 public int[] getRequirements() {
487 return getDefinition().getRequirements();
488 }
489
490 public int[] getBonuses() {
491 return getDefinition().getBonuses();
492 }
493
494 public int getBonus(int index) {
495 return getDefinition().getBonuses()[index];
496 }
497
498 public boolean equalIds(Item other) {
499 return other != null && id == other.id;
500 }
501
502 public boolean matchesId(int id) {
503 return this.id == id;
504 }
505
506 @Override
507 public final String toString() {
508 return String.format("item[id=%d amount=%d]", id, amount);
509 }
510
511 @Override
512 public boolean equals(Object obj) {
513 if (obj instanceof Item) {
514 Item other = (Item) obj;
515 return other.id == id && other.amount == amount;
516 }
517 return false;
518 }
519
520 @Override
521 public int hashCode() {
522 return amount << 16 | id & 0xFFFF;
523 }
524
525 public WeaponInterface getWeaponInterface() {
526 return getDefinition().getWeaponInterface();
527 }
528}