233 lines
7.7 KiB
C#
233 lines
7.7 KiB
C#
using System.Collections;
|
|
using Interactables.Interobjects;
|
|
using Interactables.Interobjects.DoorButtons;
|
|
using Interactables.Interobjects.DoorUtils;
|
|
using LabApi.Events.Arguments.PlayerEvents;
|
|
using LabApi.Events.Handlers;
|
|
using LabApi.Features;
|
|
using LabApi.Loader;
|
|
using UnityEngine;
|
|
using KeycardItem = InventorySystem.Items.Keycards.KeycardItem;
|
|
using Logger = LabApi.Features.Console.Logger;
|
|
|
|
namespace KeycardButModern
|
|
{
|
|
public class Plugin: LabApi.Loader.Features.Plugins.Plugin
|
|
{
|
|
public override string Name => "KeycardButModern";
|
|
public override string Description => "Ever thought you wanted your keycard implanted in your body? No? Same.";
|
|
public override string Author => "Code002Lover";
|
|
public override Version Version { get; } = new(1, 0, 0);
|
|
public override Version RequiredApiVersion { get; } = new (LabApiProperties.CompiledVersion);
|
|
|
|
public DoorLockConfiguration DoorLockConfiguration;
|
|
public static Plugin Singleton;
|
|
|
|
public override void LoadConfigs()
|
|
{
|
|
base.LoadConfigs();
|
|
|
|
DoorLockConfiguration = this.LoadConfig< DoorLockConfiguration > ("door_locks.yml");
|
|
}
|
|
|
|
private void OnInteractingDoor(PlayerInteractingDoorEventArgs ev)
|
|
{
|
|
if (ev.CanOpen)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (ev.Door.IsLocked)
|
|
{
|
|
return;
|
|
}
|
|
|
|
foreach (var playerItem in ev.Player.Items)
|
|
{
|
|
//is keycard?
|
|
if (playerItem.Type > ItemType.KeycardO5) continue;
|
|
if (playerItem.Base is not KeycardItem keycardItem)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (!ev.Door.Base.CheckPermissions(keycardItem, out _)) continue;
|
|
ev.Door.IsOpened = !ev.Door.IsOpened;
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
private void OnInteractingGenerator(PlayerInteractingGeneratorEventArgs ev)
|
|
{
|
|
if (!ev.IsAllowed)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (ev.Player.CurrentItem?.Base is KeycardItem keycard)
|
|
{
|
|
if (ev.Generator.Base.CheckPermissions(keycard, out _))
|
|
{
|
|
ev.Generator.IsUnlocked = true;
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (ev.Generator.IsUnlocked)
|
|
{
|
|
return;
|
|
}
|
|
|
|
|
|
foreach (var playerItem in ev.Player.Items)
|
|
{
|
|
//is keycard?
|
|
if (playerItem.Type > ItemType.KeycardO5) continue;
|
|
if (playerItem.Base is not KeycardItem keycardItem)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (!ev.Generator.Base.CheckPermissions(keycardItem, out _)) continue;
|
|
ev.Generator.IsOpen = !ev.Generator.IsOpen;
|
|
ev.Generator.IsUnlocked = true;
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
private void OnInteractingLocker(PlayerInteractingLockerEventArgs ev)
|
|
{
|
|
if (!ev.IsAllowed)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (ev.Chamber.Base.RequiredPermissions == DoorPermissionFlags.None)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (ev.Player.CurrentItem?.Base is KeycardItem keycard)
|
|
{
|
|
if (ev.Chamber.Base.CheckPermissions(keycard, out _)) return;
|
|
}
|
|
|
|
foreach (var playerItem in ev.Player.Items)
|
|
{
|
|
//is keycard?
|
|
if (playerItem.Type > ItemType.KeycardO5) continue;
|
|
if (playerItem.Base is not KeycardItem keycardItem)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (!ev.Chamber.Base.CheckPermissions(keycardItem, out _)) continue;
|
|
ev.Chamber.IsOpen = !ev.Chamber.IsOpen;
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
private void OnUnlockingWarhead(PlayerUnlockingWarheadButtonEventArgs ev)
|
|
{
|
|
if (ev.IsAllowed)
|
|
{
|
|
return;
|
|
}
|
|
|
|
foreach (var playerItem in ev.Player.Items)
|
|
{
|
|
//is keycard?
|
|
if (playerItem.Type > ItemType.KeycardO5) continue;
|
|
if (playerItem.Base is not KeycardItem keycardItem)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (!AlphaWarheadActivationPanel.Instance.CheckPermissions(keycardItem, out _)) continue;
|
|
ev.IsAllowed = true;
|
|
return;
|
|
}
|
|
}
|
|
|
|
private static void OnBulletHole(PlayerPlacedBulletHoleEventArgs ev)
|
|
{
|
|
var direction = (ev.HitPosition - ev.RaycastStart).normalized;
|
|
var distance = Vector3.Distance(ev.RaycastStart, ev.HitPosition);
|
|
|
|
var hits = Physics.RaycastAll(ev.RaycastStart, direction, distance);
|
|
foreach (var hit in hits)
|
|
{
|
|
ButtonVariant doorButton = hit.collider.GetComponent<LcdButton>();
|
|
if (!doorButton)
|
|
{
|
|
doorButton = hit.collider.GetComponent<CheckpointKeycardButton>();
|
|
}
|
|
if (!doorButton)
|
|
{
|
|
doorButton = hit.collider.GetComponent<KeycardButton>();
|
|
}
|
|
if (!doorButton)
|
|
{
|
|
doorButton = hit.collider.GetComponent<SimpleButton>();
|
|
}
|
|
|
|
if (!doorButton)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var doorVariant = DoorVariant.AllDoors.AsEnumerable()!.First(x => x.Buttons.Any(c=>c.GetInstanceID() == doorButton.GetInstanceID()));
|
|
|
|
doorVariant.ServerInteract(ev.Player.ReferenceHub, doorButton.ColliderId);
|
|
StartDoorLockCoroutine(doorVariant);
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
public override void Enable()
|
|
{
|
|
Logger.Debug("starting...");
|
|
Singleton = this;
|
|
|
|
PlayerEvents.InteractingDoor += OnInteractingDoor;
|
|
PlayerEvents.InteractingGenerator += OnInteractingGenerator;
|
|
PlayerEvents.InteractingLocker += OnInteractingLocker;
|
|
PlayerEvents.UnlockingWarheadButton += OnUnlockingWarhead;
|
|
|
|
PlayerEvents.PlacedBulletHole += OnBulletHole;
|
|
|
|
}
|
|
|
|
private static void StartDoorLockCoroutine(DoorVariant door)
|
|
{
|
|
door.StartCoroutine(LockDoorCoroutine(door));
|
|
}
|
|
|
|
private static IEnumerator LockDoorCoroutine(DoorVariant door)
|
|
{
|
|
var doorType = DoorLockConfiguration.GetDoorType(door);
|
|
var lockDuration = Singleton.DoorLockConfiguration.LockDurations[doorType];
|
|
|
|
door.ServerChangeLock(DoorLockReason.SpecialDoorFeature, true);
|
|
yield return new WaitForSeconds(lockDuration);
|
|
door.ServerChangeLock(DoorLockReason.SpecialDoorFeature, false);
|
|
}
|
|
|
|
public override void Disable()
|
|
{
|
|
PlayerEvents.InteractingDoor -= OnInteractingDoor;
|
|
PlayerEvents.InteractingGenerator -= OnInteractingGenerator;
|
|
PlayerEvents.InteractingLocker -= OnInteractingLocker;
|
|
PlayerEvents.UnlockingWarheadButton -= OnUnlockingWarhead;
|
|
|
|
PlayerEvents.PlacedBulletHole -= OnBulletHole;
|
|
Logger.Debug("unloading...");
|
|
|
|
Singleton = null;
|
|
}
|
|
}
|
|
} |