From 51bc17f76d1853dfc665bb16ee6e5a1d815b5e1d Mon Sep 17 00:00:00 2001 From: Pieter-Jan Briers Date: Sun, 11 Nov 2018 20:04:52 +0100 Subject: [PATCH] Fix map serialization of storage component. (#111) Items were stored since the engine can serialize containers, but the used storage volume wasn't. --- .../Items/Storage/ServerStorageComponent.cs | 34 ++++++++++++++++--- Content.Server/GameObjects/ContainerSlot.cs | 2 ++ Resources/Prototypes/Entities/Items.yml | 2 +- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/Content.Server/GameObjects/Components/Items/Storage/ServerStorageComponent.cs b/Content.Server/GameObjects/Components/Items/Storage/ServerStorageComponent.cs index bbb0047927..8b6a0ae640 100644 --- a/Content.Server/GameObjects/Components/Items/Storage/ServerStorageComponent.cs +++ b/Content.Server/GameObjects/Components/Items/Storage/ServerStorageComponent.cs @@ -25,6 +25,7 @@ namespace Content.Server.GameObjects { private Container storage; + private bool _storageInitialCalculated = false; private int StorageUsed = 0; private int StorageCapacityMax = 10000; public HashSet SubscribedSessions = new HashSet(); @@ -43,13 +44,13 @@ namespace Content.Server.GameObjects } } - public override void OnAdd() + public override void Initialize() { - base.OnAdd(); + base.Initialize(); - storage = ContainerManagerComponent.Create("storagebase", Owner); + storage = ContainerManagerComponent.Ensure("storagebase", Owner); } - + /// public override ComponentState GetComponentState() { @@ -61,6 +62,7 @@ namespace Content.Server.GameObjects base.ExposeData(serializer); serializer.DataField(ref StorageCapacityMax, "Capacity", 10000); + serializer.DataField(ref StorageUsed, "used", 0); } /// @@ -70,6 +72,7 @@ namespace Content.Server.GameObjects /// bool Remove(IEntity toremove) { + _ensureInitialCalculated(); if (storage.Remove(toremove)) { Logger.InfoS("Storage", "Storage (UID {0}) had entity (UID {1}) removed from it.", Owner.Uid, toremove.Uid); @@ -104,6 +107,7 @@ namespace Content.Server.GameObjects /// bool CanInsert(IEntity toinsert) { + _ensureInitialCalculated(); if (toinsert.TryGetComponent(out StoreableComponent store)) { if (store.ObjectSize <= (StorageCapacityMax - StorageUsed)) @@ -120,6 +124,7 @@ namespace Content.Server.GameObjects /// bool IAttackby.Attackby(IEntity user, IEntity attackwith) { + _ensureInitialCalculated(); Logger.DebugS("Storage", "Storage (UID {0}) attacked by user (UID {1}) with entity (UID {2}).", Owner.Uid, user.Uid, attackwith.Uid); var hands = user.GetComponent(); //Check that we can drop the item from our hands first otherwise we obviously cant put it inside @@ -146,6 +151,7 @@ namespace Content.Server.GameObjects /// bool IUse.UseEntity(IEntity user) { + _ensureInitialCalculated(); var user_session = user.GetComponent().playerSession; Logger.DebugS("Storage", "Storage (UID {0}) \"used\" by player session (UID {1}).", Owner.Uid, user_session.AttachedEntityUid); SubscribeSession(user_session); @@ -171,6 +177,7 @@ namespace Content.Server.GameObjects /// public void SubscribeSession(IPlayerSession session) { + _ensureInitialCalculated(); if (!SubscribedSessions.Contains(session)) { Logger.DebugS("Storage", "Storage (UID {0}) subscribed player session (UID {1}).", Owner.Uid, session.AttachedEntityUid); @@ -243,6 +250,7 @@ namespace Content.Server.GameObjects { case RemoveEntityMessage _: { + _ensureInitialCalculated(); var playerMan = IoCManager.Resolve(); var session = playerMan.GetSessionByChannel(netChannel); var playerentity = session.AttachedEntity; @@ -288,5 +296,23 @@ namespace Content.Server.GameObjects { ((IUse) this).UseEntity(user); } + + private void _ensureInitialCalculated() + { + if (_storageInitialCalculated) + { + return; + } + + StorageUsed = 0; + + foreach (var entity in storage.ContainedEntities) + { + var item = entity.GetComponent(); + StorageUsed += item.ObjectSize; + } + + _storageInitialCalculated = true; + } } } diff --git a/Content.Server/GameObjects/ContainerSlot.cs b/Content.Server/GameObjects/ContainerSlot.cs index bcc3f92d26..9e20dcb8b9 100644 --- a/Content.Server/GameObjects/ContainerSlot.cs +++ b/Content.Server/GameObjects/ContainerSlot.cs @@ -6,11 +6,13 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using SS14.Shared.ViewVariables; namespace Content.Server.GameObjects { public class ContainerSlot : BaseContainer { + [ViewVariables] public IEntity ContainedEntity { get; private set; } = null; /// diff --git a/Resources/Prototypes/Entities/Items.yml b/Resources/Prototypes/Entities/Items.yml index 0698d7db7d..8a7f5ad52a 100644 --- a/Resources/Prototypes/Entities/Items.yml +++ b/Resources/Prototypes/Entities/Items.yml @@ -84,7 +84,7 @@ #handheld lights - type: entity - name: "Lantern" + name: "Flashlight" parent: BaseItem id: FlashlightLantern description: They light the way to freedom