-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTameablePatches.cs
113 lines (103 loc) · 4.73 KB
/
TameablePatches.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using ChebsNecromancy.Minions;
using ChebsValheimLibrary.Minions;
using HarmonyLib;
// ReSharper disable InconsistentNaming
// ReSharper disable UnusedParameter.Local
// Harmony patching is very sensitive regarding parameter names. Everything in this region should be hand crafted
// and not touched by well-meaning but clueless IDE optimizations.
// eg.
// * __instance MUST be named with exactly two underscores.
// * ___m_drops MUST be named with exactly three underscores.
// * Unused parameters must be left there because they must match the method to override
// * All patch methods need to be static
//
// This is because all of this has a special meaning to Harmony.
namespace ChebsNecromancy.Patches
{
[HarmonyPatch(typeof(Tameable), "Interact")]
class TameablePatch1
{
[HarmonyPrefix]
static bool InteractPrefix(Humanoid user, bool hold, bool alt, Tameable __instance)
{
// Stop players that aren't the owner of a minion from interacting
// with it. Also call UndeadMinion wait/follow methods to
// properly update the ZDO with the waiting position.
if (__instance.TryGetComponent(out UndeadMinion undeadMinion)
&& user.TryGetComponent(out Player player))
{
if (!undeadMinion.BelongsToPlayer(player.GetPlayerName()))
{
user.Message(MessageHud.MessageType.Center, "$chebgonaz_notyourminion");
return false; // deny base method completion
}
if (!UndeadMinion.Commandable.Value)
{
return false; // deny base method completion
}
var currentStatus = undeadMinion.Status;
var nextStatus = currentStatus switch
{
ChebGonazMinion.State.Following => ChebGonazMinion.State.Waiting,
ChebGonazMinion.State.Waiting => ChebGonazMinion.State.Roaming,
_ => ChebGonazMinion.State.Following
};
// use the minion methods to ensure the ZDO is updated
if (nextStatus.Equals(ChebGonazMinion.State.Following))
{
user.Message(MessageHud.MessageType.Center, "$chebgonaz_following");
undeadMinion.Follow(player.gameObject);
return false; // deny base method completion
}
if (nextStatus.Equals(ChebGonazMinion.State.Waiting))
{
user.Message(MessageHud.MessageType.Center, "$chebgonaz_waiting");
undeadMinion.Wait(player.transform.position);
return false; // deny base method completion
}
user.Message(MessageHud.MessageType.Center, "$chebgonaz_roaming");
undeadMinion.Roam();
return false; // deny base method completion
}
return true; // permit base method to complete
}
}
[HarmonyPatch(typeof(Tameable))]
class TameablePatch2
{
[HarmonyPatch(nameof(Tameable.GetHoverText))]
[HarmonyPostfix]
static void Postfix(Tameable __instance, ref string __result)
{
if (__instance.m_nview.IsValid()
&& __instance.m_commandable
&& __instance.TryGetComponent(out UndeadMinion undeadMinion)
&& Player.m_localPlayer != null)
{
__result = undeadMinion.Status switch
{
ChebGonazMinion.State.Following => Localization.instance.Localize("$chebgonaz_wait"),
ChebGonazMinion.State.Waiting => Localization.instance.Localize("$chebgonaz_roam"),
_ => Localization.instance.Localize("$chebgonaz_follow")
};
}
}
}
[HarmonyPatch(typeof(Tameable))]
class TameablePatch3
{
[HarmonyPatch(nameof(Tameable.GetHoverName))]
[HarmonyPostfix]
static void Postfix(Tameable __instance, ref string __result)
{
if (__instance.m_nview.IsValid()
&& __instance.TryGetComponent(out UndeadMinion undeadMinion))
{
__result = $@"{Localization.instance.Localize("$chebgonaz_owner")}: {undeadMinion.UndeadMinionMaster} ({undeadMinion.Status switch {
ChebGonazMinion.State.Following => Localization.instance.Localize("$chebgonaz_minionstatus_following"),
ChebGonazMinion.State.Roaming => Localization.instance.Localize("$chebgonaz_minionstatus_roaming"),
_ => Localization.instance.Localize("$chebgonaz_minionstatus_waiting") }})";
}
}
}
}