public static bool IsWithinLimits(MyCubeBlockDefinition block, long playerId, MyObjectBuilder_CubeGrid grid, out string limitName) { limitName = string.Empty; var allow = true; if (block == null) { return(true); } var faction = MySession.Static.Factions.GetPlayerFaction(playerId); if (grid != null && Grid.IsSizeViolation(grid)) { return(false); } foreach (var item in BlockLimiterConfig.Instance.AllLimits) { limitName = item.Name; if (item.BlockList.Count == 0 || !item.IsMatch(block)) { continue; } if (item.IsExcepted(playerId) || (grid != null && item.IsExcepted(grid.EntityId))) { continue; } if (item.Limit == 0 && (item.LimitGrids || item.LimitPlayers || item.LimitFaction)) { return(false); } if (grid != null && item.IsGridType(grid, playerId)) { var gridId = grid.EntityId; if (gridId > 0 && item.LimitGrids && item.FoundEntities.TryGetValue(gridId, out var gCount)) { if (gCount >= item.Limit) { allow = false; break; } } } if (playerId > 0 && item.LimitPlayers && item.FoundEntities.TryGetValue(playerId, out var pCount)) { if (pCount >= item.Limit) { allow = false; break; } } if (faction == null || !item.LimitFaction || !item.FoundEntities.TryGetValue(faction.FactionId, out var fCount)) { continue; } { if (fCount < item.Limit) { continue; } allow = false; break; } } return(allow); }
public static bool IsWithinLimits(MyCubeBlockDefinition def, long ownerId, long gridId, int count, out string limit) { limit = string.Empty; if (def == null || Math.Abs(ownerId + gridId) < 1) { return(true); } var ownerFaction = MySession.Static.Factions.GetPlayerFaction(ownerId); var allow = true; if (Grid.IsSizeViolation(gridId)) { return(false); } if (BlockLimiterConfig.Instance.AllLimits.Count == 0) { return(true); } var foundGrid = GridCache.TryGetGridById(gridId, out var grid); if (!foundGrid) { GridCache.AddGrid(grid); } var subGrids = Grid.GetSubGrids(grid); foreach (var item in BlockLimiterConfig.Instance.AllLimits) { limit = item.Name; if (!item.IsMatch(def)) { continue; } if ((ownerId > 0 && item.IsExcepted(ownerId)) || gridId > 0 && item.IsExcepted(gridId)) { continue; } if (foundGrid && !item.IsGridType(grid)) { continue; } if (item.Limit == 0 && (item.LimitGrids || item.LimitPlayers || item.LimitFaction)) { return(false); } if (item.LimitGrids && gridId > 0) { item.FoundEntities.TryGetValue(gridId, out var gCount); if (foundGrid && item.IsGridType(grid)) { if (gCount + count > item.Limit) { allow = false; break; } //Counts found subgrid blocks too. var subGBlockCount = 0; foreach (var subGrid in subGrids) { if (!item.FoundEntities.TryGetValue(subGrid.EntityId, out var subGCount)) { continue; } subGBlockCount += subGCount; } if (subGBlockCount + count + gCount > item.Limit) { allow = false; break; } } } if (ownerId > 0 && item.LimitPlayers && item.FoundEntities.TryGetValue(ownerId, out var pCount)) { if (pCount + count > item.Limit) { allow = false; break; } } if (ownerFaction == null || !item.LimitFaction || !item.FoundEntities.TryGetValue(ownerFaction.FactionId, out var fCount)) { continue; } { if (fCount + count <= item.Limit) { continue; } allow = false; break; } } return(allow); }