|
1 | 1 | # a2s-java |
2 | | -Valve Steam Query Protocol implementation for Java |
| 2 | + |
| 3 | +A Valve Steam Query Protocol (A2S) implementation for Java using Netty. |
| 4 | + |
| 5 | +This library allows you to query game servers that implement the Source Engine Query protocol, such as Counter-Strike, Team Fortress 2, Rust, ARK, and many others. |
| 6 | + |
| 7 | +> [!NOTE] |
| 8 | +> This code is based on and used from [yeetus-desastroesus/A2S-Java](https://github.com/yeetus-desastroesus/A2S-Java). |
| 9 | +
|
| 10 | +## Features |
| 11 | + |
| 12 | +- **A2S_INFO**: Get server information (name, map, player count, etc.). |
| 13 | +- **A2S_PLAYER**: Get detailed list of players currently on the server. |
| 14 | +- **A2S_RULES**: Get server rules/CVars. |
| 15 | +- **Asynchronous**: Built on Netty for high-performance, non-blocking I/O. |
| 16 | +- **Server Implementation**: Includes a basic A2S server implementation for testing or mocking. |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +The library is published on GitHub Packages. To use it, you need to configure your build tool to include the GitHub Maven repository. |
| 21 | + |
| 22 | +### Maven |
| 23 | + |
| 24 | +1. Add the following repository to your `pom.xml`: |
| 25 | + |
| 26 | +```xml |
| 27 | +<repositories> |
| 28 | + <repository> |
| 29 | + <id>github</id> |
| 30 | + <url>https://maven.pkg.github.com/g-portal/a2s-java</url> |
| 31 | + </repository> |
| 32 | +</repositories> |
| 33 | +``` |
| 34 | + |
| 35 | +2. Add the following dependency to your `pom.xml`: |
| 36 | + |
| 37 | +```xml |
| 38 | +<dependency> |
| 39 | + <groupId>com.gportal</groupId> |
| 40 | + <artifactId>a2s</artifactId> |
| 41 | + <version>1.0.0</version> <!-- Use the desired release tag version --> |
| 42 | +</dependency> |
| 43 | +``` |
| 44 | + |
| 45 | +### Gradle |
| 46 | + |
| 47 | +1. Add the following repository to your `build.gradle`: |
| 48 | + |
| 49 | +```gradle |
| 50 | +repositories { |
| 51 | + maven { |
| 52 | + url = uri("https://maven.pkg.github.com/g-portal/a2s-java") |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +2. Add the following to your `build.gradle` dependencies: |
| 58 | + |
| 59 | +```gradle |
| 60 | +dependencies { |
| 61 | + implementation 'com.gportal:a2s:1.0.0' // Use the desired release tag version |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +## Usage |
| 66 | + |
| 67 | +### Querying a Server (Client) |
| 68 | + |
| 69 | +```java |
| 70 | +import com.gportal.source.query.QueryClient; |
| 71 | +import com.gportal.source.query.ServerInfo; |
| 72 | +import com.gportal.source.query.PlayerInfo; |
| 73 | +import java.net.InetSocketAddress; |
| 74 | +import java.util.List; |
| 75 | +import java.util.Map; |
| 76 | +import java.util.concurrent.CompletableFuture; |
| 77 | + |
| 78 | +public class Example { |
| 79 | + public static void main(String[] args) throws Exception { |
| 80 | + QueryClient client = new QueryClient(); |
| 81 | + InetSocketAddress address = new InetSocketAddress("127.0.0.1", 27015); |
| 82 | + |
| 83 | + // Query Server Info |
| 84 | + CompletableFuture<ServerInfo> infoFuture = client.queryServer(address); |
| 85 | + infoFuture.thenAccept(info -> System.out.println("Server Name: " + info.name())); |
| 86 | + |
| 87 | + // Query Players |
| 88 | + CompletableFuture<List<PlayerInfo>> playersFuture = client.queryPlayers(address); |
| 89 | + playersFuture.thenAccept(players -> players.forEach(p -> System.out.println("Player: " + p.name()))); |
| 90 | + |
| 91 | + // Query Rules |
| 92 | + CompletableFuture<Map<String, String>> rulesFuture = client.queryRules(address); |
| 93 | + rulesFuture.thenAccept(rules -> System.out.println("Rules count: " + rules.size())); |
| 94 | + |
| 95 | + // Don't forget to shutdown when done |
| 96 | + // client.shutdown(); |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +### Starting an A2S Server |
| 102 | + |
| 103 | +```java |
| 104 | +import com.gportal.source.query.QueryServer; |
| 105 | +import com.gportal.source.query.ServerInfo; |
| 106 | +import com.gportal.source.query.PlayerInfo; |
| 107 | + |
| 108 | +public class ServerExample { |
| 109 | + public static void main(String[] args) { |
| 110 | + ServerInfo info = new ServerInfo( |
| 111 | + null, (byte)17, "My Java Game Server", "de_dust2", "csgo", "Counter-Strike: Global Offensive", |
| 112 | + (short)730, (byte)0, (byte)20, (byte)0, 'd', 'l', false, true, "1.0.0.0", |
| 113 | + null, null, null, null, null, null |
| 114 | + ); |
| 115 | + |
| 116 | + QueryServer server = new QueryServer(27015, info); |
| 117 | + |
| 118 | + // Add some players or rules |
| 119 | + server.players.add(new PlayerInfo((byte)0, "Gordon Freeman", (short)100, 300.0f)); |
| 120 | + server.rules.put("mp_timelimit", "30"); |
| 121 | + |
| 122 | + System.out.println("A2S Server started on port 27015"); |
| 123 | + } |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +## License |
| 128 | + |
| 129 | +This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details. |
0 commit comments