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