From 42c2c78503bff14bc3c2a44d3cc3e34a3e7ad8f5 Mon Sep 17 00:00:00 2001 From: Shae Date: Tue, 12 Dec 2023 11:05:39 -0800 Subject: [PATCH] Addded version management ChatCommand --- .../1000-builtin/server_control_commands.py | 51 +++++++++++++++++-- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/_rsplugins/1000-builtin/server_control_commands.py b/_rsplugins/1000-builtin/server_control_commands.py index 0a0c4cb..1814b7e 100644 --- a/_rsplugins/1000-builtin/server_control_commands.py +++ b/_rsplugins/1000-builtin/server_control_commands.py @@ -2,14 +2,16 @@ #> Imports import time +import typing import RS -from RS import Flags, TellRaw, UserManager +from RS import Flags, MinecraftManager, TellRaw, UserManager # Header >/ this.c.mass_set_default('commands/', { 'stop/permission': 'ADMIN', 'restart/permission': 'TRUSTED', + 'version/permission': 'OWNER', }) this.c.set_default('functions/shutdown/default_delay', 5) this.c.mass_set_default('functions/shutdown/warning/', @@ -34,8 +36,8 @@ def _shutdown(tell: RS.UM.User, for_: str, delay: int): this.c['shutdown/warning/message'].format(for_=for_, timefmt=this.c['/shutdown/warning/now']), '#FF0000', TellRaw.TF.BOLD|TellRaw.TF.UNDERLINED) time.sleep(1) - -@RS.CC(permission=UserManager.User.perm_from_value(this.c('commands/stop/permission')), help_section=('Server Control',)) + +@RS.CC(permission=UserManager.User.perm_from_value(this.c['commands/stop/permission']), help_section=('Server Control',)) def stop(user: RS.UM.User, delay: int = this.c['functions/shutdown/default_delay'], announce: bool = True): if not RS.SM.cap_stoppable: raise NotImplementedError(f'Current server manager implementation ({RS.SM.name}) cannot be stopped') @@ -47,3 +49,46 @@ def restart(user: RS.UM.User, delay: int = this.c['functions/shutdown/default_de raise NotImplementedError(f'Current server manager implementation ({RS.SM.name}) cannot be restarted') Flags.force_restart = True _shutdown((RS.UM.User@'a' if announce else user), this.c['shutdown/warning/for_restart'], delay) + +@RS.CC(permission=UserManager.User.perm_from_value(this.c['commands/version/permission']), help_section=('Server Control',)) +def version(user: RS.UM.User, mode: typing.Literal['list', 'query', 'set', 'refresh'], target: str | None = None): + ''' + mode 'list': Lists available version IDs (least to most recent) of a specific type (run without `target` to see types) + mode 'query': Prints the current version + mode 'set': Downloads and installs the version with ID `target` (or latest version if target is unset) + mode 'refresh': Refresh the manifest database + ''' + if mode == 'refresh': + MinecraftManager.refresh() + user.tell(f'Refreshed manifest database, know of {len(MinecraftManager.versions.versions)} version(s)') + return + elif mode == 'query': + user.tell(f'Known server JAr contains version ID {MinecraftManager.jarvers()}') + return + + if MinecraftManager.version_load_time < 0: + raise RuntimeError(f'The versions manifest database has not been initialized yet, perhaps run {RS.CC.compose_command("version", "refresh")}') + + if mode == 'list': + if target is None: + user.tell('Add a target (one of "releases", "snapshots", "other", or "all")') + return + target = target.lower() + if target not in {'releases', 'snapshots', 'other', 'all'}: + raise ValueError(f'target must be one of "releases", "snapshots", "other", or "all", not "{target}"') + user.tell(f'Versions[{target}] known as of {time.ctime(MinecraftManager.version_load_time)}') + if target in {'releases', 'snapshots'}: + vers = getattr(MinecraftManager.versions, target) + elif target == 'other': + vers = MinecraftManager.versions.other['*'] + else: # all + vers = MinecraftManager.versions.versions + user.tell(', '.join(reversed(vers.keys()))) + elif mode == 'set': + if target is None: + raise TypeError('target must be a version, not None') + elif target not in MinecraftManager.versions.versions: + raise KeyError(f'"{target}" is not a known version') + MinecraftManager.install_version(target, chunk_notify=user.tell) + else: + raise RuntimeError(f'The program reached a state that should not be possible under normal conditions, debug: {user=!r} {mode=!r} {target=!r}')