private bool TryEquip(InventoryComponent inv, Slots slot, IEntity user)
        {
            if (!inv.Equip(slot, this, true, out var reason))
            {
                if (reason != null)
                {
                    Owner.PopupMessage(user, reason);
                }

                return(false);
            }

            return(true);
        }
Beispiel #2
0
        public static bool SpawnItemInSlot(this InventoryComponent inventory, Slots slot, string prototype, bool mobCheck = false)
        {
            var entityManager = inventory.Owner.EntityManager;
            var protoManager  = IoCManager.Resolve <IPrototypeManager>();
            var user          = inventory.Owner;

            // Let's do nothing if the owner of the inventory has been deleted.
            if (user.Deleted)
            {
                return(false);
            }

            // If we don't have that slot or there's already an item there, we do nothing.
            if (!inventory.HasSlot(slot) || inventory.TryGetSlotItem(slot, out ItemComponent _))
            {
                return(false);
            }

            // If the prototype in question doesn't exist, we do nothing.
            if (!protoManager.HasIndex <EntityPrototype>(prototype))
            {
                return(false);
            }

            // Let's spawn this first...
            var item = entityManager.SpawnEntity(prototype, user.Transform.MapPosition);

            // Helper method that deletes the item and returns false.
            bool DeleteItem()
            {
                item.Delete();
                return(false);
            }

            // If this doesn't have an item component, then we can't do anything with it.
            if (!item.TryGetComponent(out ItemComponent? itemComp))
            {
                return(DeleteItem());
            }

            // We finally try to equip the item, otherwise we delete it.
            return(inventory.Equip(slot, itemComp, mobCheck) || DeleteItem());
        }
        public async Task Test()
        {
            var server = StartServerDummyTicker();

            IEntity            human      = null;
            IEntity            uniform    = null;
            IEntity            idCard     = null;
            IEntity            pocketItem = null;
            InventoryComponent inventory  = null;

            server.Assert(() =>
            {
                var mapMan = IoCManager.Resolve <IMapManager>();

                mapMan.CreateNewMapEntity(MapId.Nullspace);

                var entityMan = IoCManager.Resolve <IEntityManager>();

                human          = entityMan.SpawnEntity("HumanMob_Content", MapCoordinates.Nullspace);
                uniform        = entityMan.SpawnEntity("JanitorUniform", MapCoordinates.Nullspace);
                idCard         = entityMan.SpawnEntity("AssistantIDCard", MapCoordinates.Nullspace);
                pocketItem     = entityMan.SpawnEntity("FlashlightLantern", MapCoordinates.Nullspace);
                var tooBigItem = entityMan.SpawnEntity("ToolboxEmergency", MapCoordinates.Nullspace);

                inventory = human.GetComponent <InventoryComponent>();

                Assert.That(inventory.CanEquip(Slots.INNERCLOTHING, uniform));

                // Can't equip any of these since no uniform!
                Assert.That(inventory.CanEquip(Slots.IDCARD, idCard), Is.False);
                Assert.That(inventory.CanEquip(Slots.POCKET1, pocketItem), Is.False);
                Assert.That(inventory.CanEquip(Slots.POCKET1, tooBigItem), Is.False); // This one fails either way.

                inventory.Equip(Slots.INNERCLOTHING, uniform);

                Assert.That(inventory.Equip(Slots.IDCARD, idCard));
                Assert.That(inventory.Equip(Slots.POCKET1, pocketItem));
                Assert.That(inventory.CanEquip(Slots.POCKET1, tooBigItem), Is.False); // Still failing!

                Assert.That(IsDescendant(idCard, human));
                Assert.That(IsDescendant(pocketItem, human));

                // Now drop the jumpsuit.
                inventory.Unequip(Slots.INNERCLOTHING);
            });

            server.RunTicks(2);

            server.Assert(() =>
            {
                // Items have been dropped!
                Assert.That(IsDescendant(uniform, human), Is.False);
                Assert.That(IsDescendant(idCard, human), Is.False);
                Assert.That(IsDescendant(pocketItem, human), Is.False);

                // Ensure everything null here.
                Assert.That(inventory.GetSlotItem(Slots.INNERCLOTHING), Is.Null);
                Assert.That(inventory.GetSlotItem(Slots.IDCARD), Is.Null);
                Assert.That(inventory.GetSlotItem(Slots.POCKET1), Is.Null);
            });

            await server.WaitIdleAsync();
        }
        public async Task Test()
        {
            var options = new ServerIntegrationOptions {
                ExtraPrototypes = Prototypes
            };
            var server = StartServer(options);

            EntityUid          human      = default;
            EntityUid          uniform    = default;
            EntityUid          idCard     = default;
            EntityUid          pocketItem = default;
            InventoryComponent inventory  = null;

            server.Assert(() =>
            {
                var mapMan = IoCManager.Resolve <IMapManager>();

                mapMan.CreateNewMapEntity(MapId.Nullspace);

                var entityMan = IoCManager.Resolve <IEntityManager>();

                human          = entityMan.SpawnEntity("HumanDummy", MapCoordinates.Nullspace);
                uniform        = entityMan.SpawnEntity("UniformDummy", MapCoordinates.Nullspace);
                idCard         = entityMan.SpawnEntity("IDCardDummy", MapCoordinates.Nullspace);
                pocketItem     = entityMan.SpawnEntity("FlashlightDummy", MapCoordinates.Nullspace);
                var tooBigItem = entityMan.SpawnEntity("ToolboxDummy", MapCoordinates.Nullspace);

                inventory = entityMan.GetComponent <InventoryComponent>(human);

                Assert.That(inventory.CanEquip(Slots.INNERCLOTHING, uniform));

                // Can't equip any of these since no uniform!
                Assert.That(inventory.CanEquip(Slots.IDCARD, idCard), Is.False);
                Assert.That(inventory.CanEquip(Slots.POCKET1, pocketItem), Is.False);
                Assert.That(inventory.CanEquip(Slots.POCKET1, tooBigItem), Is.False); // This one fails either way.

                inventory.Equip(Slots.INNERCLOTHING, uniform);

                Assert.That(inventory.Equip(Slots.IDCARD, idCard));
                Assert.That(inventory.Equip(Slots.POCKET1, pocketItem));
                Assert.That(inventory.CanEquip(Slots.POCKET1, tooBigItem), Is.False); // Still failing!

                Assert.That(IsDescendant(idCard, human));
                Assert.That(IsDescendant(pocketItem, human));

                // Now drop the jumpsuit.
                inventory.Unequip(Slots.INNERCLOTHING);
            });

            server.RunTicks(2);

            server.Assert(() =>
            {
                // Items have been dropped!
                Assert.That(IsDescendant(uniform, human), Is.False);
                Assert.That(IsDescendant(idCard, human), Is.False);
                Assert.That(IsDescendant(pocketItem, human), Is.False);

                // Ensure everything null here.
                Assert.That(inventory.GetSlotItem(Slots.INNERCLOTHING), Is.Null);
                Assert.That(inventory.GetSlotItem(Slots.IDCARD), Is.Null);
                Assert.That(inventory.GetSlotItem(Slots.POCKET1), Is.Null);
            });

            await server.WaitIdleAsync();
        }