1package com.osroyale.game.world.items;
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;
11import java.util.ArrayList;
13import java.util.Optional;
14import java.util.OptionalInt;
54* The container
class that represents an item that can be interacted with.
56 * @author lare96 <http:
58public class Item
implements Cloneable {
73 public Item(
int id,
int amount) {
74 if (amount < 0) amount = 0;
82 public Item(
int id,
int minAmt,
int maxAmt) {
83 if (minAmt < 0 || maxAmt < 0) amount = 0;
85 this.amount = Utility.random(minAmt, maxAmt,
true);
88 public Item(
int id,
long amount) {
92 }
else if (amount > Integer.MAX_VALUE) {
93 amount2 = Integer.MAX_VALUE;
95 amount2 = (int) amount;
98 this.amount = amount2;
101 public Item clone() {
103 return (Item) super.clone();
104 }
catch (CloneNotSupportedException e) {
115 public Item unnoted() {
116 return new Item(getDefinition().getUnnotedId(), amount);
125 public Item noted() {
126 return new Item(getDefinition().getNotedId(), amount);
135 public int getValue(PriceType type) {
136 ItemDefinition def = getDefinition();
143 return def.getValue();
144 case HIGH_ALCH_VALUE:
145 return def.getHighAlch();
147 return def.getLowAlch();
153 public int getSellValue() {
154 return (
int) Math.floor(getValue() / 2);
162 public int getValue() {
163 ItemDefinition def = getDefinition();
167 return def.getValue();
179 public Item createWithId(
int newId) {
183 return new Item(newId, amount);
195 public Item createWithAmount(
int newAmount) {
196 if (amount == newAmount) {
199 return new Item(
id, newAmount);
211 public Item createAndIncrement(
int addAmount) {
213 return createAndDecrement(Math.abs(addAmount));
216 int newAmount = amount + addAmount;
218 if (newAmount < amount) {
219 newAmount = Integer.MAX_VALUE;
223 item.setAmount(newAmount);
235 public Item createAndDecrement(
int removeAmount) {
236 if (removeAmount < 0) {
237 return createAndIncrement(-removeAmount);
240 int newAmount = amount - removeAmount;
243 if (newAmount < 1 || newAmount > amount) {
247 Item clone = clone();
248 clone.setAmount(newAmount);
257 public Item(
int id) {
267 public static final int[] convert(Item... ids) {
268 List<Integer> values =
new ArrayList<>();
269 for (Item identifier : ids) {
270 values.add(identifier.getId());
272 return values.stream().mapToInt(Integer::intValue).toArray();
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));
286 return Iterables.toArray(items, Item.class);
296 public static boolean valid(Item item) {
297 return item !=
null && item.id > 0 && item.id < ItemDefinition.DEFINITIONS.length && item.getDefinition() !=
null;
308 return new Item(
id, amount);
314 public final void incrementAmount() {
315 incrementAmountBy(1);
321 public final void decrementAmount() {
322 decrementAmountBy(1);
330 public final void incrementAmountBy(
int amount) {
331 this.amount += amount;
339 public final void decrementAmountBy(
int amount) {
340 if ((this.amount - amount) < 1) {
343 this.amount -= amount;
352 public ItemDefinition getDefinition() {
353 return ItemDefinition.get(
id);
361 public final int getId() {
370 public final void setId(
int id) {
379 public final int getAmount() {
388 public final void setAmount(
int amount) {
389 if (amount < 0) amount = 0;
390 this.amount = amount;
393 public String getName() {
394 return getDefinition().getName();
397 public String getDestroyMessage() {
398 return getDefinition().getDestroyMessage();
401 public boolean isStackable() {
402 return getDefinition().isStackable();
405 public boolean isNoteable() {
406 return getDefinition().isNoteable();
409 public boolean isNoted() {
410 return getDefinition().isNoted();
413 public boolean isEquipable() {
414 return getDefinition().getEquipmentType() != EquipmentType.NOT_WIELDABLE || getDefinition().isEquipable();
417 public boolean isTwoHanded() {
418 return getDefinition().isTwoHanded();
421 public boolean isTradeable() {
422 return getDefinition().isTradeable();
425 public boolean isDestroyable() {
426 return getDefinition().isDestroyable();
429 public int getStandAnimation() {
430 return getDefinition().getStandAnimation();
433 public int getWalkAnimation() {
434 return getDefinition().getWalkAnimation();
437 public int getRunAnimation() {
438 return getDefinition().getRunAnimation();
442 public OptionalInt getAttackAnimation(FightType type) {
443 return getDefinition().getAttackAnimation(type);
446 public OptionalInt getBlockAnimation() {
447 return getDefinition().getBlockAnimation();
450 public int getNotedId() {
451 return getDefinition().getNotedId();
454 public int getUnnotedId() {
455 return getDefinition().getUnnotedId();
458 public int getStreetValue() {
459 return getDefinition().getStreetValue();
462 public int getBaseValue() {
463 return getDefinition().getBaseValue();
466 public int getHighAlch() {
467 return getDefinition().getHighAlch();
470 public int getLowAlch() {
471 return getDefinition().getLowAlch();
474 public double getWeight() {
475 return getDefinition().getWeight();
478 public EquipmentType getEquipmentType() {
479 return getDefinition().getEquipmentType();
482 public Optional<RangedWeaponDefinition> getRangedDefinition() {
483 return getDefinition().getRangedDefinition();
486 public int[] getRequirements() {
487 return getDefinition().getRequirements();
490 public int[] getBonuses() {
491 return getDefinition().getBonuses();
494 public int getBonus(
int index) {
495 return getDefinition().getBonuses()[index];
498 public boolean equalIds(Item other) {
499 return other !=
null &&
id == other.id;
502 public boolean matchesId(
int id) {
503 return this.id == id;
507 public final String toString() {
508 return String.format(
"item[id=%d amount=%d]",
id, amount);
512 public boolean equals(Object obj) {
513 if (obj instanceof Item) {
514 Item other = (Item) obj;
515 return other.id ==
id && other.amount == amount;
521 public int hashCode() {
522 return amount << 16 |
id & 0xFFFF;
525 public WeaponInterface getWeaponInterface() {
526 return getDefinition().getWeaponInterface();