示例#1
0
        ////////////////////////////////////////////////////////////////////////////////////////
        //    Выдаем игроку нужный предмет
        ////////////////////////////////////////////////////////////////////////////////////////

        object GiveItem(PlayerSession session, string itemNameOrIdOrGuid, int amount = 1)
        {
            string             itemName  = itemNameOrIdOrGuid;
            PlayerInventory    inventory = session.WorldPlayerEntity.GetComponent <PlayerInventory>();
            ItemGeneratorAsset generator = FindItem(itemNameOrIdOrGuid);

            if (generator == null)
            {
                PrintWarning("Something is wrong with the item:" + itemNameOrIdOrGuid);
                return(false);
            }

            ItemObject itemObj;

            if (generator.IsStackable())
            {
                itemObj = GlobalItemManager.Instance.CreateItem(generator, amount);
                if (!inventory.GiveItemServer(itemObj))
                {
                    GlobalItemManager.SpawnWorldItem(itemObj, inventory);
                }
            }
            else
            {
                int amountGiven = 0;
                while (amountGiven < amount)
                {
                    itemObj = GlobalItemManager.Instance.CreateItem(generator);
                    if (!inventory.GiveItemServer(itemObj))
                    {
                        GlobalItemManager.SpawnWorldItem(itemObj, inventory);
                    }
                    amountGiven++;
                }
            }

            return(true);
        }
示例#2
0
        private object GiveItem(IPlayer target, string itemNameOrId, int amount = 1, string container = "main")
        {
            if (config.ItemBlacklist.Contains(itemNameOrId.ToLower()))
            {
                return(null);
            }

            string itemName = itemNameOrId;

#if HURTWORLDITEMV2
            PlayerSession      session   = target.Object as PlayerSession;
            PlayerInventory    inventory = session.WorldPlayerEntity.GetComponent <PlayerInventory>();
            ItemGeneratorAsset generator = FindItem(itemNameOrId);

            ItemObject itemObj;
            if (generator.IsStackable())
            {
                itemObj = GlobalItemManager.Instance.CreateItem(generator, amount);
                if (!inventory.GiveItemServer(itemObj))
                {
                    GlobalItemManager.SpawnWorldItem(itemObj, inventory);
                }
            }
            else
            {
                int amountGiven = 0;
                while (amountGiven < amount)
                {
                    itemObj = GlobalItemManager.Instance.CreateItem(generator);
                    if (!inventory.GiveItemServer(itemObj))
                    {
                        GlobalItemManager.SpawnWorldItem(itemObj, inventory);
                    }
                    amountGiven++;
                }
            }
#elif HURTWORLD
            PlayerSession   session   = target.Object as PlayerSession;
            PlayerInventory inventory = session.WorldPlayerEntity.GetComponent <PlayerInventory>();
            GlobalItemManager.Instance.GiveItem(session.Player, FindItem(itemNameOrId), amount);
#elif REIGNOFKINGS
            Player player = target.Object as Player;
            if (player == null)
            {
                return(false);
            }

            Container itemContainer = null;
            switch (container.ToLower())
            {
            case "belt":
                itemContainer = player.CurrentCharacter.Entity.GetContainerOfType(CollectionTypes.Hotbar);
                break;

            case "main":
                itemContainer = player.CurrentCharacter.Entity.GetContainerOfType(CollectionTypes.Inventory);
                break;
            }

            InvItemBlueprint blueprint = InvDefinitions.Instance.Blueprints.GetBlueprintForName(itemName, true, true);
            if (blueprint == null)
            {
                return(false);
            }

            ContainerManagement containerManagement = blueprint.TryGet <ContainerManagement>();
            int stackableAmount = containerManagement != null ? containerManagement.StackLimit : 0;
            int amountGiven     = 0;
            while (amountGiven < amount)
            {
                int amountToGive           = Mathf.Min(stackableAmount, amount - amountGiven);
                InvGameItemStack itemStack = new InvGameItemStack(blueprint, amountToGive, null);
                if (!ItemCollection.AutoMergeAdd(itemContainer.Contents, itemStack))
                {
                    int stackAmount = amountToGive - itemStack.StackAmount;
                    if (stackAmount != 0)
                    {
                        amountGiven += stackAmount;
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    amountGiven += amountToGive;
                }
                if (itemContainer.Contents.FreeSlotCount == 0)
                {
                    break;
                }
            }
#elif RUST
            Item item = ItemManager.Create(FindItem(itemNameOrId));
            if (item == null)
            {
                return(false);
            }

            item.amount = amount;

            BasePlayer basePlayer = target.Object as BasePlayer;
            if (basePlayer == null)
            {
                return(false);
            }

            ItemContainer itemContainer = null;
            switch (container.ToLower())
            {
            case "belt":
                itemContainer = basePlayer.inventory.containerBelt;
                break;

            case "main":
                itemContainer = basePlayer.inventory.containerMain;
                break;
            }

            if (!basePlayer.inventory.GiveItem(item, itemContainer))
            {
                item.Remove();
                return(false);
            }

            itemName = item.info.displayName.english;

            if (config.ShowPopupNotices)
            {
                target.Command("note.inv", item.info.itemid, amount);
                target.Command("gametip.showgametip", Lang("ItemReceived", target.Id, itemName, amount));
                timer.Once(2f, () => target.Command("gametip.hidegametip"));
            }
#endif
            if (config.ShowChatNotices)
            {
                Message(target, "ItemReceived", itemName, amount);
            }

            if (config.LogToConsole)
            {
                Log($"{target.Name} {amount} x {itemName}");
            }

            return(true);
        }