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