Fix map serialization of storage component. (#111)
Items were stored since the engine can serialize containers, but the used storage volume wasn't.
This commit is contained in:
committed by
GitHub
parent
d186f18c20
commit
51bc17f76d
@@ -25,6 +25,7 @@ namespace Content.Server.GameObjects
|
|||||||
{
|
{
|
||||||
private Container storage;
|
private Container storage;
|
||||||
|
|
||||||
|
private bool _storageInitialCalculated = false;
|
||||||
private int StorageUsed = 0;
|
private int StorageUsed = 0;
|
||||||
private int StorageCapacityMax = 10000;
|
private int StorageCapacityMax = 10000;
|
||||||
public HashSet<IPlayerSession> SubscribedSessions = new HashSet<IPlayerSession>();
|
public HashSet<IPlayerSession> SubscribedSessions = new HashSet<IPlayerSession>();
|
||||||
@@ -43,13 +44,13 @@ namespace Content.Server.GameObjects
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void OnAdd()
|
public override void Initialize()
|
||||||
{
|
{
|
||||||
base.OnAdd();
|
base.Initialize();
|
||||||
|
|
||||||
storage = ContainerManagerComponent.Create<Container>("storagebase", Owner);
|
storage = ContainerManagerComponent.Ensure<Container>("storagebase", Owner);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override ComponentState GetComponentState()
|
public override ComponentState GetComponentState()
|
||||||
{
|
{
|
||||||
@@ -61,6 +62,7 @@ namespace Content.Server.GameObjects
|
|||||||
base.ExposeData(serializer);
|
base.ExposeData(serializer);
|
||||||
|
|
||||||
serializer.DataField(ref StorageCapacityMax, "Capacity", 10000);
|
serializer.DataField(ref StorageCapacityMax, "Capacity", 10000);
|
||||||
|
serializer.DataField(ref StorageUsed, "used", 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -70,6 +72,7 @@ namespace Content.Server.GameObjects
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
bool Remove(IEntity toremove)
|
bool Remove(IEntity toremove)
|
||||||
{
|
{
|
||||||
|
_ensureInitialCalculated();
|
||||||
if (storage.Remove(toremove))
|
if (storage.Remove(toremove))
|
||||||
{
|
{
|
||||||
Logger.InfoS("Storage", "Storage (UID {0}) had entity (UID {1}) removed from it.", Owner.Uid, toremove.Uid);
|
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
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
bool CanInsert(IEntity toinsert)
|
bool CanInsert(IEntity toinsert)
|
||||||
{
|
{
|
||||||
|
_ensureInitialCalculated();
|
||||||
if (toinsert.TryGetComponent(out StoreableComponent store))
|
if (toinsert.TryGetComponent(out StoreableComponent store))
|
||||||
{
|
{
|
||||||
if (store.ObjectSize <= (StorageCapacityMax - StorageUsed))
|
if (store.ObjectSize <= (StorageCapacityMax - StorageUsed))
|
||||||
@@ -120,6 +124,7 @@ namespace Content.Server.GameObjects
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
bool IAttackby.Attackby(IEntity user, IEntity attackwith)
|
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);
|
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<HandsComponent>();
|
var hands = user.GetComponent<HandsComponent>();
|
||||||
//Check that we can drop the item from our hands first otherwise we obviously cant put it inside
|
//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
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
bool IUse.UseEntity(IEntity user)
|
bool IUse.UseEntity(IEntity user)
|
||||||
{
|
{
|
||||||
|
_ensureInitialCalculated();
|
||||||
var user_session = user.GetComponent<BasicActorComponent>().playerSession;
|
var user_session = user.GetComponent<BasicActorComponent>().playerSession;
|
||||||
Logger.DebugS("Storage", "Storage (UID {0}) \"used\" by player session (UID {1}).", Owner.Uid, user_session.AttachedEntityUid);
|
Logger.DebugS("Storage", "Storage (UID {0}) \"used\" by player session (UID {1}).", Owner.Uid, user_session.AttachedEntityUid);
|
||||||
SubscribeSession(user_session);
|
SubscribeSession(user_session);
|
||||||
@@ -171,6 +177,7 @@ namespace Content.Server.GameObjects
|
|||||||
/// <param name="actor"></param>
|
/// <param name="actor"></param>
|
||||||
public void SubscribeSession(IPlayerSession session)
|
public void SubscribeSession(IPlayerSession session)
|
||||||
{
|
{
|
||||||
|
_ensureInitialCalculated();
|
||||||
if (!SubscribedSessions.Contains(session))
|
if (!SubscribedSessions.Contains(session))
|
||||||
{
|
{
|
||||||
Logger.DebugS("Storage", "Storage (UID {0}) subscribed player session (UID {1}).", Owner.Uid, session.AttachedEntityUid);
|
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 _:
|
case RemoveEntityMessage _:
|
||||||
{
|
{
|
||||||
|
_ensureInitialCalculated();
|
||||||
var playerMan = IoCManager.Resolve<IPlayerManager>();
|
var playerMan = IoCManager.Resolve<IPlayerManager>();
|
||||||
var session = playerMan.GetSessionByChannel(netChannel);
|
var session = playerMan.GetSessionByChannel(netChannel);
|
||||||
var playerentity = session.AttachedEntity;
|
var playerentity = session.AttachedEntity;
|
||||||
@@ -288,5 +296,23 @@ namespace Content.Server.GameObjects
|
|||||||
{
|
{
|
||||||
((IUse) this).UseEntity(user);
|
((IUse) this).UseEntity(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void _ensureInitialCalculated()
|
||||||
|
{
|
||||||
|
if (_storageInitialCalculated)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
StorageUsed = 0;
|
||||||
|
|
||||||
|
foreach (var entity in storage.ContainedEntities)
|
||||||
|
{
|
||||||
|
var item = entity.GetComponent<ItemComponent>();
|
||||||
|
StorageUsed += item.ObjectSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
_storageInitialCalculated = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,11 +6,13 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using SS14.Shared.ViewVariables;
|
||||||
|
|
||||||
namespace Content.Server.GameObjects
|
namespace Content.Server.GameObjects
|
||||||
{
|
{
|
||||||
public class ContainerSlot : BaseContainer
|
public class ContainerSlot : BaseContainer
|
||||||
{
|
{
|
||||||
|
[ViewVariables]
|
||||||
public IEntity ContainedEntity { get; private set; } = null;
|
public IEntity ContainedEntity { get; private set; } = null;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
|||||||
@@ -84,7 +84,7 @@
|
|||||||
|
|
||||||
#handheld lights
|
#handheld lights
|
||||||
- type: entity
|
- type: entity
|
||||||
name: "Lantern"
|
name: "Flashlight"
|
||||||
parent: BaseItem
|
parent: BaseItem
|
||||||
id: FlashlightLantern
|
id: FlashlightLantern
|
||||||
description: They light the way to freedom
|
description: They light the way to freedom
|
||||||
|
|||||||
Reference in New Issue
Block a user