Skip to content

Commit

Permalink
added windows compatibility
Browse files Browse the repository at this point in the history
+ bug fixes
  • Loading branch information
gekigek99 authored Aug 14, 2020
1 parent 05f9ea1 commit 2509205
Showing 1 changed file with 45 additions and 19 deletions.
64 changes: 45 additions & 19 deletions go-version/minecraft-vanilla-server-hibernation.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ import (
"net"
"os"
"os/exec"
"runtime"
"strings"
"sync"
"time"
)

//------------------------modify-------------------------------//

var startminecraftserverwin = []string{"java", "-Xmx1024M", "-Xms1024M", "-jar", "server.jar", "nogui"}

const stopminecraftserverwin = "stop"
const startminecraftserver = "systemctl start minecraft-server"
const stopminecraftserver = "systemctl stop minecraft-server"

Expand All @@ -45,6 +49,34 @@ var serverstatus string = "offline"
var timelefttillup int = minecraftserverstartuptime
var stopinstances int = 0
var mutex = &sync.Mutex{}
var cmdIn io.WriteCloser

//StartMinecraftServer starts the minecraft server
func StartMinecraftServer() {
if serverstatus != "offline" {
return
}
serverstatus = "starting"

if runtime.GOOS == "linux" {
err := exec.Command("/bin/bash", "-c", startminecraftserver).Run()
if err != nil {
log.Printf("error starting minecraft server: %v\n", err)
}
} else if runtime.GOOS == "windows" {
cmd := exec.Command(startminecraftserverwin[0], startminecraftserverwin[1:]...)
cmdIn, _ = cmd.StdinPipe()
cmd.Start()
} else {
log.Print("error: OS not supported!")
os.Exit(1)
}

log.Print("*** MINECRAFT SERVER IS STARTING!")
players = 0
UpdateTimeleft()
go Timer(minecraftserverstartuptime, SetServerStatusOnline)
}

//StopEmptyMinecraftServer stops the minecraft server
func StopEmptyMinecraftServer() {
Expand All @@ -56,30 +88,24 @@ func StopEmptyMinecraftServer() {
}
mutex.Unlock()
serverstatus = "offline"
err := exec.Command("/bin/bash", "-c", stopminecraftserver).Run()
if err != nil {
log.Printf("error stopping minecraft server: %v\n", err)

if runtime.GOOS == "linux" {
err := exec.Command("/bin/bash", "-c", stopminecraftserver).Run()
if err != nil {
log.Printf("error stopping minecraft server: %v\n", err)
}
} else if runtime.GOOS == "windows" {
cmdIn.Write([]byte(stopminecraftserverwin))
cmdIn.Close()
} else {
log.Print("error: OS not supported!")
os.Exit(1)
}

log.Print("*** MINECRAFT SERVER IS SHUTTING DOWN!")
timelefttillup = minecraftserverstartuptime
}

//StartMinecraftServer starts the minecraft server
func StartMinecraftServer() {
if serverstatus != "offline" {
return
}
serverstatus = "starting"
err := exec.Command("/bin/bash", "-c", startminecraftserver).Run()
if err != nil {
log.Printf("error starting minecraft server: %v\n", err)
}
log.Print("*** MINECRAFT SERVER IS STARTING!")
players = 0
UpdateTimeleft()
go Timer(minecraftserverstartuptime, SetServerStatusOnline)
}

//SetServerStatusOnline sets the server status online
func SetServerStatusOnline() {
serverstatus = "online"
Expand Down

0 comments on commit 2509205

Please sign in to comment.