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

local MAXIMUM_DIFFERENT_ITEMS = 6

local plr = game.Players.LocalPlayer

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.MainFrame.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.MainFrame.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
		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
				end
			end
		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.MainFrame.Display.Container.AddItem:Fire(item)
	
	-- Tell preview to refresh
	plr.PlayerGui.MainGui.Crafting.MainFrame.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.MainFrame.Preview.Refresh:Fire({})
	
end)
