Repository navigation
Expand file tree
/
Copy pathProxiStackManager.cs
More file actions
338 lines (292 loc) · 12.5 KB
/
Copy pathProxiStackManager.cs
File metadata and controls
338 lines (292 loc) · 12.5 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
using System;
using System.Collections.Generic;
using UnityEngine;
namespace ProxiStack
{
/// <summary>
/// ProxiStack-Manager (Stash & Restock) – v3.0 kompatibel.
/// </summary>
public static class ProxiStackManager
{
public static XUiC_BackpackWindow BackpackWindow { get; private set; }
public static XUiC_Backpack Backpack { get; private set; }
public static XUiC_ContainerStandardControls ContainerControls { get; private set; }
private static float[] lastClickTimes = new float[2];
public static void InitializeUI(XUiC_BackpackWindow window)
{
try
{
BackpackWindow = window;
ContainerControls = window.GetChildByType<XUiC_ContainerStandardControls>();
Backpack = BackpackWindow?.backpackGrid;
lastClickTimes[0] = 0f;
lastClickTimes[1] = 0f;
Log.Out("[ProxiStack] Stash UI initialized");
}
catch (Exception ex)
{
Log.Warning($"[ProxiStack] UI init error: {ex.Message}");
}
}
/// <summary>
/// Sucht einen Button ausgehend vom Backpack-Fenster (rekursiv) und nur als
/// Fallback unter <see cref="ContainerControls"/>. Nötig, weil
/// <c>XUiController.GetChildById</c> ausschließlich den eigenen Teilbaum
/// durchsucht: seit 1.1.9 hängen btnMoveStash/btnMoveRestock in
/// <c>rect[@name='header']</c> und damit außerhalb von ContainerStandardControls.
/// </summary>
public static XUiController FindButton(string id)
{
var root = (XUiController)BackpackWindow;
if (root != null)
{
var found = root.GetChildById(id);
if (found != null) return found;
}
return ContainerControls?.GetChildById(id);
}
public static bool HasMoveDestinationOpen(XUiC_BackpackWindow backpackWindow = null)
{
try
{
XUiC_BackpackWindow window = backpackWindow ?? BackpackWindow;
if (window == null) return false;
IInventory moveDestination;
return window.TryGetMoveDestinationInventory(out moveDestination);
}
catch { return false; }
}
private static XUiM_LootContainer.EItemMoveKind GetMoveKind(int typeIndex)
{
float now = Time.unscaledTime;
float last = lastClickTimes[typeIndex];
lastClickTimes[typeIndex] = now;
return (now - last < 2.0f)
? XUiM_LootContainer.EItemMoveKind.FillAndCreate
: XUiM_LootContainer.EItemMoveKind.FillOnly;
}
// ============================================================
// STASH (EINSORTIEREN)
// ============================================================
public static void DoStash()
{
try
{
if (HasMoveDestinationOpen()) return;
if (!ConnectionManager.Instance.IsSinglePlayer && !ConnectionManager.Instance.IsServer)
{
var pkg = NetPackageManager.GetPackage<NetPackageProxiStack>()
.Setup(GameManager.Instance.World.GetPrimaryPlayerId(), ProxiStackType.Stack);
ConnectionManager.Instance.SendToServer(pkg);
return;
}
PerformStashLocal();
}
catch (Exception ex)
{
Log.Warning($"[ProxiStack] DoStash: {ex.Message}");
}
}
public static void PerformStashLocal()
{
try
{
if (Backpack == null || BackpackWindow == null) return;
var moveKind = GetMoveKind(0);
var player = GameManager.Instance.World.GetPrimaryPlayer();
if (player == null) return;
var config = ProxiStackMod.Instance.Config;
var containers = ContainerHelper.FindNearbyContainersStash(
new Vector3i(player.position), config.StashDistance);
foreach (var result in containers)
{
IInventory inv = result.Storage as IInventory;
if (inv == null) continue;
XUiM_LootContainer.StashItems(
BackpackWindow, Backpack, inv,
0, ContainerControls?.LockedSlots ?? new PackedBoolArray(0),
moveKind, ContainerControls?.MoveStartBottomRight ?? false);
result.TileEntity.SetModified();
}
}
catch (Exception ex)
{
Log.Warning($"[ProxiStack] Stash local error: {ex.Message}");
}
}
public static void PerformStashClient(Vector3i center, IEnumerable<Vector3i> offsets)
{
try
{
if (Backpack == null || HasMoveDestinationOpen()) return;
var moveKind = GetMoveKind(0);
foreach (var offset in offsets)
{
var result = ContainerHelper.GetLootableAtPosition(center + offset);
if (result.TileEntity == null || result.Storage == null) continue;
IInventory inv = result.Storage as IInventory;
if (inv == null) continue;
XUiM_LootContainer.StashItems(
BackpackWindow, Backpack, inv,
0, ContainerControls?.LockedSlots ?? new PackedBoolArray(0),
moveKind, ContainerControls?.MoveStartBottomRight ?? false);
result.TileEntity.SetModified();
}
}
catch (Exception ex)
{
Log.Warning($"[ProxiStack] Stash client error: {ex.Message}");
}
}
// ============================================================
// RESTOCK (AUFFÜLLEN)
// ============================================================
public static void DoRestock()
{
try
{
if (HasMoveDestinationOpen()) return;
if (!ConnectionManager.Instance.IsSinglePlayer && !ConnectionManager.Instance.IsServer)
{
var pkg = NetPackageManager.GetPackage<NetPackageProxiStack>()
.Setup(GameManager.Instance.World.GetPrimaryPlayerId(), ProxiStackType.Restock);
ConnectionManager.Instance.SendToServer(pkg);
return;
}
PerformRestockLocal();
}
catch (Exception ex)
{
Log.Warning($"[ProxiStack] DoRestock: {ex.Message}");
}
}
public static void PerformRestockLocal()
{
try
{
if (Backpack == null || BackpackWindow == null) return;
var player = GameManager.Instance.World.GetPrimaryPlayer();
if (player == null) return;
var config = ProxiStackMod.Instance.Config;
var containers = ContainerHelper.FindNearbyContainersStash(
new Vector3i(player.position), config.StashDistance);
foreach (var result in containers)
{
if (FillExistingBackpackStacks(player.bag, result.Items))
result.TileEntity.SetModified();
}
}
catch (Exception ex)
{
Log.Warning($"[ProxiStack] Restock local error: {ex.Message}");
}
}
public static void PerformRestockClient(Vector3i center, IEnumerable<Vector3i> offsets)
{
try
{
if (Backpack == null || HasMoveDestinationOpen()) return;
var player = GameManager.Instance.World.GetPrimaryPlayer();
if (player == null) return;
foreach (var offset in offsets)
{
var result = ContainerHelper.GetLootableAtPosition(center + offset);
if (result.TileEntity == null || result.Storage == null) continue;
if (FillExistingBackpackStacks(player.bag, result.Items))
result.TileEntity.SetModified();
}
}
catch (Exception ex)
{
Log.Warning($"[ProxiStack] Restock client error: {ex.Message}");
}
}
/// <summary>
/// Füllt ausschließlich bereits vorhandene, kompatible Rucksack-Stacks auf.
/// Leere Slots werden nie als Ziel verwendet, sodass Restock keine neuen
/// Stacks und keine zuvor nicht vorhandenen Gegenstände anlegen kann.
/// </summary>
private static bool FillExistingBackpackStacks(Bag bag, ItemStack[] sourceStacks)
{
if (bag == null || sourceStacks == null || sourceStacks.Length == 0)
return false;
// v3.3: Bag hat kein GetSlots() mehr — Slot-Array über Bag.ItemGrid.items.
ItemStack[] targetSlots = ContainerHelper.GetBagItems(bag);
if (targetSlots == null || targetSlots.Length == 0)
return false;
bool sourceModified = false;
for (int targetIndex = 0; targetIndex < targetSlots.Length; targetIndex++)
{
ItemStack targetStack = targetSlots[targetIndex];
if (targetStack == null || targetStack.IsEmpty() || targetStack.itemValue == null)
continue;
bool targetModified = false;
for (int sourceIndex = 0; sourceIndex < sourceStacks.Length; sourceIndex++)
{
ItemStack sourceStack = sourceStacks[sourceIndex];
if (sourceStack == null || sourceStack.IsEmpty() || sourceStack.itemValue == null)
continue;
if (!targetStack.itemValue.EqualsForMerging(sourceStack.itemValue))
continue;
int transferCount = targetStack.StackTransferCount(sourceStack);
if (transferCount <= 0)
continue;
targetStack.count += transferCount;
sourceStack.count -= transferCount;
if (sourceStack.count <= 0)
sourceStacks[sourceIndex] = ItemStack.Empty;
targetModified = true;
sourceModified = true;
}
if (targetModified)
bag.SetSlot(targetIndex, targetStack);
}
return sourceModified;
}
// ============================================================
// SLOT LOCK
// ============================================================
public static void ToggleSlotLock(XUiC_ItemStack itemStack)
{
try
{
if (itemStack == null) return;
itemStack.UserLockedSlot = !itemStack.UserLockedSlot;
BackpackWindow?.UpdateLockedSlots(ContainerControls);
itemStack.xui?.PlayMenuClickSound();
}
catch (Exception ex)
{
Log.Warning($"[ProxiStack] ToggleSlotLock: {ex.Message}");
}
}
public static void UpdateLockedSlotUI()
{
try
{
if (Backpack == null) return;
var config = ProxiStackMod.Instance.Config;
XUiController[] slots = Backpack.GetItemStackControllers();
for (int i = 0; i < slots.Length; i++)
{
var iconSlot = slots[i].GetChildById("iconSlotLock")?.ViewComponent as XUiV_Sprite;
if (iconSlot != null) iconSlot.Color = config.LockIconColor;
}
if (ContainerControls != null)
{
var lockBtn = ContainerControls.GetChildById("btnToggleLockMode")?.ViewComponent;
if (lockBtn != null) lockBtn.IsVisible = config.LockModeIconVisible;
}
}
catch (Exception ex)
{
Log.Warning($"[ProxiStack] UpdateLockedSlotUI: {ex.Message}");
}
}
}
public enum ProxiStackType : byte
{
Stack = 0,
Restock = 1
}
}