using Content.Shared.Lathe; using Content.Shared.Whitelist; using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List; using Content.Shared.Materials; using Robust.Shared.Audio; namespace Content.Server.Lathe.Components { [RegisterComponent] [ComponentReference(typeof(SharedMaterialStorageComponent))] public sealed class MaterialStorageComponent : SharedMaterialStorageComponent { [ViewVariables] protected override Dictionary Storage { get; set; } = new(); /// /// How much material the storage can store in total. /// [ViewVariables] public int StorageLimit => _storageLimit; [DataField("StorageLimit")] private int _storageLimit = -1; /// /// Whitelist for specifying the kind of items that can be insert into this entity. /// [ViewVariables] [DataField("whitelist")] public EntityWhitelist? EntityWhitelist; /// /// Whitelist generated on runtime for what specific materials can be inserted into this entity. /// [ViewVariables] [DataField("materialWhiteList", customTypeSerializer: typeof(PrototypeIdListSerializer))] public List MaterialWhiteList = new(); /// /// The sound that plays when inserting an item into the storage /// [DataField("insertingSound")] public SoundSpecifier? InsertingSound; public override ComponentState GetComponentState() { return new MaterialStorageState(Storage); } /// /// Checks if the storage can take a volume of material without surpassing its own limits. /// /// The volume of material /// public bool CanTakeAmount(int amount) { return CurrentAmount + amount <= StorageLimit; } /// /// Checks if it can insert a material. /// /// Material ID /// How much to insert /// Whether it can insert the material or not. public bool CanInsertMaterial(string id, int amount) { return (CanTakeAmount(amount) || StorageLimit < 0) && (!Storage.ContainsKey(id) || Storage[id] + amount >= 0); } /// /// Inserts material into the storage. /// /// Material ID /// How much to insert /// Whether it inserted it or not. public bool InsertMaterial(string id, int amount) { if (!CanInsertMaterial(id, amount)) return false; if (!Storage.ContainsKey(id)) Storage.Add(id, 0); Storage[id] += amount; Dirty(); return true; } /// /// Removes material from the storage. /// /// Material ID /// How much to remove /// Whether it removed it or not. public bool RemoveMaterial(string id, int amount) { return InsertMaterial(id, -amount); } // forgive me I needed to write a crumb of e/c code to not go fucking insane i swear i will ecs this entire shitty fucking system one day public int GetMaterialAmount(string id) { if (!Storage.TryGetValue(id, out var amount)) return 0; return amount; } } }