@@ -2,29 +2,146 @@ import { ActionType, RunePlugin } from '@server/plugins/plugin';
2
2
import { objectIds } from '@server/world/config/object-ids' ;
3
3
import { widgets } from '@server/world/config/widget' ;
4
4
import { objectAction } from '@server/world/actor/player/action/object-action' ;
5
+ import { ItemContainer } from '@server/world/items/item-container' ;
6
+ import { itemAction } from '@server/world/actor/player/action/item-action' ;
7
+ import { Item } from '@server/world/items/item' ;
5
8
6
9
7
10
export const openBankInterface : objectAction = ( details ) => {
8
11
details . player . activeWidget = {
9
- widgetId : widgets . bank . screenWidget ,
12
+ widgetId : widgets . bank . screenWidget . widgetId ,
10
13
secondaryWidgetId : widgets . bank . tabWidget . widgetId ,
11
14
type : 'SCREEN_AND_TAB' ,
12
15
closeOnWalk : true
13
16
} ;
17
+
14
18
details . player . outgoingPackets . sendUpdateAllWidgetItems ( widgets . bank . tabWidget , details . player . inventory ) ;
19
+ details . player . outgoingPackets . sendUpdateAllWidgetItems ( widgets . bank . screenWidget , details . player . bank ) ;
20
+ details . player . outgoingPackets . updateClientConfig ( 304 , details . player . sessionMetadata [ 'bankRearrangeMode' ] === 'insert' ? 1 : 0 ) ;
21
+ details . player . outgoingPackets . updateClientConfig ( 115 , details . player . sessionMetadata [ 'bankWithdrawAs' ] === 'note' ? 1 : 0 ) ;
15
22
16
23
} ;
17
24
18
- export const depositItem : objectAction = ( details ) => {
25
+ export const depositItem : itemAction = ( details ) => {
19
26
// Check if player might be spawning widget clientside
20
27
if ( ! details . player . activeWidget ||
21
- ! ( details . player . activeWidget . widgetId === widgets . bank . screenWidget ) ||
28
+ ! ( details . player . activeWidget . widgetId === widgets . bank . screenWidget . widgetId ) ||
22
29
! ( details . player . activeWidget . secondaryWidgetId === widgets . bank . tabWidget . widgetId ) ) {
23
30
return ;
24
31
}
25
32
33
+ // Check if the player has the item
34
+ if ( ! details . player . hasItemInInventory ( details . itemId ) ) {
35
+ return ;
36
+ }
37
+
38
+ let countToRemove : number ;
39
+ if ( details . option . endsWith ( 'all' ) ) {
40
+ countToRemove = - 1 ;
41
+ } else {
42
+ countToRemove = + details . option . replace ( 'deposit-' , '' ) ;
43
+ }
44
+
45
+ const playerInventory : ItemContainer = details . player . inventory ;
46
+ const playerBank : ItemContainer = details . player . bank ;
47
+ const slotsWithItem : number [ ] = playerInventory . findAll ( details . itemId ) ;
48
+ let itemAmount : number = 0 ;
49
+ slotsWithItem . forEach ( ( slot ) => itemAmount += playerInventory . items [ slot ] . amount ) ;
50
+ if ( countToRemove == - 1 || countToRemove > itemAmount ) {
51
+ countToRemove = itemAmount ;
52
+ }
53
+
54
+ if ( ! playerBank . canFit ( { itemId : details . itemId , amount : countToRemove } , true ) ) {
55
+ details . player . sendMessage ( 'Your bank is full.' ) ;
56
+ return ;
57
+ }
58
+
59
+
60
+ const itemToAdd : Item = { itemId : details . itemId , amount : 0 } ;
61
+ while ( countToRemove > 0 && playerInventory . has ( details . itemId ) ) {
62
+ const invIndex = playerInventory . findIndex ( details . itemId ) ;
63
+ const invItem = playerInventory . items [ invIndex ] ;
64
+ if ( countToRemove >= invItem . amount ) {
65
+ itemToAdd . amount += invItem . amount ;
66
+ countToRemove -= invItem . amount ;
67
+ playerInventory . remove ( invIndex ) ;
68
+ } else {
69
+ itemToAdd . amount += countToRemove ;
70
+ invItem . amount -= countToRemove ;
71
+ countToRemove = 0 ;
72
+ }
73
+ }
74
+ playerBank . addStacking ( itemToAdd ) ;
75
+
76
+
77
+ details . player . outgoingPackets . sendUpdateAllWidgetItems ( widgets . bank . tabWidget , details . player . inventory ) ;
78
+ details . player . outgoingPackets . sendUpdateAllWidgetItems ( widgets . inventory , details . player . inventory ) ;
79
+ details . player . outgoingPackets . sendUpdateAllWidgetItems ( widgets . bank . screenWidget , details . player . bank ) ;
26
80
} ;
27
81
82
+
83
+ export const withdrawItem : itemAction = ( details ) => {
84
+ // Check if player might be spawning widget clientside
85
+ if ( ! details . player . activeWidget ||
86
+ ! ( details . player . activeWidget . widgetId === widgets . bank . screenWidget . widgetId ) ||
87
+ ! ( details . player . activeWidget . secondaryWidgetId === widgets . bank . tabWidget . widgetId ) ) {
88
+ return ;
89
+ }
90
+ // Check if the player has the item
91
+ if ( ! details . player . hasItemInBank ( details . itemId ) ) {
92
+ return ;
93
+ }
94
+ let countToRemove : number ;
95
+ if ( details . option . endsWith ( 'all' ) ) {
96
+ countToRemove = - 1 ;
97
+ } else {
98
+ countToRemove = + details . option . replace ( 'withdraw-' , '' ) ;
99
+ }
100
+
101
+ const playerBank : ItemContainer = details . player . bank ;
102
+ const playerInventory : ItemContainer = details . player . inventory ;
103
+ const slotWithItem : number = playerBank . findIndex ( details . itemId ) ;
104
+ const itemAmount : number = playerBank . items [ slotWithItem ] . amount ;
105
+ if ( countToRemove == - 1 || countToRemove > itemAmount ) {
106
+ countToRemove = itemAmount ;
107
+ }
108
+
109
+ if ( ! details . itemDetails . stackable ) {
110
+ const slots = playerInventory . getOpenSlotCount ( ) ;
111
+ if ( slots < countToRemove ) {
112
+ countToRemove = slots ;
113
+ }
114
+ }
115
+
116
+ if ( ! playerInventory . canFit ( { itemId : details . itemId , amount : countToRemove } ) ) {
117
+ details . player . sendMessage ( 'Your inventory is full.' ) ;
118
+ return ;
119
+ }
120
+
121
+
122
+ const itemToAdd : Item = { itemId : details . itemId , amount : 0 } ;
123
+ while ( countToRemove > 0 && playerBank . has ( details . itemId ) ) {
124
+ const invIndex = playerBank . findIndex ( details . itemId ) ;
125
+ const invItem = playerBank . items [ invIndex ] ;
126
+ if ( countToRemove >= invItem . amount ) {
127
+ itemToAdd . amount += invItem . amount ;
128
+ countToRemove -= invItem . amount ;
129
+ playerBank . remove ( invIndex ) ;
130
+ } else {
131
+ itemToAdd . amount += countToRemove ;
132
+ invItem . amount -= countToRemove ;
133
+ countToRemove = 0 ;
134
+ }
135
+ }
136
+ playerInventory . addStacking ( itemToAdd ) ;
137
+
138
+
139
+ details . player . outgoingPackets . sendUpdateAllWidgetItems ( widgets . bank . tabWidget , details . player . inventory ) ;
140
+ details . player . outgoingPackets . sendUpdateAllWidgetItems ( widgets . inventory , details . player . inventory ) ;
141
+ details . player . outgoingPackets . sendUpdateAllWidgetItems ( widgets . bank . screenWidget , details . player . bank ) ;
142
+ } ;
143
+
144
+
28
145
export default new RunePlugin ( [ {
29
146
type : ActionType . OBJECT_ACTION ,
30
147
objectIds : objectIds . bankBooth ,
@@ -34,6 +151,11 @@ export default new RunePlugin([{
34
151
} , {
35
152
type : ActionType . ITEM_ACTION ,
36
153
widgets : widgets . bank . tabWidget ,
37
- options : [ 'deposit-1' , 'deposit-5' , 'deposit-10' ] ,
154
+ options : [ 'deposit-1' , 'deposit-5' , 'deposit-10' , 'deposit-all' ] ,
38
155
action : depositItem ,
156
+ } , {
157
+ type : ActionType . ITEM_ACTION ,
158
+ widgets : widgets . bank . screenWidget ,
159
+ options : [ 'withdraw-1' , 'withdraw-5' , 'withdraw-10' , 'withdraw-all' ] ,
160
+ action : withdrawItem ,
39
161
} ] ) ;
0 commit comments