From b58327ed847e2b40e3ccebf0838d39ec4d56bd25 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/2] 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 c2a4827dac7d4fce06da18b6ff620ec57a525e57 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/2] 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 { ///