RuneHive-Game
Loading...
Searching...
No Matches
Bank.java
Go to the documentation of this file.
1package com.runehive.game.world.items.containers.bank;
2
3import com.runehive.game.world.InterfaceConstants;
4import com.runehive.game.world.entity.mob.player.Player;
5import com.runehive.game.world.entity.mob.player.PlayerRight;
6import com.runehive.game.world.items.Item;
7import com.runehive.game.world.items.ItemDefinition;
8import com.runehive.game.world.items.containers.ItemContainer;
9import com.runehive.game.world.items.containers.ItemContainerAdapter;
10import com.runehive.game.world.items.containers.pricechecker.PriceType;
11import com.runehive.net.packet.out.*;
12import com.runehive.util.Utility;
13import plugin.itemon.item.LootingBagPlugin;
14
15import java.util.Arrays;
16import java.util.Optional;
17
18/**
19 * Handles the Bank container.
20 *
21 * @author Michael
22 * @author Daniel
23 */
24public class Bank extends ItemContainer {
25
26 /** The size of all equipment instances. */
27 public static final int SIZE = 360;
28
29 /** The tab amount array. */
30 public int[] tabAmounts = new int[10];
31
32 /** The player instance. */
33 private final Player player;
34
35 /** The current bank tab. */
36 public int bankTab = 0;
37
38 /** The noting flag. */
39 public boolean noting = false;
40
41 /** The inserting flag. */
42 public boolean inserting = false;
43
44 /** The place holder flag. */
45 public boolean placeHolder;
46
47 /** Constructs a new <code>Bank<code>. */
48 public Bank(Player player) {
49 super(SIZE, StackPolicy.ALWAYS);
50 this.player = player;
51 this.placeHolder = false;
53 }
54
55 /** Opens the bank itemcontainer. */
56 public void open() {
57 if (player.right.equals(PlayerRight.ULTIMATE_IRONMAN)) {
58 player.send(new SendMessage("As an ultimate iron man you may not use banks!"));
59 return;
60 }
61 if (player.bankPin.hasPin() && !player.bankPin.entered) {
62 player.bankPin.enter();
63 return;
64 }
65 noting = false;
66 player.send(new SendString("360", 60018));
67 player.attributes.set("BANK_KEY", Boolean.TRUE);
68 player.interfaceManager.openInventory(60000, InterfaceConstants.INVENTORY_STORE - 1);
69 refresh();
70 }
71
72 /** Refreshes the bank itemcontainer. */
73 public void refresh() {
74 player.send(new SendConfig(304, inserting ? 1 : 0));
75 player.send(new SendTooltip((inserting ? "Enable swapping" : "Enable inserting"), 60006));
76 player.send(new SendConfig(115, noting ? 1 : 0));
77 player.send(new SendTooltip((noting ? "Disable" : "Enable") + " noting", 60007));
78 player.send(new SendConfig(116, placeHolder ? 1 : 0));
79 player.send(new SendTooltip((placeHolder ? "Disable" : "Enable") + " place holders", 60073));
82 player.inventory.refresh();
83 sendValue();
84 }
85
86
87 public void sendValue() {
88 long value = 0;
89 int start = slotForTab(bankTab);
90 int end = start + tabAmounts[bankTab];
91 if (bankTab == 0) {
92 start = 0;
93 end = size();
94 }
95 for (int slot = start; slot < end; slot++) {
96 Item item = get(slot);
97 if (item == null) continue;
98 int price = item.getValue(PriceType.VALUE);
99 if (value >= Long.MAX_VALUE - price * item.getAmount()) {
100 value = Long.MAX_VALUE;
101 }
102 value += price * item.getAmount();
103 }
104 player.send(new SendString((bankTab == 0 ? "Bank" : "Tab " + bankTab) + " value: <col=FF5500>" + Utility.formatPrice(value) + "</col>", 60_079));
105 }
106
107 /** Closes the bank itemcontainer */
108 public void close() {
109 player.attributes.set("BANK_KEY", Boolean.FALSE);
110
111 if (player.pvpInstance) {
112 player.playerAssistant.setValueIcon();
113 }
114 }
115
116 /** Handles the place holder option for the contianer. */
117 public void placeHolder(int item, int slot) {
118 boolean hold = placeHolder;
119 placeHolder = true;
120 setFiringEvents(false);
121 withdraw(item, slot, Integer.MAX_VALUE);
122 setFiringEvents(true);
123 placeHolder = hold;
124 refresh();
125 }
126
127 /** Deposits item into bank. */
128 public void deposit(int slot, int amount) {
129 boolean withinBank = player.interfaceManager.isInterfaceOpen(60000) || player.interfaceManager.isInterfaceOpen(4465);
130 if (!withinBank) {
131 System.out.println("interface: " + player.interfaceManager.getMain());
132 return;
133 }
134
135 Item item = player.inventory.get(slot);
136 if (item == null)
137 return;
138
139 int id = item.getId();
140
141 if (LootingBagPlugin.isLootingBag(item) && !player.lootingBag.isEmpty()) {
142 player.interfaceManager.openInventory(60000, 26700);
143 return;
144 }
145
146 int invAmount = player.inventory.computeAmountForId(id);
147
148 if (invAmount < amount) {
149 amount = invAmount;
150 }
151
152 setFiringEvents(false);
153
154 if (item.isNoted())
155 id = item.getUnnotedId();
156
157 if (!contains(id)) {
158 if (size() + 1 > capacity()) {
159 player.message("Your bank is full! You need to clear some items from your bank.");
160 setFiringEvents(true);
161 return;
162 }
163
165 add(id, amount);
166 int from = computeIndexForId(id);
167 int to = slotForTab(bankTab);
168 swap(true, from, to, false);
169 } else {
170 Item fucking = get(computeIndexForId(id));
171 if (Integer.MAX_VALUE - fucking.getAmount() < amount) {
172 amount = Integer.MAX_VALUE - fucking.getAmount();
173 player.send(new SendMessage("Your bank didn't have enough space to deposit all that!"));
174 }
175 fucking.incrementAmountBy(amount);
176 }
177
178 if (amount > 0) {
179 if(item.getId() == 12791) player.runePouch.clearInterface();
180 player.inventory.remove(item.getId(), amount);
181 }
182 setFiringEvents(true);
183
184 refresh();
185 }
186
187 /** Withdraws item from bank. */
188 public void withdraw(int itemId, int slot, int amount) {
189 if (!player.interfaceManager.isInterfaceOpen(60000)) {
190 return;
191 }
192 slot = computeIndexForId(itemId);
193 if (itemId < 0) return;
194
195 Item item = get(slot);
196 if (item == null || itemId != item.getId())
197 return;
198
199 if (item.getAmount() == 0) {//Releasing place holders
200 boolean hold = placeHolder;
201 placeHolder = false;
202 int tabSlot = computeIndexForId(item.getId());
203 int tab = tabForSlot(tabSlot);
204 changeTabAmount(tab, -1);
205 remove(item);
206 shift();
207 placeHolder = hold;
208 refresh();
209 return;
210 }
211
212 if (item.getAmount() < amount) {
213 amount = item.getAmount();
214 }
215
216 int id = item.getId();
217 if (noting) {
218 if (!item.isNoteable()) {
219 player.send(new SendMessage("This item cannot be withdrawn as a note."));
220 } else {
221 id = item.getNotedId();
222 }
223 }
224
225 setFiringEvents(false);
226 if (!new Item(id).isStackable() && amount > player.inventory.getFreeSlots()) {
227 amount = player.inventory.getFreeSlots();
228 } else if (ItemDefinition.get(id).isStackable() && player.inventory.getFreeSlots() == 0) {
229 if (!player.inventory.contains(id)) {
230 amount = 0;
231 } else if (player.inventory.computeAmountForId(id) + amount > Integer.MAX_VALUE) {
232 amount = Integer.MAX_VALUE - player.inventory.computeAmountForId(id);
233 }
234 }
235
236 if (amount == 0) {
237 player.send(new SendMessage("You do not have enough inventory spaces to withdraw this item."));
238 return;
239 }
240
241 int fuckingSlot = player.inventory.computeIndexForId(id);
242 if (fuckingSlot != -1) {
243 Item fuckingStan = player.inventory.get(fuckingSlot);
244 if (Integer.MAX_VALUE - fuckingStan.getAmount() < amount) {
245 amount = Integer.MAX_VALUE - fuckingStan.getAmount();
246 player.send(new SendMessage("Your inventory didn't have enough space to withdraw all that!"));
247 }
248 }
249
250
251 if (remove(item.getId(), amount)) {
252 player.inventory.add(id, amount);
253 if(id == 12791) player.runePouch.refresh();
254 // when an item is taken out of the bank completely, it removes one amount from the tab amounts array
255 if (!contains(item.getId())) {
256 int tab = tabForSlot(slot);
257 changeTabAmount(tab, -1);
258 shift();
259 }
260 }
261 setFiringEvents(true);
262 refresh();
263 }
264
265 /**
266 * Collapses a tab and shifts other tabs one slot to the left.
267 *
268 * @param tab The initial tab.
269 */
270 public void collapse(int tab, boolean collapseAll) {
271 if (!player.interfaceManager.isInterfaceOpen(60000)) {
272 return;
273 }
274 if (tab == 0 && collapseAll) {
275 Arrays.fill(tabAmounts, 0);
276 tabAmounts[0] = size();
277 shift();
278 return;
279 }
280
281 /* Move the remaining items to the main tab. */
282 int tabAmount = tabAmounts[tab];
283 if (tabAmount > 0) moveTab(tab, 0);
284
285 /* Shift the remaining tabs to the left to fill the gap. */
287 player.send(new SendConfig(211, bankTab = 0));
288 }
289
290 private void recursiveCollapse(int tab) {
291 if (tab == tabAmounts.length - 1) return;
292 moveTab(tab + 1, tab);
293 recursiveCollapse(tab + 1);
294 }
295
296 private void moveTab(int tab, int toTab) {
297 int tabAmount = tabAmounts[tab];
298 int fromSlot = slotForTab(tab);
299 int toSlot = slotForTab(toTab) + 1;
300 tabAmounts[tab] -= tabAmount;
301 tabAmounts[toTab] += tabAmount;
302
303 setFiringEvents(false);
304 for (int i = 0; i < tabAmount; i++) {
305 swap(true, fromSlot, toSlot, false);
306 }
307 setFiringEvents(true);
308 }
309
310 /**
311 * Changes the amount of items stored in a tab.
312 *
313 * @param tab The tab to modify.
314 * @param amount The amount to change.
315 */
316 public void changeTabAmount(int tab, int amount) {
317 if (tab < 0 || tab >= tabAmounts.length) {
318 return;
319 }
320
321 tabAmounts[tab] += amount;
322 if (tabAmounts[tab] == 0) {
323 collapse(tab, false);
324 }
325 }
326
327 public int depositFromNothing(Item item, int tab) {
328 setFiringEvents(false);
329 item = item.copy();
330 int id = item.getUnnotedId();
331 if (!contains(id)) {
332 if (size() + 1 > capacity()) {
333 return 0;
334 }
335
336 add(id, item.getAmount());
337 changeTabAmount(tab, 1);
338 int from = computeIndexForId(id);
339 int to = slotForTab(tab);
340 swap(true, from, to, false);
341 } else {
342 Item fucking = get(computeIndexForId(id));
343 if (Integer.MAX_VALUE - fucking.getAmount() < item.getAmount()) {
344 item.setAmount(Integer.MAX_VALUE - fucking.getAmount());
345 }
346 fucking.incrementAmountBy(item.getAmount());
347 }
348 setFiringEvents(true);
349 return item.getAmount();
350 }
351
352 /** Handles depositing the entire inventory. */
353 public void depositeInventory(boolean message) {
354 int tab = bankTab;
355 while (tab > 0 && tabAmounts[tab - 1] <= 0) tab--;
356 Item[] items = player.inventory.getItems();
357 for (int index = 0; index < items.length; index++) {
358 if (items[index] == null) continue;
359 Item item = items[index].copy();
360 item.setAmount(depositFromNothing(item, tab));
361 if(item.getId() == 12791) player.runePouch.clearInterface();
362 if (item.getAmount() > 0)
363 player.inventory.remove(item, index, false);
364 }
365 if (message) {
366 player.send(new SendMessage("You deposited all your inventory items."));
367 }
368 refresh();
369 }
370
371 /** Handles depositing all the equipment. */
372 public void depositeEquipment(boolean message) {
373 int tab = bankTab;
374 while (tab > 0 && tabAmounts[tab - 1] <= 0) tab--;
375 Item[] items = player.equipment.getItems();
376 for (int index = 0; index < items.length; index++) {
377 if (items[index] == null) continue;
378 Item item = items[index].copy();
379 item.setAmount(depositFromNothing(item, tab));
380 if (item.getAmount() > 0)
381 player.equipment.remove(item, index, false);
382 }
383 if (message) {
384 player.send(new SendMessage("You deposited all your worn equipment."));
385 }
386 player.equipment.login();
387 refresh();
388 }
389
390 private void itemToTab(int slot, int toTab) {
391 int fromTab = tabForSlot(slot);
392
393 /* Item is already in this tab. */
394 if (fromTab == toTab) return;
395
396 /*
397 * The tab to the left of the chosen tab is empty,
398 * so don't create a new tab.
399 */
400 if (toTab > 1 && tabAmounts[toTab - 1] == 0 && tabAmounts[toTab] == 0) {
401 return;
402 }
403
404 tabAmounts[toTab]++;
405 tabAmounts[fromTab]--;
406 int toSlot = slotForTab(toTab);
407
408 if (tabAmounts[fromTab] == 0) {
409 collapse(fromTab, false);
410 }
411
412 swap(true, slot, toSlot, false);
413 refresh();
414 }
415
416 /**
417 * Moves an item within the bank. <p> An opcode of 0 performs a swap
418 * operation, with {@code from} being the origin slot and {@code to} being
419 * the destination slot. </p> <p> An opcode of 1 performs an insert
420 * operation, with {@code from} being the origin slot and {@code to} being
421 * the destination slot. </p> <p> An opcode of 2 moves an item from one tab
422 * to another, with {@code from} being the origin <b>slot</b> and {@code to}
423 * being the destination <b>tab</b>. </p>
424 *
425 * @param opcode The opcode.
426 * @param from The origin slot or tab.
427 * @param to The destination slot or tab.
428 */
429 public void moveItem(int opcode, int from, int to) {
430 if (opcode == 2) {
431 itemToTab(from, to);
432 } else if (opcode == 1) {
433 swap(true, from, to, false);
434 int fromTab = tabForSlot(from);
435 int toTab = tabForSlot(to);
436 if (fromTab != toTab) {
437 changeTabAmount(toTab, 1);
438 changeTabAmount(fromTab, -1);
439 refresh();
440 }
441 } else {
442 swap(from, to);
443 }
444 }
445
446 public int tabForSlot(int slot) {
447 if (slot <= -1)
448 return -1;
449 int passed = -1;
450 for (int tab = 0; tab < tabAmounts.length; tab++) {
451 if (slot <= passed + tabAmounts[tab])
452 return tab;
453 passed += tabAmounts[tab];
454 }
455 return -1;
456 }
457
458 private int slotForTab(int tab) {
459 int passed = -1;
460 for (int index = tab; index >= 0; index--) {
461 passed += tabAmounts[index];
462 }
463 return passed;
464 }
465
466 @Override
467 public void clear() {
468 Arrays.fill(tabAmounts, 0);
469 super.clear();
470 }
471
472 @Override
473 public void shift() {
474 int[] amounts = Arrays.copyOf(tabAmounts, tabAmounts.length);
475 super.shift();
476 amounts = Arrays.copyOf(amounts, amounts.length);
477 System.arraycopy(amounts, 0, tabAmounts, 0, amounts.length);
478 }
479
480 @Override
481 public boolean allowZero() {
482 return placeHolder;
483 }
484
485 private final class BankListener extends ItemContainerAdapter {
486
488 super(player);
489 }
490
491 @Override
492 public int getWidgetId() {
494 }
495
496 @Override
497 public String getCapacityExceededMsg() {
498 return "Your bank is currently full!";
499 }
500
501 @Override
502 public void itemUpdated(ItemContainer container, Optional<Item> oldItem, Optional<Item> newItem, int index, boolean refresh, boolean login) {
503 }
504
505 @Override
506 public void bulkItemsUpdated(ItemContainer container) {
507 refresh();
508 }
509 }
510
511}
The class that contains helpful information on interfaces.
This class represents a character controlled by a player.
Definition Player.java:125
Represents all of an in-game Item's attributes.
static ItemDefinition get(int id)
Gets an item definition.
The container class that represents an item that can be interacted with.
Definition Item.java:21
final void setAmount(int amount)
Sets the quantity of this item.
Definition Item.java:351
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
Item copy()
A substitute for Object#clone() that creates another 'copy' of this instance.
Definition Item.java:270
int getValue(PriceType type)
Gets the value for this item.
Definition Item.java:98
final void incrementAmountBy(int amount)
Increments the amount by amount.
Definition Item.java:293
ItemContainerAdapter(Player player)
Creates a new ItemContainerAdapter.
ItemContainer(int capacity, StackPolicy policy, Item[] items)
Creates a new ItemContainer.
void setFiringEvents(boolean firingEvents)
Sets the value for firingEvents.
Item[] items
The Items within this container.
boolean add(Item item)
Attempts to deposit item into this container.
final int capacity
The capacity of this container.
final int computeIndexForId(int id)
Computes the first index found that id is in.
final boolean addListener(ItemContainerListener listener)
Adds an ItemContainerListener to this container.
boolean contains(int id)
Determines if this container contains id.
final Item[] toArray()
Returns a shallow copy of the backing array.
final void swap(int oldIndex, int newIndex)
Swaps the Items on oldIndex and newIndex.
void bulkItemsUpdated(ItemContainer container)
Fired when an Items are added, removed, or replaced in bulk.
Definition Bank.java:506
void itemUpdated(ItemContainer container, Optional< Item > oldItem, Optional< Item > newItem, int index, boolean refresh, boolean login)
Fired when an Item is added, removed, or replaced.
Definition Bank.java:502
void collapse(int tab, boolean collapseAll)
Collapses a tab and shifts other tabs one slot to the left.
Definition Bank.java:270
static final int SIZE
The size of all equipment instances.
Definition Bank.java:27
void depositeEquipment(boolean message)
Handles depositing all the equipment.
Definition Bank.java:372
boolean placeHolder
The place holder flag.
Definition Bank.java:45
void depositeInventory(boolean message)
Handles depositing the entire inventory.
Definition Bank.java:353
void moveItem(int opcode, int from, int to)
Moves an item within the bank.
Definition Bank.java:429
void open()
Opens the bank itemcontainer.
Definition Bank.java:56
void close()
Closes the bank itemcontainer.
Definition Bank.java:108
final Player player
The player instance.
Definition Bank.java:33
Bank(Player player)
Constructs a new Bank.
Definition Bank.java:48
void clear()
Removes all of the items from this container.
Definition Bank.java:467
void withdraw(int itemId, int slot, int amount)
Withdraws item from bank.
Definition Bank.java:188
void changeTabAmount(int tab, int amount)
Changes the amount of items stored in a tab.
Definition Bank.java:316
void refresh()
Refreshes the bank itemcontainer.
Definition Bank.java:73
void deposit(int slot, int amount)
Deposits item into bank.
Definition Bank.java:128
void shift()
Percolates the null indices to the end of the stack.
Definition Bank.java:473
void placeHolder(int item, int slot)
Handles the place holder option for the contianer.
Definition Bank.java:117
The OutgoingPacket responsible for changing settings on a client.
The OutgoingPacket that sends a message to a Players chatbox in the client.
The OutgoingPacket that sends a string to a Players itemcontainer in the client.
Handles miscellaneous methods.
Definition Utility.java:27
static String formatPrice(final long amount)
Formats a price for longs.
Definition Utility.java:56
An enumerated type defining policies for stackable Items.
ALWAYS
The ALWAYS policy, items are always stacked regardless of their ItemDefinition table.