local G=6 local z=game.Players.LocalPlayer local o=game:GetService("\071\117\105\083\101\114\118\105\099\101")function getItemTable()local G={}for z,o in pairs(script.Parent:GetChildren())do if o.Name=="\073\116\101\109"then for z=1,o.Count.Value,1 do table.insert(G,o.Item.Value.Name)end end end return G end function refreshOutlines()local G={}for z,o in pairs(script.Parent:GetChildren())do if o.Name=="\073\116\101\109"then table.insert(G,o.Item.Value.Name)end end if#G>0 then local z={}for o,a in pairs(game.ReplicatedStorage.Recipes:GetChildren())do local C=true for G,z in pairs(G)do if a.Ingredients:FindFirstChild(z)==nil then C=false break end end if C then for G,o in pairs(a.Ingredients:GetChildren())do table.insert(z,o.Name)end end end for G,o in pairs(script.Parent.Parent.Parent.Display.Container:GetChildren())do if o.Name=="\073\116\101\109"then local G=false for a,C in pairs(z)do if C==o.Item.Value.Name then G=true table.remove(z,a)break end end o.BlueEdge.Visible=G end end else for G,z in pairs(script.Parent.Parent.Parent.Display.Container:GetChildren())do if z.Name=="\073\116\101\109"then z.BlueEdge.Visible=false end end end end script.Parent.AddItem.OnInvoke=function(o)local a=nil for G,z in pairs(script.Parent:GetChildren())do if z.Name=="\073\116\101\109"then if z.Item.Value==o then a=z break end end end if a==nil then local a=#script.Parent:GetChildren()-5 if a>=G then return false end local C=script.Parent.ItemTemplate:clone()C.Name="\073\116\101\109"C.Item.Value=o C.ItemIcon.Image=o.IconAsset.Value C.ItemNameLabel.Text=o.Name C.ItemNameLabel.TextColor3=z.PlayerScripts.Functions.GetRarityColor:Invoke(o.Rarity.Value)if o.Rarity.Value=="\073\109\109\111\114\116\097\108"or o.Rarity.Value=="\068\101\118\101\108\111\112\101\114"then C.ItemName.TextStrokeColor3=Color3.fromRGB(255,255,255)end C.Position=UDim2.new(0,(a%3)*116+10,0,math.floor(a/3)*40+10)C.Parent=script.Parent C.Visible=true C.InteractionHandler.Disabled=false z.PlayerGui.MainGui.Crafting.Preview.Refresh:Fire(getItemTable())refreshOutlines()return true else a.Count.Value=a.Count.Value+1 a.ItemCount.Text="\120 "..a.Count.Value z.PlayerGui.MainGui.Crafting.Preview.Refresh:Fire(getItemTable())return true end end script.Parent.RemoveItem.Event:connect(function(G)local a=G.Item.Value G.Count.Value=G.Count.Value-1 if G.Count.Value<=0 then local a=((G.Position.Y.Offset-10)/40)*3+(G.Position.X.Offset-10)/116 G:destroy()local C=nil local E=nil local c=nil local j=nil for G,z in pairs(script.Parent:GetChildren())do if z.Name=="\073\116\101\109"then local G=((z.Position.Y.Offset-10)/40)*3+(z.Position.X.Offset-10)/116 if G>a then z.Position=z.Position+UDim2.new(0,-116,0,0)if z.Position.X.Offset<0 then z.Position=z.Position+UDim2.new(0,348,0,-40)end if C==nil or G<E then C=z E=G end end if c==nil or G>j then c=z j=G end end end if C~=nil then o.SelectedObject=C.ClickArea elseif c~=nil then o.SelectedObject=c.ClickArea else o.SelectedObject=z.PlayerGui.MainGui.Crafting.OpenRecipeBook end elseif G.Count.Value>1 then G.ItemCount.Text="\120 "..G.Count.Value else G.ItemCount.Text=""end z.PlayerGui.MainGui.Crafting.Display.Container.AddItem:Fire(a)z.PlayerGui.MainGui.Crafting.Preview.Refresh:Fire(getItemTable())refreshOutlines()end)script.Parent.ClearItems.Event:connect(function()for G,z in pairs(script.Parent:GetChildren())do if z.Name=="\073\116\101\109"then z:destroy()end end z.PlayerGui.MainGui.Crafting.Preview.Refresh:Fire({})end)

local MAXIMUM_DIFFERENT_ITEMS = 6

local plr = game.Players.LocalPlayer
local guiService = game:GetService("GuiService")

function getItemTable()
	
	-- Formulate and return a table of items
	local items = {}
	for _, i in pairs(script.Parent:GetChildren()) do
		if i.Name == "Item" then
			for _=1, i.Count.Value do
				table.insert(items, i.Item.Value.Name)
			end
		end
	end
	return items
	
end

