RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
Bank.java
1package com.osroyale.game.world.items.containers.bank;
2
3import com.osroyale.game.world.InterfaceConstants;
4import com.osroyale.game.world.entity.mob.player.Player;
5import com.osroyale.game.world.entity.mob.player.PlayerRight;
6import com.osroyale.game.world.items.Item;
7import com.osroyale.game.world.items.ItemDefinition;
8import com.osroyale.game.world.items.containers.ItemContainer;
9import com.osroyale.game.world.items.containers.ItemContainerAdapter;
10import com.osroyale.game.world.items.containers.pricechecker.PriceType;
11import com.osroyale.net.packet.out.*;
12import com.osroyale.util.Utility;
13import plugin.itemon.item.LootingBagPlugin;
14
15import java.util.Arrays;
16import java.util.Optional;
17
60
61public class Bank extends ItemContainer {
62
64 public static final int SIZE = 360;
65
67 public int[] tabAmounts = new int[10];
68
70 private final Player player;
71
73 public int bankTab = 0;
74
76 public boolean noting = false;
77
79 public boolean inserting = false;
80
82 public boolean placeHolder;
83
85 public Bank(Player player) {
86 super(SIZE, StackPolicy.ALWAYS);
87 this.player = player;
88 this.placeHolder = false;
89 addListener(new BankListener());
90 }
91
93 public void open() {
94 if (player.right.equals(PlayerRight.ULTIMATE_IRONMAN)) {
95 player.send(new SendMessage("As an ultimate iron man you may not use banks!"));
96 return;
97 }
98 if (player.bankPin.hasPin() && !player.bankPin.entered) {
99 player.bankPin.enter();
100 return;
101 }
102 noting = false;
103 player.send(new SendString("360", 60018));
104 player.attributes.set("BANK_KEY", Boolean.TRUE);
105 player.interfaceManager.openInventory(60000, InterfaceConstants.INVENTORY_STORE - 1);
106 refresh();
107 }
108
110 public void refresh() {
111 player.send(new SendConfig(304, inserting ? 1 : 0));
112 player.send(new SendTooltip((inserting ? "Enable swapping" : "Enable inserting"), 60006));
113 player.send(new SendConfig(115, noting ? 1 : 0));
114 player.send(new SendTooltip((noting ? "Disable" : "Enable") + " noting", 60007));
115 player.send(new SendConfig(116, placeHolder ? 1 : 0));
116 player.send(new SendTooltip((placeHolder ? "Disable" : "Enable") + " place holders", 60073));
117 player.send(new SendItemOnInterface(InterfaceConstants.WITHDRAW_BANK, tabAmounts, toArray()));
118 player.inventory.refresh(player, InterfaceConstants.INVENTORY_STORE);
119 player.inventory.refresh();
120 sendValue();
121 }
122
123
124 public void sendValue() {
125 long value = 0;
126 int start = slotForTab(bankTab);
127 int end = start + tabAmounts[bankTab];
128 if (bankTab == 0) {
129 start = 0;
130 end = size();
131 }
132 for (int slot = start; slot < end; slot++) {
133 Item item = get(slot);
134 if (item == null) continue;
135 int price = item.getValue(PriceType.VALUE);
136 if (value >= Long.MAX_VALUE - price * item.getAmount()) {
137 value = Long.MAX_VALUE;
138 }
139 value += price * item.getAmount();
140 }
141 player.send(new SendString((bankTab == 0 ? "Bank" : "Tab " + bankTab) + " value: <col=FF5500>" + Utility.formatPrice(value) + "</col>", 60_079));
142 }
143
145 public void close() {
146 player.attributes.set("BANK_KEY", Boolean.FALSE);
147
148 if (player.pvpInstance) {
149 player.playerAssistant.setValueIcon();
150 }
151 }
152
154 public void placeHolder(int item, int slot) {
155 boolean hold = placeHolder;
156 placeHolder = true;
157 setFiringEvents(false);
158 withdraw(item, slot, Integer.MAX_VALUE);
159 setFiringEvents(true);
160 placeHolder = hold;
161 refresh();
162 }
163
165 public void deposit(int slot, int amount) {
166 boolean withinBank = player.interfaceManager.isInterfaceOpen(60000) || player.interfaceManager.isInterfaceOpen(4465);
167 if (!withinBank) {
168 System.out.println("interface: " + player.interfaceManager.getMain());
169 return;
170 }
171
172 Item item = player.inventory.get(slot);
173 if (item == null)
174 return;
175
176 int id = item.getId();
177
178 if (LootingBagPlugin.isLootingBag(item) && !player.lootingBag.isEmpty()) {
179 player.interfaceManager.openInventory(60000, 26700);
180 return;
181 }
182
183 int invAmount = player.inventory.computeAmountForId(id);
184
185 if (invAmount < amount) {
186 amount = invAmount;
187 }
188
189 setFiringEvents(false);
190
191 if (item.isNoted())
192 id = item.getUnnotedId();
193
194 if (!contains(id)) {
195 if (size() + 1 > capacity()) {
196 player.message("Your bank is full! You need to clear some items from your bank.");
197 setFiringEvents(true);
198 return;
199 }
200
202 add(id, amount);
203 int from = computeIndexForId(id);
204 int to = slotForTab(bankTab);
205 swap(true, from, to, false);
206 } else {
207 Item fucking = get(computeIndexForId(id));
208 if (Integer.MAX_VALUE - fucking.getAmount() < amount) {
209 amount = Integer.MAX_VALUE - fucking.getAmount();
210 player.send(new SendMessage("Your bank didn't have enough space to deposit all that!"));
211 }
212 fucking.incrementAmountBy(amount);
213 }
214
215 if (amount > 0) {
216 if(item.getId() == 12791) player.runePouch.clearInterface();
217 player.inventory.remove(item.getId(), amount);
218 }
219 setFiringEvents(true);
220
221 refresh();
222 }
223
225 public void withdraw(int itemId, int slot, int amount) {
226 if (!player.interfaceManager.isInterfaceOpen(60000)) {
227 return;
228 }
229 slot = computeIndexForId(itemId);
230 if (itemId < 0) return;
231
232 Item item = get(slot);
233 if (item == null || itemId != item.getId())
234 return;
235
236 if (item.getAmount() == 0) {//Releasing place holders
237 boolean hold = placeHolder;
238 placeHolder = false;
239 int tabSlot = computeIndexForId(item.getId());
240 int tab = tabForSlot(tabSlot);
241 changeTabAmount(tab, -1);
242 remove(item);
243 shift();
244 placeHolder = hold;
245 refresh();
246 return;
247 }
248
249 if (item.getAmount() < amount) {
250 amount = item.getAmount();
251 }
252
253 int id = item.getId();
254 if (noting) {
255 if (!item.isNoteable()) {
256 player.send(new SendMessage("This item cannot be withdrawn as a note."));
257 } else {
258 id = item.getNotedId();
259 }
260 }
261
262 setFiringEvents(false);
263 if (!new Item(id).isStackable() && amount > player.inventory.getFreeSlots()) {
264 amount = player.inventory.getFreeSlots();
265 } else if (ItemDefinition.get(id).isStackable() && player.inventory.getFreeSlots() == 0) {
266 if (!player.inventory.contains(id)) {
267 amount = 0;
268 } else if (player.inventory.computeAmountForId(id) + amount > Integer.MAX_VALUE) {
269 amount = Integer.MAX_VALUE - player.inventory.computeAmountForId(id);
270 }
271 }
272
273 if (amount == 0) {
274 player.send(new SendMessage("You do not have enough inventory spaces to withdraw this item."));
275 return;
276 }
277
278 int fuckingSlot = player.inventory.computeIndexForId(id);
279 if (fuckingSlot != -1) {
280 Item fuckingStan = player.inventory.get(fuckingSlot);
281 if (Integer.MAX_VALUE - fuckingStan.getAmount() < amount) {
282 amount = Integer.MAX_VALUE - fuckingStan.getAmount();
283 player.send(new SendMessage("Your inventory didn't have enough space to withdraw all that!"));
284 }
285 }
286
287
288 if (remove(item.getId(), amount)) {
289 player.inventory.add(id, amount);
290 if(id == 12791) player.runePouch.refresh();
291 // when an item is taken out of the bank completely, it removes one amount from the tab amounts array
292 if (!contains(item.getId())) {
293 int tab = tabForSlot(slot);
294 changeTabAmount(tab, -1);
295 shift();
296 }
297 }
298 setFiringEvents(true);
299 refresh();
300 }
301
307 public void collapse(int tab, boolean collapseAll) {
308 if (!player.interfaceManager.isInterfaceOpen(60000)) {
309 return;
310 }
311 if (tab == 0 && collapseAll) {
312 Arrays.fill(tabAmounts, 0);
313 tabAmounts[0] = size();
314 shift();
315 return;
316 }
317
318 /* Move the remaining items to the main tab. */
319 int tabAmount = tabAmounts[tab];
320 if (tabAmount > 0) moveTab(tab, 0);
321
322 /* Shift the remaining tabs to the left to fill the gap. */
323 recursiveCollapse(tab);
324 player.send(new SendConfig(211, bankTab = 0));
325 }
326
327 private void recursiveCollapse(int tab) {
328 if (tab == tabAmounts.length - 1) return;
329 moveTab(tab + 1, tab);
330 recursiveCollapse(tab + 1);
331 }
332
333 private void moveTab(int tab, int toTab) {
334 int tabAmount = tabAmounts[tab];
335 int fromSlot = slotForTab(tab);
336 int toSlot = slotForTab(toTab) + 1;
337 tabAmounts[tab] -= tabAmount;
338 tabAmounts[toTab] += tabAmount;
339
340 setFiringEvents(false);
341 for (int i = 0; i < tabAmount; i++) {
342 swap(true, fromSlot, toSlot, false);
343 }
344 setFiringEvents(true);
345 }
346
353 public void changeTabAmount(int tab, int amount) {
354 if (tab < 0 || tab >= tabAmounts.length) {
355 return;
356 }
357
358 tabAmounts[tab] += amount;
359 if (tabAmounts[tab] == 0) {
360 collapse(tab, false);
361 }
362 }
363
364 public int depositFromNothing(Item item, int tab) {
365 setFiringEvents(false);
366 item = item.copy();
367 int id = item.getUnnotedId();
368 if (!contains(id)) {
369 if (size() + 1 > capacity()) {
370 return 0;
371 }
372
373 add(id, item.getAmount());
374 changeTabAmount(tab, 1);
375 int from = computeIndexForId(id);
376 int to = slotForTab(tab);
377 swap(true, from, to, false);
378 } else {
379 Item fucking = get(computeIndexForId(id));
380 if (Integer.MAX_VALUE - fucking.getAmount() < item.getAmount()) {
381 item.setAmount(Integer.MAX_VALUE - fucking.getAmount());
382 }
383 fucking.incrementAmountBy(item.getAmount());
384 }
385 setFiringEvents(true);
386 return item.getAmount();
387 }
388
390 public void depositeInventory(boolean message) {
391 int tab = bankTab;
392 while (tab > 0 && tabAmounts[tab - 1] <= 0) tab--;
393 Item[] items = player.inventory.getItems();
394 for (int index = 0; index < items.length; index++) {
395 if (items[index] == null) continue;
396 Item item = items[index].copy();
397 item.setAmount(depositFromNothing(item, tab));
398 if(item.getId() == 12791) player.runePouch.clearInterface();
399 if (item.getAmount() > 0)
400 player.inventory.remove(item, index, false);
401 }
402 if (message) {
403 player.send(new SendMessage("You deposited all your inventory items."));
404 }
405 refresh();
406 }
407
409 public void depositeEquipment(boolean message) {
410 int tab = bankTab;
411 while (tab > 0 && tabAmounts[tab - 1] <= 0) tab--;
412 Item[] items = player.equipment.getItems();
413 for (int index = 0; index < items.length; index++) {
414 if (items[index] == null) continue;
415 Item item = items[index].copy();
416 item.setAmount(depositFromNothing(item, tab));
417 if (item.getAmount() > 0)
418 player.equipment.remove(item, index, false);
419 }
420 if (message) {
421 player.send(new SendMessage("You deposited all your worn equipment."));
422 }
423 player.equipment.login();
424 refresh();
425 }
426
427 private void itemToTab(int slot, int toTab) {
428 int fromTab = tabForSlot(slot);
429
430 /* Item is already in this tab. */
431 if (fromTab == toTab) return;
432
433 /*
434 * The tab to the left of the chosen tab is empty,
435 * so don't create a new tab.
436 */
437 if (toTab > 1 && tabAmounts[toTab - 1] == 0 && tabAmounts[toTab] == 0) {
438 return;
439 }
440
441 tabAmounts[toTab]++;
442 tabAmounts[fromTab]--;
443 int toSlot = slotForTab(toTab);
444
445 if (tabAmounts[fromTab] == 0) {
446 collapse(fromTab, false);
447 }
448
449 swap(true, slot, toSlot, false);
450 refresh();
451 }
452
466 public void moveItem(int opcode, int from, int to) {
467 if (opcode == 2) {
468 itemToTab(from, to);
469 } else if (opcode == 1) {
470 swap(true, from, to, false);
471 int fromTab = tabForSlot(from);
472 int toTab = tabForSlot(to);
473 if (fromTab != toTab) {
474 changeTabAmount(toTab, 1);
475 changeTabAmount(fromTab, -1);
476 refresh();
477 }
478 } else {
479 swap(from, to);
480 }
481 }
482
483 public int tabForSlot(int slot) {
484 if (slot <= -1)
485 return -1;
486 int passed = -1;
487 for (int tab = 0; tab < tabAmounts.length; tab++) {
488 if (slot <= passed + tabAmounts[tab])
489 return tab;
490 passed += tabAmounts[tab];
491 }
492 return -1;
493 }
494
495 private int slotForTab(int tab) {
496 int passed = -1;
497 for (int index = tab; index >= 0; index--) {
498 passed += tabAmounts[index];
499 }
500 return passed;
501 }
502
503 @Override
504 public void clear() {
505 Arrays.fill(tabAmounts, 0);
506 super.clear();
507 }
508
509 @Override
510 public void shift() {
511 int[] amounts = Arrays.copyOf(tabAmounts, tabAmounts.length);
512 super.shift();
513 amounts = Arrays.copyOf(amounts, amounts.length);
514 System.arraycopy(amounts, 0, tabAmounts, 0, amounts.length);
515 }
516
517 @Override
518 public boolean allowZero() {
519 return placeHolder;
520 }
521
522 private final class BankListener extends ItemContainerAdapter {
523
524 BankListener() {
525 super(player);
526 }
527
528 @Override
529 public int getWidgetId() {
530 return InterfaceConstants.WITHDRAW_BANK;
531 }
532
533 @Override
534 public String getCapacityExceededMsg() {
535 return "Your bank is currently full!";
536 }
537
538 @Override
539 public void itemUpdated(ItemContainer container, Optional<Item> oldItem, Optional<Item> newItem, int index, boolean refresh, boolean login) {
540 }
541
542 @Override
543 public void bulkItemsUpdated(ItemContainer container) {
544 refresh();
545 }
546 }
547
548}
ItemContainer(int capacity, StackPolicy policy, Item[] items)
final void swap(int oldIndex, int newIndex)
final boolean addListener(ItemContainerListener listener)
void withdraw(int itemId, int slot, int amount)
Definition Bank.java:225
void collapse(int tab, boolean collapseAll)
Definition Bank.java:307
void moveItem(int opcode, int from, int to)
Definition Bank.java:466
static String formatPrice(final long amount)
Definition Utility.java:93