Exemple #1
0
        public async Task SpawnItemInSlotTest()
        {
            var options = new ServerIntegrationOptions {
                ExtraPrototypes = Prototypes
            };
            var server = StartServer(options);

            await server.WaitIdleAsync();

            var sEntities = server.ResolveDependency <IEntityManager>();

            EntityUid          human     = default;
            InventoryComponent inventory = null;

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

                mapMan.CreateNewMapEntity(MapId.Nullspace);

                human     = sEntities.SpawnEntity("InventoryStunnableDummy", MapCoordinates.Nullspace);
                inventory = sEntities.GetComponent <InventoryComponent>(human);

                // Can't do the test if this human doesn't have the slots for it.
                Assert.That(inventory.HasSlot(Slots.INNERCLOTHING));
                Assert.That(inventory.HasSlot(Slots.IDCARD));

                Assert.That(inventory.SpawnItemInSlot(Slots.INNERCLOTHING, "InventoryJumpsuitJanitorDummy", true));

                // Do we actually have the uniform equipped?
                Assert.That(inventory.TryGetSlotItem(Slots.INNERCLOTHING, out ItemComponent uniform));
                Assert.That(sEntities.GetComponent <MetaDataComponent>(uniform.Owner).EntityPrototype is
                {
                    ID: "InventoryJumpsuitJanitorDummy"
                });

                EntitySystem.Get <StunSystem>().TryStun(human, TimeSpan.FromSeconds(1f), true);

                // Since the mob is stunned, they can't equip this.
                Assert.That(inventory.SpawnItemInSlot(Slots.IDCARD, "InventoryIDCardDummy", true), Is.False);

                // Make sure we don't have the ID card equipped.
                Assert.That(inventory.TryGetSlotItem(Slots.IDCARD, out ItemComponent _), Is.False);

                // Let's try skipping the interaction check and see if it equips it!
                Assert.That(inventory.SpawnItemInSlot(Slots.IDCARD, "InventoryIDCardDummy"));
                Assert.That(inventory.TryGetSlotItem(Slots.IDCARD, out ItemComponent id));
                Assert.That(sEntities.GetComponent <MetaDataComponent>(id.Owner).EntityPrototype is
                {
                    ID: "InventoryIDCardDummy"
                });
Exemple #2
0
        public async Task SpawnItemInSlotTest()
        {
            var options = new ServerIntegrationOptions {
                ExtraPrototypes = PROTOTYPES
            };
            var server = StartServerDummyTicker(options);

            IEntity            human     = null;
            InventoryComponent inventory = null;
            StunnableComponent stun      = null;


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

                mapMan.CreateNewMapEntity(MapId.Nullspace);

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

                human     = entityMan.SpawnEntity("InventoryStunnableDummy", MapCoordinates.Nullspace);
                inventory = human.GetComponent <InventoryComponent>();
                stun      = human.GetComponent <StunnableComponent>();

                // Can't do the test if this human doesn't have the slots for it.
                Assert.That(inventory.HasSlot(Slots.INNERCLOTHING));
                Assert.That(inventory.HasSlot(Slots.IDCARD));

                Assert.That(inventory.SpawnItemInSlot(Slots.INNERCLOTHING, "ClothingUniformJumpsuitJanitor", true));

                // Do we actually have the uniform equipped?
                Assert.That(inventory.TryGetSlotItem(Slots.INNERCLOTHING, out ItemComponent uniform));
                Assert.That(uniform.Owner.Prototype != null && uniform.Owner.Prototype.ID == "ClothingUniformJumpsuitJanitor");

                stun.Stun(1f);

                // Since the mob is stunned, they can't equip this.
                Assert.That(inventory.SpawnItemInSlot(Slots.IDCARD, "AssistantIDCard", true), Is.False);

                // Make sure we don't have the ID card equipped.
                Assert.That(inventory.TryGetSlotItem(Slots.IDCARD, out ItemComponent _), Is.False);

                // Let's try skipping the interaction check and see if it equips it!
                Assert.That(inventory.SpawnItemInSlot(Slots.IDCARD, "AssistantIDCard", false));
                Assert.That(inventory.TryGetSlotItem(Slots.IDCARD, out ItemComponent id));
                Assert.That(id.Owner.Prototype != null && id.Owner.Prototype.ID == "AssistantIDCard");
            });

            await server.WaitIdleAsync();
        }
Exemple #3
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());
        }