Skip to content

Commit 6f035b4

Browse files
committed
Fixing some items not applying their effects when dropped (a bit of a workaround way but it works)
1 parent 18c3455 commit 6f035b4

4 files changed

Lines changed: 60 additions & 11 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
/packages
99
LabExtended.sln.DotSettings.user
1010
global.json
11+
/LabExtended.ImageConvertor/obj

LabExtended.ImageConvertor/LabExtended.ImageConvertor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net10.0</TargetFramework>
5+
<TargetFramework>net9.0</TargetFramework>
66
<LangVersion>preview</LangVersion>
77
<ImplicitUsings>enable</ImplicitUsings>
88
<Nullable>enable</Nullable>

LabExtended/Extensions/ItemExtensions.cs

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using InventorySystem;
22
using InventorySystem.Items;
3+
using InventorySystem.Items.DebugTools;
4+
using InventorySystem.Items.Firearms;
5+
using InventorySystem.Items.Keycards;
36
using InventorySystem.Items.Pickups;
4-
7+
using InventorySystem.Items.Usables.Scp1344;
58
using LabExtended.API;
69
using LabExtended.Utilities;
710
using Mirror;
8-
911
using UnityEngine;
1012

1113
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
@@ -37,15 +39,15 @@ public static void ReloadPrefabs()
3739
/// Gets the current inventory slot of an item.
3840
/// </summary>
3941
/// <param name="item">The target item.</param>
40-
/// <returns>The item slot number (1 - 8)</returns>
42+
/// <returns>The item slot number (1 - 8 or 0 if the item does not have an owner)</returns>
4143
/// <exception cref="InvalidOperationException"></exception>
4244
public static byte GetInventorySlot(this ItemBase item)
4345
{
4446
if (item == null)
4547
throw new ArgumentNullException(nameof(item));
46-
47-
if (item.Owner is null)
48-
throw new InvalidOperationException("The targeted item must be owned by a player.");
48+
49+
if (item.Owner == null)
50+
return 0;
4951

5052
return (byte)(item.OwnerInventory.UserInventory.Items.FindKeyIndex(item.ItemSerial) + 1);
5153
}
@@ -189,6 +191,48 @@ public static bool TryGetRigidbody(this ItemPickupBase pickup, out Rigidbody? ri
189191
return itemPickupBase.GetComponent<Rigidbody>();
190192
}
191193

194+
/// <summary>
195+
/// Applies effects granted to an item's owner once the item is dropped.
196+
/// </summary>
197+
/// <param name="item">The item to simulate drop of.</param>
198+
/// <param name="keepItem">Whether or not the item should be kept in the player's inventory.</param>
199+
/// <returns>true if a pickup of the item should be dropped</returns>
200+
public static bool SimulateDrop(this ItemBase item, out bool keepItem)
201+
{
202+
keepItem = false;
203+
204+
if (item != null && item.Owner != null)
205+
{
206+
if (item is Scp1344Item scp1344 && scp1344 != null)
207+
{
208+
if (scp1344.Status is Scp1344Status.Deactivating)
209+
{
210+
keepItem = true;
211+
return false;
212+
}
213+
214+
if (scp1344.Status is Scp1344Status.Active)
215+
{
216+
scp1344.ServerSetStatus(Scp1344Status.Dropping);
217+
218+
keepItem = true;
219+
return false;
220+
}
221+
}
222+
223+
if (item is ParticleDisruptor particleDisruptor && particleDisruptor.DeleteOnDrop)
224+
return false;
225+
226+
if (item is SingleUseKeycardItem singleUseKeycard && singleUseKeycard._destroyed)
227+
return false;
228+
229+
if (item is RagdollMover)
230+
return false;
231+
}
232+
233+
return true;
234+
}
235+
192236
/// <summary>
193237
/// Freezes the specified pickup.
194238
/// </summary>

LabExtended/Patches/Events/Player/PlayerThrowingItemPatch.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,13 @@ private static bool Prefix(Inventory __instance, ushort itemSerial, bool tryThro
6767

6868
CustomItemPickupBehaviour? pickupBehaviour = null;
6969

70-
var pickup = pickupType != item.ItemTypeId || pickupScale != Vector3.one
71-
? ExMap.SpawnItem(pickupType, player.Position, pickupScale, player.Rotation, itemSerial)
72-
: player.ReferenceHub.inventory.ServerDropItem(itemSerial);
70+
var spawnPickup = item.SimulateDrop(out var keepItem);
71+
72+
var pickup = spawnPickup
73+
? (pickupType != item.ItemTypeId || pickupScale != Vector3.one
74+
? ExMap.SpawnItem(pickupType, player.Position, pickupScale, player.Rotation, itemSerial)
75+
: player.ReferenceHub.inventory.ServerDropItem(itemSerial))
76+
: null;
7377

7478
__instance.SendItemsNextFrame = true;
7579

@@ -88,7 +92,7 @@ private static bool Prefix(Inventory __instance, ushort itemSerial, bool tryThro
8892
pickupBehaviour = inventoryBehaviour.ProcessDropped(pickup, player, droppedArgs);
8993
}
9094

91-
if (item != null)
95+
if (item != null && !keepItem)
9296
item.DestroyItem();
9397

9498
if (pickup != null && player.Toggles.CanThrowItems && tryThrow && pickup.TryGetRigidbody(out var rigidbody))

0 commit comments

Comments
 (0)