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

Simplify door/lift api #2467

Merged
merged 17 commits into from
May 15, 2024
1 change: 0 additions & 1 deletion Exiled.API/Enums/DoorLockType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace Exiled.API.Enums
/// <summary>
/// All possible door locks.
/// </summary>
/// <seealso cref="Door.LockAll(float, DoorLockType)"/>
/// <seealso cref="Door.ChangeLock(DoorLockType)"/>
[Flags]
public enum DoorLockType
Expand Down
192 changes: 32 additions & 160 deletions Exiled.API/Features/Doors/Door.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Exiled.API.Features.Doors
using Exiled.API.Interfaces;
using Interactables.Interobjects;
using Interactables.Interobjects.DoorUtils;
using MapGeneration.Distributors;
using MEC;
using Mirror;
using UnityEngine;
Expand Down Expand Up @@ -128,6 +129,15 @@ public bool IsOpen
set => Base.NetworkTargetState = value;
}

/// <summary>
/// Gets or sets a value indicating whether the door is close.
/// </summary>
public bool IsClose
louis1706 marked this conversation as resolved.
Show resolved Hide resolved
louis1706 marked this conversation as resolved.
Show resolved Hide resolved
{
get => !IsOpen;
set => IsOpen = !value;
}

/// <summary>
/// Gets a value indicating whether or not this door is a gate.
/// </summary>
Expand Down Expand Up @@ -208,12 +218,12 @@ public bool AllowsScp106
/// <summary>
/// Gets a value indicating whether or not the door is locked.
/// </summary>
public bool IsLocked => DoorLockType > 0;
public bool IsLocked => LockType > 0;

