Make item slot containers nullable (#6198)

This commit is contained in:
Leon Friedrich
2022-01-18 08:22:35 +13:00
committed by GitHub
parent 167e6e5cb6
commit 5679f1e4fb
4 changed files with 21 additions and 15 deletions

View File

@@ -43,7 +43,7 @@ namespace Content.Server.Cabinet
private void OnComponentStartup(EntityUid uid, ItemCabinetComponent cabinet, ComponentStartup args)
{
UpdateAppearance(uid, cabinet);
_itemSlotsSystem.SetLock(uid, cabinet.CabinetSlot.ID, !cabinet.Opened);
_itemSlotsSystem.SetLock(uid, cabinet.CabinetSlot, !cabinet.Opened);
}
private void UpdateAppearance(EntityUid uid,
@@ -105,7 +105,7 @@ namespace Content.Server.Cabinet
cabinet.Opened = !cabinet.Opened;
SoundSystem.Play(Filter.Pvs(uid), cabinet.DoorSound.GetSound(), uid, AudioHelpers.WithVariation(0.15f));
_itemSlotsSystem.SetLock(uid, cabinet.CabinetSlot.ID, !cabinet.Opened);
_itemSlotsSystem.SetLock(uid, cabinet.CabinetSlot, !cabinet.Opened);
UpdateAppearance(uid, cabinet);
}

View File

@@ -27,7 +27,7 @@ namespace Content.Server.Containers
foreach (var slot in component.Slots.Values)
{
if (slot.EjectOnDeconstruct && slot.Item != null)
slot.ContainerSlot.Remove(slot.Item.Value);
slot.ContainerSlot?.Remove(slot.Item.Value);
}
}

View File

@@ -156,7 +156,7 @@ namespace Content.Shared.Containers.ItemSlots
public string? EjectVerbText;
[ViewVariables]
public ContainerSlot ContainerSlot = default!;
public ContainerSlot? ContainerSlot = default!;
/// <summary>
/// If this slot belongs to some de-constructible component, should the item inside the slot be ejected upon
@@ -192,10 +192,10 @@ namespace Content.Shared.Containers.ItemSlots
[DataField("swap")]
public bool Swap = true;
public string ID => ContainerSlot.ID;
public string? ID => ContainerSlot?.ID;
// Convenience properties
public bool HasItem => ContainerSlot.ContainedEntity != null;
public EntityUid? Item => ContainerSlot.ContainedEntity;
public bool HasItem => ContainerSlot?.ContainedEntity != null;
public EntityUid? Item => ContainerSlot?.ContainedEntity;
}
}

View File

@@ -61,7 +61,7 @@ namespace Content.Shared.Containers.ItemSlots
continue;
var item = EntityManager.SpawnEntity(slot.StartingItem, EntityManager.GetComponent<TransformComponent>(itemSlots.Owner).Coordinates);
slot.ContainerSlot.Insert(item);
slot.ContainerSlot?.Insert(item);
}
}
@@ -95,6 +95,9 @@ namespace Content.Shared.Containers.ItemSlots
/// </summary>
public void RemoveItemSlot(EntityUid uid, ItemSlot slot, ItemSlotsComponent? itemSlots = null)
{
if (slot.ContainerSlot == null)
return;
slot.ContainerSlot.Shutdown();
// Don't log missing resolves. when an entity has all of its components removed, the ItemSlotsComponent may
@@ -102,7 +105,7 @@ namespace Content.Shared.Containers.ItemSlots
if (!Resolve(uid, ref itemSlots, logMissing: false))
return;
itemSlots.Slots.Remove(slot.ID);
itemSlots.Slots.Remove(slot.ContainerSlot.ID);
if (itemSlots.Slots.Count == 0)
EntityManager.RemoveComponent(uid, itemSlots);
@@ -193,7 +196,7 @@ namespace Content.Shared.Containers.ItemSlots
/// Useful for predicted interactions</param>
private void Insert(EntityUid uid, ItemSlot slot, EntityUid item, EntityUid? user, bool excludeUserAudio = false)
{
slot.ContainerSlot.Insert(item);
slot.ContainerSlot?.Insert(item);
// ContainerSlot automatically raises a directed EntInsertedIntoContainerMessage
PlaySound(uid, slot.InsertSound, slot.SoundOptions, excludeUserAudio ? user : null);
@@ -242,7 +245,7 @@ namespace Content.Shared.Containers.ItemSlots
return false;
}
return slot.ContainerSlot.CanInsertIfEmpty(usedUid, EntityManager);
return slot.ContainerSlot?.CanInsertIfEmpty(usedUid, EntityManager) ?? false;
}
/// <summary>
@@ -305,7 +308,7 @@ namespace Content.Shared.Containers.ItemSlots
if (slot.Locked || slot.Item == null)
return false;
return slot.ContainerSlot.CanRemove(slot.Item.Value, EntityManager);
return slot.ContainerSlot?.CanRemove(slot.Item.Value, EntityManager) ?? false;
}
/// <summary>
@@ -316,7 +319,7 @@ namespace Content.Shared.Containers.ItemSlots
/// Useful for predicted interactions</param>
private void Eject(EntityUid uid, ItemSlot slot, EntityUid item, EntityUid? user, bool excludeUserAudio = false)
{
slot.ContainerSlot.Remove(item);
slot.ContainerSlot?.Remove(item);
// ContainerSlot automatically raises a directed EntRemovedFromContainerMessage
PlaySound(uid, slot.EjectSound, slot.SoundOptions, excludeUserAudio ? user : null);
@@ -520,14 +523,17 @@ namespace Content.Shared.Containers.ItemSlots
if (!itemSlots.Slots.TryGetValue(id, out var slot))
return;
SetLock(itemSlots, slot, locked);
SetLock(uid, slot, locked, itemSlots);
}
/// <summary>
/// Lock an item slot. This stops items from being inserted into or ejected from this slot.
/// </summary>
public void SetLock(ItemSlotsComponent itemSlots, ItemSlot slot, bool locked)
public void SetLock(EntityUid uid, ItemSlot slot, bool locked, ItemSlotsComponent? itemSlots = null)
{
if (!Resolve(uid, ref itemSlots))
return;
slot.Locked = locked;
itemSlots.Dirty();
}