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