Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: update codebase for 14.0 (originally made by Joker) #307

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions EXILED/EXILED.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<HarmonyVersion>2.2.2</HarmonyVersion>
<StyleCopVersion>1.1.118</StyleCopVersion>
<SemanticVersioningVersion>2.0.2</SemanticVersioningVersion>
<Nullable>disable</Nullable>

<Copyright>Copyright © $(Authors) 2020 - $([System.DateTime]::Now.ToString("yyyy"))</Copyright>
<RepositoryType>Git</RepositoryType>
Expand Down
88 changes: 52 additions & 36 deletions EXILED/Exiled.API/Features/Items/Firearm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,18 @@ namespace Exiled.API.Features.Items

using CameraShaking;
using Enums;
using Exiled.API.Features.Pickups;
using Exiled.API.Interfaces;
using Exiled.API.Structs;
using Extensions;
using InventorySystem;
using InventorySystem.Items;
using InventorySystem.Items.Firearms;
using Interfaces;
using InventorySystem.Items.Autosync;
using InventorySystem.Items.Firearms.Attachments;
using InventorySystem.Items.Firearms.Attachments.Components;
using InventorySystem.Items.Firearms.BasicMessages;
using InventorySystem.Items.Firearms.Modules;
using InventorySystem.Items.Pickups;
using MEC;
using UnityEngine;
using Pickups;
using Structs;

using BaseFirearm = InventorySystem.Items.Firearms.Firearm;
using FirearmPickup = Pickups.FirearmPickup;
using Object = UnityEngine.Object;

/// <summary>
/// A wrapper class for <see cref="InventorySystem.Items.Firearms.Firearm"/>.
Expand All @@ -47,6 +41,8 @@ public class Firearm : Item, IWrapper<BaseFirearm>
/// </summary>
internal static readonly Dictionary<FirearmType, uint> BaseCodesValue = new();

private readonly IPrimaryAmmoContainerModule ammoModule;

/// <summary>
/// Initializes a new instance of the <see cref="Firearm"/> class.
/// </summary>
Expand All @@ -55,6 +51,11 @@ public Firearm(BaseFirearm itemBase)
: base(itemBase)
{
Base = itemBase;
foreach (ModuleBase mod in itemBase.Modules)
{
if (mod is IPrimaryAmmoContainerModule primary)
ammoModule = primary;
}
}

