first commit
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
status
|
||||
statusBedrock
|
118
README.md
Normal file
118
README.md
Normal file
@ -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.
|
5
go.mod
Normal file
5
go.mod
Normal file
@ -0,0 +1,5 @@
|
||||
module minecraft_util
|
||||
|
||||
go 1.24.4
|
||||
|
||||
require github.com/mcstatus-io/mcutil/v4 v4.0.1 // indirect
|
2
go.sum
Normal file
2
go.sum
Normal file
@ -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=
|
40
status.go
Normal file
40
status.go
Normal file
@ -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))
|
||||
}
|
40
statusBedrock.go
Normal file
40
statusBedrock.go
Normal file
@ -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))
|
||||
}
|
Reference in New Issue
Block a user