当前位置: 首页>>代码示例>>C#>>正文


C# ArgumentList.TryPopAny方法代码示例

本文整理汇总了C#中ArgumentList.TryPopAny方法的典型用法代码示例。如果您正苦于以下问题:C# ArgumentList.TryPopAny方法的具体用法?C# ArgumentList.TryPopAny怎么用?C# ArgumentList.TryPopAny使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ArgumentList的用法示例。


在下文中一共展示了ArgumentList.TryPopAny方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SpawnNPC

        /// <summary>
        /// Spawns specified NPC type.
        /// </summary>
        /// <param name="sender">Sending player</param>
        /// <param name="args">Arguments sent with command</param>
        /// <remarks>This function also allows NPC custom health.</remarks>
        public static void SpawnNPC(ISender sender, ArgumentList args)
        {
            if (Main.stopSpawns && !Program.properties.NPCSpawnsOverride)
                throw new CommandError("NPC Spawing is disabled.");

            var health = -1;
            var customHealth = args.TryPopAny<Int32>("-health", out health);

            Player player = sender as Player;
            int NPCAmount;
            if (args.Count > 3)
                throw new CommandError(Languages.TooManyArguments);
            else if (sender is ConsoleSender && args.Count <= 2)
            {
                if (!NetPlay.anyClients || !Server.TryGetFirstOnlinePlayer(out player))
                    throw new CommandError(Languages.NobodyOnline);
            }
            else if (args.Count == 3)
                player = args.GetOnlinePlayer(2);

            var npcName = args.GetString(1).ToLower().Trim();

            // Get the class id of the npc
            int realNPCId = 0;
            NPC fclass = Registries.NPC.FindClass(npcName);
            if (fclass.Name != String.Empty)
                realNPCId = fclass.Type;
            else
                throw new CommandError(Languages.NPCDoesntExist);

            try
            {
                NPCAmount = args.GetInt(0);
                if (NPCAmount > Program.properties.SpawnNPCMax && sender is Player)
                {
                    (sender as Player).Kick(Languages.DontSpawnThatMany);
                    return;
                }
            }
            catch
            {
                throw new CommandError(Languages.ExpectedSpawnInteger);
            }

            string realNPCName = String.Empty;
            for (int i = 0; i < NPCAmount; i++)
            {
                Vector2 location = World.GetRandomClearTile(((int)player.Position.X / 16), ((int)player.Position.Y / 16), 100, true, 100, 50);
                int npcIndex = NPC.NewNPC(((int)location.X * 16), ((int)location.Y * 16), fclass.Name, 0, Main.SpawnsOverride);

                if (customHealth)
                {
                    Main.npcs[npcIndex].life = health;
                    Main.npcs[npcIndex].lifeMax = health;
                }

                realNPCName = Main.npcs[npcIndex].Name;
            }
            Server.notifyOps("Spawned " + NPCAmount.ToString() + " of " +
                    realNPCName + " [" + player.Name + "]", true);
        }
开发者ID:jason14747,项目名称:Terraria-s-Dedicated-Server-Mod,代码行数:67,代码来源:Commands.cs

示例2: Give

        /// <summary>
        /// Gives specified item to the specified player.
        /// </summary>
        /// <param name="sender">Sending player</param>
        /// <param name="args">Arguments sent with command</param>
        public static void Give(ISender sender, ArgumentList args)
        {
            // /give <player> <stack> <name>

            string _prefix;
            args.TryPopAny("-prefix", out _prefix);

            byte prefix;
            if (!Byte.TryParse(_prefix, out prefix))
            {
                Affix affix;
                if (!AffixExtensions.Parse(_prefix ?? String.Empty, out affix, true)) prefix = 0;
                else prefix = (byte)affix;
            }

            Player receiver = args.GetOnlinePlayer(0);
            int stack = args.GetInt(1);
            string NameOrId = args.GetString(2);

            List<ItemInfo> itemlist;
            if (Server.TryFindItemByName(NameOrId, out itemlist) && itemlist.Count > 0)
            {
                if (itemlist.Count > 1)
                    throw new CommandError(String.Format(Languages.MoreThanOneItemFoundNameId, itemlist.Count));

                var item = itemlist[0];

                var index = receiver.GiveItem(item.Type, stack, sender, item.NetID, true, prefix);

                if (item.NetID < 0)
                    Main.item[index] = Item.netDefaults(item.NetID);

                Main.item[index].Prefix = prefix;
            }
            else
            {
                int Id = -1;
                try
                {
                    Id = Int32.Parse(NameOrId);
                }
                catch
                {
                    throw new CommandError(String.Format(Languages.MoreThanOneItemFoundNameId, itemlist.Count));
                }

                if (Server.TryFindItemByType(Id, out itemlist) && itemlist.Count > 0)
                {
                    if (itemlist.Count > 1)
                        throw new CommandError(String.Format(Languages.MoreThanOneItemFoundType, itemlist.Count));

                    //receiver.GiveItem(itemlist[0].Type, stack, sender);
                    var item = itemlist[0];

                    var index = receiver.GiveItem(item.Type, stack, sender, item.NetID, true, prefix);

                    if (item.NetID < 0)
                        Main.item[index] = Item.netDefaults(item.NetID);

                    Main.item[index].Prefix = prefix;
                }
                else
                {
                    throw new CommandError(String.Format(Languages.MoreThanOneItemFoundNameId, "no"));
                }
            }
        }
