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


C# IMyCubeGrid.GetAllSmallOwners方法代码示例

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


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

示例1: _cubeGrid_OnBlockOwnershipChanged

        private void _cubeGrid_OnBlockOwnershipChanged(IMyCubeGrid cubeGrid)
        {
            // only execute on server instance
            if (ChatCommandLogic.Instance != null && ChatCommandLogic.Instance.ServerCfg == null)
                return;

            if (_firstOwnershipChange)
            {
                _firstOwnershipChange = false;
                _cachedOwners = new List<long>(cubeGrid.GetAllSmallOwners());
                return;
            }

            var allSmallOwners = cubeGrid.GetAllSmallOwners();

            if (_cachedOwners == allSmallOwners)
                return;

            // if the grid wasn't owned or a owner was removed, we dont need to do anything but update the cached owners
            if (_cachedOwners.Count == 0 || _cachedOwners.Count > allSmallOwners.Count)
            {
                _cachedOwners = new List<long>(allSmallOwners);
                return;
            }

            var newOwners = allSmallOwners.Except(_cachedOwners).ToList();

            if (newOwners.Count == 0)
                return;

            if (!ProtectionHandler.IsProtected(cubeGrid))
            {
                _cachedOwners = new List<long>(allSmallOwners);
                return;
            }

            Dictionary<long, int> blocksPerOwner = new Dictionary<long, int>();

            foreach (IMyCubeGrid attachedCubeGrid in cubeGrid.GetAttachedGrids(AttachedGrids.Static))
            {

                List<IMySlimBlock> blocks = new List<IMySlimBlock>();
                attachedCubeGrid.GetBlocks(blocks, b => b.FatBlock != null);

                foreach (IMySlimBlock block in blocks)
                {
                    long ownerId = block.FatBlock.OwnerId;

                    // we dont want the new owners, the small owners or the 'nobody' (0)
                    if (ownerId == 0 || !attachedCubeGrid.BigOwners.Contains(ownerId) || newOwners.Contains(ownerId))
                        continue;

                    if (!blocksPerOwner.ContainsKey(ownerId))
                        blocksPerOwner.Add(ownerId, 1);
                    else
                        blocksPerOwner[ownerId]++;
                }
            }

            var sortedBpo = new List<KeyValuePair<long, int>>(blocksPerOwner.OrderBy(pair => pair.Value));

            // if we cannot identify an owner we allow the change
            if (sortedBpo.Count == 0)
            {
                _cachedOwners = new List<long>(allSmallOwners);
                return;
            }

            var bigOwner = sortedBpo[0].Key;

            List<IMySlimBlock> ownershipChangedBlocks = new List<IMySlimBlock>();
            cubeGrid.GetBlocks(ownershipChangedBlocks, b => b.FatBlock != null && newOwners.Contains(b.FatBlock.OwnerId));
            foreach (IMySlimBlock slimBlock in ownershipChangedBlocks)
            {
                var block = (Sandbox.Game.Entities.MyCubeBlock)slimBlock.FatBlock;
                block.ChangeOwner(bigOwner, MyOwnershipShareModeEnum.None);
                ConnectionHelper.SendMessageToAllPlayers(new MessageSyncBlockOwner() {OwnerId = bigOwner, EntityId = block.EntityId});
                // no need to update the cached owners as we don't want them to change
            }

            // TODO maybe allow the faction to build...
        }
开发者ID:DigTron,项目名称:Space-Engineers-Admin-script-mod,代码行数:82,代码来源:ProtectedCubeGrid.cs


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