private void OnStackStarted(EntityUid uid, SharedStackComponent component, ComponentStartup args)
        {
            if (!ComponentManager.TryGetComponent(uid, out SharedAppearanceComponent? appearance))
            {
                return;
            }

            appearance.SetData(StackVisuals.Actual, component.Count);
            appearance.SetData(StackVisuals.MaxCount, component.MaxCount);
            appearance.SetData(StackVisuals.Hide, false);
        }
        /// <summary>
        ///     Try to use an amount of items on this stack. Returns whether this succeeded.
        /// </summary>
        public bool Use(EntityUid uid, SharedStackComponent stack, int amount)
        {
            // Check if we have enough things in the stack for this...
            if (stack.Count < amount)
            {
                // Not enough things in the stack, return false.
                return(false);
            }

            // We do have enough things in the stack, so remove them and change.
            SetCount(uid, stack, stack.Count - amount);
            return(true);
        }
        private void OnStackExamined(EntityUid uid, SharedStackComponent component, ExaminedEvent args)
        {
            if (!args.IsInDetailsRange)
            {
                return;
            }

            args.Message.AddText("\n");
            args.Message.AddMarkup(
                Loc.GetString("comp-stack-examine-detail-count",
                              ("count", component.Count),
                              ("markupCountColor", "lightgray")
                              )
                );
        }
        protected void OnStackCountChange(EntityUid uid, SharedStackComponent component, StackChangeCountEvent args)
        {
            if (args.Amount == component.Count)
            {
                return;
            }

            var old = component.Count;

            if (args.Amount > component.MaxCount)
            {
                args.Amount  = component.MaxCount;
                args.Clamped = true;
            }

            if (args.Amount < 0)
            {
                args.Amount  = 0;
                args.Clamped = true;
            }

            component.Count = args.Amount;
            component.Dirty();

            // Queue delete stack if count reaches zero.
            if (component.Count <= 0)
            {
                EntityManager.QueueDeleteEntity(uid);
            }

            // Change appearance data.
            if (ComponentManager.TryGetComponent(uid, out SharedAppearanceComponent? appearance))
            {
                appearance.SetData(StackVisuals.Actual, component.Count);
            }

            RaiseLocalEvent(uid, new StackCountChangedEvent(old, component.Count));
        }
Beispiel #5
0
        /// <summary>
        ///     Try to split this stack into two. Returns a non-null <see cref="IEntity"/> if successful.
        /// </summary>
        public IEntity?Split(EntityUid uid, SharedStackComponent stack, int amount, EntityCoordinates spawnPosition)
        {
            // Get a prototype ID to spawn the new entity. Null is also valid, although it should rarely be picked...
            var prototype = _prototypeManager.TryIndex <StackPrototype>(stack.StackTypeId, out var stackType)
                ? stackType.Spawn
                : stack.Owner.Prototype?.ID ?? null;

            // Try to remove the amount of things we want to split from the original stack...
            if (!Use(uid, stack, amount))
            {
                return(null);
            }

            // Set the output parameter in the event instance to the newly split stack.
            var entity = EntityManager.SpawnEntity(prototype, spawnPosition);

            if (ComponentManager.TryGetComponent(entity.Uid, out SharedStackComponent? stackComp))
            {
                // Set the split stack's count.
                SetCount(entity.Uid, stackComp, amount);
            }

            return(entity);
        }