Merge branch 'feature/paper-builds' into 'dev'

Implement paper builds using paper API

See merge request crafty-controller/crafty-4!688
This commit is contained in:
Iain Powrie 2024-01-16 18:57:38 +00:00
commit c89dbed26c
4 changed files with 66 additions and 7 deletions

View File

@ -1,7 +1,7 @@
# Changelog
## --- [4.2.3] - 2023/TBD
### New features
TBD
- Use Papermc Group's API for `paper` & `folia` builds in server builder ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/688))
### Bug fixes
- Fix bukkit and downstream fork MOTD crash ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/686))
### Tweaks

View File

@ -11,12 +11,18 @@ from app.classes.models.server_permissions import PermissionsServers
from app.classes.shared.websocket_manager import WebSocketManager
logger = logging.getLogger(__name__)
PAPERJARS = ["paper", "folia"]
class ServerJars:
def __init__(self, helper):
self.helper = helper
self.base_url = "https://serverjars.com"
self.paper_base = "https://api.papermc.io"
@staticmethod
def get_paper_jars():
return PAPERJARS
def _get_api_result(self, call_url: str):
full_url = f"{self.base_url}{call_url}"
@ -38,6 +44,40 @@ class ServerJars:
return api_response
def get_paper_versions(self, project):
try:
response = requests.get(
f"{self.paper_base}/v2/projects/{project}/", timeout=2
)
response.raise_for_status()
api_data = json.loads(response.content)
except Exception as e:
logger.error(
f"Unable to load https://api.papermc.io/v2/projects/{project}/"
f"api due to error: {e}"
)
return {}
versions = api_data.get("versions", [])
versions.reverse()
return versions
def get_paper_build(self, project, version):
try:
response = requests.get(
f"{self.paper_base}/v2/projects/{project}/versions/{version}/builds/",
timeout=2,
)
response.raise_for_status()
api_data = json.loads(response.content)
except Exception as e:
logger.error(
f"Unable to load https://api.papermc.io/v2/projects/{project}/"
f"api due to error: {e}"
)
return {}
build = api_data.get("builds", [])[-1]
return build
def _read_cache(self):
cache_file = self.helper.serverjar_cache
cache = {}
@ -95,6 +135,8 @@ class ServerJars:
for j in data["types"].get(s):
versions = self._get_jar_details(j, s)
data["types"][s].update({j: versions})
for item in PAPERJARS:
data["types"]["servers"][item] = self.get_paper_versions(item)
# save our cache
try:
with open(cache_file, "w", encoding="utf-8") as f:
@ -133,6 +175,8 @@ class ServerJars:
for j in data["types"].get(s):
versions = self._get_jar_details(j, s)
data["types"][s].update({j: versions})
for item in PAPERJARS:
data["types"]["servers"][item] = self.get_paper_versions(item)
# save our cache
try:
with open(cache_file, "w", encoding="utf-8") as f:
@ -171,7 +215,17 @@ class ServerJars:
def a_download_jar(self, jar, server, version, path, server_id):
# delaying download for server register to finish
time.sleep(3)
fetch_url = f"{self.base_url}/api/fetchJar/{jar}/{server}/{version}"
if server not in PAPERJARS:
fetch_url = f"{self.base_url}/api/fetchJar/{jar}/{server}/{version}"
else:
build = self.get_paper_build(server, version).get("build", None)
if not build:
return
fetch_url = (
f"{self.paper_base}/v2/projects"
f"/{server}/versions/{version}/builds/{build}/downloads/"
f"{server}-{version}-{build}.jar"
)
server_users = PermissionsServers.get_server_user_list(server_id)
# We need to make sure the server is registered before

View File

@ -536,10 +536,14 @@ class Controller:
if data["create_type"] == "minecraft_java":
if root_create_data["create_type"] == "download_jar":
# modded update urls from server jars will only update the installer
if create_data["category"] != "modded":
if (
create_data["category"] != "modded"
and create_data["type"] not in ServerJars.get_paper_jars()
):
server_obj = self.servers.get_server_obj(new_server_id)
url = (
f"https://serverjars.com/api/fetchJar/{create_data['category']}"
"https://serverjars.com/api/fetchJar/"
f"{create_data['category']}"
f"/{create_data['type']}/{create_data['version']}"
)
server_obj.executable_update_url = url

View File

@ -43,6 +43,7 @@ with redirect_stderr(NullWriter()):
from psutil import NoSuchProcess
logger = logging.getLogger(__name__)
SUCCESSMSG = "SUCCESS! Forge install completed"
def callback(called_func):
@ -723,7 +724,7 @@ class ServerInstance:
f' -jar "{file_name}" nogui'
)
server_obj.execution_command = execution_command
Console.debug("SUCCESS! Forge install completed")
Console.debug(SUCCESSMSG)
elif version_major <= 1 and version_minor < 20:
# NEW VERSION >= 1.17 and <= 1.20
@ -771,7 +772,7 @@ class ServerInstance:
f" {server_command[4]}"
)
server_obj.execution_command = execution_command
Console.debug("SUCCESS! Forge install completed")
Console.debug(SUCCESSMSG)
else:
# NEW VERSION >= 1.20
# (executable jar is back in server dir)
@ -802,7 +803,7 @@ class ServerInstance:
f' -jar "{file_name}" nogui'
)
server_obj.execution_command = execution_command
Console.debug("SUCCESS! Forge install completed")
Console.debug(SUCCESSMSG)
except:
logger.debug("Could not find run file.")
# TODO Use regex to get version and rebuild simple execution