|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import os.path |
| 5 | +import re |
| 6 | +import select |
| 7 | +import shutil |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +import tarfile |
| 11 | +from pathlib import Path |
| 12 | +from subprocess import check_call |
| 13 | +from tempfile import TemporaryDirectory |
| 14 | + |
| 15 | +from hypy_utils import ensure_dir |
| 16 | +from hypy_utils.downloader import download_file |
| 17 | + |
| 18 | +from .download_jdk import download_oracle, ensure_java |
| 19 | + |
| 20 | +MAX_RAM = '16384M' |
| 21 | +MIN_RAM = MAX_RAM |
| 22 | +TIME = 5 |
| 23 | + |
| 24 | +version_re = re.compile(r'[0-9].[0-9]+.[0-9]+') |
| 25 | + |
| 26 | + |
| 27 | +def start(java: Path, mc_path: Path): |
| 28 | + # Find server jar |
| 29 | + jar = '' |
| 30 | + for f in [str(f) for f in os.listdir(mc_path) if f.endswith('.jar')]: |
| 31 | + ver = version_re.findall(f) |
| 32 | + if any(k in f.lower() for k in ['craftbukkit', 'spigot', 'paper', 'purpur']) or len(ver) != 0: |
| 33 | + jar = f |
| 34 | + break |
| 35 | + |
| 36 | + # Auto Restart |
| 37 | + while True: |
| 38 | + cmd = f"{java.absolute()} -Xmx{MAX_RAM} -Xms{MIN_RAM} --add-modules=jdk.incubator.vector -jar {jar} nogui" |
| 39 | + print(f'Executing {cmd}...') |
| 40 | + subprocess.Popen(cmd, shell=True, cwd=mc_path).wait() |
| 41 | + |
| 42 | + print('Server stopped, restarting in 5s\nPress any key to stop the server.') |
| 43 | + i, o, e = select.select([sys.stdin], [], [], 5) |
| 44 | + if i: |
| 45 | + print('Server stops.') |
| 46 | + exit(0) |
| 47 | + |
| 48 | + |
| 49 | +if __name__ == '__main__': |
| 50 | + # Download JDK |
| 51 | + java = ensure_java("19", 'build/jdk19') |
| 52 | + |
| 53 | + # Download server tar |
| 54 | + mc_path = Path('build/mc-server') |
| 55 | + if not mc_path.is_dir(): |
| 56 | + with TemporaryDirectory() as tmp: |
| 57 | + tmp = Path(tmp) |
| 58 | + tgz = tmp / 'server.tgz' |
| 59 | + ext = tmp / 'ext' |
| 60 | + |
| 61 | + # Download tarball |
| 62 | + print('Downloading server tarball...') |
| 63 | + download_file('https://github.com/hykilpikonna/mcpm-test-server/tarball/master', tgz) |
| 64 | + |
| 65 | + # Extract |
| 66 | + print('Extracting server...') |
| 67 | + with tarfile.open(tgz) as f: |
| 68 | + f.extractall(ext) |
| 69 | + |
| 70 | + print('Copying server...') |
| 71 | + shutil.move(ext / str(os.listdir(ext)[0]), mc_path) |
| 72 | + |
| 73 | + print(f'Server installed to {mc_path}') |
| 74 | + |
| 75 | + # Build project |
| 76 | + print('Building project...') |
| 77 | + build_jar = Path('build/libs') |
| 78 | + shutil.rmtree(build_jar, ignore_errors=True) |
| 79 | + check_call('./gradlew build', shell=True) |
| 80 | + |
| 81 | + # Install plugin |
| 82 | + print('Installing our MCPM plugin...') |
| 83 | + plugin_path = mc_path / 'plugins/mcpm.jar' |
| 84 | + shutil.rmtree(plugin_path, ignore_errors=True) |
| 85 | + ensure_dir(plugin_path.parent) |
| 86 | + shutil.copy2(build_jar / str(os.listdir(build_jar)[0]), plugin_path) |
| 87 | + |
| 88 | + # Start server |
| 89 | + print('Starting server...') |
| 90 | + start(java, mc_path) |
0 commit comments