@@ -243,25 +243,28 @@ function SkillsTabClass:SkillsTab(build)
243243 local item = getSelectedItem ()
244244 return not not item
245245 end
246- self .controls .optimiseSockets = new (" ButtonControl" ):ButtonControl ({ " LEFT" , self .controls .socketsLabel , " RIGHT" }, { 4 , 0 , 120 , 18 }, " ^7Optimise Sockets" , function ()
246+ local function getSocketCounts (item )
247+ local abyssalSocketCount = 0
248+ for _ , socket in ipairs (item .sockets ) do
249+ if socket .color == " A" then
250+ abyssalSocketCount = abyssalSocketCount + 1
251+ end
252+ end
253+ local maxSockets = (item .base .socketLimit or 0 ) - abyssalSocketCount
254+ return maxSockets , abyssalSocketCount
255+ end
256+ self .controls .optimiseSockets = new (" ButtonControl" ):ButtonControl ({ " LEFT" , self .controls .socketsLabel , " RIGHT" }, { 4 , 0 , 120 , 18 }, " Optimise Sockets" , function ()
247257 local item , groupSlot = getSelectedItem ()
248258 if not item or not groupSlot or not item .base then
249259 return
250260 end
251261
252262 self .build .itemsTab :AddUndoState ()
253263
254- -- save count of abyssal sockets
255- local abyssalSocketCount = 0
256- for _ , socket in ipairs (item .sockets ) do
257- if socket .color == " A" then
258- abyssalSocketCount = abyssalSocketCount + 1
259- end
260- end
264+ local maxSockets , abyssalSocketCount = getSocketCounts (item )
261265
262266 local groupCount = 0
263267 item .sockets = {}
264- local maxSockets = (item .base .socketLimit or 0 ) - abyssalSocketCount
265268 for _ , group in ipairs (self .socketGroupList ) do
266269 local colours = { " R" , " G" , " B" }
267270 if group .slot == groupSlot .slotName then
@@ -276,7 +279,6 @@ function SkillsTabClass:SkillsTab(build)
276279 groupCount = groupCount + 1
277280 end
278281 end
279-
280282 for _ = 0 , abyssalSocketCount - 1 do
281283 groupCount = groupCount + 1
282284 table.insert (item .sockets , { color = " A" , group = groupCount })
@@ -289,6 +291,26 @@ function SkillsTabClass:SkillsTab(build)
289291 local item = getSelectedItem ()
290292 return item and (item .base .socketLimit ~= nil )
291293 end
294+ self .controls .optimiseSockets .enabled = function ()
295+ local item , groupSlot = getSelectedItem ()
296+ if not item or not groupSlot or not item .base then
297+ return false
298+ end
299+ local maxSockets = getSocketCounts (item )
300+ for _ , group in ipairs (self .socketGroupList ) do
301+ if group .slot == groupSlot .slotName then
302+ for _ , gem in ipairs (group .gemList ) do
303+ if (gem .grantedEffect or (gem .gemData and gem .gemData .grantedEffect )) and maxSockets > 0 then
304+ if not gem .matchesSocket then
305+ return true
306+ end
307+ maxSockets = maxSockets - 1
308+ end
309+ end
310+ end
311+ end
312+ return false
313+ end
292314 self .controls .optimiseSockets .tooltipText = " Rebuild the item's sockets to match the groups assigned to it."
293315 -- self.imbuedSupportBySlot is used by CalcSetup to add an ExtraSupport mod of the selected gem
294316 -- Each displayGroup has its own "imbuedSupport" and is saved to the xml to load when changing sockets or loading a build
0 commit comments