From 84e496ffb9cc7c45c27b10c86a6989c88faad7ea Mon Sep 17 00:00:00 2001 From: Misaka-ZeroTwo <45165615+Misaka-ZeroTwo@users.noreply.github.com> Date: Fri, 1 Dec 2023 01:14:46 -0500 Subject: [PATCH] v8.4.3 (#2305) --- EXILED.props | 2 +- Exiled.API/Features/Items/Firearm.cs | 6 +- Exiled.API/Features/Player.cs | 24 ++++++- .../API/Features/CustomWeapon.cs | 7 ++- .../Map/SpawningTeamVehicleEventArgs.cs | 2 +- .../Player/ClosingGeneratorEventArgs.cs | 8 +-- .../EventArgs/Player/ItemRemovedEventArgs.cs | 50 +++++++++++++++ .../Player/PickingUpItemEventArgs.cs | 6 +- .../Player/TogglingFlashlightEventArgs.cs | 5 +- .../Scp244/OpeningScp244EventArgs.cs | 8 +-- .../EventArgs/Scp244/UsingScp244EventArgs.cs | 8 +-- Exiled.Events/Events.cs | 1 + Exiled.Events/Handlers/Player.cs | 14 +++++ .../Patches/Events/Map/SpawningTeamVehicle.cs | 62 ++++++++++++++++--- Exiled.Events/Patches/Events/Player/Joined.cs | 4 +- .../Events/Player/TogglingFlashlight.cs | 6 +- 16 files changed, 173 insertions(+), 40 deletions(-) create mode 100644 Exiled.Events/EventArgs/Player/ItemRemovedEventArgs.cs diff --git a/EXILED.props b/EXILED.props index b92d785d8d..9271862ac3 100644 --- a/EXILED.props +++ b/EXILED.props @@ -15,7 +15,7 @@ - 8.4.2 + 8.4.3 false diff --git a/Exiled.API/Features/Items/Firearm.cs b/Exiled.API/Features/Items/Firearm.cs index 4aeda30e11..b6ab15fb4e 100644 --- a/Exiled.API/Features/Items/Firearm.cs +++ b/Exiled.API/Features/Items/Firearm.cs @@ -130,13 +130,13 @@ public byte MaxAmmo switch (Base.AmmoManagerModule) { case TubularMagazineAmmoManager tubularMagazineAmmoManager: - tubularMagazineAmmoManager.MaxAmmo = value; + tubularMagazineAmmoManager.MaxAmmo = (byte)(value - Base.AttachmentsValue(AttachmentParam.MagazineCapacityModifier) - (Base.Status.Flags.HasFlagFast(FirearmStatusFlags.Cocked) ? tubularMagazineAmmoManager.ChamberedRounds : 0)); break; case ClipLoadedInternalMagAmmoManager clipLoadedInternalMagAmmoManager: - clipLoadedInternalMagAmmoManager.MaxAmmo = value; + clipLoadedInternalMagAmmoManager.MaxAmmo = (byte)(value - Base.AttachmentsValue(AttachmentParam.MagazineCapacityModifier)); break; case AutomaticAmmoManager automaticAmmoManager: - automaticAmmoManager.MaxAmmo = value; + automaticAmmoManager.MaxAmmo = (byte)(value - Base.AttachmentsValue(AttachmentParam.MagazineCapacityModifier) - automaticAmmoManager.ChamberedAmount); break; default: Log.Warn($"MaxAmmo can't be used for this Item: {Type} ({Base.AmmoManagerModule})"); diff --git a/Exiled.API/Features/Player.cs b/Exiled.API/Features/Player.cs index c425e92a94..2febc7e6a2 100644 --- a/Exiled.API/Features/Player.cs +++ b/Exiled.API/Features/Player.cs @@ -916,7 +916,7 @@ public float HumeShield /// /// Gets or sets the item in the player's hand. Value will be if the player is not holding anything. /// - /// + /// public Item CurrentItem { get => Item.Get(Inventory.CurInstance); @@ -1834,9 +1834,27 @@ public void Broadcast(Broadcast broadcast, bool shouldClearPrevious = false) /// /// Drops an item from the player's inventory. /// - /// The item to be dropped. + /// The to be dropped. + /// Is the item Thrown?. + public void DropItem(Item item, bool isThrown = false) + { + if (item is null) + return; + Inventory.UserCode_CmdDropItem__UInt16__Boolean(item.Serial, isThrown); + } + + /// + /// Drops an item from the player's inventory. + /// + /// The to be dropped. /// dropped . - public Pickup DropItem(Item item) => Pickup.Get(Inventory.ServerDropItem(item.Serial)); + public Pickup DropItem(Item item) => item is not null ? Pickup.Get(Inventory.ServerDropItem(item.Serial)) : null; + + /// + /// Drops the held item. Will not do anything if the player is not holding an item. + /// + /// Is the item Thrown?. + public void DropHeldItem(bool isThrown = false) => DropItem(CurrentItem, isThrown); /// /// Drops the held item. Will not do anything if the player is not holding an item. diff --git a/Exiled.CustomItems/API/Features/CustomWeapon.cs b/Exiled.CustomItems/API/Features/CustomWeapon.cs index bc16769bcb..7b3ce94054 100644 --- a/Exiled.CustomItems/API/Features/CustomWeapon.cs +++ b/Exiled.CustomItems/API/Features/CustomWeapon.cs @@ -9,7 +9,6 @@ namespace Exiled.CustomItems.API.Features { using System; - using Exiled.API.Enums; using Exiled.API.Extensions; using Exiled.API.Features; using Exiled.API.Features.DamageHandlers; @@ -19,7 +18,6 @@ namespace Exiled.CustomItems.API.Features using InventorySystem.Items.Firearms.Attachments; using InventorySystem.Items.Firearms.Attachments.Components; - using InventorySystem.Items.Firearms.BasicMessages; using UnityEngine; @@ -77,6 +75,7 @@ public override ItemType Type firearm.AddAttachment(Attachments); firearm.Ammo = ClipSize; + firearm.MaxAmmo = ClipSize; Pickup? pickup = firearm.CreatePickup(position); @@ -102,7 +101,9 @@ public override ItemType Type { if (!Attachments.IsEmpty()) firearm.AddAttachment(Attachments); + byte ammo = firearm.Ammo; + firearm.MaxAmmo = ClipSize; Log.Debug($"{nameof(Name)}.{nameof(Spawn)}: Spawning weapon with {ammo} ammo."); Pickup? pickup = firearm.CreatePickup(position); pickup.Scale = Scale; @@ -126,7 +127,9 @@ public override void Give(Player player, bool displayMessage = true) { if (!Attachments.IsEmpty()) firearm.AddAttachment(Attachments); + firearm.Ammo = ClipSize; + firearm.MaxAmmo = ClipSize; } Log.Debug($"{nameof(Give)}: Adding {item.Serial} to tracker."); diff --git a/Exiled.Events/EventArgs/Map/SpawningTeamVehicleEventArgs.cs b/Exiled.Events/EventArgs/Map/SpawningTeamVehicleEventArgs.cs index 439253cc72..c412c6f2ab 100644 --- a/Exiled.Events/EventArgs/Map/SpawningTeamVehicleEventArgs.cs +++ b/Exiled.Events/EventArgs/Map/SpawningTeamVehicleEventArgs.cs @@ -38,6 +38,6 @@ public SpawningTeamVehicleEventArgs(SpawnableTeamType team, bool isAllowed = tru /// /// Gets or sets a value indicating whether or not the vehicle can be spawned. /// - public bool IsAllowed { get; set; } = true; + public bool IsAllowed { get; set; } } } \ No newline at end of file diff --git a/Exiled.Events/EventArgs/Player/ClosingGeneratorEventArgs.cs b/Exiled.Events/EventArgs/Player/ClosingGeneratorEventArgs.cs index c3d0d812ea..a8340f7939 100644 --- a/Exiled.Events/EventArgs/Player/ClosingGeneratorEventArgs.cs +++ b/Exiled.Events/EventArgs/Player/ClosingGeneratorEventArgs.cs @@ -30,18 +30,18 @@ public ClosingGeneratorEventArgs(Player player, Scp079Generator generator, bool } /// - /// Gets or sets a value indicating whether or not the generator door can be opened. + /// Gets or sets a value indicating whether or not the generator door can be closed. /// public bool IsAllowed { get; set; } /// - /// Gets the generator that is opening. + /// Gets the generator that is being closed. /// public Generator Generator { get; } /// - /// Gets the player who's opening the generator door. + /// Gets the player who's closing the generator door. /// public Player Player { get; } } -} \ No newline at end of file +} diff --git a/Exiled.Events/EventArgs/Player/ItemRemovedEventArgs.cs b/Exiled.Events/EventArgs/Player/ItemRemovedEventArgs.cs new file mode 100644 index 0000000000..0ae4c68b95 --- /dev/null +++ b/Exiled.Events/EventArgs/Player/ItemRemovedEventArgs.cs @@ -0,0 +1,50 @@ +// ----------------------------------------------------------------------- +// +// Copyright (c) Exiled Team. All rights reserved. +// Licensed under the CC BY-SA 3.0 license. +// +// ----------------------------------------------------------------------- + +namespace Exiled.Events.EventArgs.Player +{ + using Exiled.API.Features.Items; + using Exiled.API.Features.Pickups; + using Exiled.Events.EventArgs.Interfaces; + + using InventorySystem.Items; + using InventorySystem.Items.Pickups; + + /// + /// Contains all information after removing an item from a player's inventory. + /// + public class ItemRemovedEventArgs : IPlayerEvent, IItemEvent, IPickupEvent + { + /// + /// Initializes a new instance of the class. + /// + /// The the item was removed to. + /// The removed . + /// The the originated from, or if the item was not picked up. + public ItemRemovedEventArgs(ReferenceHub referenceHub, ItemBase itemBase, ItemPickupBase pickupBase) + { + Player = API.Features.Player.Get(referenceHub); + Item = Item.Get(itemBase); + Pickup = Pickup.Get(pickupBase); + } + + /// + /// Gets the player that had the item removed. + /// + public API.Features.Player Player { get; } + + /// + /// Gets the item that was removed. + /// + public Item Item { get; } + + /// + /// Gets the pickup that the item originated from or if the item was not picked up. + /// + public Pickup Pickup { get; } + } +} \ No newline at end of file diff --git a/Exiled.Events/EventArgs/Player/PickingUpItemEventArgs.cs b/Exiled.Events/EventArgs/Player/PickingUpItemEventArgs.cs index 59e496e673..2d7ace6ff3 100644 --- a/Exiled.Events/EventArgs/Player/PickingUpItemEventArgs.cs +++ b/Exiled.Events/EventArgs/Player/PickingUpItemEventArgs.cs @@ -43,13 +43,13 @@ public PickingUpItemEventArgs(Player player, ItemPickupBase pickup, bool isAllow public bool IsAllowed { get; set; } /// - /// Gets the dropped pickup. + /// Gets the pickup that's being picked up. /// public Pickup Pickup { get; } /// - /// Gets the player who dropped the item. + /// Gets the player who's picking up an item. /// public Player Player { get; } } -} \ No newline at end of file +} diff --git a/Exiled.Events/EventArgs/Player/TogglingFlashlightEventArgs.cs b/Exiled.Events/EventArgs/Player/TogglingFlashlightEventArgs.cs index d5dcbcb8bf..167d85c689 100644 --- a/Exiled.Events/EventArgs/Player/TogglingFlashlightEventArgs.cs +++ b/Exiled.Events/EventArgs/Player/TogglingFlashlightEventArgs.cs @@ -11,8 +11,7 @@ namespace Exiled.Events.EventArgs.Player using API.Features.Items; using Interfaces; - - using InventorySystem.Items.ToggleableLights.Flashlight; + using InventorySystem.Items.ToggleableLights; /// /// Contains all information before a player toggles a flashlight. @@ -33,7 +32,7 @@ public class TogglingFlashlightEventArgs : IPlayerEvent, IDeniableEvent, IItemEv /// /// /// - public TogglingFlashlightEventArgs(ReferenceHub hub, FlashlightItem flashlight, bool newState) + public TogglingFlashlightEventArgs(ReferenceHub hub, ToggleableLightItemBase flashlight, bool newState) { Player = Player.Get(hub); Flashlight = (Flashlight)Item.Get(flashlight); diff --git a/Exiled.Events/EventArgs/Scp244/OpeningScp244EventArgs.cs b/Exiled.Events/EventArgs/Scp244/OpeningScp244EventArgs.cs index 133eaf3161..aecdaaa96e 100644 --- a/Exiled.Events/EventArgs/Scp244/OpeningScp244EventArgs.cs +++ b/Exiled.Events/EventArgs/Scp244/OpeningScp244EventArgs.cs @@ -13,7 +13,7 @@ namespace Exiled.Events.EventArgs.Scp244 using InventorySystem.Items.Usables.Scp244; /// - /// Contains all information before a player picks up an SCP-244. + /// Contains all information before a player opens SCP-244. /// public class OpeningScp244EventArgs : IDeniableEvent { @@ -29,13 +29,13 @@ public OpeningScp244EventArgs(Scp244DeployablePickup pickup) } /// - /// Gets a value representing the being picked up. + /// Gets a value representing the being opened. /// public Scp244Pickup Pickup { get; } /// - /// Gets or sets a value indicating whether or not the player can interact with SCP-330. + /// Gets or sets a value indicating whether or not the player can open SCP-244. /// public bool IsAllowed { get; set; } = true; } -} \ No newline at end of file +} diff --git a/Exiled.Events/EventArgs/Scp244/UsingScp244EventArgs.cs b/Exiled.Events/EventArgs/Scp244/UsingScp244EventArgs.cs index af4bdb6856..9277019690 100644 --- a/Exiled.Events/EventArgs/Scp244/UsingScp244EventArgs.cs +++ b/Exiled.Events/EventArgs/Scp244/UsingScp244EventArgs.cs @@ -15,7 +15,7 @@ namespace Exiled.Events.EventArgs.Scp244 using InventorySystem.Items.Usables.Scp244; /// - /// Contains all information before radio battery charge is changed. + /// Contains all information before SCP-244 is used. /// public class UsingScp244EventArgs : IPlayerEvent, IDeniableEvent { @@ -44,13 +44,13 @@ public UsingScp244EventArgs(Scp244Item scp244, Player player, bool isAllowed = t public Scp244 Scp244 { get; } /// - /// Gets or sets a value indicating whether the radio battery charge can be changed or not. + /// Gets or sets a value indicating whether the SCP-244 can be used. /// public bool IsAllowed { get; set; } /// - /// Gets the player who's using the radio. + /// Gets the player who's using the SCP-244. /// public Player Player { get; } } -} \ No newline at end of file +} diff --git a/Exiled.Events/Events.cs b/Exiled.Events/Events.cs index 3db2af8221..af40330c7d 100644 --- a/Exiled.Events/Events.cs +++ b/Exiled.Events/Events.cs @@ -72,6 +72,7 @@ public override void OnEnabled() CharacterClassManager.OnRoundStarted += Handlers.Server.OnRoundStarted; InventorySystem.InventoryExtensions.OnItemAdded += Handlers.Player.OnItemAdded; + InventorySystem.InventoryExtensions.OnItemRemoved += Handlers.Player.OnItemRemoved; RagdollManager.OnRagdollSpawned += Handlers.Internal.RagdollList.OnSpawnedRagdoll; RagdollManager.OnRagdollRemoved += Handlers.Internal.RagdollList.OnRemovedRagdoll; diff --git a/Exiled.Events/Handlers/Player.cs b/Exiled.Events/Handlers/Player.cs index 0a1d5520a6..8830898d29 100644 --- a/Exiled.Events/Handlers/Player.cs +++ b/Exiled.Events/Handlers/Player.cs @@ -477,6 +477,11 @@ public class Player /// public static Event ItemAdded { get; set; } = new(); + /// + /// Invoked after a has an item removed from their inventory. + /// + public static Event ItemRemoved { get; set; } = new(); + /// /// Invoked before KillPlayer is called. /// @@ -927,6 +932,15 @@ public class Player public static void OnItemAdded(ReferenceHub referenceHub, InventorySystem.Items.ItemBase itemBase, InventorySystem.Items.Pickups.ItemPickupBase pickupBase) => ItemAdded.InvokeSafely(new ItemAddedEventArgs(referenceHub, itemBase, pickupBase)); + /// + /// Called after a has an item removed from their inventory. + /// + /// The the item was removed from. + /// The removed . + /// The the originated from, or if the item was not picked up. + public static void OnItemRemoved(ReferenceHub referenceHub, InventorySystem.Items.ItemBase itemBase, InventorySystem.Items.Pickups.ItemPickupBase pickupBase) + => ItemRemoved.InvokeSafely(new ItemRemovedEventArgs(referenceHub, itemBase, pickupBase)); + /// /// Called before a enters in an environmental hazard. /// diff --git a/Exiled.Events/Patches/Events/Map/SpawningTeamVehicle.cs b/Exiled.Events/Patches/Events/Map/SpawningTeamVehicle.cs index 7f31c01bdb..63f0f8fe04 100644 --- a/Exiled.Events/Patches/Events/Map/SpawningTeamVehicle.cs +++ b/Exiled.Events/Patches/Events/Map/SpawningTeamVehicle.cs @@ -7,14 +7,19 @@ namespace Exiled.Events.Patches.Events.Map { - using Exiled.Events.Attributes; + using System.Collections.Generic; + using System.Reflection.Emit; + using Exiled.API.Features.Pools; + using Exiled.Events.Attributes; using Exiled.Events.EventArgs.Map; using HarmonyLib; using Respawning; + using static HarmonyLib.AccessTools; + /// /// Patches . /// Adds the event. @@ -23,15 +28,58 @@ namespace Exiled.Events.Patches.Events.Map [HarmonyPatch(typeof(RespawnEffectsController), nameof(RespawnEffectsController.ExecuteAllEffects))] internal static class SpawningTeamVehicle { - private static bool Prefix(RespawnEffectsController.EffectType type, SpawnableTeamType team) + private static IEnumerable Transpiler(IEnumerable instructions, ILGenerator generator) { - if (type != RespawnEffectsController.EffectType.Selection) - return true; + List newInstructions = ListPool.Pool.Get(instructions); + + Label retLabel = generator.DefineLabel(); + Label continueLabel = generator.DefineLabel(); + + LocalBuilder ev = generator.DeclareLocal(typeof(SpawningTeamVehicleEventArgs)); + + newInstructions.InsertRange(0, new CodeInstruction[] + { + // if (type != RespawnEffectsController.EffectType.Selection) + // goto continueLabel; + new(OpCodes.Ldarg_0), + new(OpCodes.Ldc_I4_0), + new(OpCodes.Ceq), + new(OpCodes.Brfalse_S, continueLabel), + + // team + new(OpCodes.Ldarg_1), + + // true + new(OpCodes.Ldc_I4_1), + + // SpawningTeamVehicleEventArgs ev = new(SpawnableTeamType, bool); + new(OpCodes.Newobj, GetDeclaredConstructors(typeof(SpawningTeamVehicleEventArgs))[0]), + new(OpCodes.Dup), + new(OpCodes.Dup), + new(OpCodes.Stloc_S, ev.LocalIndex), + + // Handlers.Map.OnSpawningTeamVehicle(ev); + new(OpCodes.Call, Method(typeof(Handlers.Map), nameof(Handlers.Map.OnSpawningTeamVehicle))), + + // if (!ev.IsAllowed) + // return; + new(OpCodes.Callvirt, PropertyGetter(typeof(SpawningTeamVehicleEventArgs), nameof(SpawningTeamVehicleEventArgs.IsAllowed))), + new(OpCodes.Brfalse_S, retLabel), + + // team = ev.Team + new(OpCodes.Ldloc_S, ev), + new(OpCodes.Callvirt, PropertyGetter(typeof(SpawningTeamVehicleEventArgs), nameof(SpawningTeamVehicleEventArgs.Team))), + new(OpCodes.Starg_S, 1), + + new CodeInstruction(OpCodes.Nop).WithLabels(continueLabel), + }); + + newInstructions[newInstructions.Count - 1].WithLabels(retLabel); - SpawningTeamVehicleEventArgs ev = new(team); - Handlers.Map.OnSpawningTeamVehicle(ev); + for (int z = 0; z < newInstructions.Count; z++) + yield return newInstructions[z]; - return ev.IsAllowed; + ListPool.Pool.Return(newInstructions); } } } \ No newline at end of file diff --git a/Exiled.Events/Patches/Events/Player/Joined.cs b/Exiled.Events/Patches/Events/Player/Joined.cs index ba27333eb0..8b580bd503 100644 --- a/Exiled.Events/Patches/Events/Player/Joined.cs +++ b/Exiled.Events/Patches/Events/Player/Joined.cs @@ -37,14 +37,14 @@ internal static void CallEvent(ReferenceHub hub, out Player player) Log.Debug($"Object exists {player is not null}"); Log.Debug($"Creating player object for {hub.nicknameSync.Network_displayName}"); #endif + Player.UnverifiedPlayers.Add(hub.gameObject, player); + if (ReferenceHub.HostHub == null) { Server.Host = player; } else { - Player.UnverifiedPlayers.Add(hub.gameObject, player); - Handlers.Player.OnJoined(new JoinedEventArgs(player)); } } diff --git a/Exiled.Events/Patches/Events/Player/TogglingFlashlight.cs b/Exiled.Events/Patches/Events/Player/TogglingFlashlight.cs index 067dc9acc9..9fd1328522 100644 --- a/Exiled.Events/Patches/Events/Player/TogglingFlashlight.cs +++ b/Exiled.Events/Patches/Events/Player/TogglingFlashlight.cs @@ -44,14 +44,14 @@ private static IEnumerable Transpiler(IEnumerable Transpiler(IEnumerable.Pool.Return(newInstructions); } } -} \ No newline at end of file +}