48 lines
1.2 KiB
Python
48 lines
1.2 KiB
Python
from threading import Thread
|
|
from typing import Callable
|
|
from re import compile
|
|
|
|
import libcommon
|
|
|
|
|
|
def reader() -> None:
|
|
if libcommon.minecraftProc.stdout:
|
|
for line in libcommon.minecraftProc.stdout:
|
|
line = line.rstrip("\n")
|
|
print(line)
|
|
for pattern, func in libcommon.minecraftOutputBinds:
|
|
match = pattern.match(line)
|
|
if match:
|
|
func(*match.groups())
|
|
break
|
|
|
|
|
|
def onConsoleOutput(pattern: str) -> Callable:
|
|
def decorator(func: Callable) -> Callable:
|
|
libcommon.minecraftOutputBinds.append((compile(pattern), func))
|
|
return func
|
|
return decorator
|
|
|
|
|
|
def sendCommand(cmd: str) -> None:
|
|
if libcommon.minecraftProc.stdin:
|
|
libcommon.minecraftProc.stdin.write(cmd+"\n")
|
|
libcommon.minecraftProc.stdin.flush()
|
|
|
|
|
|
def main() -> None:
|
|
libcommon.minecraftInit("sudo ./start.sh")
|
|
|
|
t = Thread(target=reader, daemon=True)
|
|
t.start()
|
|
|
|
try:
|
|
while libcommon.minecraftProc.poll() is None:
|
|
sendCommand(input())
|
|
except KeyboardInterrupt:
|
|
sendCommand("stop")
|
|
libcommon.minecraftProc.wait()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|