function MoveTool.MovePartsAlongAxesByFace(I,U,A,O,p,C)local c=Vector3.FromNormalId(U)*A if I.Axes=="\071\108\111\098\097\108"then for U,A in pairs(O)do U.CFrame=A.CFrame+c end for U,A in pairs(p)do U.WorldPivot=A.Pivot+c end elseif I.Axes=="\076\111\099\097\108"then for U,A in pairs(O)do U.CFrame=A.CFrame*CFrame.new(c)end elseif I.Axes=="\076\097\115\116"then local U=C*CFrame.new(c)for A,O in pairs(O)do local p=C:toObjectSpace(O.CFrame)A.CFrame=U*p end for A,O in pairs(p)do local p=C:ToObjectSpace(O.Pivot)A.WorldPivot=U*p end end end function MoveTool.BindShortcutKeys(U)U.Maid.HotkeyStart=UserInputService.InputBegan:Connect(function(A,O)if O then return end if A.UserInputType~=Enum.UserInputType.Keyboard then return end if UserInputService:GetFocusedTextBox()then return end if A.KeyCode==Enum.KeyCode.Return or A.KeyCode==Enum.KeyCode.KeypadEnter then if U.Axes=="\071\108\111\098\097\108"then U:SetAxes("\076\111\099\097\108")elseif U.Axes=="\076\111\099\097\108"then U:SetAxes("\076\097\115\116")elseif U.Axes=="\076\097\115\116"then U:SetAxes("\071\108\111\098\097\108")end elseif A.KeyCode==Enum.KeyCode.R and not Selection.Multiselecting then U:StartSnapping()elseif A.KeyCode==Enum.KeyCode.KeypadEight then U:NudgeSelectionByFace(Enum.NormalId.Top)elseif A.KeyCode==Enum.KeyCode.KeypadTwo then U:NudgeSelectionByFace(Enum.NormalId.Bottom)elseif A.KeyCode==Enum.KeyCode.KeypadNine then U:NudgeSelectionByFace(Enum.NormalId.Front)elseif A.KeyCode==Enum.KeyCode.KeypadOne then U:NudgeSelectionByFace(Enum.NormalId.Back)elseif A.KeyCode==Enum.KeyCode.KeypadFour then U:NudgeSelectionByFace(Enum.NormalId.Left)elseif A.KeyCode==Enum.KeyCode.KeypadSix then U:NudgeSelectionByFace(Enum.NormalId.Right)elseif A.KeyCode==Enum.KeyCode.T and not Selection.Multiselecting then U.FreeDragging:AlignSelectionToTarget()end end)U.Maid.HotkeyRelease=UserInputService.InputEnded:Connect(function(A,O)if O then return end if A.UserInputType~=Enum.UserInputType.Keyboard then return end if A.KeyCode==Enum.KeyCode.R then if UserInputService:GetFocusedTextBox()then return end if not U.FreeDragging.IsDragging then U:SetAxes(U.Axes)end SnapTracking.StopTracking()elseif A.KeyCode.Name=="\077\105\110\117\115"or A.KeyCode.Name=="\075\101\121\112\097\100\077\105\110\117\115"then U.UIController:FocusIncrementInput()end end)end function MoveTool.StartSnapping(U)U.HandleDragging:AttachHandles(nil,true)BoundingBox.ClearBoundingBox()if U.FreeDragging.IsDragging then SnapTracking.TargetBlacklist=Selection.Items end SnapTracking.StartTracking(function(A)if A then U.SnappedPoint=A.p U.PointSnapped:Fire(U.SnappedPoint)end end)end function MoveTool.SetAxisPosition(O,U,A)O:TrackChange()local p=O:PrepareSelectionForDragging()for O in pairs(p)do O.CFrame=CFrame.new(U=="\088"and A or O.Position.X,U=="\089"and A or O.Position.Y,U=="\090"and A or O.Position.Z)*(O.CFrame-O.CFrame.p)end local C=Security.GetPermissions(Security.GetSelectionAreas(Selection.Parts),Core.Player)if Core.Mode=="\084\111\111\108"and Security.ArePartsViolatingAreas(Selection.Parts,Core.Player,false,C)then for U,A in pairs(p)do U.CFrame=A.CFrame end end for U,A in pairs(p)do U:MakeJoints()Core.RestoreJoints(A.Joints)U.CanCollide=A.CanCollide U.Anchored=A.Anchored end O:RegisterChange()end function MoveTool.NudgeSelectionByFace(A,U)local O=A.Increment local p=Support.FlipTable(Support.GetListMembers(UserInputService:GetKeysPressed(),"\075\101\121\067\111\100\101"))if p[Enum.KeyCode.LeftShift]or p[Enum.KeyCode.RightShift]then O=-O end A:TrackChange()local C,I,c=A:PrepareSelectionForDragging()A:MovePartsAlongAxesByFace(U,O,C,I,c)A.DragChanged:Fire(O)local Q=Security.GetPermissions(Security.GetSelectionAreas(Selection.Parts),Core.Player)if Core.Mode=="\084\111\111\108"and Security.ArePartsViolatingAreas(Selection.Parts,Core.Player,false,Q)then for U,A in pairs(C)do U.CFrame=A.CFrame end end for U,A in pairs(C)do U:MakeJoints()Core.RestoreJoints(A.Joints)U.CanCollide=A.CanCollide U.Anchored=A.Anchored end A:RegisterChange()end function MoveTool.TrackChange(U)U.HistoryRecord={Parts=Support.CloneTable(Selection.Parts),Models=Support.CloneTable(Selection.Models),BeforeCFrame={},AfterCFrame={};Selection=Selection.Items,Unapply=function(U)Selection.Replace(U.Selection)local A={}for O,p in ipairs(U.Parts)do table.insert(A,{Part=p;CFrame=U.BeforeCFrame[p]})end for O,p in ipairs(U.Models)do table.insert(A,{Model=p,Pivot=U.BeforeCFrame[p]})end Core.SyncAPI:Invoke("\083\121\110\099\077\111\118\101",A)end,Apply=function(U)Selection.Replace(U.Selection)local A={}for O,p in ipairs(U.Parts)do table.insert(A,{Part=p,CFrame=U.AfterCFrame[p]})end for O,p in ipairs(U.Models)do table.insert(A,{Model=p;Pivot=U.AfterCFrame[p]})end Core.SyncAPI:Invoke("\083\121\110\099\077\111\118\101",A)end}for A,O in pairs(U.HistoryRecord.Parts)do U.HistoryRecord.BeforeCFrame[O]=O.CFrame end pcall(function()for A,O in ipairs(U.HistoryRecord.Models)do U.HistoryRecord.BeforeCFrame[O]=O:GetPivot()end end)end function MoveTool.RegisterChange(U)if not U.HistoryRecord then return end local A={}for O,p in pairs(U.HistoryRecord.Parts)do U.HistoryRecord.AfterCFrame[p]=p.CFrame table.insert(A,{Part=p;CFrame=p.CFrame})end pcall(function()for O,p in pairs(U.HistoryRecord.Models)do U.HistoryRecord.AfterCFrame[p]=p:GetPivot()table.insert(A,{Model=p,Pivot=p:GetPivot()})end end)Core.SyncAPI:Invoke("\083\121\110\099\077\111\118\101",A)Core.History.Add(U.HistoryRecord)U.HistoryRecord=nil end

