コード例 #1
0
ファイル: CmdSpawnMob.cs プロジェクト: seaboy1234/c-raft
        public void Use(IClient client, string commandName, string[] tokens)
        {
            MobType type     = MobType.Sheep;
            int     amount   = 1;
            bool    validMob = false;

            if (tokens.Length > 1)
            {
                Int32.TryParse(tokens[1], out amount);
            }

            if (tokens.Length > 0)
            {
                int mobId;
                Int32.TryParse(tokens[0], out mobId);
                string mobName = Enum.GetName(typeof(MobType), mobId);
                if (mobId == 0)
                {
                    if (mobId.ToString() != tokens[0])
                    {
                        Enum.TryParse(tokens[0], true, out type);
                        validMob = true;
                    }
                }
                else if (!string.IsNullOrEmpty(mobName))
                {
                    type     = (MobType)Enum.Parse(typeof(MobType), mobName);
                    validMob = true;
                }
            }
            else
            {
                validMob = true;
            }

            if (amount < 1 || !validMob)
            {
                Help(client);
                return;
            }

            IServer        server     = client.GetServer();
            AbsWorldCoords position   = client.GetOwner().Position;
            IMobFactory    mobFactory = server.GetMobFactory();

            for (int i = 0; i < amount; i++)
            {
                var mob = mobFactory.CreateMob(client.GetOwner().GetWorld(), server, type, null);
                mob.Position = position;

                //Event
                EntitySpawnEventArgs e = new EntitySpawnEventArgs(mob, mob.Position);
                server.GetPluginManager().CallEvent(Event.EntitySpawn, e);
                if (e.EventCanceled)
                {
                    continue;
                }
                mob.Position = e.Location;
                //End Event

                server.AddEntity(mob);
            }
        }