Rewritten and finished framework and bot

This commit is contained in:
Malasaur 2025-08-28 20:39:20 +02:00
parent f045967591
commit 49a9964ec5
7 changed files with 150 additions and 33 deletions

48
libminecraft.py Normal file
View file

@ -0,0 +1,48 @@
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("python -u program.py")
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()