-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.cs
85 lines (76 loc) · 3.43 KB
/
Plugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using BepInEx;
using BepInEx.Logging;
using BepInEx.Configuration;
using HarmonyLib;
namespace TermMacros
{
[BepInPlugin("me.kdcf.termspeak", "TermSpeak", "1.1.0")]
[BepInProcess("Lethal Company.exe")]
[HarmonyPatch(typeof(Terminal))]
public class TermSpeak : BaseUnityPlugin
{
private static ConfigEntry<bool> needWalkieInHand;
private Harmony _harmony = new Harmony("me.kdcf.termspeak");
private static ManualLogSource _log = BepInEx.Logging.Logger.CreateLogSource("TermSpeak");
private void Awake()
{
// Plugin startup logic
needWalkieInHand = Config.Bind("General", "needWalkieInHand", false, "If set to 'true', you must have the walkie in your hand to activate it. If set to 'false', it only needs to be in your inventory.");
Logger.LogInfo($"Plugin me.kdcf.termspeak is loaded!");
_harmony.PatchAll(typeof(TermSpeak));
}
[HarmonyPatch(nameof(Terminal.BeginUsingTerminal))]
[HarmonyPostfix]
public static void OpenTerminal(Terminal __instance)
{
SetWalkieMode(true);
}
[HarmonyPatch(nameof(Terminal.QuitTerminal))]
[HarmonyPostfix]
public static void CloseTerminal(Terminal __instance)
{
SetWalkieMode(false);
}
public static void SetWalkieMode(bool enabled)
{
_log.LogInfo("Terminal opened! Trying to find walkie talkie...");
var player = GameNetworkManager.Instance.localPlayerController;
_log.LogInfo($"There are {player.ItemSlots.Length} item slots to check.");
GrabbableObject walkieTalkie = null;
if (!needWalkieInHand.Value)
{
// We don't need the walkie talkie in our hand, check if it's in our inventory
for (int i = 0; i < player.ItemSlots.Length; i++)
{
// isBeingUsed on the WalkieTalkie represents if it's powered or not.
// It doesn't represent if it's being held or not, which is what we want.
// We don't care if it's powered or not when trying to disable it, just in case
// it's somehow unpowered while the user is holding it.
if (player.ItemSlots[i] is WalkieTalkie && (!enabled || player.ItemSlots[i].isBeingUsed))
{
walkieTalkie = player.ItemSlots[i];
break;
}
}
}
else
{
_log.LogInfo("Terminal opened! Checking for walkie talkie...");
// We need in our hand, just check if it's in our hand
if (player.ItemSlots[player.currentItemSlot] is WalkieTalkie && (!enabled || player.ItemSlots[player.currentItemSlot].isBeingUsed))
{
walkieTalkie = player.ItemSlots[player.currentItemSlot];
}
}
if (walkieTalkie == null)
{
_log.LogInfo("No walkie talkie found!");
}
else
{
_log.LogInfo("Found walkie talkie! Trying to set its mode...");
walkieTalkie.UseItemOnClient(enabled); // Start the walkie talkie
}
}
}
}