local x={}function NewPartTool.Equip()ShowUI()EnableClickCreation()SetType(NewPartTool.Type)end function NewPartTool.Unequip()HideUI()ClearConnections()end function ClearConnections()for g,u in pairs(x)do u:disconnect()x[g]=nil end end function ShowUI()if UI then UI.Visible=true return end UI=Core.Tool.Interfaces.BTNewPartToolGUI:Clone()UI.Parent=Core.UI UI.Visible=true Types={"\078\111\114\109\097\108";"\084\114\117\115\115";"\087\101\100\103\101","\067\111\114\110\101\114";"\067\121\108\105\110\100\101\114";"\066\097\108\108";"\083\101\097\116","\086\101\104\105\099\108\101 \083\101\097\116";"\083\112\097\119\110"}TypeDropdown=(Core.Cheer(UI.TypeOption.Dropdown)).Start(Types,"",function(x)SetType(x)end)end function HideUI()if not UI then return end UI.Visible=false end function SetType(x)NewPartTool.Type=x TypeDropdown.SetOption(x)end function EnableClickCreation()x.ClickCreationListener=UserInputService.InputBegan:connect(function(x,g)if g then return end if x.UserInputType~=Enum.UserInputType.MouseButton1 then return end DragNewParts=true Core.Targeting.CancelSelecting()CreatePart(NewPartTool.Type)end)x.ClickReleaseListener=Support.AddUserInputListener("\069\110\100\101\100","\077\111\117\115\101\066\117\116\116\111\110\049",true,function()DragNewParts=false end)end function CreatePart(x)local g=Core.SyncAPI:Invoke("\067\114\101\097\116\101\080\097\114\116",x,CFrame.new(Core.Mouse.Hit.p))if not g then return end local u={Part=g,Unapply=function(x)Core.SyncAPI:Invoke("\082\101\109\111\118\101",{x.Part})end;Apply=function(x)Core.SyncAPI:Invoke("\085\110\100\111\082\101\109\111\118\101",{x.Part})end}Core.History.Add(u)Selection.Replace({g})local e=require(Core.Tool.Tools.MoveTool)Core.EquipTool(e)if DragNewParts then e.SetUpDragging(g)end end

local Connections = {};

function NewPartTool.Equip()
	-- Enables the tool's equipped functionality

	-- Start up our interface
	ShowUI();
	EnableClickCreation();

	-- Set our current type
	SetType(NewPartTool.Type);

end;

function NewPartTool.Unequip()
	-- Disables the tool's equipped functionality

	-- Clear unnecessary resources
	HideUI();
	ClearConnections();

end;

function ClearConnections()
	-- Clears out temporary connections

	for ConnectionKey, Connection in pairs(Connections) do
		Connection:disconnect();
		Connections[ConnectionKey] = nil;
	end;

end;

function ShowUI()
	-- Creates and reveals the UI

	-- Reveal UI if already created
	if UI then

		-- Reveal the UI
		UI.Visible = true;

		-- Skip UI creation
		return;

	end;

	-- Create the UI
	UI = Core.Tool.Interfaces.BTNewPartToolGUI:Clone();
	UI.Parent = Core.UI;
	UI.Visible = true;

	-- Creatable part types
	Types = { 'Normal', 'Truss', 'Wedge', 'Corner', 'Cylinder', 'Ball', 'Seat', 'Vehicle Seat', 'Spawn' };

	-- Create the type selection dropdown
	TypeDropdown = Core.Cheer(UI.TypeOption.Dropdown).Start(Types, '', function (Type)
		SetType(Type);
	end);

end;

function HideUI()
	-- Hides the tool UI

	-- Make sure there's a UI
	if not UI then
		return;
	end;

	-- Hide the UI
	UI.Visible = false;

end;

function SetType(Type)

	-- Update the tool option
	NewPartTool.Type = Type;

	-- Update the UI
	TypeDropdown.SetOption(Type);

end;

function EnableClickCreation()
	-- Allows the user to click anywhere and create a new part

	-- Listen for clicks
	Connections.ClickCreationListener = UserInputService.InputBegan:connect(function (Input, GameProcessedEvent)

		-- Make sure this is an intentional event
		if GameProcessedEvent then
			return;
		end;

		-- Make sure this was button 1 being released
		if Input.UserInputType ~= Enum.UserInputType.MouseButton1 then
			return;
		end;

		-- Enable new part dragging
		DragNewParts = true;
		Core.Targeting.CancelSelecting();

		-- Create the part
		CreatePart(NewPartTool.Type);

	end);

	-- Listen for click releases
	Connections.ClickReleaseListener = Support.AddUserInputListener('Ended', 'MouseButton1', true, function ()

		-- Cancel dragging new parts if mouse button is released
		DragNewParts = false;

	end);

end;

function CreatePart(Type)

	-- Send the creation request to the server
	local Part = Core.SyncAPI:Invoke('CreatePart', Type, CFrame.new(Core.Mouse.Hit.p));

	-- Make sure the part creation succeeds
	if not Part then
		return;
	end;

	-- Put together the history record
	local HistoryRecord = {
		Part = Part;

		Unapply = function (HistoryRecord)
			-- Reverts this change

			-- Remove the part
			Core.SyncAPI:Invoke('Remove', { HistoryRecord.Part });

		end;

		Apply = function (HistoryRecord)
			-- Reapplies this change

			-- Restore the part
			Core.SyncAPI:Invoke('UndoRemove', { HistoryRecord.Part });

		end;

	};

	-- Register the history record
	Core.History.Add(HistoryRecord);

	-- Select the part
	Selection.Replace({ Part });

	-- Switch to the move tool
	local MoveTool = require(Core.Tool.Tools.MoveTool);
	Core.EquipTool(MoveTool);

	-- Enable dragging to allow easy positioning of the created part
	if DragNewParts then
		MoveTool.SetUpDragging(Part);
	end;

end;
