Storage bugfixes. (#17011)

This commit is contained in:
Pieter-Jan Briers
2023-06-02 03:13:56 +02:00
committed by GitHub
parent f1614cd28c
commit 4be515b8e2
2 changed files with 57 additions and 7 deletions

View File

@@ -173,7 +173,10 @@ namespace Content.Server.Storage.EntitySystems
if (HasComp<PlaceableSurfaceComponent>(uid)) if (HasComp<PlaceableSurfaceComponent>(uid))
return; return;
if (PlayerInsertHeldEntity(uid, args.User, storageComp)) PlayerInsertHeldEntity(uid, args.User, storageComp);
// Always handle it, even if insertion fails.
// We don't want to trigger any AfterInteract logic here.
// Example bug: placing wires if item doesn't fit in backpack.
args.Handled = true; args.Handled = true;
} }
@@ -517,12 +520,20 @@ namespace Content.Server.Storage.EntitySystems
return false; return false;
} }
if (!HasComp<StackComponent>(insertEnt) && TryComp(insertEnt, out ItemComponent? itemComp) && if (TryComp(insertEnt, out ItemComponent? itemComp) &&
itemComp.Size > storageComp.StorageCapacityMax - storageComp.StorageUsed) itemComp.Size > storageComp.StorageCapacityMax - storageComp.StorageUsed)
{
// If this is a stack, we may be able to combine it with an existing stack in the storage.
// If so, no extra space would be used.
//
// TODO: This doesn't allow any sort of top-up behavior.
// You either combine the whole stack, or insert nothing.
if (!TryComp(insertEnt, out StackComponent? stackComp) || !CanCombineStacks(storageComp, stackComp))
{ {
reason = "comp-storage-insufficient-capacity"; reason = "comp-storage-insufficient-capacity";
return false; return false;
} }
}
reason = null; reason = null;
return true; return true;
@@ -599,6 +610,29 @@ namespace Content.Server.Storage.EntitySystems
return true; return true;
} }
private bool CanCombineStacks(
ServerStorageComponent storageComp,
StackComponent stack)
{
if (storageComp.Storage == null)
return false;
var stackQuery = GetEntityQuery<StackComponent>();
var countLeft = stack.Count;
foreach (var ent in storageComp.Storage.ContainedEntities)
{
if (!stackQuery.TryGetComponent(ent, out var destStack))
continue;
if (destStack.StackTypeId != stack.StackTypeId)
continue;
countLeft -= _stack.GetAvailableSpace(stack);
}
return countLeft <= 0;
}
// REMOVE: remove and drop on the ground // REMOVE: remove and drop on the ground
public bool RemoveAndDrop(EntityUid uid, EntityUid removeEnt, ServerStorageComponent? storageComp = null) public bool RemoveAndDrop(EntityUid uid, EntityUid removeEnt, ServerStorageComponent? storageComp = null)
{ {
@@ -624,12 +658,18 @@ namespace Content.Server.Storage.EntitySystems
var toInsert = hands.ActiveHandEntity; var toInsert = hands.ActiveHandEntity;
if (!CanInsert(uid, toInsert.Value, out var reason, storageComp) || !_sharedHandsSystem.TryDrop(player, toInsert.Value, handsComp: hands)) if (!CanInsert(uid, toInsert.Value, out var reason, storageComp))
{ {
Popup(uid, player, reason ?? "comp-storage-cant-insert", storageComp); Popup(uid, player, reason ?? "comp-storage-cant-insert", storageComp);
return false; return false;
} }
if (!_sharedHandsSystem.TryDrop(player, toInsert.Value, handsComp: hands))
{
PopupEnt(uid, player, "comp-storage-cant-drop", toInsert.Value, storageComp);
return false;
}
return PlayerInsertEntityInWorld(uid, player, toInsert.Value, storageComp); return PlayerInsertEntityInWorld(uid, player, toInsert.Value, storageComp);
} }
@@ -725,5 +765,14 @@ namespace Content.Server.Storage.EntitySystems
_popupSystem.PopupEntity(Loc.GetString(message), player, player); _popupSystem.PopupEntity(Loc.GetString(message), player, player);
} }
private void PopupEnt(EntityUid uid, EntityUid player, string message, EntityUid entityUid,
ServerStorageComponent storageComp)
{
if (!storageComp.ShowPopup)
return;
_popupSystem.PopupEntity(Loc.GetString(message, ("entity", entityUid)), player, player);
}
} }
} }

View File

@@ -1,8 +1,9 @@
comp-storage-no-item-size = N/A comp-storage-no-item-size = N/A
comp-storage-cant-insert = Can't insert. comp-storage-cant-insert = Can't insert.
comp-storage-insufficient-capacity = Insufficient capacity. comp-storage-insufficient-capacity = Insufficient capacity.
comp-storage-invalid-container = Invalid container for this item. comp-storage-invalid-container = This doesn't go in there!
comp-storage-anchored-failure = Can't insert an anchored item. comp-storage-anchored-failure = Can't insert an anchored item.
comp-storage-cant-drop = You can't let go of { THE($entity) }!
comp-storage-window-title = Storage Item comp-storage-window-title = Storage Item
comp-storage-window-volume = Items: { $itemCount }, Stored: { $usedVolume }/{ $maxVolume } comp-storage-window-volume = Items: { $itemCount }, Stored: { $usedVolume }/{ $maxVolume }
comp-storage-window-volume-unlimited = Items: { $itemCount } comp-storage-window-volume-unlimited = Items: { $itemCount }