2016-04-16 191 views
1

我想寫一個Haskell腳本來處理與一個Minecraft服務器的交互。Minecraft服務器管理器

要將命令發送到服務器,我有一個文件server.cmd,其中第一行1命令可寫入應在服務器中執行的命令(例如stop)。

所以,這裏是我的代碼:

-- servermanager.hs 

{-# OPTIONS_GHC -fno-warn-warnings-deprecations #-} 

module ServerManager where 

import System.Process 
import System.Exit 
import GHC.IO.Handle 
import Control.Monad 
import System.Posix.Unistd 

managerfile :: FilePath 
managerfile = "manager.cmd" 

serverfile :: FilePath 
serverfile = "server.cmd" 

main :: IO() 
main = do 
    (Just hin, _, _, _) <- createProcess (proc "java" ["-jar", "minecraft_server.1.8.9.jar", "nogui"]) {cwd = Just "/home/tekkkz/Downloads", std_in = CreatePipe, std_out = CreatePipe} 
    sleep 20 
    servercmd <- readFile serverfile 
    case servercmd of 
    "stop" -> do 
     putStrLn ">> [S] Stop" 
     hPutStr hin "stop" 
    _ -> return() 

當在我server.cmd文件「停止」,其打印的出字符串,但不停止服務器......爲什麼不呢?

回答

3

由於IO懶惰,您沒有刷新管道,因此程序在hPutStr有機會完成其工作之前默默終止。

嘗試加入這一行hPutStr hin "stop"後:

hFlush hin