/// <summary>
Expand Down Expand Up @@ -86,7 +87,7 @@ public static IReadOnlyDictionary<Player, Dictionary<FirearmType, AttachmentIden
IEnumerable<KeyValuePair<Player, Dictionary<FirearmType, AttachmentIdentifier[]>>> playerPreferences =
AttachmentsServerHandler.PlayerPreferences.Where(
kvp => kvp.Key is not null).Select(
(KeyValuePair<ReferenceHub, Dictionary<ItemType, uint>> keyValuePair) =>
keyValuePair =>
{
return new KeyValuePair<Player, Dictionary<FirearmType, AttachmentIdentifier[]>>(
Player.Get(keyValuePair.Key),
Expand All @@ -109,8 +110,8 @@ public static IReadOnlyDictionary<Player, Dictionary<FirearmType, AttachmentIden
/// </summary>
public int Ammo
{
get => (Base.Modules[Array.IndexOf(Base.Modules, typeof(MagazineModule))] as MagazineModule).AmmoStored;
set => (Base.Modules[Array.IndexOf(Base.Modules, typeof(MagazineModule))] as MagazineModule).AmmoStored = value;
get => ammoModule?.AmmoStored ?? 0;
set => ammoModule?.ServerModifyAmmo(value);
}

/// <summary>
Expand All @@ -119,8 +120,21 @@ public int Ammo
/// <remarks>Disruptor can't be used for MaxAmmo.</remarks>
public int MaxAmmo
{
get => (Base.Modules[Array.IndexOf(Base.Modules, typeof(MagazineModule))] as MagazineModule).AmmoMax;
set => (Base.Modules[Array.IndexOf(Base.Modules, typeof(MagazineModule))] as MagazineModule)._defaultCapacity = value; // Synced?
get => ammoModule?.AmmoMax ?? 0;
set
{
switch (ammoModule)
{
case null:
throw new InvalidOperationException("Cannot change max ammo for non-ammo weapons.");
case MagazineModule mag:
mag._defaultCapacity = value;
break;
case CylinderAmmoModule:
CylinderAmmoModule.ServerPrepareNewChambers(Serial, value);
break;
}
}
}

/// <summary>
Expand All @@ -131,7 +145,7 @@ public int MaxAmmo
/// <summary>
/// Gets the <see cref="Enums.AmmoType"/> of the firearm.
/// </summary>
public AmmoType AmmoType => (Base.Modules.OfType<MagazineModule>().FirstOrDefault()?.AmmoType ?? ItemType.None).GetAmmoType();
public AmmoType AmmoType => ammoModule?.AmmoType.GetAmmoType() ?? AmmoType.None;

/// <summary>
/// Gets a value indicating whether the firearm is being aimed.
Expand Down Expand Up @@ -408,7 +422,7 @@ public bool TryGetAttachment(AttachmentIdentifier identifier, out Attachment fir
{
firearmAttachment = default;

if (!Attachments.Any(attachment => attachment.Name == identifier.Name))
if (Attachments.All(attachment => attachment.Name != identifier.Name))
return false;

firearmAttachment = GetAttachment(identifier);
Expand Down Expand Up @@ -560,11 +574,10 @@ public void RemovePreference(IEnumerable<Player> players, IEnumerable<FirearmTyp
/// <param name="player">The <see cref="Player"/> of which must be cleared.</param>
public void ClearPreferences(Player player)
{
if (AttachmentsServerHandler.PlayerPreferences.TryGetValue(player.ReferenceHub, out Dictionary<ItemType, uint> dictionary))
{
foreach (KeyValuePair<ItemType, uint> kvp in dictionary)
dictionary[kvp.Key] = kvp.Key.GetFirearmType().GetBaseCode();
}
if (!AttachmentsServerHandler.PlayerPreferences.TryGetValue(player.ReferenceHub, out Dictionary<ItemType, uint> dictionary))
return;
foreach (KeyValuePair<ItemType, uint> kvp in dictionary)
dictionary[kvp.Key] = kvp.Key.GetFirearmType().GetBaseCode();
}

/// <summary>
Expand Down Expand Up @@ -592,20 +605,22 @@ public void ClearPreferences()
/// <param name="emptyMagazine">Whether empty magazine should be loaded.</param>
public void Reload(bool emptyMagazine = false)
{
MagazineModule magazineModule = Base.Modules.OfType<MagazineModule>().FirstOrDefault();

if (magazineModule == null)
return;

magazineModule.ServerRemoveMagazine();
if (ammoModule is MagazineModule magazineModule)
{
magazineModule.ServerRemoveMagazine();

Timing.CallDelayed(0.1f, () =>
Timing.CallDelayed(0.1f, () =>
{
if (emptyMagazine)
magazineModule.ServerInsertEmptyMagazine();
else
magazineModule.ServerInsertMagazine();
});
}
else if (Base.TryGetModule(out AnimatorReloaderModuleBase animatorModule))
{
if (emptyMagazine)
magazineModule.ServerInsertEmptyMagazine();
else
magazineModule.ServerInsertMagazine();
});
animatorModule.StartReloading();
}
BoltonDev marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
Expand Down Expand Up @@ -641,6 +656,8 @@ internal override void ChangeOwner(Player oldOwner, Player newOwner)
{
Base.Owner = newOwner.ReferenceHub;
Base._footprintCacheSet = false;
foreach (SubcomponentBase component in Base.AllSubcomponents)
component.OnAdded();
}

/// <inheritdoc/>
Expand All @@ -650,8 +667,7 @@ internal override void ReadPickupInfo(Pickup pickup)

if (pickup is FirearmPickup firearmPickup)
{
// TODO If synced
// MaxAmmo = firearmPickup.MaxAmmo;
Timing.CallDelayed(0.1f, () => Ammo = firearmPickup.Ammo);
}
}
}
Expand Down
20 changes: 9 additions & 11 deletions EXILED/Exiled.API/Features/Lockers/Locker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@ namespace Exiled.API.Features.Lockers
using System.Collections.Generic;
using System.Linq;

using Exiled.API.Enums;
using Exiled.API.Extensions;
using Exiled.API.Features;
using Exiled.API.Features.Pickups;
using Exiled.API.Interfaces;

using Enums;
using Extensions;
using Features;
using Interfaces;
using InventorySystem.Items.Pickups;
using MapGeneration.Distributors;

using Mirror;
using Pickups;
using UnityEngine;

using BaseLocker = MapGeneration.Distributors.Locker;
#nullable enable

/// <summary>
/// The in-game Locker.
/// </summary>
Expand Down Expand Up @@ -79,7 +77,7 @@ public Locker(BaseLocker locker)
/// <summary>
/// Gets the <see cref="Features.Room"/> in which the <see cref="Locker"/> is located.
/// </summary>
public Room? Room => Room.Get(Position);
public Room Room => Room.Get(Position);

/// <summary>
/// Gets the <see cref="ZoneType"/> in which the locker is located.
Expand Down Expand Up @@ -126,7 +124,7 @@ public Vector3 RandomChamberPosition
/// </summary>
/// <param name="locker">The <see cref="BaseLocker"/> to get.</param>
/// <returns>A <see cref="Locker"/> or <see langword="null"/> if not found.</returns>
public static Locker? Get(BaseLocker locker) => locker == null ? null :
public static Locker Get(BaseLocker locker) => locker == null ? null :
BaseToExiledLockers.TryGetValue(locker, out Locker supply) ? supply : new Locker(locker);

/// <summary>
Expand All @@ -149,7 +147,7 @@ public Vector3 RandomChamberPosition
/// <param name="zone">The <see cref="ZoneType"/> to filter by. If unspecified, all zones are considered.</param>
/// <param name="lockerType">The <see cref="LockerType"/> to filter by. If unspecified, all locker types are considered.</param>
/// <returns>A random <see cref="Locker"/> object, or <see langword="null"/> if no matching locker is found.</returns>
public static Locker? Random(ZoneType zone = ZoneType.Unspecified, LockerType lockerType = LockerType.Unknow)
public static Locker Random(ZoneType zone = ZoneType.Unspecified, LockerType lockerType = LockerType.Unknow)
{
IEnumerable<Locker> filteredLockers = List;

Expand Down
31 changes: 15 additions & 16 deletions EXILED/Exiled.API/Features/Npc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,21 @@

namespace Exiled.API.Features
{
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

using CentralAuth;
using CommandSystem;
using Exiled.API.Enums;
using Exiled.API.Features.Components;
using Exiled.API.Features.Roles;
using Components;
using Enums;
using Footprinting;
using GameCore;
using MEC;
using Mirror;
using PlayerRoles;
using Roles;
using UnityEngine;

using Object = UnityEngine.Object;
Expand Down Expand Up @@ -68,70 +67,70 @@ public override Vector3 Position
/// </summary>
/// <param name="rHub">The ReferenceHub to retrieve the NPC for.</param>
/// <returns>The NPC associated with the ReferenceHub, or <c>null</c> if not found.</returns>
public static new Npc? Get(ReferenceHub rHub) => Player.Get(rHub) as Npc;
public static new Npc Get(ReferenceHub rHub) => Player.Get(rHub) as Npc;

/// <summary>
/// Retrieves the NPC associated with the specified GameObject.
/// </summary>
/// <param name="gameObject">The GameObject to retrieve the NPC for.</param>
/// <returns>The NPC associated with the GameObject, or <c>null</c> if not found.</returns>
public static new Npc? Get(GameObject gameObject) => Player.Get(gameObject) as Npc;
public static new Npc Get(GameObject gameObject) => Player.Get(gameObject) as Npc;

/// <summary>
/// Retrieves the NPC associated with the specified user ID.
/// </summary>
/// <param name="userId">The user ID to retrieve the NPC for.</param>
/// <returns>The NPC associated with the user ID, or <c>null</c> if not found.</returns>
public static new Npc? Get(string userId) => Player.Get(userId) as Npc;
public static new Npc Get(string userId) => Player.Get(userId) as Npc;

/// <summary>
/// Retrieves the NPC associated with the specified ID.
/// </summary>
/// <param name="id">The ID to retrieve the NPC for.</param>
/// <returns>The NPC associated with the ID, or <c>null</c> if not found.</returns>
public static new Npc? Get(int id) => Player.Get(id) as Npc;
public static new Npc Get(int id) => Player.Get(id) as Npc;

/// <summary>
/// Retrieves the NPC associated with the specified ICommandSender.
/// </summary>
/// <param name="sender">The ICommandSender to retrieve the NPC for.</param>
/// <returns>The NPC associated with the ICommandSender, or <c>null</c> if not found.</returns>
public static new Npc? Get(ICommandSender sender) => Player.Get(sender) as Npc;
public static new Npc Get(ICommandSender sender) => Player.Get(sender) as Npc;

/// <summary>
/// Retrieves the NPC associated with the specified Footprint.
/// </summary>
/// <param name="footprint">The Footprint to retrieve the NPC for.</param>
/// <returns>The NPC associated with the Footprint, or <c>null</c> if not found.</returns>
public static new Npc? Get(Footprint footprint) => Player.Get(footprint) as Npc;
public static new Npc Get(Footprint footprint) => Player.Get(footprint) as Npc;

/// <summary>
/// Retrieves the NPC associated with the specified CommandSender.
/// </summary>
/// <param name="sender">The CommandSender to retrieve the NPC for.</param>
/// <returns>The NPC associated with the CommandSender, or <c>null</c> if not found.</returns>
public static new Npc? Get(CommandSender sender) => Player.Get(sender) as Npc;
public static new Npc Get(CommandSender sender) => Player.Get(sender) as Npc;

/// <summary>
/// Retrieves the NPC associated with the specified Collider.
/// </summary>
/// <param name="collider">The Collider to retrieve the NPC for.</param>
/// <returns>The NPC associated with the Collider, or <c>null</c> if not found.</returns>
public static new Npc? Get(Collider collider) => Player.Get(collider) as Npc;
public static new Npc Get(Collider collider) => Player.Get(collider) as Npc;

/// <summary>
/// Retrieves the NPC associated with the specified net ID.
/// </summary>
/// <param name="netId">The net ID to retrieve the NPC for.</param>
/// <returns>The NPC associated with the net ID, or <c>null</c> if not found.</returns>
public static new Npc? Get(uint netId) => Player.Get(netId) as Npc;
public static new Npc Get(uint netId) => Player.Get(netId) as Npc;

/// <summary>
/// Retrieves the NPC associated with the specified NetworkConnection.
/// </summary>
/// <param name="conn">The NetworkConnection to retrieve the NPC for.</param>
/// <returns>The NPC associated with the NetworkConnection, or <c>null</c> if not found.</returns>
public static new Npc? Get(NetworkConnection conn) => Player.Get(conn) as Npc;
public static new Npc Get(NetworkConnection conn) => Player.Get(conn) as Npc;

/// <summary>
/// Docs.
Expand Down Expand Up @@ -166,7 +165,7 @@ public static Npc Create(string name, RoleTypeId role, Vector3 position)
[Obsolete("This method is marked as obsolete due to a bug that make player have the same id. Use Npc.Spawn(string) instead", true)]
public static Npc Spawn(string name, RoleTypeId role, int id = 0, string userId = PlayerAuthenticationManager.DedicatedId, Vector3? position = null)
{
GameObject newObject = UnityEngine.Object.Instantiate(Mirror.NetworkManager.singleton.playerPrefab);
GameObject newObject = Object.Instantiate(NetworkManager.singleton.playerPrefab);

Npc npc = new(newObject)
{
Expand Down Expand Up @@ -241,7 +240,7 @@ public static Npc Spawn(string name, RoleTypeId role, int id = 0, string userId
/// <returns>The <see cref="Npc"/> spawned.</returns>
public static Npc Spawn(string name, RoleTypeId role = RoleTypeId.None, bool ignored = false, string userId = PlayerAuthenticationManager.DedicatedId, Vector3? position = null)
{
GameObject newObject = UnityEngine.Object.Instantiate(Mirror.NetworkManager.singleton.playerPrefab);
GameObject newObject = Object.Instantiate(NetworkManager.singleton.playerPrefab);

Npc npc = new(newObject)
{
Expand Down
Loading
Loading