Refactor stacks to use prototypes (#3387)
* Refactor stacks to use prototypes * Fix not assigned warning * Add names to stacks * Make machine baords and material constructions use the name as well * Remove defaulting stacks to prototype id * Fix tests Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
#nullable enable
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Content.Shared.GameObjects.Components;
|
||||
using Content.Shared.Materials;
|
||||
using Content.Shared.Stacks;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
@@ -12,30 +17,34 @@ namespace Content.Shared.Construction
|
||||
{
|
||||
// TODO: Make this use the material system.
|
||||
// TODO TODO: Make the material system not shit.
|
||||
public StackType Material { get; private set; }
|
||||
private string MaterialPrototypeId { get; [UsedImplicitly] set; } = default!;
|
||||
|
||||
public StackPrototype MaterialPrototype =>
|
||||
IoCManager.Resolve<IPrototypeManager>().Index<StackPrototype>(MaterialPrototypeId);
|
||||
|
||||
public int Amount { get; private set; }
|
||||
|
||||
public override void ExposeData(ObjectSerializer serializer)
|
||||
{
|
||||
base.ExposeData(serializer);
|
||||
|
||||
serializer.DataField(this, x => x.Material, "material", StackType.Metal);
|
||||
serializer.DataField(this, x => x.MaterialPrototypeId, "material", "Steel");
|
||||
serializer.DataField(this, x => x.Amount, "amount", 1);
|
||||
}
|
||||
|
||||
public override void DoExamine(FormattedMessage message, bool inDetailsRange)
|
||||
{
|
||||
message.AddMarkup(Loc.GetString("Next, add [color=yellow]{0}x[/color] [color=cyan]{1}[/color].", Amount, Material));
|
||||
message.AddMarkup(Loc.GetString("Next, add [color=yellow]{0}x[/color] [color=cyan]{1}[/color].", Amount, MaterialPrototype.Name));
|
||||
}
|
||||
|
||||
public override bool EntityValid(IEntity entity)
|
||||
{
|
||||
return entity.TryGetComponent(out SharedStackComponent? stack) && stack.StackType.Equals(Material);
|
||||
return entity.TryGetComponent(out SharedStackComponent? stack) && stack.StackTypeId.Equals(MaterialPrototypeId);
|
||||
}
|
||||
|
||||
public bool EntityValid(IEntity entity, [NotNullWhen(true)] out SharedStackComponent? stack)
|
||||
{
|
||||
if(entity.TryGetComponent(out SharedStackComponent? otherStack) && otherStack.StackType.Equals(Material))
|
||||
if (entity.TryGetComponent(out SharedStackComponent? otherStack) && otherStack.StackTypeId.Equals(MaterialPrototypeId))
|
||||
stack = otherStack;
|
||||
else
|
||||
stack = null;
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
using System;
|
||||
using Content.Shared.Stacks;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Players;
|
||||
using Robust.Shared.Reflection;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
@@ -10,6 +12,8 @@ namespace Content.Shared.GameObjects.Components
|
||||
{
|
||||
public abstract class SharedStackComponent : Component
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
|
||||
private const string SerializationCache = "stack";
|
||||
|
||||
public sealed override string Name => "Stack";
|
||||
@@ -47,7 +51,9 @@ namespace Content.Shared.GameObjects.Components
|
||||
|
||||
[ViewVariables] public int AvailableSpace => MaxCount - Count;
|
||||
|
||||
[ViewVariables] public object StackType { get; private set; }
|
||||
[ViewVariables] public string StackTypeId { get; private set; } = string.Empty;
|
||||
|
||||
public StackPrototype StackType => _prototypeManager.Index<StackPrototype>(StackTypeId);
|
||||
|
||||
public override void ExposeData(ObjectSerializer serializer)
|
||||
{
|
||||
@@ -59,31 +65,29 @@ namespace Content.Shared.GameObjects.Components
|
||||
return;
|
||||
}
|
||||
|
||||
if (serializer.TryGetCacheData(SerializationCache, out object stackType))
|
||||
if (serializer.TryGetCacheData(SerializationCache, out string stackType))
|
||||
{
|
||||
StackType = stackType;
|
||||
StackTypeId = stackType;
|
||||
return;
|
||||
}
|
||||
|
||||
if (serializer.TryReadDataFieldCached("stacktype", out string raw))
|
||||
{
|
||||
var refl = IoCManager.Resolve<IReflectionManager>();
|
||||
if (refl.TryParseEnumReference(raw, out var @enum))
|
||||
{
|
||||
stackType = @enum;
|
||||
}
|
||||
else
|
||||
{
|
||||
stackType = raw;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
stackType = Owner.Prototype.ID;
|
||||
}
|
||||
serializer.DataFieldCached(ref stackType, "stackType", string.Empty);
|
||||
|
||||
serializer.SetCacheData(SerializationCache, stackType);
|
||||
StackType = stackType;
|
||||
if (!string.IsNullOrEmpty(stackType))
|
||||
{
|
||||
serializer.SetCacheData(SerializationCache, stackType);
|
||||
StackTypeId = stackType;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Startup()
|
||||
{
|
||||
base.Startup();
|
||||
|
||||
if (!_prototypeManager.HasIndex<StackPrototype>(StackTypeId))
|
||||
{
|
||||
Logger.Error($"No {nameof(StackPrototype)} found with id {StackTypeId}");
|
||||
}
|
||||
}
|
||||
|
||||
public override ComponentState GetComponentState(ICommonSession player)
|
||||
@@ -116,34 +120,4 @@ namespace Content.Shared.GameObjects.Components
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum StackType
|
||||
{
|
||||
Metal,
|
||||
Glass,
|
||||
ReinforcedGlass,
|
||||
Plasteel,
|
||||
Cable,
|
||||
Wood,
|
||||
Plastic,
|
||||
MVCable,
|
||||
HVCable,
|
||||
Gold,
|
||||
Plasma,
|
||||
Ointment,
|
||||
Gauze,
|
||||
Brutepack,
|
||||
FloorTileSteel,
|
||||
FloorTileCarpet,
|
||||
FloorTileWhite,
|
||||
FloorTileDark,
|
||||
FloorTileWood,
|
||||
MetalRod,
|
||||
PaperRolling,
|
||||
CigaretteFilter,
|
||||
GroundTobacco,
|
||||
GroundCannabis,
|
||||
LeavesTobaccoDried,
|
||||
LeavesCannabisDried
|
||||
}
|
||||
}
|
||||
|
||||
33
Content.Shared/Stacks/StackPrototype.cs
Normal file
33
Content.Shared/Stacks/StackPrototype.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
#nullable enable
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Utility;
|
||||
using YamlDotNet.RepresentationModel;
|
||||
|
||||
namespace Content.Shared.Stacks
|
||||
{
|
||||
[Prototype("stack")]
|
||||
public class StackPrototype : IPrototype
|
||||
{
|
||||
public string ID { get; private set; } = string.Empty;
|
||||
|
||||
public string Name { get; private set; } = string.Empty;
|
||||
|
||||
public SpriteSpecifier? Icon { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// The entity id that will be spawned by default from this stack.
|
||||
/// </summary>
|
||||
public string? Spawn { get; private set; }
|
||||
|
||||
public void LoadFrom(YamlMappingNode mapping)
|
||||
{
|
||||
var reader = YamlObjectSerializer.NewReader(mapping);
|
||||
|
||||
reader.DataField(this, x => x.ID, "id", string.Empty);
|
||||
reader.DataField(this, x => x.Name, "name", string.Empty);
|
||||
reader.DataField(this, x => x.Icon, "icon", null);
|
||||
reader.DataField(this, x => x.Spawn, "spawn", null);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user