-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSkipSleep.cs
92 lines (79 loc) · 3.21 KB
/
SkipSleep.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
86
87
88
89
90
91
92
using System.Collections.Generic;
using System.Linq;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using System.IO;
using System;
namespace ModSkipSleepValheim
{
[BepInPlugin("com.rinsev.skipsleep", "ModSkipSleepValheim", "1.0.5")]
[HarmonyPatch]
public class Mod : BaseUnityPlugin
{
private static readonly Harmony harmony = new(typeof(Mod).GetCustomAttributes(typeof(BepInPlugin), false)
.Cast<BepInPlugin>()
.First()
.GUID);
private static ConfigFile configFile = new ConfigFile(Path.Combine(Paths.ConfigPath, "SkipSleep.cfg"), true);
private static ConfigEntry<double> ratio = configFile.Bind("General", "ratio", 0.5, "Threshold of ratio of players that need to be sleeping, must be > 0");
private static ConfigEntry<bool> message = configFile.Bind("General", "showMessage", true, "Show a continuous message of the amount of players currently sleeping (if > 0)");
private void Awake()
{
harmony.PatchAll();
}
private void OnDestroy()
{
harmony.UnpatchSelf();
}
[HarmonyPrefix]
[HarmonyPatch(typeof(Game), "EverybodyIsTryingToSleep")]
static bool SkipSleep(ref Game __instance, ref bool __result)
{
// Amount of players in bed
int count = 0;
// Get all players
List<ZDO> allCharacterZdos = ZNet.instance.GetAllCharacterZDOS();
// Return false if none
if (allCharacterZdos.Count == 0)
{
__result = false;
return false;
}
// Count number of players in bed
foreach (ZDO zdo in allCharacterZdos)
{
if (zdo.GetBool("inBed"))
count++;
}
// Calculate current ratio of people sleeping
double sleepRatio = (double)count / allCharacterZdos.Count;
// If showMessage is true
if (message.Value)
{
// If people are sleeping
if (count >= 1)
{
foreach (ZDO zdo in allCharacterZdos)
{
// Send message to everyone at everyone's position
ZRoutedRpc.instance.InvokeRoutedRPC(ZRoutedRpc.Everybody, "ChatMessage", zdo.GetPosition(), 2, "SkipSleep", $"{count}/{allCharacterZdos.Count} sleeping ({Math.Round(sleepRatio * 100)} %)");
}
}
}
//UnityEngine.Debug.Log($"Players sleeping: {count}");
//UnityEngine.Debug.Log($"Ratio of players sleeping: {sleepRatio}");
//UnityEngine.Debug.Log($"Threshold needed: {ratio.Value}");
// If the ratio of the amount of players sleeping vs awake reaches the threshold, return true to sleep
if (sleepRatio >= ratio.Value)
{
UnityEngine.Debug.Log($"SkipSleep: Threshold of {ratio.Value} reached, sleeping...");
__result = true;
return false;
}
// Otherwise, result false
__result = false;
return false;
}
}
}