forked from Exiled-Official/EXILED
-
Notifications
You must be signed in to change notification settings - Fork 110
feat: Add Exiled.API.Features.Workstation #568
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
825b045
Add Exiled.API.Features.Workstation
GoldenPig1205 6cd8ff0
fix
GoldenPig1205 e4b77a8
Approve the feedback
GoldenPig1205 7d147b4
omg
GoldenPig1205 70d0062
fix: Workstation ctor could be called twice
louis1706 4d56789
use Transform
louis1706 52325cf
Merge branch 'dev' into pr8
louis1706 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="Workstation.cs" company="ExMod Team"> | ||
| // Copyright (c) ExMod Team. All rights reserved. | ||
| // Licensed under the CC BY-SA 3.0 license. | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| namespace Exiled.API.Features | ||
| { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Linq; | ||
|
|
||
| using Exiled.API.Enums; | ||
| using Exiled.API.Interfaces; | ||
| using InventorySystem.Items.Firearms.Attachments; | ||
| using Mirror; | ||
| using UnityEngine; | ||
|
|
||
| /// <summary> | ||
| /// A wrapper class for <see cref="WorkstationController"/>. | ||
| /// </summary> | ||
| public class Workstation : IWrapper<WorkstationController>, IWorldSpace | ||
| { | ||
| /// <summary> | ||
| /// A dictionary mapping <see cref="WorkstationController"/> to <see cref="Workstation"/>. | ||
| /// </summary> | ||
| internal static readonly Dictionary<WorkstationController, Workstation> WorkstationControllerToWorkstation = new(new ComponentsEqualityComparer()); | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="Workstation"/> class. | ||
| /// </summary> | ||
| /// <param name="workstationController">The <see cref="WorkstationController"/> to wrap.</param> | ||
| internal Workstation(WorkstationController workstationController) | ||
| { | ||
| WorkstationControllerToWorkstation.Add(workstationController, this); | ||
| Base = workstationController; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets a read-only collection of all <see cref="Workstation"/> instances. | ||
| /// </summary> | ||
| public static IReadOnlyCollection<Workstation> List => WorkstationControllerToWorkstation.Values; | ||
|
|
||
| /// <summary> | ||
| /// Gets the underlying <see cref="WorkstationController"/> instance. | ||
| /// </summary> | ||
| public WorkstationController Base { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the <see cref="GameObject"/> of the workstation. | ||
| /// </summary> | ||
| public GameObject GameObject => Base.gameObject; | ||
|
|
||
| /// <summary> | ||
| /// Gets the <see cref="Transform"/> of the workstation. | ||
| /// </summary> | ||
| public Transform Transform => Base.transform; | ||
|
|
||
| /// <summary> | ||
| /// Gets the <see cref="Room"/> the workstation is located in. | ||
| /// </summary> | ||
| public Room Room => Room.Get(Transform.position); | ||
|
|
||
| /// <summary> | ||
| /// Gets the <see cref="ZoneType"/> of the workstation's room. | ||
| /// </summary> | ||
| public ZoneType Zone => Room.Zone; | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the position of the workstation. | ||
| /// </summary> | ||
| public Vector3 Position | ||
| { | ||
| get => GameObject.transform.position; | ||
| set | ||
| { | ||
| NetworkServer.UnSpawn(GameObject); | ||
| GameObject.transform.position = value; | ||
| NetworkServer.Spawn(GameObject); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the rotation of the workstation. | ||
| /// </summary> | ||
| public Quaternion Rotation | ||
| { | ||
| get => GameObject.transform.rotation; | ||
| set | ||
| { | ||
| NetworkServer.UnSpawn(GameObject); | ||
| GameObject.transform.rotation = value; | ||
| NetworkServer.Spawn(GameObject); | ||
| } | ||
| } | ||
|
GoldenPig1205 marked this conversation as resolved.
|
||
|
|
||
| /// <summary> | ||
| /// Gets or sets the status of the workstation. | ||
| /// </summary> | ||
| public WorkstationController.WorkstationStatus Status | ||
| { | ||
| get => (WorkstationController.WorkstationStatus)Base.Status; | ||
| set => Base.NetworkStatus = (byte)value; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the <see cref="Stopwatch"/> used by the workstation. | ||
| /// </summary> | ||
| public Stopwatch Stopwatch => Base.ServerStopwatch; | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the player known to be using the workstation. | ||
| /// </summary> | ||
| public Player KnownUser | ||
| { | ||
| get => Player.Get(Base.KnownUser); | ||
| set => Base.KnownUser = value.ReferenceHub; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets all <see cref="Workstation"/> instances that match the specified predicate. | ||
| /// </summary> | ||
| /// <param name="predicate">The predicate to filter workstations.</param> | ||
| /// <returns>An <see cref="IEnumerable{Workstation}"/> of matching workstations.</returns> | ||
| public static IEnumerable<Workstation> Get(Func<Workstation, bool> predicate) => List.Where(predicate); | ||
|
|
||
| /// <summary> | ||
| /// Tries to get all <see cref="Workstation"/> instances that match the specified predicate. | ||
| /// </summary> | ||
| /// <param name="predicate">The predicate to filter workstations.</param> | ||
| /// <param name="workstations">The matching workstations, if any.</param> | ||
| /// <returns><c>true</c> if any workstations were found; otherwise, <c>false</c>.</returns> | ||
| public static bool TryGet(Func<Workstation, bool> predicate, out IEnumerable<Workstation> workstations) | ||
| { | ||
| workstations = Get(predicate); | ||
| return workstations.Any(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Determines whether the specified player is in range of the workstation. | ||
| /// </summary> | ||
| /// <param name="player">The player to check.</param> | ||
| /// <returns><c>true</c> if the player is in range; otherwise, <c>false</c>.</returns> | ||
| public bool IsInRange(Player player) => Base.IsInRange(player.ReferenceHub); | ||
|
|
||
| /// <summary> | ||
| /// Interacts with the workstation as the specified player. | ||
| /// </summary> | ||
| /// <param name="player">The player to interact as.</param> | ||
| public void Interact(Player player) => Base.ServerInteract(player.ReferenceHub, Base.ActivateCollider.ColliderId); | ||
|
|
||
| /// <summary> | ||
| /// Returns the Room in a human-readable format. | ||
| /// </summary> | ||
| /// <returns>A string containing Workstation-related data.</returns> | ||
| public override string ToString() => $"{GameObject.name} ({Zone}) [{Room}]"; | ||
| } | ||
| } | ||
39 changes: 39 additions & 0 deletions
39
EXILED/Exiled.Events/Patches/Generic/WorkstationListAdd.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="WorkstationListAdd.cs" company="ExMod Team"> | ||
| // Copyright (c) ExMod Team. All rights reserved. | ||
| // Licensed under the CC BY-SA 3.0 license. | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| namespace Exiled.Events.Patches.Generic | ||
| { | ||
| #pragma warning disable SA1313 | ||
| #pragma warning disable SA1402 | ||
|
|
||
| using HarmonyLib; | ||
| using InventorySystem.Items.Firearms.Attachments; | ||
|
|
||
| /// <summary> | ||
| /// Patch for adding <see cref="API.Features.Workstation"/> to list. | ||
| /// </summary> | ||
| [HarmonyPatch(typeof(WorkstationController), nameof(WorkstationController.Start))] | ||
| internal class WorkstationListAdd | ||
| { | ||
| private static void Postfix(WorkstationController __instance) | ||
| { | ||
| _ = new API.Features.Workstation(__instance); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Patch for removing <see cref="API.Features.Workstation"/> to list. | ||
| /// </summary> | ||
| [HarmonyPatch(typeof(WorkstationController), nameof(WorkstationController.OnDestroy))] | ||
| internal class WorkstationListRemove | ||
| { | ||
| private static void Postfix(WorkstationController __instance) | ||
| { | ||
| API.Features.Workstation.WorkstationControllerToWorkstation.Remove(__instance); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.