Skip to content

Commit 445f736

Browse files
committed
bank inserting
1 parent e0f9479 commit 445f736

File tree

4 files changed

+61
-12
lines changed

4 files changed

+61
-12
lines changed

src/net/incoming-packets/item-swap-packet.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { incomingPacket } from '../incoming-packet';
22
import { Player } from '../../world/actor/player/player';
3-
import { swapItemAction } from '../../world/actor/player/action/swap-item-action';
3+
import { insertItemAction, swapItemAction } from '../../world/actor/player/action/swap-item-action';
44
import { ByteBuffer } from '@runejs/byte-buffer';
55

66
export const itemSwapPacket: incomingPacket = (player: Player, packetId: number, packetSize: number, packet: ByteBuffer): void => {
@@ -18,6 +18,6 @@ export const itemSwapPacket: incomingPacket = (player: Player, packetId: number,
1818
// Swap
1919
swapItemAction(player, fromSlot, toSlot, { widgetId, containerId });
2020
} else if(swapType === 1) {
21-
// @TODO insert
21+
insertItemAction(player, fromSlot, toSlot, { widgetId, containerId });
2222
}
2323
};

src/plugins/objects/bank/bank-plugin.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { widgets, widgetScripts } from '@server/world/config/widget';
44
import { objectAction } from '@server/world/actor/player/action/object-action';
55
import { ItemContainer } from '@server/world/items/item-container';
66
import { itemAction } from '@server/world/actor/player/action/item-action';
7-
import { fromNote, Item } from '@server/world/items/item';
7+
import { fromNote, Item, toNote } from '@server/world/items/item';
88
import { buttonAction } from '@server/world/actor/player/action/button-action';
99
import { logger } from '@runejs/logger/dist/logger';
1010
import { hasValueNotNull } from '@server/util/data';
@@ -109,6 +109,18 @@ export const withdrawItem: itemAction = (details) => {
109109
if (!details.player.hasItemInBank(details.itemId)) {
110110
return;
111111
}
112+
113+
let itemIdToAdd: number = details.itemId;
114+
if (details.player.settings.bankWithdrawNoteMode) {
115+
let toNoteId: number = toNote(details.itemId);
116+
if (toNoteId > -1) {
117+
itemIdToAdd = toNoteId;
118+
} else {
119+
details.player.sendMessage('This item can not be withdrawn as a note.');
120+
}
121+
}
122+
123+
112124
let countToRemove: number;
113125
if (details.option.endsWith('all')) {
114126
countToRemove = -1;
@@ -130,14 +142,13 @@ export const withdrawItem: itemAction = (details) => {
130142
countToRemove = slots;
131143
}
132144
}
133-
134-
if (!playerInventory.canFit({itemId: details.itemId, amount: countToRemove})) {
145+
if (!playerInventory.canFit({itemId: itemIdToAdd, amount: countToRemove}) || countToRemove === 0) {
135146
details.player.sendMessage('Your inventory is full.');
136147
return;
137148
}
138149

139150

140-
const itemToAdd: Item = {itemId: details.itemId, amount: 0};
151+
const itemToAdd: Item = {itemId: itemIdToAdd, amount: 0};
141152
while (countToRemove > 0 && playerBank.has(details.itemId)) {
142153
const invIndex = playerBank.findIndex(details.itemId);
143154
const invItem = playerBank.items[invIndex];
@@ -151,7 +162,9 @@ export const withdrawItem: itemAction = (details) => {
151162
countToRemove = 0;
152163
}
153164
}
154-
playerInventory.addStacking(itemToAdd);
165+
for (let i = 0; i < itemToAdd.amount; i++) {
166+
playerInventory.add({itemId: itemIdToAdd, amount: 1});
167+
}
155168

156169

157170
details.player.outgoingPackets.sendUpdateAllWidgetItems(widgets.bank.tabWidget, details.player.inventory);
Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,56 @@
11
import { Player } from '../player';
22
import { widgets } from '../../../config/widget';
3+
import { logger } from '@runejs/logger/dist/logger';
34

45
export const swapItemAction = (player: Player, fromSlot: number, toSlot: number, widget: { widgetId: number, containerId: number }) => {
5-
if(widget.widgetId === widgets.inventory.widgetId && widget.containerId === widgets.inventory.containerId) {
6+
if (widget.widgetId === widgets.inventory.widgetId && widget.containerId === widgets.inventory.containerId) {
67
const inventory = player.inventory;
78

8-
if(toSlot > inventory.size - 1 || fromSlot > inventory.size - 1) {
9+
if (toSlot > inventory.size - 1 || fromSlot > inventory.size - 1) {
910
return;
1011
}
1112

1213
inventory.swap(fromSlot, toSlot);
1314
}
14-
if(widget.widgetId === widgets.bank.screenWidget.widgetId && widget.containerId === widgets.bank.screenWidget.containerId) {
15+
if (widget.widgetId === widgets.bank.screenWidget.widgetId && widget.containerId === widgets.bank.screenWidget.containerId) {
1516
const bank = player.bank;
1617

17-
if(toSlot > bank.size - 1 || fromSlot > bank.size - 1) {
18+
if (toSlot > bank.size - 1 || fromSlot > bank.size - 1) {
1819
return;
1920
}
2021

2122
bank.swap(fromSlot, toSlot);
2223
}
2324
};
25+
26+
27+
export const insertItemAction = (player: Player, fromSlot: number, toSlot: number, widget: { widgetId: number, containerId: number }) => {
28+
if (widget.widgetId === widgets.bank.screenWidget.widgetId && widget.containerId === widgets.bank.screenWidget.containerId) {
29+
const bank = player.bank;
30+
31+
if (toSlot > bank.size - 1 || fromSlot > bank.size - 1) {
32+
return;
33+
}
34+
if (fromSlot < toSlot) {
35+
let slot = toSlot;
36+
let current = bank.remove(fromSlot);
37+
while (slot >= fromSlot) {
38+
let temp = bank.remove(slot);
39+
bank.set(slot, current);
40+
current = temp;
41+
slot--;
42+
}
43+
} else {
44+
let slot = toSlot;
45+
let current = bank.remove(fromSlot);
46+
while (slot <= fromSlot) {
47+
let temp = bank.remove(slot);
48+
bank.set(slot, current);
49+
current = temp;
50+
slot++;
51+
}
52+
}
53+
player.outgoingPackets.sendUpdateAllWidgetItems(widgets.bank.screenWidget, player.bank);
54+
55+
}
56+
};

src/world/items/item-container.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,15 @@ export class ItemContainer {
196196
return slot;
197197
}
198198

199-
public remove(slot: number, fireEvent: boolean = true): void {
199+
public remove(slot: number, fireEvent: boolean = true): Item {
200+
let item = this._items[slot];
200201
this._items[slot] = null;
201202

202203
if (fireEvent) {
203204
this._containerUpdated.next({type: 'REMOVE', slot});
204205
}
206+
return item;
207+
205208
}
206209

207210
public getFirstOpenSlot(): number {

0 commit comments

Comments
 (0)