function refreshOutlines()
	
	-- Create table of item names
	local items = {}
	for _, i in pairs(script.Parent:GetChildren()) do
		if i.Name == "Item" then
			table.insert(items, i.Item.Value.Name)
		end
	end
	
	-- Check size
	if #items > 0 then
	
		-- Create table of connected items
		local connectedItems = {}
		for _, i in pairs(game.ReplicatedStorage.Recipes:GetChildren()) do
			local okay = true
			for _, o in pairs(items) do
				if i.Ingredients:FindFirstChild(o) == nil then
					okay = false
					break
				end
			end
			if okay then
				for _, o in pairs(i.Ingredients:GetChildren()) do
					table.insert(connectedItems, o.Name)
				end
			end
		end
		
		-- Go through displayed items adding/removing outlines
		for _, i in pairs(script.Parent.Parent.Parent.Display.Container:GetChildren()) do
			if i.Name == "Item" then
				local okay = false
				for n, o in pairs(connectedItems) do
					if o == i.Item.Value.Name then
						okay = true
						table.remove(connectedItems, n)
						break
					end
				end
				i.BlueEdge.Visible = okay
			end
		end
		
	else
		
		-- Remove all outlines
		for _, i in pairs(script.Parent.Parent.Parent.Display.Container:GetChildren()) do
			if i.Name == "Item" then
				i.BlueEdge.Visible = false
			end
		end
		
	end
	
end

script.Parent.AddItem.OnInvoke = function(item)
	
	-- Check if there already is an entry for the item
	local existing = nil
	for _, i in pairs(script.Parent:GetChildren()) do
		if i.Name == "Item" then
			if i.Item.Value == item then
				existing = i
				break
			end
		end
	end
	if existing == nil then
		
		-- Check if the item count maximum has been reached
		local entryCount = #script.Parent:GetChildren() - 5
		if entryCount >= MAXIMUM_DIFFERENT_ITEMS then
			return false
		end
		
		-- Create an item GUI element and add to container
		local itemGui = script.Parent.ItemTemplate:clone()
		itemGui.Name = "Item"
		itemGui.Item.Value = item
		itemGui.ItemIcon.Image = item.IconAsset.Value
		itemGui.ItemNameLabel.Text = item.Name
		itemGui.ItemNameLabel.TextColor3 = plr.PlayerScripts.Functions.GetRarityColor:Invoke(item.Rarity.Value)
		if item.Rarity.Value == "Immortal" or item.Rarity.Value == "Developer" then
			itemGui.ItemName.TextStrokeColor3 = Color3.fromRGB(255, 255, 255)
		end
		itemGui.Position = UDim2.new(0, (entryCount % 3) * 116 + 10, 0, math.floor(entryCount / 3) * 40 + 10)
		itemGui.Parent = script.Parent
		itemGui.Visible = true
		itemGui.InteractionHandler.Disabled = false
		
		-- Tell preview to refresh
		plr.PlayerGui.MainGui.Crafting.Preview.Refresh:Fire(getItemTable())
		refreshOutlines()
		
		-- Return success
		return true
		
	else
	
		-- Increment count value
		existing.Count.Value = existing.Count.Value + 1
		existing.ItemCount.Text = "x " .. existing.Count.Value
		
		-- Tell preview to refresh
		plr.PlayerGui.MainGui.Crafting.Preview.Refresh:Fire(getItemTable())
		
		-- Return success
		return true
		
	end
	
end

script.Parent.RemoveItem.Event:connect(function(itemGui)
	
	-- Get item
	local item = itemGui.Item.Value
	
	-- Decrement count
	itemGui.Count.Value = itemGui.Count.Value - 1
	
	-- Check if none left
	if itemGui.Count.Value <= 0 then
		
		-- Calculate gui rank to be used below
		local thisRank = (((itemGui.Position.Y.Offset - 10) / 40) * 3) + ((itemGui.Position.X.Offset - 10) / 116)
	
		-- Destroy the gui
		itemGui:destroy()
		
		-- Move each item over/up
		local closestItem = nil
		local closestItemRank = nil
		local lastItem = nil
		local lastItemRank = nil
		for _, i in pairs(script.Parent:GetChildren()) do
			if i.Name == "Item" then
				local rank = (((i.Position.Y.Offset - 10) / 40) * 3) + ((i.Position.X.Offset - 10) / 116)
				if rank > thisRank then
					i.Position = i.Position + UDim2.new(0, -116, 0, 0)
					if i.Position.X.Offset < 0 then
						i.Position = i.Position + UDim2.new(0, 348, 0, -40)
					end
					if closestItem == nil or rank < closestItemRank then
						closestItem = i
						closestItemRank = rank
					end
				end
				if lastItem == nil or rank > lastItemRank then
					lastItem = i
					lastItemRank = rank
				end
			end
		end
		if closestItem ~= nil then
			guiService.SelectedObject = closestItem.ClickArea
		elseif lastItem ~= nil then
			guiService.SelectedObject = lastItem.ClickArea
		else
			guiService.SelectedObject = plr.PlayerGui.MainGui.Crafting.OpenRecipeBook
		end
		
	elseif itemGui.Count.Value > 1 then
		
		-- Change text
		itemGui.ItemCount.Text = "x " .. itemGui.Count.Value
		
	else
		
		-- Change text
		itemGui.ItemCount.Text = ""
		
	end
	
	-- Add back to main display
	plr.PlayerGui.MainGui.Crafting.Display.Container.AddItem:Fire(item)
	
	-- Tell preview to refresh
	plr.PlayerGui.MainGui.Crafting.Preview.Refresh:Fire(getItemTable())
	refreshOutlines()
	
end)

script.Parent.ClearItems.Event:connect(function()
	
	-- Destroy all items
	for _, i in pairs(script.Parent:GetChildren()) do
		if i.Name == "Item" then
			i:destroy()
		end
	end
	
	-- Tell preview to refresh
	plr.PlayerGui.MainGui.Crafting.Preview.Refresh:Fire({})
	
end)
