diff --git a/code/datums/components/rename.dm b/code/datums/components/rename.dm
new file mode 100644
index 00000000000..ad98c861dde
--- /dev/null
+++ b/code/datums/components/rename.dm
@@ -0,0 +1,65 @@
+/**
+ The rename component.
+
+ This component is used to manage names and descriptions changed with the pen.
+
+ Atoms can only have one instance of this component at a time.
+
+ When a player renames or changes the description of an atom with a pen, this component gets applied to it.
+ If a player resets the name and description, they will be reverted to their state before being changed and the component will be removed.
+ */
+/datum/component/rename
+ dupe_mode = COMPONENT_DUPE_UNIQUE_PASSARGS
+ ///The name the player is applying to the parent.
+ var/custom_name
+ ///The desc the player is applying to the parent.
+ var/custom_desc
+ ///The name before the player changed it.
+ var/original_name
+ ///The desc before the player changed it.
+ var/original_desc
+
+/datum/component/rename/Initialize(custom_name, custom_desc)
+ if(!isatom(parent))
+ return COMPONENT_INCOMPATIBLE
+
+ src.custom_name = custom_name
+ src.custom_desc = custom_desc
+ apply_rename()
+
+/**
+ This proc will fire after the parent's name or desc is changed with a pen, which is trying to apply another rename component.
+ Since the parent already has a rename component, it will remove the old one and apply the new one.
+ The name and description changes will be merged or overwritten.
+*/
+/datum/component/rename/InheritComponent(datum/component/rename/new_comp , i_am_original, custom_name, custom_desc)
+ revert_rename()
+ if(new_comp)
+ src.custom_name = new_comp.custom_name
+ src.custom_desc = new_comp.custom_desc
+ else
+ src.custom_name = custom_name
+ src.custom_desc = custom_desc
+ apply_rename()
+
+///Saves the current name and description before changing them to the player's inputs.
+/datum/component/rename/proc/apply_rename()
+ var/atom/owner = parent
+ original_name = owner.name
+ original_desc = owner.desc
+ owner.name = custom_name
+ owner.desc = custom_desc
+
+///Reverts the name and description to the state before they were changed.
+/datum/component/rename/proc/revert_rename()
+ var/atom/owner = parent
+ owner.name = original_name
+ owner.desc = original_desc
+
+/datum/component/rename/proc/remove_component()
+ revert_rename()
+ qdel(src)
+
+/datum/component/rename/Destroy()
+ revert_rename()
+ return ..()
diff --git a/code/modules/paperwork/pen.dm b/code/modules/paperwork/pen.dm
index fa446f10fcf..55bf668ae3f 100644
--- a/code/modules/paperwork/pen.dm
+++ b/code/modules/paperwork/pen.dm
@@ -150,10 +150,10 @@
var/oldname = O.name
if(QDELETED(O) || !user.canUseTopic(O, BE_CLOSE))
return
- if(oldname == input || input == "")
+ if(input == oldname || !input)
to_chat(user, "You changed [O] to... well... [O].")
else
- O.name = input
+ O.AddComponent(/datum/component/rename, input, O.desc)
var/datum/component/label/label = O.GetComponent(/datum/component/label)
if(label)
label.remove_label()
@@ -166,22 +166,25 @@
var/olddesc = O.desc
if(QDELETED(O) || !user.canUseTopic(O, BE_CLOSE))
return
- if(olddesc == input || input == "")
+ if(input == olddesc || !input)
to_chat(user, "You decide against changing [O]'s description.")
else
- O.desc = input
+ O.AddComponent(/datum/component/rename, O.name, input)
to_chat(user, "You have successfully changed [O]'s description.")
O.renamedByPlayer = TRUE
if(penchoice == "Reset")
if(QDELETED(O) || !user.canUseTopic(O, BE_CLOSE))
return
- O.desc = initial(O.desc)
- O.name = initial(O.name)
+
+ qdel(O.GetComponent(/datum/component/rename))
+
+ //reapply any label to name
var/datum/component/label/label = O.GetComponent(/datum/component/label)
if(label)
label.remove_label()
label.apply_label()
+
to_chat(user, "You have successfully reset [O]'s name and description.")
O.renamedByPlayer = FALSE
diff --git a/tgstation.dme b/tgstation.dme
index f7fb77f6c03..ab1a4152b77 100644
--- a/tgstation.dme
+++ b/tgstation.dme
@@ -581,6 +581,7 @@
#include "code\datums\components\radioactive.dm"
#include "code\datums\components\religious_tool.dm"
#include "code\datums\components\remote_materials.dm"
+#include "code\datums\components\rename.dm"
#include "code\datums\components\rot.dm"
#include "code\datums\components\rotation.dm"
#include "code\datums\components\shell.dm"