diff --git a/EXILED/Exiled.API/Features/Items/Firearm.cs b/EXILED/Exiled.API/Features/Items/Firearm.cs index e77622d675..9be7967728 100644 --- a/EXILED/Exiled.API/Features/Items/Firearm.cs +++ b/EXILED/Exiled.API/Features/Items/Firearm.cs @@ -23,6 +23,7 @@ namespace Exiled.API.Features.Items using Extensions; using InventorySystem; using InventorySystem.Items; + using InventorySystem.Items.Autosync; using InventorySystem.Items.Firearms; using InventorySystem.Items.Firearms.Attachments; using InventorySystem.Items.Firearms.Attachments.Components; @@ -690,6 +691,7 @@ public override Item Clone() /// new owner. internal override void ChangeOwner(Player oldOwner, Player newOwner) { + Base.InstantiationStatus = newOwner == Server.Host ? AutosyncInstantiationStatus.SimulatedInstance : AutosyncInstantiationStatus.InventoryInstance; Base.Owner = newOwner.ReferenceHub; Base._footprintCacheSet = false; foreach (ModuleBase module in Base.Modules) diff --git a/EXILED/Exiled.API/Features/Items/FirearmModules/Barrel/PumpBarrelMagazine.cs b/EXILED/Exiled.API/Features/Items/FirearmModules/Barrel/PumpBarrelMagazine.cs index 57ab59b08a..afeb1f3248 100644 --- a/EXILED/Exiled.API/Features/Items/FirearmModules/Barrel/PumpBarrelMagazine.cs +++ b/EXILED/Exiled.API/Features/Items/FirearmModules/Barrel/PumpBarrelMagazine.cs @@ -80,7 +80,7 @@ public override bool IsCocked set { - PumpBarrel.SyncCocked = MaxAmmo; + PumpBarrel.SyncCocked = value ? MaxAmmo : 0; Resync(); } } diff --git a/EXILED/Exiled.API/Features/Items/FirearmModules/Primary/NormalMagazine.cs b/EXILED/Exiled.API/Features/Items/FirearmModules/Primary/NormalMagazine.cs index dd8cfaa217..6c4bb19c28 100644 --- a/EXILED/Exiled.API/Features/Items/FirearmModules/Primary/NormalMagazine.cs +++ b/EXILED/Exiled.API/Features/Items/FirearmModules/Primary/NormalMagazine.cs @@ -43,6 +43,16 @@ public override int MaxAmmo set => MagazineModule._defaultCapacity = value; } + /// + public override int Ammo + { + set + { + MagazineModule.SyncData[MagazineModule.ItemSerial] = Math.Max(value, 0) + 1; + Resync(); + } + } + /// public override int ConstantMaxAmmo => MagazineModule._defaultCapacity; diff --git a/EXILED/Exiled.API/Features/Items/Item.cs b/EXILED/Exiled.API/Features/Items/Item.cs index c55a6e75ba..757e31f2c8 100644 --- a/EXILED/Exiled.API/Features/Items/Item.cs +++ b/EXILED/Exiled.API/Features/Items/Item.cs @@ -62,6 +62,8 @@ public Item(ItemBase itemBase) ushort serial = ItemSerialGenerator.GenerateNext(); Serial = serial; itemBase.OnAdded(null); + if (Base is ModularAutosyncItem syncItem) + syncItem.InstantiationStatus = AutosyncInstantiationStatus.SimulatedInstance; #if DEBUG Log.Debug($"{nameof(Item)}.ctor: Generating new serial number. Serial should now be: {serial}. // {Serial}"); #endif diff --git a/EXILED/Exiled.API/Features/Items/MicroHid.cs b/EXILED/Exiled.API/Features/Items/MicroHid.cs index f83dab90d3..0495a3b3bc 100644 --- a/EXILED/Exiled.API/Features/Items/MicroHid.cs +++ b/EXILED/Exiled.API/Features/Items/MicroHid.cs @@ -12,6 +12,9 @@ namespace Exiled.API.Features.Items using Exiled.API.Features.Pickups; using Exiled.API.Interfaces; + + using InventorySystem.Items.Autosync; + using InventorySystem.Items.Firearms.Modules; using InventorySystem.Items.MicroHID; using InventorySystem.Items.MicroHID.Modules; @@ -214,5 +217,18 @@ public bool TryGetFireController(MicroHidFiringMode firingMode, out T module) /// /// A string containing MicroHid-related data. public override string ToString() => $"{Type} ({Serial}) [{Weight}] *{Scale}* |{Energy}| -{State}-"; + + /// + internal override void ChangeOwner(Player oldOwner, Player newOwner) + { + Base.Owner = newOwner.ReferenceHub; + + for (int i = 0; i < Base.AllSubcomponents.Length; i++) + { + Base.AllSubcomponents[i].OnAdded(); + } + + Base.InstantiationStatus = newOwner == Server.Host ? AutosyncInstantiationStatus.SimulatedInstance : AutosyncInstantiationStatus.InventoryInstance; + } } } \ No newline at end of file diff --git a/EXILED/Exiled.API/Features/Room.cs b/EXILED/Exiled.API/Features/Room.cs index 1af977bbd8..098340fa77 100644 --- a/EXILED/Exiled.API/Features/Room.cs +++ b/EXILED/Exiled.API/Features/Room.cs @@ -401,8 +401,8 @@ public void UnlockAll() /// Factory method to create and add a component to a Transform. /// We can add parameters to be set privately here. /// - /// The Game Object to attach the Room component to. - internal static void CreateComponent(RoomIdentifier roomIdentifier) => roomIdentifier.gameObject.AddComponent(); + /// The Game Object to attach the Room component to. + internal static void CreateComponent(GameObject baseRoom) => baseRoom.AddComponent().InternalCreate(); /// /// Factory method to complete all element inside a Room. diff --git a/EXILED/Exiled.Events/Patches/Fixes/ServerHubMicroHidFix.cs b/EXILED/Exiled.Events/Patches/Fixes/ServerHubMicroHidFix.cs new file mode 100644 index 0000000000..8bd604cb53 --- /dev/null +++ b/EXILED/Exiled.Events/Patches/Fixes/ServerHubMicroHidFix.cs @@ -0,0 +1,32 @@ +// ----------------------------------------------------------------------- +// +// Copyright (c) ExMod Team. All rights reserved. +// Licensed under the CC BY-SA 3.0 license. +// +// ----------------------------------------------------------------------- + +namespace Exiled.Events.Patches.Fixes +{ +#pragma warning disable SA1313 + using API.Features.Items; + using Exiled.API.Features; + + using HarmonyLib; + + using InventorySystem.Items.Autosync; + using InventorySystem.Items.MicroHID.Modules; + using InventorySystem.Items.Pickups; + using InventorySystem.Items.Usables.Scp330; + + /// + /// Patches to fix phantom for . + /// + [HarmonyPatch(typeof(CycleSyncModule), nameof(CycleSyncModule.Update))] + internal static class ServerHubMicroHidFix + { + private static bool Prefix(CycleSyncModule __instance) + { + return __instance.MicroHid.InstantiationStatus == AutosyncInstantiationStatus.InventoryInstance; + } + } +} diff --git a/EXILED/Exiled.Events/Patches/Generic/AmmoDrain.cs b/EXILED/Exiled.Events/Patches/Generic/AmmoDrain.cs index cf51d62adc..0fb3c4669c 100644 --- a/EXILED/Exiled.Events/Patches/Generic/AmmoDrain.cs +++ b/EXILED/Exiled.Events/Patches/Generic/AmmoDrain.cs @@ -20,6 +20,7 @@ namespace Exiled.Events.Patches.Generic using HarmonyLib; + using InventorySystem; using InventorySystem.Items; using InventorySystem.Items.Firearms; using InventorySystem.Items.Firearms.Modules; @@ -66,6 +67,22 @@ internal static IEnumerable GetInstructions(LocalBuilder firear yield return new CodeInstruction(OpCodes.Nop).WithLabels(continueLabel); } + /// + /// Calculates modified ammo limit. + /// + /// Current ammo limit for reloading. + /// Target . + /// Modified ammo limit for reloading. + internal static int GetAmmoDrainLimit(int ammoLimit, int ammoDrain) + { + if (ammoDrain == 0) + { + return int.MaxValue; + } + + return ammoLimit / ammoDrain; + } + // that patch for open bolted firearms [HarmonyPatch(nameof(AutomaticActionModule.ServerShoot))] [HarmonyTranspiler] @@ -89,7 +106,7 @@ private static IEnumerable First(IEnumerable c newInstructions.InsertRange(index, new CodeInstruction[] { new(OpCodes.Ldloc_S, ammoDrain.LocalIndex), - new(OpCodes.Div), + new(OpCodes.Call, Method(typeof(AmmoDrainAutomatic), nameof(AmmoDrainAutomatic.GetAmmoDrainLimit))), }); offset = 0; @@ -131,7 +148,7 @@ private static IEnumerable Second(IEnumerable newInstructions.InsertRange(index, new CodeInstruction[] { new(OpCodes.Ldloc_S, ammoDrain.LocalIndex), - new(OpCodes.Div), + new(OpCodes.Call, Method(typeof(AmmoDrainAutomatic), nameof(AmmoDrainAutomatic.GetAmmoDrainLimit))), }); offset = 0; @@ -177,7 +194,7 @@ private static IEnumerable Transpiler(IEnumerable Transpiler(IEnumerable /// Patch for adding support for revolvers. /// - [HarmonyPatch(typeof(RevolverClipReloaderModule), nameof(RevolverClipReloaderModule.InsertAmmoFromClip))] + [HarmonyPatch(typeof(RevolverClipReloaderModule), nameof(RevolverClipReloaderModule.ServerWithholdAmmo))] internal class AmmoDrainRevolver { private static IEnumerable Transpiler(IEnumerable codeInstructions, ILGenerator generator) @@ -224,17 +241,17 @@ private static IEnumerable Transpiler(IEnumerable i.Calls(Method(typeof(IPrimaryAmmoContainerModule), nameof(IPrimaryAmmoContainerModule.ServerModifyAmmo)))) + offset; + index = newInstructions.FindIndex(i => i.Calls(Method(typeof(InventoryExtensions), nameof(InventoryExtensions.ServerAddAmmo)))) + offset; - // and divide ammo that are inserting in magazine, to implement AmmoDrain + // and multiply ammo that are inserting in magazine, to implement AmmoDrain newInstructions.InsertRange(index, new CodeInstruction[] { new(OpCodes.Ldloc_S, ammoDrain.LocalIndex), - new(OpCodes.Div), + new(OpCodes.Mul), }); for (int z = 0; z < newInstructions.Count; z++) diff --git a/EXILED/Exiled.Events/Patches/Generic/RoomLightControllersList.cs b/EXILED/Exiled.Events/Patches/Generic/RoomLightControllersList.cs index 129585e268..d27b7d9332 100644 --- a/EXILED/Exiled.Events/Patches/Generic/RoomLightControllersList.cs +++ b/EXILED/Exiled.Events/Patches/Generic/RoomLightControllersList.cs @@ -9,7 +9,6 @@ namespace Exiled.Events.Patches.Generic { using Exiled.API.Features; #pragma warning disable SA1313 -#pragma warning disable SA1402 using HarmonyLib; @@ -24,16 +23,4 @@ private static void Postfix(RoomLightController __instance) Room.Get(__instance.Room).RoomLightControllersValue.Add(__instance); } } - - /// - /// Patch for removing to list. - /// - [HarmonyPatch(typeof(RoomLightController), nameof(RoomLightController.OnDestroy))] - internal class RoomLightControllersList2 - { - private static void Postfix(RoomLightController __instance) - { - Room.Get(__instance.Room).RoomLightControllersValue.Remove(__instance); - } - } } \ No newline at end of file diff --git a/EXILED/Exiled.Events/Patches/Generic/RoomList.cs b/EXILED/Exiled.Events/Patches/Generic/RoomList.cs index 53f6ada70d..037103be04 100644 --- a/EXILED/Exiled.Events/Patches/Generic/RoomList.cs +++ b/EXILED/Exiled.Events/Patches/Generic/RoomList.cs @@ -44,8 +44,7 @@ private static IEnumerable Transpiler(IEnumerable Transpiler(IEnumerable - /// Patches . - /// - [HarmonyPatch(typeof(RoomIdentifier), nameof(RoomIdentifier.Awake))] - internal class RoomListAdd - { - private static void Postfix(RoomIdentifier __instance) - { - Room.CreateComponent(__instance); - } - } - /// /// Patches . ///