Skip to content

Commit

Permalink
Addded version management ChatCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiger-Tom committed Dec 12, 2023
1 parent 88eb2c4 commit 42c2c78
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions _rsplugins/1000-builtin/server_control_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

#> Imports
import time
import typing
import RS
from RS import Flags, TellRaw, UserManager
from RS import Flags, MinecraftManager, TellRaw, UserManager
#</Imports

#> 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/',
Expand All @@ -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')
Expand All @@ -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}')

0 comments on commit 42c2c78

Please sign in to comment.