-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandbookSpaceCrafting.cs
More file actions
343 lines (281 loc) · 9.88 KB
/
Copy pathHandbookSpaceCrafting.cs
File metadata and controls
343 lines (281 loc) · 9.88 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
339
340
341
342
343
using Vintagestory.API.Client;
using Vintagestory.API.Common;
using Vintagestory.API.Config;
namespace QuickCraft;
internal static class HandbookSpaceCrafting
{
private const int MaxCraftAllCycles = 512;
private const int OutputTransferAttempts = 16;
private const int OutputTransferRetryDelayMs = 25;
private static ICoreClientAPI? api;
private static bool craftAllRunning;
public static ICoreClientAPI? ClientApi => api;
public static void SetApi(ICoreClientAPI? clientApi)
{
api = clientApi;
}
public static bool TryFillRecipes(GridRecipe[] recipes, bool max)
{
ICoreClientAPI? capi = api;
if (capi == null)
{
return false;
}
if (!CanUseCraftingActions(capi))
{
return true;
}
if (!HasCraftingGrid(capi, out _))
{
return true;
}
CraftFillResult result = RecipeGridFiller.TryFill(capi, recipes, max);
return HandleFillResult(capi, result);
}
public static bool TryCraftRecipes(GridRecipe[] recipes)
{
ICoreClientAPI? capi = api;
if (capi == null)
{
return false;
}
if (!CanUseCraftingActions(capi))
{
return true;
}
if (!HasCraftingGrid(capi, out IInventory? craftingGrid))
{
return true;
}
CraftFillResult fillResult = RecipeGridFiller.TryFillMax(capi, recipes);
if (fillResult == CraftFillResult.Success || fillResult == CraftFillResult.AlreadyFull)
{
RequestOutputCraftAfterFill(capi, fillResult);
return true;
}
return HandleFillResult(capi, fillResult);
}
public static bool TryCraftAllRecipes(GridRecipe[] recipes)
{
ICoreClientAPI? capi = api;
if (capi == null)
{
return false;
}
if (craftAllRunning)
{
return true;
}
if (!CanUseCraftingActions(capi))
{
return true;
}
if (!HasCraftingGrid(capi, out _))
{
return true;
}
craftAllRunning = true;
try
{
return CraftAllImmediate(capi, recipes);
}
finally
{
craftAllRunning = false;
}
}
public static bool CanUseCraftingActions(ICoreClientAPI capi)
{
IInventory? craftingGrid = capi.World.Player.InventoryManager.GetOwnInventory("craftinggrid");
return craftingGrid != null && craftingGrid.Count >= 10;
}
private static bool HasCraftingGrid(ICoreClientAPI capi, out IInventory? craftingGrid)
{
craftingGrid = capi.World.Player.InventoryManager.GetOwnInventory("craftinggrid");
if (craftingGrid != null && craftingGrid.Count >= 10)
{
return true;
}
capi.Gui.PlaySound("menubutton_wood", false, 1f);
capi.TriggerIngameError(typeof(HandbookSpaceCrafting), "quickcraft-nogrid", Lang.Get(ModIds.ModId + ":nocraftinggrid"));
return false;
}
private static bool HandleFillResult(ICoreClientAPI capi, CraftFillResult result)
{
switch (result)
{
case CraftFillResult.Success:
capi.Gui.PlaySound("menubutton_press", false, 1f);
return true;
case CraftFillResult.AlreadyFull:
return true;
default:
capi.Gui.PlaySound("menubutton_wood", false, 1f);
capi.TriggerIngameError(typeof(HandbookSpaceCrafting), "quickcraft-missing", Lang.Get(ModIds.ModId + ":cannotfill"));
return false;
}
}
private static bool TryTransferOutput(ICoreClientAPI capi, IInventory craftingGrid, bool requireOutputReady = false)
{
RefreshCraftingOutput(craftingGrid);
ItemSlot outputSlot = craftingGrid[9];
if (requireOutputReady && (outputSlot == null || outputSlot.Empty || outputSlot.StackSize <= 0))
{
return false;
}
IClientPlayer player = capi.World.Player;
IPlayerInventoryManager manager = player.InventoryManager;
if (!manager.MouseItemSlot.Empty)
{
return false;
}
ItemStackMoveOperation op = new(capi.World, EnumMouseButton.Left, EnumModifierKey.SHIFT, EnumMergePriority.AutoMerge, outputSlot.StackSize)
{
ActingPlayer = player
};
if (craftingGrid is not InventoryBase inventoryBase)
{
return false;
}
object? packet = inventoryBase.ActivateSlot(9, manager.MouseItemSlot, ref op);
SendPacket(capi, packet);
return packet != null || op.MovedQuantity > 0;
}
private static void TransferOutputWithRetries(ICoreClientAPI capi, IInventory craftingGrid, int attemptsLeft, Action<bool> completed)
{
if (!CanUseCraftingActions(capi))
{
completed(false);
return;
}
if (TryTransferOutput(capi, craftingGrid))
{
completed(true);
return;
}
if (attemptsLeft <= 0)
{
completed(false);
return;
}
RegisterMenuCallback(capi, _ => TransferOutputWithRetries(capi, craftingGrid, attemptsLeft - 1, completed), OutputTransferRetryDelayMs);
}
private static void SendPacket(ICoreClientAPI capi, object? packet)
{
InventoryPacketPatcher.Send(capi, packet);
}
private static void RequestOutputCraftAfterFill(ICoreClientAPI capi, CraftFillResult fillResult)
{
capi.Gui.PlaySound("menubutton_press", false, 1f);
RequestOutputCraftNow(capi, showError: true);
}
private static bool RequestOutputCraftNow(ICoreClientAPI capi, bool showError)
{
if (!CanUseCraftingActions(capi))
{
return false;
}
if (!HasCraftingGrid(capi, out IInventory? craftingGrid))
{
return false;
}
TransferOutputWithRetries(capi, craftingGrid!, OutputTransferAttempts, moved =>
{
if (!moved && showError)
{
capi.TriggerIngameError(typeof(HandbookSpaceCrafting), "quickcraft-outputblocked", Lang.Get(ModIds.ModId + ":outputblocked"));
}
});
return true;
}
private static bool CraftAllImmediate(ICoreClientAPI capi, GridRecipe[] recipes)
{
bool craftedAny = false;
for (int cycles = 0; cycles < MaxCraftAllCycles; cycles++)
{
if (!CanUseCraftingActions(capi))
{
return true;
}
if (!HasCraftingGrid(capi, out IInventory? craftingGrid))
{
return true;
}
CraftFillResult fillResult = RecipeGridFiller.TryFillMax(capi, recipes);
if (fillResult != CraftFillResult.Success && fillResult != CraftFillResult.AlreadyFull)
{
if (!craftedAny)
{
HandleFillResult(capi, fillResult);
}
return craftedAny;
}
string inputSignatureBeforeCraft = GetInputSignature(craftingGrid!);
if (!TryTransferReadyOutput(capi, craftingGrid!))
{
if (!craftedAny)
{
capi.TriggerIngameError(typeof(HandbookSpaceCrafting), "quickcraft-outputblocked", Lang.Get(ModIds.ModId + ":outputblocked"));
}
return craftedAny;
}
craftedAny = true;
if (GetInputSignature(craftingGrid!) == inputSignatureBeforeCraft)
{
return true;
}
}
capi.TriggerIngameError(typeof(HandbookSpaceCrafting), "quickcraft-craftalllimit", Lang.Get(ModIds.ModId + ":craftalllimit"));
return true;
}
private static bool TryTransferReadyOutput(ICoreClientAPI capi, IInventory craftingGrid)
{
RefreshCraftingOutput(craftingGrid);
ItemSlot outputSlot = craftingGrid[9];
if (outputSlot == null || outputSlot.Empty || outputSlot.StackSize <= 0)
{
return false;
}
IClientPlayer player = capi.World.Player;
IPlayerInventoryManager manager = player.InventoryManager;
if (!manager.MouseItemSlot.Empty || craftingGrid is not InventoryBase inventoryBase)
{
return false;
}
ItemStackMoveOperation op = new(capi.World, EnumMouseButton.Left, EnumModifierKey.SHIFT, EnumMergePriority.AutoMerge, outputSlot.StackSize)
{
ActingPlayer = player
};
object? packet = inventoryBase.ActivateSlot(9, manager.MouseItemSlot, ref op);
SendPacket(capi, packet);
return op.MovedQuantity > 0;
}
private static long RegisterMenuCallback(ICoreClientAPI capi, Action<float> callback, int delayMs)
{
return capi.Event.RegisterCallback(callback, delayMs, permittedWhilePaused: true);
}
private static void RefreshCraftingOutput(IInventory craftingGrid)
{
try
{
craftingGrid.GetType()
.GetMethod("FindMatchingRecipe", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic)
?.Invoke(craftingGrid, null);
}
catch
{
// The server will still validate and craft from the activate-slot packet.
}
}
private static string GetInputSignature(IInventory craftingGrid)
{
string[] parts = new string[9];
for (int i = 0; i < parts.Length; i++)
{
ItemSlot slot = craftingGrid[i];
ItemStack? stack = slot?.Itemstack;
parts[i] = stack?.Collectible?.Code == null ? "-" : stack.Collectible.Code + ":" + stack.StackSize;
}
return string.Join("|", parts);
}
}