Secure way to use multiple objects as combined item source
Need a PluginManager solution for the following code. The idea is to have a list of items as well as a list of objects.
If the objects inventories in combination can fullfill the item lists request, then those items are removed from the inventories (on request) and true is returned. If the request cannot be fullfilled, then false is returned and no items are removed.
This check might take a while, so the object access has to be protected against timing attacks where people remove items from the objects in the right moment.
params["_itemList", "_sourcesList", ["_removeItems", false]];
private _originalSourcesCargo = [];
private _newSourcesCargo = [];
// backup cargo of source objects
{
private _crate = _x;
private _cargo = magazinesAmmoCargo _crate;
if (_removeItems) then {
clearMagazineCargoGlobal _crate;
};
_originalSourcesCargo pushBack _cargo;
} forEach _sourcesList;
{
// only procede if _itemList is not empty
if !(_itemList isEqualTo []) then {
private _cargo = _x;
private _newCargo = [];
{
private _mag = toLower(_cargo select 0);
private _count = _cargo select 1;
private _index = _itemList find _mag;
if(_index != -1) then {
_itemList deleteAt _index;
} else {
_newCargo pushback (_cargo);
};
} forEach _cargo;
_newSourcesCargo pushback (_newCargo);
} else {
_newSourcesCargo pushback (_x);
};
} forEach _originalSourcesCargo;
// readd removed items
if (_removeItems) then {
// if items are missing readd all items
if !(_itemList isEqualTo []) then {
_newSourcesCargo = _originalSourcesCargo;
};
{
private _crate = _x;
private _cargo = _newSourcesCargo select _forEachIndex;
{
_crate addMagazineAmmoCargo _x;
} forEach _cargo;
} forEach _sourcesList;
};
_itemList isEqualTo []
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information