From 2714a852d470f841b7e9101cc3cc761d394cccfd Mon Sep 17 00:00:00 2001 From: MCHost Date: Mon, 16 Jun 2025 10:22:44 -0400 Subject: [PATCH] first commit --- .gitignore | 2 + README.md | 118 +++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 5 ++ go.sum | 2 + status.go | 40 ++++++++++++++++ statusBedrock.go | 40 ++++++++++++++++ 6 files changed, 207 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 status.go create mode 100644 statusBedrock.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..44eb49b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +status +statusBedrock diff --git a/README.md b/README.md new file mode 100644 index 0000000..b3e056a --- /dev/null +++ b/README.md @@ -0,0 +1,118 @@ +# Minecraft Server Status Checker + +This project provides two Go programs to query the status of Minecraft servers: one for Bedrock Edition (`statusBedrock.go`) and one for Java Edition (`status.go`). Both programs use the `mcstatus-io/mcutil` library to fetch server information and output the results in formatted JSON. + +## Features + +- Query Minecraft server status for Bedrock or Java Edition. +- Configurable hostname and port via command-line flags. +- Timeout handling to prevent hanging on unresponsive servers. +- Formatted JSON output for easy parsing or reading. +- Lightweight and simple to use. + +## Prerequisites + +- [Go](https://golang.org/dl/) (version 1.16 or later recommended) +- Internet access to query the Minecraft server +- The `mcstatus-io/mcutil` library (automatically installed via `go get`) + +## Installation + +1. Clone or download this repository: + ```bash + git clone https://git.ssh.surf/hypermc/mc-status.git + cd mc-status + ``` + +2. Install the required Go module: + ```bash + go get github.com/mcstatus-io/mcutil/v4 + ``` + +3. Ensure your Go environment is set up correctly (`go mod init` if needed). + +## Usage + +### Bedrock Edition (`statusBedrock.go`) + +This program queries a Minecraft Bedrock Edition server. + +1. Compile the program: + ```bash + go build statusBedrock.go + ``` + +2. Run the program with default settings (queries `my-mc.link:25565`): + ```bash + ./statusBedrock + ``` + +3. Customize the hostname and port using flags: + ```bash + ./statusBedrock -host=example.com -port=19132 + ``` + +### Java Edition (`status.go`) + +This program queries a Minecraft Java Edition server. + +1. Compile the program: + ```bash + go build status.go + ``` + +2. Run the program with default settings (queries `my-mc.link:25565`): + ```bash + ./status.go + ``` + +3. Customize the hostname and port using flags: + ```bash + ./status.go -host=example.com -port=25565 + ``` + +### Example Output + +Running either program will output the server status in JSON format. For example: + +```json +{ + "version": { + "name": "1.20.1", + "protocol": 763 + }, + "players": { + "online": 5, + "max": 20 + }, + "description": { + "text": "Welcome to My Minecraft Server!" + }, + "favicon": "data:image/png;base64,...", + "ping": 45 +} +``` + +### Command-Line Flags + +Both programs support the following flags: + +- `-host`: Specifies the server hostname (default: `my-mc.link`). +- `-port`: Specifies the server port (default: `25565` for Java, often `19132` for Bedrock). + +## Notes + +- The Bedrock program (`statusBedrock.go`) uses a 20-second timeout, while the Java program (`status.go`) uses a 5-second timeout. Adjust these in the source code if needed. +- Ensure the server is online and accessible. Firewalls or network issues may cause errors. +- The `mcstatus-io/mcutil` library handles the protocol details, supporting modern Minecraft server versions. + +## Troubleshooting + +- **Error: "context deadline exceeded"**: The server is unreachable or took too long to respond. Check the hostname, port, and server status. +- **Error: "json: cannot unmarshal"**: The server response was malformed. Ensure the server is running a compatible version. +- **Dependency issues**: Run `go mod tidy` to resolve missing or outdated dependencies. + +## Acknowledgments + +- Thanks to [mcstatus-io](https://github.com/mcstatus-io) for the `mcutil` library. +- Inspired by the need for simple, reliable Minecraft server status tools. \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..17c2769 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module minecraft_util + +go 1.24.4 + +require github.com/mcstatus-io/mcutil/v4 v4.0.1 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..a0a3220 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/mcstatus-io/mcutil/v4 v4.0.1 h1:/AQkHrz7irCU7USGnrH3kneQw80aDQOVdOWc8xu/NUY= +github.com/mcstatus-io/mcutil/v4 v4.0.1/go.mod h1:yC91WInI1U2GAMFWgpPgsAULPVS2o+4JCZbiiWhHwxM= diff --git a/status.go b/status.go new file mode 100644 index 0000000..e64cac1 --- /dev/null +++ b/status.go @@ -0,0 +1,40 @@ +package main + +import ( + "context" + "encoding/json" + "flag" + "fmt" + "os" + "time" + + "github.com/mcstatus-io/mcutil/v4/status" +) + +func main() { + // Define flags + hostname := flag.String("host", "my-mc.link", "Minecraft server hostname") + port := flag.Int("port", 25565, "Minecraft server port") + flag.Parse() + + // Create context with timeout + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + defer cancel() + + // Query server status + response, err := status.Modern(ctx, *hostname, uint16(*port)) + if err != nil { + fmt.Fprintf(os.Stderr, "Error: %v\n", err) + os.Exit(1) + } + + // Convert response to formatted JSON + jsonOutput, err := json.MarshalIndent(response, "", " ") + if err != nil { + fmt.Fprintf(os.Stderr, "Error marshaling JSON: %v\n", err) + os.Exit(1) + } + + // Print JSON + fmt.Println(string(jsonOutput)) +} diff --git a/statusBedrock.go b/statusBedrock.go new file mode 100644 index 0000000..6779d9a --- /dev/null +++ b/statusBedrock.go @@ -0,0 +1,40 @@ +package main + +import ( + "context" + "encoding/json" + "flag" + "fmt" + "os" + "time" + + "github.com/mcstatus-io/mcutil/v4/status" +) + +func main() { + // Define flags + hostname := flag.String("host", "my-mc.link", "Minecraft server hostname") + port := flag.Int("port", 25565, "Minecraft server port") + flag.Parse() + + // Create context with timeout + ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) + defer cancel() + + // Query server status + response, err := status.Bedrock(ctx, *hostname, uint16(*port)) + if err != nil { + fmt.Fprintf(os.Stderr, "Error: %v\n", err) + os.Exit(1) + } + + // Convert response to formatted JSON + jsonOutput, err := json.MarshalIndent(response, "", " ") + if err != nil { + fmt.Fprintf(os.Stderr, "Error marshaling JSON: %v\n", err) + os.Exit(1) + } + + // Print JSON + fmt.Println(string(jsonOutput)) +}