开发者ID:jason14747,项目名称:Terraria-s-Dedicated-Server-Mod,代码行数:72,代码来源:Commands.cs

示例3: SpawnNPC

        /// <summary>
        /// Spawns specified NPC type.
        /// </summary>
        /// <param name="sender">Sending player</param>
        /// <param name="args">Arguments sent with command</param>
        /// <remarks>This function also allows NPC custom health.</remarks>
        public void SpawnNPC(ISender sender, ArgumentList args)
        {
            //if (Main.stopSpawns && !Program.properties.NPCSpawnsOverride)
            //    throw new CommandError("NPC Spawing is disabled.");

            //var health = -1;
            //var customHealth = args.TryPopAny<Int32>("-health", out health);

            Player player = sender as Player;
            int amount, offset = -1;
            if (args.Count > 5)
                throw new CommandError("Too many arguments");
            else if (sender is ConsoleSender && args.Count <= 2)
            {
                if (!Netplay.anyClients || !Tools.TryGetFirstOnlinePlayer(out player))
                    throw new CommandError("No players online.");
            }
            else if (args.Count == 3)
                player = args.GetOnlinePlayer(2);
            else if (args.Count >= 4)
            {
                player = args.GetOnlinePlayer(2);
                args.TryPopAny<Int32>("-item", out offset);
            }

            var npcName = args.GetString(1).ToLower().Trim();

            // Get the class id of the npc
            var npcs = DefinitionManager.FindNPC(npcName);
            if (npcs == null || npcs.Length == 0)
            {
                int npcId;
                if (Int32.TryParse(npcName, out npcId))
                {
                    npcs = DefinitionManager.FindNPC(npcId);
                    if (npcs == null || npcs.Length == 0)
                    {
                        throw new CommandError("No npc exists by type {0}", npcId);
                    }
                }
                else throw new CommandError("No npc exists {0}", npcName);
            }

            npcs = npcs.OrderBy(x => x.Name).ToArray();
            if (npcs.Length > 1)
            {
                if (offset == -1)
                {
                    sender.SendMessage("Npcs matching " + npcName + ':');
                    for (var x = 0; x < npcs.Length; x++)
                    {
                        if (sender is ConsoleSender)
                        {
                            sender.SendMessage($"\t{x}\t- {npcs[x].Name}");
                        }
                        else
                        {
                            sender.SendMessage($"{x} - {npcs[x].Name}");
                        }
                    }
                    return;
                }
            }
            else offset = 0;

            var npc = npcs[offset];
            if (npc.Boss.HasValue && npc.Boss == true)
                throw new CommandError("This NPC can only be summoned by the SPAWNBOSS command.");
            try
            {
                amount = args.GetInt(0);
                //if (NPCAmount > Program.properties.SpawnNPCMax && sender is Player)
                //{
                //    (sender as Player).Kick("Don't spawn that many.");
                //    return;
                //}
            }
            catch
            {
                throw new CommandError("Expected amount to spawn");
            }

            var max = Tools.AvailableNPCSlots; //Perhaps remove a few incase of spawns
            if (amount > max)
                throw new CommandError("Cannot spawn that many, available slots: {0}", max);

            string realNPCName = String.Empty;
            for (int i = 0; i < amount; i++)
            {
                Vector2 location = World.GetRandomClearTile(((int)player.position.X / 16), ((int)player.position.Y / 16), 100, 100, 50);
                int npcIndex = NPC.NewNPC(((int)location.X * 16), ((int)location.Y * 16), npc.Id, 0);

                //if (customHealth)
                //{
//.........这里部分代码省略.........
开发者ID:DeathCradle,项目名称:Terraria-s-Dedicated-Server-Mod,代码行数:101,代码来源:SpawnNpcCommand.cs


注:本文中的ArgumentList.TryPopAny方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。