Skip to content

simulatecraft.minecraft.rcon

simulatecraft.minecraft.rcon

Minecraft console commands via RCON, with docker rcon-cli fallback.

Used for watcher role assignment (OP / spectator) and agent teleport.

RconError

Bases: RuntimeError

RCON authentication or command failure.

MinecraftRcon

MinecraftRcon(host: str, port: int, password: str, *, timeout: float = 5.0)

Blocking RCON session. Prefer short-lived use per command batch.

Source code in src/simulatecraft/minecraft/rcon.py
def __init__(self, host: str, port: int, password: str, *, timeout: float = 5.0) -> None:
    self.host = host
    self.port = port
    self.password = password
    self.timeout = timeout
    self._sock: socket.socket | None = None
    self._req_id = 0

default_rcon_settings

default_rcon_settings() -> tuple[str, int, str]

Host/port/password from env, with SimulateCraft defaults.

Source code in src/simulatecraft/minecraft/rcon.py
def default_rcon_settings() -> tuple[str, int, str]:
    """Host/port/password from env, with SimulateCraft defaults."""
    host = os.getenv("RCON_HOST", os.getenv("MC_RCON_HOST", "127.0.0.1")).strip() or "127.0.0.1"
    port = int(os.getenv("RCON_PORT", os.getenv("MC_RCON_PORT", "25575")))
    password = os.getenv("RCON_PASSWORD", os.getenv("MC_RCON_PASSWORD", "simulatecraft")).strip()
    if not password:
        password = "simulatecraft"
    return host, port, password

run_commands

run_commands(commands: list[str], *, host: str | None = None, port: int | None = None, password: str | None = None) -> list[str]

Run console commands via TCP RCON, falling back to docker rcon-cli.

Source code in src/simulatecraft/minecraft/rcon.py
def run_commands(
    commands: list[str],
    *,
    host: str | None = None,
    port: int | None = None,
    password: str | None = None,
) -> list[str]:
    """Run console commands via TCP RCON, falling back to docker rcon-cli."""
    if not commands:
        return []
    dh, dp, dw = default_rcon_settings()
    try:
        with MinecraftRcon(host or dh, port if port is not None else dp, password or dw) as rcon:
            return [rcon.command(cmd) for cmd in commands]
    except Exception as exc:
        log.warning("TCP RCON failed (%s); trying docker rcon-cli", exc)
        try:
            return _docker_rcon_cli(commands)
        except Exception as fallback_exc:
            raise RconError(
                f"Could not reach Minecraft RCON ({exc}). "
                f"Docker fallback also failed ({fallback_exc}). "
                "Is the server up with ENABLE_RCON=true on port 25575?"
            ) from fallback_exc