/// <summary>
/// Gets or sets the door lock type.
/// </summary>
public DoorLockType DoorLockType
public DoorLockType LockType
{
get => (DoorLockType)Base.NetworkActiveLocks;
set
Expand Down Expand Up @@ -425,135 +435,9 @@ public static Door GetClosest(Vector3 position, out float distance)
/// <param name="type">Filters by <see cref="ZoneType"/>.</param>
/// <param name="onlyUnbroken">A value indicating whether it filters broken doors.</param>
/// <returns><see cref="Door"/> object.</returns>
public static Door Random(ZoneType type = ZoneType.Unspecified, bool onlyUnbroken = false)
{
List<Door> doors = onlyUnbroken || type is not ZoneType.Unspecified ? Get(x =>
(x.Room is null || x.Room.Zone.HasFlag(type) || type == ZoneType.Unspecified) && (x is Breakable { IsDestroyed: true } || !onlyUnbroken)).
ToList() :
DoorVariantToDoor.Values.ToList();

return doors[UnityEngine.Random.Range(0, doors.Count)];
}

/// <summary>
/// Permanently locks a <see cref="Door"/> corresponding to the given type.
/// </summary>
/// <param name="type">The door to affect.</param>
/// <param name="lockType">The specified <see cref="Enums.DoorLockType"/>.</param>
public static void Lock(DoorType type, DoorLockType lockType = DoorLockType.Regular079) => Get(type)?.Lock(lockType);

/// <summary>
/// Temporary locks a <see cref="Door"/> corresponding to the given type.
/// </summary>
/// <param name="type">The door to affect.</param>
/// <param name="duration">The duration of the lockdown.</param>
/// <param name="lockType">The specified <see cref="Enums.DoorLockType"/>.</param>
public static void Lock(DoorType type, float duration, DoorLockType lockType = DoorLockType.Regular079) => Get(type)?.Lock(duration, lockType);

/// <summary>
/// Unlocks a <see cref="Door"/> corresponding to the specified type.
/// </summary>
/// <param name="type">The <see cref="DoorType"/>.</param>
public static void Unlock(DoorType type) => Get(type)?.Unlock();

/// <summary>
/// Locks a <see cref="Door">doors</see> corresponding to the given type.
/// </summary>
/// <param name="type">The door to affect.</param>
/// <param name="duration">The duration of the lockdown.</param>
/// <param name="lockType">The specified <see cref="Enums.DoorLockType"/>.</param>
public static void LockAll(DoorType type, float duration, DoorLockType lockType = DoorLockType.Regular079) => Get(type)?.Lock(duration, lockType);

/// <summary>
/// Permanently locks all <see cref="Door">doors</see> in the facility.
/// </summary>
/// <param name="lockType">The specified <see cref="Enums.DoorLockType"/>.</param>
public static void LockAll(DoorLockType lockType = DoorLockType.Regular079) => List.ForEach(door => door.Lock(lockType));

/// <summary>
/// Locks all <see cref="Door">doors</see> in the facility.
/// </summary>
/// <param name="duration">The duration of the lockdown.</param>
/// <param name="lockType">The specified <see cref="Enums.DoorLockType"/>.</param>
public static void LockAll(float duration, DoorLockType lockType = DoorLockType.Regular079) => List.ForEach(door => door.Lock(duration, lockType, true));

/// <summary>
/// Permanently locks all <see cref="Door">doors</see> given the specified <see cref="ZoneType"/>.
/// </summary>
/// <param name="type">The <see cref="ZoneType"/> to affect.</param>
/// <param name="lockType">The specified <see cref="Enums.DoorLockType"/>.</param>
public static void LockAll(ZoneType type, DoorLockType lockType = DoorLockType.Regular079) => Get(type).ForEach(door => door.Lock(lockType, true));

/// <summary>
/// Permanently locks all <see cref="Door">doors</see> given the specified zones.
/// </summary>
/// <param name="types">The zones to affect.</param>
/// <param name="lockType">The specified <see cref="Enums.DoorLockType"/>.</param>
public static void LockAll(IEnumerable<ZoneType> types, DoorLockType lockType = DoorLockType.Regular079) => Get(types).ForEach(door => door.Lock(lockType, true));

/// <summary>
/// Temporary locks all <see cref="Door">doors</see> given the specified <see cref="ZoneType"/>.
/// </summary>
/// <param name="type">The <see cref="ZoneType"/> to affect.</param>
/// <param name="duration">The duration of the lockdown.</param>
/// <param name="lockType">The specified <see cref="Enums.DoorLockType"/>.</param>
public static void LockAll(ZoneType type, float duration, DoorLockType lockType = DoorLockType.Regular079) => Get(type).ForEach(door => door.Lock(lockType, true));

/// <summary>
/// Temporary locks all <see cref="Door">doors</see> given the specified zones.
/// </summary>
/// <param name="types">The zones to affect.</param>
/// <param name="duration">The duration of the lockdown.</param>
/// <param name="lockType">The specified <see cref="Enums.DoorLockType"/>.</param>
public static void LockAll(IEnumerable<ZoneType> types, float duration, DoorLockType lockType = DoorLockType.Regular079) => types.ForEach(t => LockAll(t, duration, lockType));

/// <summary>
/// Permanently locks all <see cref="Door">doors</see> corresponding to the given types.
/// </summary>
/// <param name="types">The doors to affect.</param>
/// <param name="lockType">The specified <see cref="Enums.DoorLockType"/>.</param>
public static void LockAll(IEnumerable<DoorType> types, DoorLockType lockType = DoorLockType.Regular079) => Get(types).ForEach(door => door.Lock(lockType, true));

/// <summary>
/// Temporary locks all <see cref="Door">doors</see> corresponding to the given types.
/// </summary>
/// <param name="types">The doors to affect.</param>
/// <param name="duration">The duration of the lockdown.</param>
/// <param name="lockType">The specified <see cref="Enums.DoorLockType"/>.</param>
public static void LockAll(IEnumerable<DoorType> types, float duration, DoorLockType lockType = DoorLockType.Regular079) => types.ForEach(t => LockAll(t, duration, lockType));

/// <summary>
/// Unlocks all <see cref="Door">doors</see> in the facility.
/// </summary>
public static void UnlockAll() => List.ForEach(door => door.Unlock());

/// <summary>
/// Unlocks all <see cref="Door">doors</see> in the facility.
/// </summary>
/// <param name="type">The zones to affect.</param>
public static void UnlockAll(ZoneType type) => UnlockAll(door => door.Zone.HasFlag(type));

/// <summary>
/// Unlocks all <see cref="Door">doors</see> in the facility.
/// </summary>
/// <param name="types">The zones to affect.</param>
public static void UnlockAll(params ZoneType[] types) => UnlockAll(door => types.Contains(door.Zone));

/// <summary>
/// Unlocks all <see cref="Door">doors</see> in the facility.
/// </summary>
/// <param name="types">The zones to affect.</param>
public static void UnlockAll(IEnumerable<ZoneType> types) => UnlockAll(door => types.Contains(door.Zone));

/// <summary>
/// Unlocks all <see cref="Door">doors</see> in the facility.
/// </summary>
/// <param name="predicate">The condition to satisfy.</param>
public static void UnlockAll(Func<Door, bool> predicate)
{
foreach (Door door in Get(predicate))
door.Unlock();
}
public static Door Random(ZoneType type = ZoneType.Unspecified, bool onlyUnbroken = false) => onlyUnbroken || type is not ZoneType.Unspecified ?
Get(x => (x.Room is null || x.Room.Zone.HasFlag(type) || type == ZoneType.Unspecified) && (x is Breakable { IsDestroyed: true } || !onlyUnbroken)).Random() :
DoorVariantToDoor.Values.Random();

/// <summary>
/// Makes the door play a beep sound.
Expand All @@ -576,48 +460,36 @@ public void PlaySound(DoorBeepType beep)
/// Change the door lock with the given lock type.
/// </summary>
/// <param name="lockType">The <see cref="Enums.DoorLockType"/> to use.</param>
public void ChangeLock(DoorLockType lockType)
{
if (lockType is DoorLockType.None)
{
Base.NetworkActiveLocks = 0;
}
else
{
DoorLockType locks = DoorLockType;
if (locks.HasFlag(lockType))
locks &= ~lockType;
else
locks |= lockType;

Base.NetworkActiveLocks = (ushort)locks;
}

DoorEvents.TriggerAction(Base, IsLocked ? DoorAction.Locked : DoorAction.Unlocked, null);
}
public void ChangeLock(DoorLockType lockType) => Base.ServerChangeLock((DoorLockReason)lockType, !LockType.HasFlag(lockType));

/// <summary>
/// Permanently locks all active locks on the door, and then reverts back any changes after a specified length of time.
/// </summary>
/// <param name="lockType">The <see cref="Enums.DoorLockType"/> of the lockdown.</param>
/// <param name="shouldBeClosed">A value indicating whether the door should be closed.</param>
public void Lock(DoorLockType lockType = DoorLockType.Regular079, bool shouldBeClosed = false)
/// <param name="updateTheDoorState">A value indicating whether the door state should be modified.</param>
public void Lock(DoorLockType lockType = DoorLockType.AdminCommand, bool updateTheDoorState = true)
{
if (shouldBeClosed)
IsOpen = false;

ChangeLock(lockType);

if (updateTheDoorState)
{
DoorLockMode mode = DoorLockUtils.GetMode((DoorLockReason)LockType);
if (mode is DoorLockMode.CanOpen)
IsOpen = true;
else if (mode is DoorLockMode.CanClose)
IsOpen = false;
}
}

/// <summary>
/// Temporary locks all active locks on the door, and then reverts back any changes after a specified length of time.
/// </summary>
/// <param name="time">The amount of time that must pass before unlocking the door.</param>
/// <param name="lockType">The <see cref="Enums.DoorLockType"/> of the lockdown.</param>
/// <param name="shouldBeClosed">A value indicating whether the door should be closed.</param>
public void Lock(float time, DoorLockType lockType = DoorLockType.Regular079, bool shouldBeClosed = false)
/// <param name="updateTheDoorState">A value indicating whether the door state should be modified.</param>
public void Lock(float time, DoorLockType lockType = DoorLockType.AdminCommand, bool updateTheDoorState = true)
{
Lock(lockType, shouldBeClosed);
Lock(lockType, updateTheDoorState);
Unlock(time, lockType);
}

Expand All @@ -630,7 +502,7 @@ public void Lock(float time, DoorLockType lockType = DoorLockType.Regular079, bo
/// Unlocks and clears all active locks on the door after a specified length of time.
/// </summary>
/// <param name="time">The amount of time that must pass before unlocking the door.</param>
/// <param name="flagsToUnlock">The <see cref="Enums.DoorLockType"/> of the lockdown.</param>
/// <param name="flagsToUnlock">The <see cref="DoorLockType"/> of the lockdown.</param>
public void Unlock(float time, DoorLockType flagsToUnlock) => DoorScheduledUnlocker.UnlockLater(Base, time, (DoorLockReason)flagsToUnlock);

/// <summary>
Expand All @@ -644,7 +516,7 @@ public void Lock(float time, DoorLockType lockType = DoorLockType.Regular079, bo
/// Returns the Door in a human-readable format.
/// </summary>
/// <returns>A string containing Door-related data.</returns>
public override string ToString() => $"{Type} ({Zone}) [{Room}] *{DoorLockType}* ={RequiredPermissions.RequiredPermissions}=";
public override string ToString() => $"{Type} ({Zone}) [{Room}] *{LockType}* ={RequiredPermissions.RequiredPermissions}=";

/// <summary>
/// Creates the door object associated with a specific <see cref="DoorVariant"/>.
Expand Down
Loading
Loading