-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathQuickerStackPlugin.cs
More file actions
263 lines (239 loc) · 11.8 KB
/
QuickerStackPlugin.cs
File metadata and controls
263 lines (239 loc) · 11.8 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using System.Threading;
using static ItemDrop;
namespace QuickerStack
{
[BepInPlugin("org.bepinex.plugins.valheim.quicker_stack", "Quicker Stack", VERSION)]
public class QuickerStackPlugin : BaseUnityPlugin
{
private const string VERSION = "0.0.5";
public void Awake()
{
Logger.LogInfo(String.Format("Initializing Quicker Stack {0}.", VERSION));
if (File.Exists(this.ConfigPath))
{
QuickerStackPlugin.configFile = new ConfigFile(this.ConfigPath, true);
this.LoadConfig();
}
else
{
QuickerStackPlugin.configFile = base.Config;
this.LoadConfig();
}
Config.SettingChanged += OnSettingChanged;
Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), null);
}
private T BindParameter<T>(T param, string key, string description)
{
return QuickerStackPlugin.configFile.Bind<T>("QuickerStack", key, param, description).Value;
}
public static UserConfig GetPlayerConfig(long playerID)
{
UserConfig userConfig = null;
if (QuickerStackPlugin.playerConfigs.TryGetValue(playerID, out userConfig))
{
return userConfig;
}
userConfig = new UserConfig(playerID);
QuickerStackPlugin.playerConfigs[playerID] = userConfig;
return userConfig;
}
private void LoadConfig()
{
string value = this.BindParameter<string>(QuickStackKey.ToString(), nameof(QuickStackKey), "Get key codes here: https://docs.unity3d.com/ScriptReference/KeyCode.html");
try
{
QuickStackKey = (KeyCode)Enum.Parse(typeof(KeyCode), value, true);
}
catch (Exception)
{
base.Logger.LogError("Failed to parse QuickStackKey, using default");
}
value = this.BindParameter<string>(ExclusionKey.ToString(), nameof(ExclusionKey), "Key to press when clicking an item to exclude from sort. Get key codes here: https://docs.unity3d.com/ScriptReference/KeyCode.html");
try
{
ExclusionKey = (KeyCode)Enum.Parse(typeof(KeyCode), value, true);
}
catch (Exception)
{
base.Logger.LogError("Failed to parse ExclusionKey, using default");
}
value = this.BindParameter<string>(ExclusionKey2.ToString(), nameof(ExclusionKey2), "Key to press when clicking an item to exclude from sort. Get key codes here: https://docs.unity3d.com/ScriptReference/KeyCode.html");
try
{
ExclusionKey2 = (KeyCode)Enum.Parse(typeof(KeyCode), value, true);
}
catch (Exception)
{
base.Logger.LogError("Failed to parse ExclusionKey2, using default");
}
NearbyRange = this.BindParameter<float>(NearbyRange, nameof(NearbyRange), "How far from you is nearby, greater value = greater range");
IgnoreConsumable = this.BindParameter<bool>(IgnoreConsumable, nameof(IgnoreConsumable), "Whether to completely exclude consumables from quick stacking (food, potions).");
IgnoreAmmo = this.BindParameter<bool>(IgnoreAmmo, nameof(IgnoreAmmo), "Whether to completely exclude ammo from quick stacking (arrows)");
CoalesceTrophies = this.BindParameter<bool>(CoalesceTrophies, nameof(CoalesceTrophies), "Whether to put all types of trophies in the container if any trophy is found in that container.");
StackToCurrentContainer = this.BindParameter<bool>(StackToCurrentContainer, nameof(StackToCurrentContainer), "Whether to ignore the container that you are currently using or not.");
StackOnlyToCurrentContainer = this.BindParameter<bool>(StackOnlyToCurrentContainer, nameof(StackOnlyToCurrentContainer), "Whether to only stack to the currently open container (like QuickStack did), or also look at all nearby containers afterwards.");
UseThreading = this.BindParameter<bool>(UseThreading, nameof(UseThreading), "Whether to enable threading when stacking. (/!\\ Warning: Unstable)");
}
private void OnSettingChanged(object sender, SettingChangedEventArgs e)
{
LoadConfig();
}
public static List<Container> FindContainersInRange(Vector3 point, float range)
{
List<Container> list = new List<Container>();
foreach (Container container in QuickerStackPlugin.AllContainers)
{
if (!(container == null) && !(container.transform == null) && Vector3.Distance(point, container.transform.position) < range)
{
list.Add(container);
}
}
return list;
}
public static List<Container> FindNearbyContainers(Vector3 point)
{
return QuickerStackPlugin.FindContainersInRange(point, QuickerStackPlugin.NearbyRange);
}
internal static int StackItems(Player player, Inventory fromInventory, Inventory toInventory)
{
UserConfig playerConfig = QuickerStackPlugin.GetPlayerConfig(player.GetPlayerID());
List<ItemDrop.ItemData> list = new List<ItemDrop.ItemData>(fromInventory.GetAllItems());
List<ItemDrop.ItemData> list2 = new List<ItemDrop.ItemData>();
List<ItemDrop.ItemData> list3 = new List<ItemDrop.ItemData>();
toInventory.GetAllItems(ItemDrop.ItemData.ItemType.Trophie, list2);
bool flag = QuickerStackPlugin.CoalesceTrophies && list2.Count != 0;
int num = 0;
foreach (ItemDrop.ItemData itemData in list)
{
if (itemData.m_shared.m_maxStackSize != 1 &&
(!QuickerStackPlugin.IgnoreAmmo || itemData.m_shared.m_itemType != ItemDrop.ItemData.ItemType.Ammo) &&
(!QuickerStackPlugin.IgnoreConsumable || itemData.m_shared.m_itemType != ItemDrop.ItemData.ItemType.Consumable) &&
!playerConfig.IsMarked(itemData.m_gridPos) &&
!playerConfig.IsMarked(itemData.m_shared))
{
if (itemData.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Trophie && flag)
{
if (toInventory.AddItem(itemData))
{
fromInventory.RemoveItem(itemData);
num++;
}
else
{
list3.Add(itemData);
}
}
else if (toInventory.HaveItem(itemData.m_shared.m_name))
{
if (toInventory.AddItem(itemData))
{
player.Message(MessageHud.MessageType.Center, String.Format("Storing {0} in container", itemData.m_shared.m_name));
fromInventory.RemoveItem(itemData);
num++;
}
else
{
list3.Add(itemData);
}
}
}
}
foreach (ItemDrop.ItemData itemData2 in list3)
{
if (toInventory.AddItem(itemData2))
{
player.Message(MessageHud.MessageType.Center, String.Format("Storing {0} in container", itemData2.m_shared.m_name));
num++;
fromInventory.RemoveItem(itemData2);
}
}
toInventory.Changed();
fromInventory.Changed();
return num;
}
public static void ReportResult(Player player, int movedCount)
{
player.Message(
MessageHud.MessageType.Center,
(movedCount > 0) ? string.Format("Stacked {0} item{1}", movedCount, (movedCount == 1) ? "" : "s") : "Nothing to stack",
0,
null
);
}
private static void StackToMany(Player player, List<Container> containers, int initialReportCount = 0)
{
Inventory inventory = player.GetInventory();
int num = initialReportCount;
foreach (Container container in containers)
{
if ((!container.m_checkGuardStone ||
PrivateArea.CheckAccess(container.transform.position, 0f, true, false)) &&
container.CheckAccess(player.GetPlayerID()) &&
!container.IsInUse())
{
ZNetView nview = container.GetNView();
nview.ClaimOwnership();
ZDOMan.instance.ForceSendZDO(ZNet.instance.GetUID(), nview.GetZDO().m_uid);
container.SetInUse(true);
num += QuickerStackPlugin.StackItems(player, inventory, container.GetInventory());
container.SetInUse(false);
}
}
QuickerStackPlugin.ReportResult(player, num);
}
public static void DoQuickStack(Player player)
{
if (player.IsTeleporting())
return;
int movedCount = 0;
Container container = InventoryGui.instance.GetCurrentContainer();
if (StackToCurrentContainer && container != null)
{
movedCount = StackItems(player, player.GetInventory(), container.GetInventory());
if (StackOnlyToCurrentContainer)
{
ReportResult(player, movedCount);
return;
}
}
List<Container> containers = QuickerStackPlugin.FindNearbyContainers(player.transform.position);
if (containers.Count == 0)
return;
Debug.Log(String.Format("found {0} containers in range", containers.Count));
if (UseThreading)
{
Debug.Log("Use thread as your own risk !");
Thread stackThread = new Thread(() => QuickerStackPlugin.StackToMany(player, containers, movedCount));
stackThread.Start();
}
else
{
QuickerStackPlugin.StackToMany(player, containers, movedCount);
}
}
public static List<Container> AllContainers = new List<Container>();
private readonly string ConfigPath = Path.Combine(Paths.ConfigPath, "QuickerStack.cfg");
private static ConfigFile configFile = null;
public static bool IgnoreAmmo = true;
public static bool IgnoreConsumable = true;
internal static float NearbyRange = 15f;
public static KeyCode QuickStackKey = KeyCode.P;
public static bool CoalesceTrophies = true;
public static bool UseThreading = false;
public static bool StackToCurrentContainer = true;
public static bool StackOnlyToCurrentContainer = false;
public static KeyCode ExclusionKey = KeyCode.LeftAlt;
public static KeyCode ExclusionKey2 = KeyCode.RightAlt;
private static Dictionary<long, UserConfig> playerConfigs = new Dictionary<long, UserConfig>();
}
}