function MoveTool:MovePartsAlongAxesByFace(Face, Distance, InitialPartStates, InitialModelStates, InitialFocusCFrame)

	-- Calculate the shift along the direction of the face
	local Shift = Vector3.FromNormalId(Face) * Distance

	-- Move along global axes
	if self.Axes == 'Global' then
		for Part, InitialState in pairs(InitialPartStates) do
			Part.CFrame = InitialState.CFrame + Shift
		end
		for Model, InitialState in pairs(InitialModelStates) do
			Model.WorldPivot = InitialState.Pivot + Shift
		end

	-- Move along individual items' axes
	elseif self.Axes == 'Local' then
		for Part, InitialState in pairs(InitialPartStates) do
			Part.CFrame = InitialState.CFrame * CFrame.new(Shift)
		end
		-- for Model, InitialState in pairs(InitialModelStates) do
		-- 	Model.WorldPivot = InitialState.Pivot * CFrame.new(Shift)
		-- end

	-- Move along focused item's axes
	elseif self.Axes == 'Last' then

		-- Calculate focused item's position
		local FocusCFrame = InitialFocusCFrame * CFrame.new(Shift)

		-- Move parts based on initial offset from focus
		for Part, InitialState in pairs(InitialPartStates) do
			local FocusOffset = InitialFocusCFrame:toObjectSpace(InitialState.CFrame)
			Part.CFrame = FocusCFrame * FocusOffset
		end
		for Model, InitialState in pairs(InitialModelStates) do
			local FocusOffset = InitialFocusCFrame:ToObjectSpace(InitialState.Pivot)
			Model.WorldPivot = FocusCFrame * FocusOffset
		end

	end

