#!/usr/bin/env python3 """Schritt 2: Erzeugt mit dem unveränderten Vanilla-Server EINE Welt (fester Seed, vorab generierter Bereich) und legt je Programm eine Vorlage an. Jeder Messlauf startet später von einer frischen Kopie derselben Welt. Aufruf: flock /root/studien/.lock python3 02-vorlagen.py""" import json, shutil, subprocess, sys, time from pathlib import Path sys.path.insert(0, str(Path(__file__).parent)) from gemeinsam import * RAND = 88 # Chunks je Richtung um den Ursprung (±1408 Blöcke) BLOCK = 16 # forceload erlaubt höchstens 256 Chunks je Befehl def erzeuge_welt(): ziel = VORLAGEN / 'welt' if (ziel / '.fertig').exists(): print('Welt liegt schon vor', flush=True) return o = LAUF / 'weltbau' shutil.rmtree(o, ignore_errors=True) o.mkdir(parents=True) shutil.copy(JARS / 'vanilla-26.3.jar', o / 'server.jar') schreibe_eigenschaften(o) log = o / 'konsole.log' cmd = [JAVA, '-Xms10G', '-Xmx10G', '-XX:+UseG1GC', '-jar', 'server.jar', '--nogui'] p = subprocess.Popen(cmd, cwd=o, stdin=subprocess.PIPE, stdout=open(log, 'ab'), stderr=subprocess.STDOUT, start_new_session=True) try: warte_bereit(p, log) r = Rcon(rcon_passwort()) t0 = time.time() bloecke = [(x, z) for x in range(-RAND, RAND + 1, BLOCK) for z in range(-RAND, RAND + 1, BLOCK)] for n, (cx, cz) in enumerate(bloecke, 1): x1, z1 = cx * 16, cz * 16 x2, z2 = (min(cx + BLOCK - 1, RAND)) * 16, (min(cz + BLOCK - 1, RAND)) * 16 r.befehl(f'forceload add {x1} {z1} {x2} {z2}') frist = time.time() + 300 while time.time() < frist: a = r.befehl(f'execute if loaded {x1} 64 {z1} if loaded {x2} 64 {z2} if loaded {x1} 64 {z2} if loaded {x2} 64 {z1}') if 'Test passed' in a: break time.sleep(0.25) else: raise TimeoutError(f'Block {n} nicht geladen') r.befehl(f'forceload remove {x1} {z1} {x2} {z2}') if n % 12 == 0: print(f'{n}/{len(bloecke)} Blöcke, {time.time() - t0:.0f} s', flush=True) r.befehl('save-all flush') time.sleep(20) stoppe(p, r) finally: if p.poll() is None: p.kill() shutil.rmtree(ziel, ignore_errors=True) shutil.copytree(o / 'world', ziel) (ziel / '.fertig').write_text(time.strftime('%FT%TZ', time.gmtime())) print('Welt fertig', subprocess.run(['du', '-sh', str(ziel)], capture_output=True, text=True).stdout.strip(), flush=True) def vorlage(name): info = PROGRAMME[name] o = VORLAGEN / name if (o / '.fertig').exists(): print(name, 'liegt schon vor', flush=True) return shutil.rmtree(o, ignore_errors=True) o.mkdir(parents=True) shutil.copy(JARS / info['jar'], o / info['start']) if name == 'fabric-lithium': shutil.copy(JARS / 'vanilla-26.3.jar', o / 'server.jar') (o / 'mods').mkdir() shutil.copy(JARS / 'lithium-fabric-0.26.4+mc26.3.jar', o / 'mods') schreibe_eigenschaften(o) log = o / 'erststart.log' p, _ = starte_java(o, info['start'], ['-XX:+UseG1GC'], log) try: warte_bereit(p, log) stoppe(p, Rcon(rcon_passwort())) finally: if p.poll() is None: p.kill() for rest in ('world', 'world_nether', 'world_the_end', 'logs', 'crash-reports'): shutil.rmtree(o / rest, ignore_errors=True) if name in ('paper', 'purpur'): b = o / 'bukkit.yml' t = b.read_text() if 'connection-throttle: 4000' not in t: raise RuntimeError('connection-throttle nicht gefunden') # Alle Bots kommen von 127.0.0.1; die Standard-Drossel je IP würde sonst Anmeldungen abweisen. b.write_text(t.replace('connection-throttle: 4000', 'connection-throttle: -1')) (o / '.fertig').write_text(time.strftime('%FT%TZ', time.gmtime())) print(name, 'Vorlage fertig', flush=True) if __name__ == '__main__': erzeuge_welt() for n in PROGRAMME: vorlage(n) print('VORLAGEN_OK', flush=True)