From fed2d16ad893f61a5d539c5c20f71216a1f7c1c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mustafa=20SAVA=C5=9E?= Date: Mon, 9 Mar 2026 18:39:28 +0300 Subject: [PATCH 1/3] generic singleton instance pattern --- .../API/Features/CustomItem{T}.cs | 181 ++++++++++++++++++ .../API/Features/CustomRole{T}.cs | 39 ++++ 2 files changed, 220 insertions(+) create mode 100644 EXILED/Exiled.CustomItems/API/Features/CustomItem{T}.cs create mode 100644 EXILED/Exiled.CustomRoles/API/Features/CustomRole{T}.cs diff --git a/EXILED/Exiled.CustomItems/API/Features/CustomItem{T}.cs b/EXILED/Exiled.CustomItems/API/Features/CustomItem{T}.cs new file mode 100644 index 000000000..eca71f91a --- /dev/null +++ b/EXILED/Exiled.CustomItems/API/Features/CustomItem{T}.cs @@ -0,0 +1,181 @@ +// ----------------------------------------------------------------------- +// +// Copyright (c) ExMod Team. All rights reserved. +// Licensed under the CC BY-SA 3.0 license. +// +// ----------------------------------------------------------------------- +#pragma warning disable SA1402 // File may only contain a single type +namespace Exiled.CustomItems.API.Features +{ + using YamlDotNet.Serialization; + + /// + /// A generic base class for that provides a typed singleton . + /// + /// The concrete type. + public abstract class CustomItem : CustomItem + where T : CustomItem + { + /// + /// Gets the singleton instance of this . + /// + [YamlIgnore] + public static T? Instance { get; private set; } + + /// + public override void Init() + { + base.Init(); + Instance = this as T; + } + + /// + public override void Destroy() + { + Instance = null; + base.Destroy(); + } + } + + /// + /// A generic base class for that provides a typed singleton . + /// + /// The concrete type. + public abstract class CustomWeapon : CustomWeapon + where T : CustomWeapon + { + /// + /// Gets the singleton instance of this . + /// + [YamlIgnore] + public static T? Instance { get; private set; } + + /// + public override void Init() + { + base.Init(); + Instance = this as T; + } + + /// + public override void Destroy() + { + Instance = null; + base.Destroy(); + } + } + + /// + /// A generic base class for that provides a typed singleton . + /// + /// The concrete type. + public abstract class CustomKeycard : CustomKeycard + where T : CustomKeycard + { + /// + /// Gets the singleton instance of this . + /// + [YamlIgnore] + public static T? Instance { get; private set; } + + /// + public override void Init() + { + base.Init(); + Instance = this as T; + } + + /// + public override void Destroy() + { + Instance = null; + base.Destroy(); + } + } + + /// + /// A generic base class for that provides a typed singleton . + /// + /// The concrete type. + public abstract class CustomGrenade : CustomGrenade + where T : CustomGrenade + { + /// + /// Gets the singleton instance of this . + /// + [YamlIgnore] + public static T? Instance { get; private set; } + + /// + public override void Init() + { + base.Init(); + Instance = this as T; + } + + /// + public override void Destroy() + { + Instance = null; + base.Destroy(); + } + } + + /// + /// A generic base class for that provides a typed singleton . + /// + /// The concrete type. + public abstract class CustomArmor : CustomArmor + where T : CustomArmor + { + /// + /// Gets the singleton instance of this . + /// + [YamlIgnore] + public static T? Instance { get; private set; } + + /// + public override void Init() + { + base.Init(); + Instance = this as T; + } + + /// + public override void Destroy() + { + Instance = null; + base.Destroy(); + } + } + + /// + /// A generic base class for that provides a typed singleton . + /// + /// The concrete type. + public abstract class CustomGoggles : CustomGoggles + + where T : CustomGoggles + { + /// + /// Gets the singleton instance of this . + /// + [YamlIgnore] + public static T? Instance { get; private set; } + + /// + public override void Init() + { + base.Init(); + Instance = this as T; + } + + /// + public override void Destroy() + { + Instance = null; + base.Destroy(); + } + } +} +#pragma warning restore SA1402 // File may only contain a single type \ No newline at end of file diff --git a/EXILED/Exiled.CustomRoles/API/Features/CustomRole{T}.cs b/EXILED/Exiled.CustomRoles/API/Features/CustomRole{T}.cs new file mode 100644 index 000000000..c91ffad08 --- /dev/null +++ b/EXILED/Exiled.CustomRoles/API/Features/CustomRole{T}.cs @@ -0,0 +1,39 @@ +// ----------------------------------------------------------------------- +// +// Copyright (c) ExMod Team. All rights reserved. +// Licensed under the CC BY-SA 3.0 license. +// +// ----------------------------------------------------------------------- + +namespace Exiled.CustomRoles.API.Features +{ + using YamlDotNet.Serialization; + + /// + /// A generic base class for that provides a typed singleton . + /// + /// The concrete type. + public abstract class CustomRole : CustomRole + where T : CustomRole + { + /// + /// Gets the singleton instance of this . + /// + [YamlIgnore] + public static T? Instance { get; private set; } + + /// + public override void Init() + { + base.Init(); + Instance = this as T; + } + + /// + public override void Destroy() + { + Instance = null; + base.Destroy(); + } + } +} \ No newline at end of file From 3510f0ea20e0b31d10294b19eaa8c172e71d7c1c Mon Sep 17 00:00:00 2001 From: MS-crew <100300664+MS-crew@users.noreply.github.com> Date: Tue, 17 Mar 2026 14:47:43 +0300 Subject: [PATCH 2/3] Update CustomItem{T}.cs --- EXILED/Exiled.CustomItems/API/Features/CustomItem{T}.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/EXILED/Exiled.CustomItems/API/Features/CustomItem{T}.cs b/EXILED/Exiled.CustomItems/API/Features/CustomItem{T}.cs index eca71f91a..7d157b9a8 100644 --- a/EXILED/Exiled.CustomItems/API/Features/CustomItem{T}.cs +++ b/EXILED/Exiled.CustomItems/API/Features/CustomItem{T}.cs @@ -154,7 +154,6 @@ public override void Destroy() /// /// The concrete type. public abstract class CustomGoggles : CustomGoggles - where T : CustomGoggles { /// From ced753af79b15a136d85a1cef5c88976fd39888a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mustafa=20SAVA=C5=9E?= Date: Sun, 29 Mar 2026 00:29:49 +0300 Subject: [PATCH 3/3] new for someone --- .../Exiled.CustomItems/API/Features/CustomItem{T}.cs | 12 ++++++------ .../Exiled.CustomRoles/API/Features/CustomRole{T}.cs | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/EXILED/Exiled.CustomItems/API/Features/CustomItem{T}.cs b/EXILED/Exiled.CustomItems/API/Features/CustomItem{T}.cs index 7d157b9a8..65fd3bd07 100644 --- a/EXILED/Exiled.CustomItems/API/Features/CustomItem{T}.cs +++ b/EXILED/Exiled.CustomItems/API/Features/CustomItem{T}.cs @@ -14,7 +14,7 @@ namespace Exiled.CustomItems.API.Features /// /// The concrete type. public abstract class CustomItem : CustomItem - where T : CustomItem + where T : CustomItem, new() { /// /// Gets the singleton instance of this . @@ -42,7 +42,7 @@ public override void Destroy() /// /// The concrete type. public abstract class CustomWeapon : CustomWeapon - where T : CustomWeapon + where T : CustomWeapon, new() { /// /// Gets the singleton instance of this . @@ -70,7 +70,7 @@ public override void Destroy() /// /// The concrete type. public abstract class CustomKeycard : CustomKeycard - where T : CustomKeycard + where T : CustomKeycard, new() { /// /// Gets the singleton instance of this . @@ -98,7 +98,7 @@ public override void Destroy() /// /// The concrete type. public abstract class CustomGrenade : CustomGrenade - where T : CustomGrenade + where T : CustomGrenade, new() { /// /// Gets the singleton instance of this . @@ -126,7 +126,7 @@ public override void Destroy() /// /// The concrete type. public abstract class CustomArmor : CustomArmor - where T : CustomArmor + where T : CustomArmor, new() { /// /// Gets the singleton instance of this . @@ -154,7 +154,7 @@ public override void Destroy() /// /// The concrete type. public abstract class CustomGoggles : CustomGoggles - where T : CustomGoggles + where T : CustomGoggles, new() { /// /// Gets the singleton instance of this . diff --git a/EXILED/Exiled.CustomRoles/API/Features/CustomRole{T}.cs b/EXILED/Exiled.CustomRoles/API/Features/CustomRole{T}.cs index c91ffad08..5d4149e8c 100644 --- a/EXILED/Exiled.CustomRoles/API/Features/CustomRole{T}.cs +++ b/EXILED/Exiled.CustomRoles/API/Features/CustomRole{T}.cs @@ -14,7 +14,7 @@ namespace Exiled.CustomRoles.API.Features /// /// The concrete type. public abstract class CustomRole : CustomRole - where T : CustomRole + where T : CustomRole, new() { /// /// Gets the singleton instance of this .