-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathPlugin.cs
More file actions
163 lines (140 loc) · 6.11 KB
/
Copy pathPlugin.cs
File metadata and controls
163 lines (140 loc) · 6.11 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (c) 2022-2026, David Karnok & Contributors
// Licensed under the Apache License, Version 2.0
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using SpaceCraft;
using System.Collections;
using UnityEngine;
using static UnityEngine.InputForUI.InputManagerProvider;
namespace UIShowGrabNMineCount
{
[BepInPlugin("akarnokd.theplanetcraftermods.uishowgrabnminecount", "(UI) Show Grab N Mine Count", PluginInfo.PLUGIN_VERSION)]
public class Plugin : BaseUnityPlugin
{
static ConfigEntry<bool> isEnabled;
static AccessTools.FieldRef<ActionMinable, bool> fActionMinableMining;
public void Awake()
{
LibCommon.BepInExLoggerFix.ApplyFix();
LibCommon.HarmonyIntegrityCheck.Check(typeof(Plugin));
LibCommon.GameVersionCheck.Patch(new Harmony(PluginInfo.PLUGIN_GUID + "_Ver"), PluginInfo.PLUGIN_NAME + " - v" + PluginInfo.PLUGIN_VERSION);
// Plugin startup logic
Logger.LogInfo($"Plugin is loaded!");
isEnabled = Config.Bind("General", "Enabled", true, "Is the visual notification enabled?");
fActionMinableMining = AccessTools.FieldRefAccess<ActionMinable, bool>("_mining");
Harmony.CreateAndPatchAll(typeof(Plugin));
}
[HarmonyPrefix]
[HarmonyPatch(typeof(ActionMinable), "FinishMining")]
static bool ActionMinable_FinishMining(float time,
PlayerMainController ____playerSource,
PlayerAnimations ____playerAnimations,
ItemWorldDislpayer ____itemWorldDisplayer,
ActionMinable __instance,
ref IEnumerator __result)
{
__result = ActionMinable_FinishMining_Override(time,
____playerSource, ____playerAnimations,
____itemWorldDisplayer, __instance);
return false;
}
static IEnumerator ActionMinable_FinishMining_Override(float time,
PlayerMainController ____playerSource,
PlayerAnimations ____playerAnimations,
ItemWorldDislpayer ____itemWorldDisplayer,
ActionMinable __instance)
{
yield return new WaitForSeconds(time);
__instance.StopAllCoroutines();
if (fActionMinableMining(__instance))
{
____playerSource.GetMultitool().GetMultiToolMine().StopMining(true);
____playerSource.GetPlayerShareState().StopMining();
____itemWorldDisplayer.Hide();
____playerAnimations.AnimateRecolt(false, -1f);
____playerSource.GetMultitool().SetState(DataConfig.MultiToolState.Null);
____playerSource.GetMultitool().SetState(DataConfig.MultiToolState.Build);
}
fActionMinableMining(__instance) = false;
// This also should fix two potential NPEs with quickly disappearing ores
var woa = __instance.GetComponent<WorldObjectAssociated>();
var wo = woa != null ? woa.GetWorldObject() : null;
if (wo != null)
{
var inv = ____playerSource.GetPlayerBackpack().GetInventory();
InventoriesHandler.Instance.AddWorldObjectToInventory(
wo,
inv,
grabbed: false,
success =>
{
if (success)
{
ShowInventoryAdded(wo, inv);
}
else
{
WorldObjectsHandler.Instance.DisableWorldObjectFromScene(wo, true, __instance.transform.position + new Vector3(0f, 1f, 0f));
WorldObjectsHandler.Instance.DestroyWorldObjectGOOnAllClients(wo.GetId(), 0f);
WorldObjectsHandler.Instance.DestroyWorldObject(wo.GetId(), false);
}
}
);
}
else
{
Destroy(__instance.gameObject);
}
}
[HarmonyPrefix]
[HarmonyPatch(typeof(ActionGrabable), "AddToInventory")]
static bool ActionGrabable_AddToInventory(
WorldObject worldObject,
PlayerMainController ____playerSource)
{
if (isEnabled.Value)
{
var inv = ____playerSource.GetPlayerBackpack().GetInventory();
// FIXME: grabbed: true ???
InventoriesHandler.Instance.AddWorldObjectToInventory(worldObject, inv,
grabbed: true, success =>
{
if (success)
{
____playerSource.GetPlayerAudio().PlayGrab();
Managers.GetManager<DisplayersHandler>()?.GetItemWorldDisplayer()?.Hide();
/*try
{
__instance.grabedEvent?.Invoke(worldObject);
__instance.grabedEvent = null;
}
finally
{*/
ShowInventoryAdded(worldObject, inv);
// }
}
});
return false;
}
return true;
}
static void ShowInventoryAdded(WorldObject worldObject, Inventory inventory)
{
int c = 0;
Group group = worldObject.GetGroup();
string gid = group.GetId();
foreach (WorldObject wo in inventory.GetInsideWorldObjects())
{
if (wo.GetGroup().GetId() == gid)
{
c++;
}
}
string text = Readable.GetGroupName(group) + " + 1 ( " + c + " )";
InformationsDisplayer informationsDisplayer = Managers.GetManager<DisplayersHandler>().GetInformationsDisplayer();
informationsDisplayer.AddInformation(2f, text, DataConfig.UiInformationsType.InInventory, group.GetImage());
}
}
}