Automatically merge stacks (money, ammo, etc.) when quickly transferring items to containers not marked with a @loot tag

This commit is contained in:
CactusPie 2024-01-14 20:28:59 +01:00
parent 1bee34fc26
commit 545725d380
2 changed files with 102 additions and 30 deletions

View File

@ -13,6 +13,8 @@ namespace CactusPie.ContainerQuickLoot
internal static ConfigEntry<bool> AutoMergeStacks { get; set; }
internal static ConfigEntry<bool> AutoMergeStacksForNonLootContainers { get; set; }
[UsedImplicitly]
internal void Start()
{
@ -51,6 +53,18 @@ namespace CactusPie.ContainerQuickLoot
)
);
AutoMergeStacksForNonLootContainers = Config.Bind
(
sectionName,
"Merge stacks for non-loot containers",
true,
new ConfigDescription
(
"Automatically merge stacks (money, ammo, etc.) when quickly transferring items to " +
"containers not marked with a @loot tag"
)
);
new QuickTransferPatch().Enable();
}
}

View File

@ -30,6 +30,8 @@ namespace CactusPie.ContainerQuickLoot
GClass2585.EMoveItemOrder order,
bool simulate)
{
Inventory inventory;
// If is ctrl+click loot
if (order == GClass2585.EMoveItemOrder.MoveToAnotherSide)
{
@ -38,24 +40,20 @@ namespace CactusPie.ContainerQuickLoot
return true;
}
}
// If is loose loot pick up
else if (order == GClass2585.EMoveItemOrder.PickUp && controller.OwnerType == EOwnerType.Profile)
{
if (!ContainerQuickLootPlugin.EnableForLooseLoot.Value)
{
return true;
}
}
else
if (!TryGetInventory(out inventory))
{
return true;
}
GameWorld gameWorld = Singleton<GameWorld>.Instance;
// If gameWorld is null that means the game is currently not in progress, for instance you're in your hideout
if (gameWorld == null)
return !TryMergeItemIntoAnExistingStack(item, inventory, controller, simulate, ref __result);
}
}
else
{
return true;
}
@ -66,10 +64,7 @@ namespace CactusPie.ContainerQuickLoot
return true;
}
Player player = GetLocalPlayerFromWorld(gameWorld);
var inventory = (Inventory)typeof(Player).GetProperty("Inventory", BindingFlags.NonPublic | BindingFlags.Instance)?.GetValue(player);
if (inventory == null)
if (!TryGetInventory(out inventory))
{
return true;
}
@ -80,7 +75,7 @@ namespace CactusPie.ContainerQuickLoot
{
if (!(collectionContainer is GClass2318 container))
{
return true;
return !TryMergeItemIntoAnExistingStack(item, inventory, controller, simulate, ref __result);
}
// ReSharper disable once PossibleMultipleEnumeration
@ -109,7 +104,6 @@ namespace CactusPie.ContainerQuickLoot
}
}
GClass2580 location = container.FindLocationForItem(item);
if (location == null)
{
@ -130,6 +124,31 @@ namespace CactusPie.ContainerQuickLoot
return false;
}
return !TryMergeItemIntoAnExistingStack(item, inventory, controller, simulate, ref __result);
}
private static bool TryGetInventory(out Inventory inventory)
{
GameWorld gameWorld = Singleton<GameWorld>.Instance;
// If gameWorld is null that means the game is currently not in progress, for instance you're in your hideout
if (gameWorld == null)
{
inventory = null;
return false;
}
Player player = GetLocalPlayerFromWorld(gameWorld);
inventory = (Inventory)typeof(Player)
.GetProperty("Inventory", BindingFlags.NonPublic | BindingFlags.Instance)
?.GetValue(player);
if (inventory == null)
{
return false;
}
return true;
}
@ -184,6 +203,45 @@ namespace CactusPie.ContainerQuickLoot
return result;
}
// If there are not matching @loot containers found, we will try to merge the item into an existing stack
// anyway - but only if this behavior is enabled in the config
private static bool TryMergeItemIntoAnExistingStack(
Item item,
Inventory inventory,
TraderControllerClass controller,
bool simulate,
ref GStruct375<GInterface275> result)
{
if (!ContainerQuickLootPlugin.AutoMergeStacksForNonLootContainers.Value)
{
return false;
}
if (item.Template.StackMaxSize <= 1)
{
return false;
}
foreach (Item targetItem in inventory.Equipment.GetAllItems())
{
if (targetItem.Template._id != item.Template._id)
{
continue;
}
if (targetItem.StackObjectsCount + item.StackObjectsCount > item.Template.StackMaxSize)
{
continue;
}
GStruct375<GClass2599> mergeResult = GClass2585.Merge(item, targetItem, controller, simulate);
result = new GStruct375<GInterface275>(mergeResult.Value);
return true;
}
return false;
}
private static Player GetLocalPlayerFromWorld(GameWorld gameWorld)
{
if (gameWorld == null || gameWorld.MainPlayer == null)