RuneHive-Game
Loading...
Searching...
No Matches
RunePouch.java
Go to the documentation of this file.
1package com.runehive.content.skill.impl.magic;
2
3import com.runehive.game.world.entity.mob.player.Player;
4import com.runehive.game.world.items.Item;
5import com.runehive.game.world.position.Area;
6import com.runehive.net.packet.out.SendItemOnInterface;
7import com.runehive.util.Utility;
8
9import java.util.LinkedList;
10import java.util.List;
11
12/**
13 * Handles the rune pouch.
14 *
15 * @author Daniel
16 */
17public class RunePouch {
18
19 /** The maximum amount of total runes the player can carry in their rune pouch. */
20 private static final int MAXIMUM_RUNE_CAPACITY = 16_000;
21
22 /** Array of all runes allowed to be inside the rune pouch. */
23 private final Item[] ALLOWED_RUNES = {
24 new Item(554), new Item(555), new Item(556), new Item(557), new Item(558),
25 new Item(559), new Item(560), new Item(561), new Item(562), new Item(563),
26 new Item(564), new Item(565), new Item(566), new Item(9075), new Item(21880)
27 };
28
29 /** The player instance. */
30 private final Player player;
31
32 /** The runes stores in the rune pouch; */
33 public List<Item> runes = new LinkedList<>();
34
35 /** Constructs a new <code>RunePouch</code>. */
37 this.player = player;
38 }
39
40 public void open() {
41 refresh();
42 player.interfaceManager.open(41700);
43 }
44
45 public void clear(boolean open) {
46 if(open) runes.forEach(player.inventory::add);
47 runes.clear();
48 if(open) player.interfaceManager.open(41700);
49 refresh();
50 }
51
52 public void clear() {
53 clear(true);
54 }
55
56 public void refresh() {
57 player.send(new SendItemOnInterface(41710, runes.toArray(new Item[runes.size()])));
58 player.send(new SendItemOnInterface(41711, player.inventory.getItems()));
59 }
60
61 public void clearInterface() {
62 player.send(new SendItemOnInterface(41710, new Item[0]));
63 }
64
65 public void withdraw(int item, int amount) {
66 for (Item rune : runes) {
67 if (rune.getId() == item) {
68 int current = player.inventory.computeAmountForId(item);
69 if (rune.getAmount() - amount < 0) amount = rune.getAmount();
70 player.inventory.add(item, amount);
71 int newAm = player.inventory.computeAmountForId(item);
72 if (newAm - current < amount) amount = newAm - current;
73 rune.decrementAmountBy(amount);
74 if (rune.getAmount() == 0)
75 runes.remove(rune);
76 refresh();
77 return;
78 }
79 }
80 }
81
82 public void deposit(Item item, int amount) {
83 boolean allowed = false;
84 for (Item rune : ALLOWED_RUNES) {
85 if (rune.getId() == item.getId()) {
86 allowed = true;
87 break;
88 }
89 }
90 if (!allowed) {
91 player.message("You can only deposit runes into the rune pouch!");
92 return;
93 }
94
95 for (Item rune : runes) {
96 if (item.getId() == rune.getId()) {
97 if (rune.getAmount() + amount > MAXIMUM_RUNE_CAPACITY) {
98 amount = MAXIMUM_RUNE_CAPACITY - rune.getAmount();
99 player.message("You can only have a total of " + Utility.formatDigits(MAXIMUM_RUNE_CAPACITY) + " of the same rune in your rune pouch.");
100 }
101 player.inventory.remove(item.getId(), amount);
102 rune.incrementAmountBy(amount);
103 refresh();
104 return;
105 }
106 }
107
108 if (runes.size() >= 3) {
109 player.message("Your rune pouch is currently full and can not hold any more types of runes!");
110 return;
111 }
112
113 if (amount > MAXIMUM_RUNE_CAPACITY) {
114 amount = MAXIMUM_RUNE_CAPACITY;
115 player.message("You can only have a total of " + Utility.formatDigits(MAXIMUM_RUNE_CAPACITY) + " of the same rune in your rune pouch.");
116 }
117
118 player.inventory.remove(item.getId(), amount);
119 runes.add(new Item(item.getId(), amount));
120 refresh();
121 }
122
123 public int getRuneAmount() {
124 int amount = 0;
125 for (Item rune : runes) {
126 amount += rune.getAmount();
127 }
128 return amount;
129 }
130
131 public int getRuneAmount(int id) {
132 int amount = 0;
133 for (Item rune : runes) {
134 if (rune.getId() == id)
135 amount += rune.getAmount();
136 }
137 return amount;
138 }
139
140 public boolean contains(Item item) {
141 for (Item rune : runes) {
142 if (rune.getId() == item.getId() && rune.getAmount() >= item.getAmount())
143 return true;
144 }
145 return false;
146 }
147
148 public boolean containsId(int item) {
149 for (Item rune : runes) {
150 if (rune.getId() == item)
151 return true;
152 }
153 return false;
154 }
155
156 public void remove(Item item) {
157 for (Item rune : runes) {
158 if (rune.equalIds(item)) {
159 rune.decrementAmountBy(item.getAmount());
160 if (rune.getAmount() == 0)
161 runes.remove(rune);
162 return;
163 }
164 }
165 }
166
167 public boolean death(Item item) {
168 if (item.getId() == 12971 && Area.inWilderness(player)) {
169 runes.clear();
170 return true;
171 }
172
173 return false;
174 }
175}
List< Item > runes
The runes stores in the rune pouch;.
RunePouch(Player player)
Constructs a new RunePouch.
static final int MAXIMUM_RUNE_CAPACITY
The maximum amount of total runes the player can carry in their rune pouch.
final Item[] ALLOWED_RUNES
Array of all runes allowed to be inside the rune pouch.
final Player player
The player instance.
This class represents a character controlled by a player.
Definition Player.java:125
The container class that represents an item that can be interacted with.
Definition Item.java:21
final int getId()
Gets the identification of this item.
Definition Item.java:324
final int getAmount()
Gets the quantity of this item.
Definition Item.java:342
Handles checking if mobs are in a certain area.
Definition Area.java:13
static boolean inWilderness(Position position)
Definition Area.java:272
Handles miscellaneous methods.
Definition Utility.java:27
static String formatDigits(final int amount)
Formats digits for integers.
Definition Utility.java:41