end

function MoveTool:BindShortcutKeys()
	-- Enables useful shortcut keys for this tool

	-- Track user input while this tool is equipped
	self.Maid.HotkeyStart = UserInputService.InputBegan:Connect(function (InputInfo, GameProcessedEvent)
		if GameProcessedEvent then
			return
		end

		-- Make sure this input is a key press
		if InputInfo.UserInputType ~= Enum.UserInputType.Keyboard then
			return;
		end;

		-- Make sure it wasn't pressed while typing
		if UserInputService:GetFocusedTextBox() then
			return;
		end;

		-- Check if the enter key was pressed
		if InputInfo.KeyCode == Enum.KeyCode.Return or InputInfo.KeyCode == Enum.KeyCode.KeypadEnter then

			-- Toggle the current axis mode
			if self.Axes == 'Global' then
				self:SetAxes('Local')
			elseif self.Axes == 'Local' then
				self:SetAxes('Last')
			elseif self.Axes == 'Last' then
				self:SetAxes('Global')
			end

		-- Check if the R key was pressed down, and it's not the selection clearing hotkey
		elseif InputInfo.KeyCode == Enum.KeyCode.R and not Selection.Multiselecting then

			-- Start tracking snap points nearest to the mouse
			self:StartSnapping()

		-- Nudge up if the 8 button on the keypad is pressed
		elseif InputInfo.KeyCode == Enum.KeyCode.KeypadEight then
			self:NudgeSelectionByFace(Enum.NormalId.Top)

		-- Nudge down if the 2 button on the keypad is pressed
		elseif InputInfo.KeyCode == Enum.KeyCode.KeypadTwo then
			self:NudgeSelectionByFace(Enum.NormalId.Bottom)

		-- Nudge forward if the 9 button on the keypad is pressed
		elseif InputInfo.KeyCode == Enum.KeyCode.KeypadNine then
			self:NudgeSelectionByFace(Enum.NormalId.Front)

		-- Nudge backward if the 1 button on the keypad is pressed
		elseif InputInfo.KeyCode == Enum.KeyCode.KeypadOne then
			self:NudgeSelectionByFace(Enum.NormalId.Back)

		-- Nudge left if the 4 button on the keypad is pressed
		elseif InputInfo.KeyCode == Enum.KeyCode.KeypadFour then
			self:NudgeSelectionByFace(Enum.NormalId.Left)

		-- Nudge right if the 6 button on the keypad is pressed
		elseif InputInfo.KeyCode == Enum.KeyCode.KeypadSix then
			self:NudgeSelectionByFace(Enum.NormalId.Right)

		-- Align the selection to the current target surface if T is pressed
		elseif (InputInfo.KeyCode == Enum.KeyCode.T) and (not Selection.Multiselecting) then
			self.FreeDragging:AlignSelectionToTarget()
		end
	end)

	-- Track ending user input while this tool is equipped
	self.Maid.HotkeyRelease = UserInputService.InputEnded:Connect(function (InputInfo, GameProcessedEvent)
		if GameProcessedEvent then
			return
		end

		-- Make sure this is input from the keyboard
		if InputInfo.UserInputType ~= Enum.UserInputType.Keyboard then
			return;
		end;

		-- Check if the R key was let go
		if InputInfo.KeyCode == Enum.KeyCode.R then

			-- Make sure it wasn't pressed while typing
			if UserInputService:GetFocusedTextBox() then
				return;
			end;

			-- Reset handles if not dragging
			if not self.FreeDragging.IsDragging then
				self:SetAxes(self.Axes)
			end

			-- Stop snapping point tracking if it was enabled
			SnapTracking.StopTracking();

		-- If - key was released, focus on increment input
		elseif (InputInfo.KeyCode.Name == 'Minus') or (InputInfo.KeyCode.Name == 'KeypadMinus') then
			self.UIController:FocusIncrementInput()
		end
	end)

