Skip to content

Commit f93a49b

Browse files
committed
[+] Start server script
1 parent 8eac937 commit f93a49b

File tree

4 files changed

+107
-3
lines changed

4 files changed

+107
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ build
3030
.gradle
3131
data
3232
.mcpm
33+
*.pyc

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
requests
2-
hypy_utils>=1.0.15
2+
hypy_utils>=1.0.16
33
py-cpuinfo
44
tqdm

download_jdk.py renamed to tools/download_jdk.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,20 @@
2525
JAVA = Literal['19', '18', '17', '16', '15', '14', '13', '12', '11', '10', '9', '1.8', '1.7']
2626

2727

28-
def download_oracle(java_ver: JAVA, path: str | Path):
28+
def ensure_java(java_ver: Literal['19', '17'], path: str | Path) -> Path:
29+
path = Path(path)
30+
if not path.is_dir():
31+
download_oracle(java_ver, path)
32+
33+
if sys == 'Darwin':
34+
return path / 'Contents/Home/bin/java'
35+
if sys == 'Linux':
36+
return path / 'bin/java'
37+
if sys == 'Windows':
38+
return path / 'bin/java.exe'
39+
40+
41+
def download_oracle(java_ver: Literal['19', '17'], path: str | Path):
2942
path = Path(path)
3043

3144
# Normalize OS and architecture
@@ -52,7 +65,7 @@ def download_oracle(java_ver: JAVA, path: str | Path):
5265
file = tmp / fname
5366
extract = ensure_dir(tmp / 'extract')
5467

55-
print('Downloading JDK...')
68+
print(f'Downloading JDK from {url}...')
5669
download_file(url, file)
5770

5871
print('Extracting JDK...')

tools/start_server.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)