|
1 | 1 | # McQuery.Net
|
| 2 | + |
2 | 3 | Library for .Net which implements Minecraft Query protocol. You can use it for getting statuses of a Minecraft server.
|
3 | 4 |
|
4 | 5 | # Example of using
|
5 | 6 |
|
6 | 7 | ```cs
|
7 |
| -static async Task DoSomething(IEnumerable<IPEndPoint> mcServersEndPoints) |
8 |
| -{ |
9 |
| - McQueryService service = new(5, 5000, 500, 1000); |
| 8 | +IMcQueryClientFactory factory = new McQueryClientFactory(); |
| 9 | +using var client = factory.Get(); |
10 | 10 |
|
11 |
| - List<Server> servers = mcServersEndPoints.Select(service.RegistrateServer).ToList(); |
| 11 | +async Task ExecuteQueries(IReadOnlyCollection<IPEndPoint> endpoints, CancellationToken cancellationToken = default) |
| 12 | +{ |
| 13 | + var queryTasks = endpoints.SelectMany<IPEndPoint, Task, Task>( |
| 14 | + endpoint => |
| 15 | + [ |
| 16 | + GetBasicStatusAndPrint(endpoint, cancellationToken), |
| 17 | + GetFullStatusAndPrint(endpoint, cancellationToken) |
| 18 | + ], |
| 19 | + (_, task) => task |
| 20 | + ).ToArray(); |
12 | 21 |
|
13 |
| - List<Task<IResponse>> requests = new(); |
14 |
| - foreach (Server server in servers) |
15 |
| - { |
16 |
| - requests.Add(service.GetBasicStatusCommon(server)); |
17 |
| - requests.Add(service.GetFullStatusCommon(server)); |
18 |
| - } |
| 22 | + await Task.WhenAll(queryTasks); |
| 23 | +} |
19 | 24 |
|
20 |
| - Task.WaitAll(requests.ToArray()); |
| 25 | +async Task GetBasicStatusAndPrint(IPEndPoint endpoint, CancellationToken cancellationToken = default) |
| 26 | +{ |
| 27 | + Console.WriteLine(await client.GetBasicStatusAsync(endpoint, cancellationToken)); |
| 28 | +} |
21 | 29 |
|
22 |
| - foreach (Task<IResponse> request in requests) |
23 |
| - { |
24 |
| - IResponse response = await request; |
25 |
| - Console.WriteLine(response.ToString() + "\n"); |
26 |
| - } |
| 30 | +async Task GetFullStatusAndPrint(IPEndPoint endpoint, CancellationToken cancellationToken = default) |
| 31 | +{ |
| 32 | + Console.WriteLine(await client.GetFullStatusAsync(endpoint, cancellationToken)); |
27 | 33 | }
|
28 | 34 | ```
|
0 commit comments