-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathPlugin_CraftManager.cs
More file actions
162 lines (141 loc) · 7.17 KB
/
Copy pathPlugin_CraftManager.cs
File metadata and controls
162 lines (141 loc) · 7.17 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
// Copyright (c) 2022-2026, David Karnok & Contributors
// Licensed under the Apache License, Version 2.0
using HarmonyLib;
using SpaceCraft;
using System.Collections.Generic;
using UnityEngine;
namespace CheatInventoryStacking
{
public partial class Plugin
{
/// <summary>
/// Vanilla creates the object in the player's backpack
/// or replaces the equipment inplace.
/// It calls Inventory::IsFull 0 to 2 times so we can't sneak in the groupId.
/// </summary>
[HarmonyPrefix]
[HarmonyPatch(typeof(CraftManager), nameof(CraftManager.TryToCraftInInventory))]
static bool Patch_CraftManager_TryToCraftInInventory(
GroupItem groupItem,
PlayerMainController playerController,
ActionCrafter sourceCrafter,
ref bool ____crafting,
int ____tempSpaceInInventory,
ref bool __result
)
{
if (stackSize.Value > 1 && !apiTryToCraftInInventoryHandled())
{
var ingredients = groupItem.GetRecipe().GetIngredientsGroupInRecipe();
var backpack = playerController.GetPlayerBackpack();
var backpackInventory = backpack.GetInventory();
var equipment = playerController.GetPlayerEquipment();
var equipmentInventory = equipment.GetInventory();
var isFreeCraft = Managers.GetManager<GameSettingsHandler>().GetCurrentGameSettings().GetFreeCraft();
var useFromEquipment = new List<Group>();
// Should allow Craft From Containers to work on its own by overriding ItemsContainsStatus.
var availableBackpack = backpackInventory.ItemsContainsStatus(ingredients);
// due to Craft From Containers tricking ItemsContainsStatus
// we need to double check which ingredient would be taken from the backpack
// so that the fullness check can work correctly.
var toRemoveFromBackpack = new HashSet<int>();
var ingredientsCopy = new List<Group>(ingredients);
var backpackContents = fInventoryWorldObjectsInInventory(backpackInventory);
foreach (var wo in backpackContents)
{
for (int i = 0; i < ingredientsCopy.Count; i++)
{
Group ingr = ingredientsCopy[i];
if (wo.GetGroup() == ingr)
{
toRemoveFromBackpack.Add(wo.GetId());
ingredientsCopy.RemoveAt(i);
break;
}
}
if (ingredientsCopy.Count == 0)
{
break;
}
}
if (availableBackpack.Contains(item: false))
{
var availableEquipment = equipmentInventory.ItemsContainsStatus(ingredients);
for (int i = 0; i < availableEquipment.Count; i++)
{
if (!availableBackpack[i] && availableEquipment[i])
{
availableBackpack[i] = true;
useFromEquipment.Add(ingredients[i]);
}
}
}
if (!availableBackpack.Contains(item: false) || isFreeCraft)
{
if (IsFullStackedWithRemoveOfInventory(backpackInventory, toRemoveFromBackpack, groupItem.id))
{
Managers.GetManager<BaseHudHandler>().DisplayCursorText("UI_InventoryFull", 2f);
__result = false;
return false;
}
____crafting = true;
sourceCrafter?.CraftAnimation(groupItem);
if (useFromEquipment.Count != 0)
{
InventoriesHandler.Instance.SetInventorySize(equipmentInventory, ____tempSpaceInInventory, Vector3.zero);
InventoriesHandler.Instance.SetInventorySize(backpackInventory, ____tempSpaceInInventory, Vector3.zero);
}
InventoriesHandler.Instance.RemoveItemsFromInventory(ingredients, backpackInventory, true, true);
equipment.DestroyItemsFromEquipment(useFromEquipment, success =>
{
InventoriesHandler.Instance.AddItemToInventory(groupItem, backpackInventory, (success, id) =>
{
fCraftManagerCrafting(null) = false;
if (!success)
{
if (id > 0)
{
WorldObjectsHandler.Instance.DestroyWorldObject(id);
}
RestoreInventorySizes(backpackInventory, equipment,
-____tempSpaceInInventory, useFromEquipment.Count != 0);
}
else
{
if (useFromEquipment.Count != 0 && groupItem.GetEquipableType() != DataConfig.EquipableType.Null)
{
equipment.WatchForModifications(enabled: true);
var wo = WorldObjectsHandler.Instance.GetWorldObjectViaId(id);
InventoriesHandler.Instance.TransferItem(backpackInventory, equipmentInventory,
wo, _ => RestoreInventorySizes(backpackInventory, equipment,
-____tempSpaceInInventory, useFromEquipment.Count != 0)
);
}
else
{
RestoreInventorySizes(backpackInventory, equipment,
-____tempSpaceInInventory, useFromEquipment.Count != 0);
}
}
});
});
WorldObjectsHandler.Instance.AddOneToTotalCraft();
__result = true;
return false;
}
__result = false;
return false;
}
return true;
}
static void RestoreInventorySizes(Inventory backpack, PlayerEquipment equipment, int delta, bool equipmentUsed)
{
if (equipmentUsed)
{
equipment.WatchForModifications(false);
InventoriesHandler.Instance.SetInventorySize(equipment.GetInventory(), delta, Vector3.zero);
InventoriesHandler.Instance.SetInventorySize(backpack, delta, Vector3.zero);
}
}
}
}