end

function MoveTool:StartSnapping()
	-- Starts tracking snap points nearest to the mouse

	-- Hide any handles or bounding boxes
	self.HandleDragging:AttachHandles(nil, true)
	BoundingBox.ClearBoundingBox();

	-- Avoid targeting snap points in selected parts while dragging
	if self.FreeDragging.IsDragging then
		SnapTracking.TargetBlacklist = Selection.Items;
	end;

	-- Start tracking the closest snapping point
	SnapTracking.StartTracking(function (NewPoint)

		-- Fire `SnappedPoint` and update `SnappedPoint` when there is a new snap point in focus
		if NewPoint then
			self.SnappedPoint = NewPoint.p
			self.PointSnapped:Fire(self.SnappedPoint)
		end

	end)

end

function MoveTool:SetAxisPosition(Axis, Position)
	-- Sets the selection's position on axis `Axis` to `Position`

	-- Track this change
	self:TrackChange()

	-- Prepare parts to be moved
	local InitialPartStates = self:PrepareSelectionForDragging()

	-- Update each part
	for Part in pairs(InitialPartStates) do

		-- Set the part's new CFrame
		Part.CFrame = CFrame.new(
			Axis == 'X' and Position or Part.Position.X,
			Axis == 'Y' and Position or Part.Position.Y,
			Axis == 'Z' and Position or Part.Position.Z
		) * (Part.CFrame - Part.CFrame.p);

	end;

	-- Cache up permissions for all private areas
	local AreaPermissions = Security.GetPermissions(Security.GetSelectionAreas(Selection.Parts), Core.Player);

	-- Revert changes if player is not authorized to move parts to target destination
	if Core.Mode == 'Tool' and Security.ArePartsViolatingAreas(Selection.Parts, Core.Player, false, AreaPermissions) then
		for Part, State in pairs(InitialPartStates) do
			Part.CFrame = State.CFrame;
		end;
	end;

	-- Restore the parts' original states
	for Part, State in pairs(InitialPartStates) do
		Part:MakeJoints();
		Core.RestoreJoints(State.Joints);
		Part.CanCollide = State.CanCollide;
		Part.Anchored = State.Anchored;
	end;

	-- Register the change
	self:RegisterChange()

end

