mc_man/mc_man.go
2014-12-21 13:30:42 -06:00

135 lines
3.3 KiB
Go

package main
import (
"fmt"
"github.com/br0xen/mc_man/util"
"log"
"os"
"os/exec"
"strings"
)
func main() {
// Load the Config
util.LoadConfig()
xmxVal := "1024M"
xmsVal := "1024M"
args := os.Args[1:]
if len(args) > 0 {
if args[0] == "-help" {
fmt.Println("Usage: mc_man <Xmx Value> <Xms Value>")
fmt.Println(" <Xmx Value> - The Maximum Memory Allocation Pool for the JVM")
fmt.Println(" <Xms Value> - The Initial Memory Allocation Pool")
os.Exit(0)
}
if len(args) > 0 {
xmxVal = args[0]
if len(args) > 1 {
xmsVal = args[1]
}
}
}
// The minecraft server command
cmd := exec.Command("java", "-Xmx"+xmxVal, "-Xms"+xmsVal, "-jar", "minecraft_server.jar", "nogui")
// Control StdIn
stdin, err := cmd.StdinPipe()
if err != nil {
log.Fatal(err)
}
// Pipe the process' stdout to stdout for monitoring
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
// Kick off the process
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
// Build a string channel for stdout
ch := make(chan string)
// And a routine to keep the channel updated
go func() {
buf := make([]byte, 1024)
for {
n, err := stdout.Read(buf)
if n != 0 {
// Something there throw it into the channel
ch <- string(buf[:n])
}
if err != nil {
// Error, break out of loop
break
}
}
fmt.Println("mc_man stopped")
close(ch)
}()
listen_for := ""
listen_for_user := ""
// The forever loop to monitor the channel
loop:
for {
s, ok := <-ch
if !ok {
break loop
}
m := util.NewMessage(s)
// First check for anything we're listening for
if listen_for != "" && strings.Contains(m.Text, listen_for) {
r := strings.Split(m.Text, listen_for)
if len(r) > 0 {
p_str := r[1]
p_str = strings.Replace(p_str, ",", "", -1)
util.SetHome(listen_for_user, p_str)
stdin.Write([]byte("tell " + listen_for_user + " Set your home to " + p_str))
listen_for = ""
listen_for_user = ""
}
} else {
if m.IsStopRequest() {
stdin.Write([]byte("stop\n"))
} else if m.IsHomeRequest() {
home_str, found := util.GetHome(m.User.Name)
if found {
stdin.Write([]byte("tp " + m.User.Name + " " + home_str + "\n"))
}
} else if m.IsSetHomeRequest() {
listen_for = "Teleported " + m.User.Name + " to "
listen_for_user = m.User.Name
stdin.Write([]byte("tp " + m.User.Name + " ~ ~ ~\n"))
} else if m.IsVisitRequest() {
visiting_user, err := m.VisitingUser()
if !err {
fmt.Printf("\x1b[31;1m%s requested a tp visit to %s\x1b[0m\n", m.User.Name, visiting_user)
porch_str, found := util.GetPorch(visiting_user)
fmt.Printf(">>> Porch String: " + porch_str)
if found {
stdin.Write([]byte("tp " + m.User.Name + " " + porch_str + "\n"))
} else {
stdin.Write([]byte("tell " + m.User.Name + " Couldn't find that user's porch 1\n"))
}
} else {
stdin.Write([]byte("tell " + m.User.Name + " Couldn't find that user's porch 2\n"))
}
} else if m.IsSetPorchRequest() {
listen_for = "Teleported " + m.User.Name + " to "
listen_for_user = m.User.Name
stdin.Write([]byte("tp " + m.User.Name + " ~ ~ ~\n"))
} else {
// fmt.Printf("\x1b[34;1m%s\x1b[0m", m.Output())
}
}
fmt.Printf("\x1b[34;1m%s\x1b[0m", m.Output())
}
}