function MoveTool:NudgeSelectionByFace(Face)
	-- Nudges the selection along the current axes mode in the direction of the focused part's face

	-- Get amount to nudge by
	local NudgeAmount = self.Increment

	-- Reverse nudge amount if shift key is held while nudging
	local PressedKeys = Support.FlipTable(Support.GetListMembers(UserInputService:GetKeysPressed(), 'KeyCode'));
	if PressedKeys[Enum.KeyCode.LeftShift] or PressedKeys[Enum.KeyCode.RightShift] then
		NudgeAmount = -NudgeAmount;
	end;

	-- Track this change
	self:TrackChange()

	-- Prepare parts to be moved
	local InitialPartStates, InitialModelStates, InitialFocusCFrame = self:PrepareSelectionForDragging()

	-- Perform the movement
	self:MovePartsAlongAxesByFace(Face, NudgeAmount, InitialPartStates, InitialModelStates, InitialFocusCFrame)

	-- Indicate updated drag distance
	self.DragChanged:Fire(NudgeAmount)

	-- Cache up permissions for all private areas
	local AreaPermissions = Security.GetPermissions(Security.GetSelectionAreas(Selection.Parts), Core.Player);

	-- Revert changes if player is not authorized to move parts to target destination
	if Core.Mode == 'Tool' and Security.ArePartsViolatingAreas(Selection.Parts, Core.Player, false, AreaPermissions) then
		for Part, State in pairs(InitialPartStates) do
			Part.CFrame = State.CFrame;
		end;
	end;

	-- Restore the parts' original states
	for Part, State in pairs(InitialPartStates) do
		Part:MakeJoints();
		Core.RestoreJoints(State.Joints);
		Part.CanCollide = State.CanCollide;
		Part.Anchored = State.Anchored;
	end;

	-- Register the change
	self:RegisterChange()

end

function MoveTool:TrackChange()

	-- Start the record
	self.HistoryRecord = {
		Parts = Support.CloneTable(Selection.Parts);
		Models = Support.CloneTable(Selection.Models);
		BeforeCFrame = {};
		AfterCFrame = {};
		Selection = Selection.Items;

		Unapply = function (Record)
			-- Reverts this change

			-- Select the changed parts
			Selection.Replace(Record.Selection)

			-- Put together the change request
			local Changes = {}
			for _, Part in ipairs(Record.Parts) do
				table.insert(Changes, {
					Part = Part;
					CFrame = Record.BeforeCFrame[Part];
				})
			end
			for _, Model in ipairs(Record.Models) do
				table.insert(Changes, {
					Model = Model;
					Pivot = Record.BeforeCFrame[Model];
				})
			end

			-- Send the change request
			Core.SyncAPI:Invoke('SyncMove', Changes);

		end;

		Apply = function (Record)
			-- Applies this change

			-- Select the changed parts
			Selection.Replace(Record.Selection)

			-- Put together the change request
			local Changes = {};
			for _, Part in ipairs(Record.Parts) do
				table.insert(Changes, {
					Part = Part;
					CFrame = Record.AfterCFrame[Part];
				})
			end
			for _, Model in ipairs(Record.Models) do
				table.insert(Changes, {
					Model = Model;
					Pivot = Record.AfterCFrame[Model];
				})
			end

			-- Send the change request
			Core.SyncAPI:Invoke('SyncMove', Changes);

		end;

	};

	-- Collect the selection's initial state
	for _, Part in pairs(self.HistoryRecord.Parts) do
		self.HistoryRecord.BeforeCFrame[Part] = Part.CFrame
	end
	pcall(function ()
		for _, Model in ipairs(self.HistoryRecord.Models) do
			self.HistoryRecord.BeforeCFrame[Model] = Model:GetPivot()
		end
	end)

end

function MoveTool:RegisterChange()
	-- Finishes creating the history record and registers it

	-- Make sure there's an in-progress history record
	if not self.HistoryRecord then
		return
	end

	-- Collect the selection's final state
	local Changes = {}
	for _, Part in pairs(self.HistoryRecord.Parts) do
		self.HistoryRecord.AfterCFrame[Part] = Part.CFrame
		table.insert(Changes, {
			Part = Part;
			CFrame = Part.CFrame;
		})
	end;
	pcall(function ()
		for _, Model in pairs(self.HistoryRecord.Models) do
			self.HistoryRecord.AfterCFrame[Model] = Model:GetPivot()
			table.insert(Changes, {
				Model = Model;
				Pivot = Model:GetPivot();
			})
		end
	end)

	-- Send the change to the server
	Core.SyncAPI:Invoke('SyncMove', Changes);

	-- Register the record and clear the staging
	Core.History.Add(self.HistoryRecord)
	self.HistoryRecord = nil

end
