2022-07-01 14:28:39 +04:00
|
|
|
"""Modifying server and world options"""
|
|
|
|
|
2022-01-11 18:08:59 +04:00
|
|
|
import enum
|
2021-10-15 15:12:45 +04:00
|
|
|
import re
|
|
|
|
import lxml.html
|
2022-06-17 12:30:58 +04:00
|
|
|
from typing import Any, Dict, List, Union, Optional
|
2021-10-15 19:31:47 +04:00
|
|
|
from typing import TYPE_CHECKING
|
2021-10-15 15:12:45 +04:00
|
|
|
|
2021-10-15 19:31:47 +04:00
|
|
|
if TYPE_CHECKING:
|
2022-06-23 15:13:56 +04:00
|
|
|
from .atserver import AternosServer
|
|
|
|
|
|
|
|
DAT_PREFIX = 'Data:'
|
|
|
|
DAT_GR_PREFIX = 'Data:GameRules:'
|
|
|
|
|
2021-10-15 19:31:47 +04:00
|
|
|
|
2022-01-11 18:08:59 +04:00
|
|
|
class ServerOpts(enum.Enum):
|
2022-06-17 12:30:58 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
"""server.options file"""
|
|
|
|
|
|
|
|
players = 'max-players'
|
|
|
|
gm = 'gamemode'
|
|
|
|
difficulty = 'difficulty'
|
|
|
|
whl = 'white-list'
|
|
|
|
online = 'online-mode'
|
|
|
|
pvp = 'pvp'
|
|
|
|
cmdblock = 'enable-command-block'
|
|
|
|
flight = 'allow-flight'
|
|
|
|
animals = 'spawn-animals'
|
|
|
|
monsters = 'spawn-monsters'
|
|
|
|
villagers = 'spawn-npcs'
|
|
|
|
nether = 'allow-nether'
|
|
|
|
forcegm = 'force-gamemode'
|
|
|
|
spawnlock = 'spawn-protection'
|
|
|
|
cmds = 'allow-cheats'
|
|
|
|
packreq = 'require-resource-pack'
|
|
|
|
pack = 'resource-pack'
|
2021-10-15 15:12:45 +04:00
|
|
|
|
2022-01-11 18:08:59 +04:00
|
|
|
|
|
|
|
class WorldOpts(enum.Enum):
|
2022-06-17 12:30:58 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
"""level.dat file"""
|
|
|
|
|
|
|
|
seed12 = 'randomseed'
|
|
|
|
seed = 'seed'
|
|
|
|
hardcore = 'hardcore'
|
|
|
|
difficulty = 'Difficulty'
|
2022-06-17 12:30:58 +04:00
|
|
|
|
2021-10-15 15:12:45 +04:00
|
|
|
|
2022-01-11 18:08:59 +04:00
|
|
|
class WorldRules(enum.Enum):
|
2022-06-17 12:30:58 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
"""/gamerule list"""
|
|
|
|
|
|
|
|
advs = 'announceAdvancements'
|
|
|
|
univanger = 'universalAnger'
|
|
|
|
cmdout = 'commandBlockOutput'
|
|
|
|
elytra = 'disableElytraMovementCheck'
|
|
|
|
raids = 'disableRaids'
|
|
|
|
daynight = 'doDaylightCycle'
|
|
|
|
entdrop = 'doEntityDrops'
|
|
|
|
fire = 'doFireTick'
|
|
|
|
phantoms = 'doInsomnia'
|
|
|
|
immrespawn = 'doImmediateRespawn'
|
|
|
|
limitcraft = 'doLimitedCrafting'
|
|
|
|
mobloot = 'doMobLoot'
|
|
|
|
mobs = 'doMobSpawning'
|
|
|
|
patrols = 'doPatrolSpawning'
|
|
|
|
blockdrop = 'doTileDrops'
|
|
|
|
traders = 'doTraderSpawning'
|
|
|
|
weather = 'doWeatherCycle'
|
|
|
|
drowndmg = 'drowningDamage'
|
|
|
|
falldmg = 'fallDamage'
|
|
|
|
firedmg = 'fireDamage'
|
|
|
|
snowdmg = 'freezeDamage'
|
|
|
|
forgive = 'forgiveDeadPlayers'
|
|
|
|
keepinv = 'keepInventory'
|
|
|
|
deathmsg = 'showDeathMessages'
|
|
|
|
admincmdlog = 'logAdminCommands'
|
|
|
|
cmdlen = 'maxCommandChainLength'
|
|
|
|
entcram = 'maxEntityCramming'
|
|
|
|
mobgrief = 'mobGriefing'
|
|
|
|
regen = 'naturalRegeneration'
|
|
|
|
sleeppct = 'playersSleepingPercentage'
|
|
|
|
rndtick = 'randomTickSpeed'
|
|
|
|
spawnradius = 'spawnRadius'
|
|
|
|
reducedf3 = 'reducedDebugInfo'
|
|
|
|
spectchunkgen = 'spectatorsGenerateChunks'
|
|
|
|
cmdfb = 'sendCommandFeedback'
|
|
|
|
|
2021-10-15 15:12:45 +04:00
|
|
|
|
2022-01-11 18:08:59 +04:00
|
|
|
class Gamemode(enum.IntEnum):
|
2022-06-17 12:30:58 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
"""/gamemode numeric list"""
|
|
|
|
|
|
|
|
survival = 0
|
|
|
|
creative = 1
|
|
|
|
adventure = 2
|
|
|
|
spectator = 3
|
2022-06-17 12:30:58 +04:00
|
|
|
|
2021-10-15 15:12:45 +04:00
|
|
|
|
2022-01-11 18:08:59 +04:00
|
|
|
class Difficulty(enum.IntEnum):
|
2022-06-17 12:30:58 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
"""/difficulty numeric list"""
|
|
|
|
|
|
|
|
peaceful = 0
|
|
|
|
easy = 1
|
|
|
|
normal = 2
|
|
|
|
hard = 3
|
2022-06-17 12:30:58 +04:00
|
|
|
|
2021-10-15 15:12:45 +04:00
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
# checking timezone format
|
|
|
|
tzcheck = re.compile(r'(^[A-Z]\w+\/[A-Z]\w+$)|^UTC$')
|
|
|
|
# options types converting
|
2022-03-18 18:38:36 +04:00
|
|
|
convert = {
|
2022-06-23 15:13:56 +04:00
|
|
|
'config-option-number': int,
|
|
|
|
'config-option-select': int,
|
|
|
|
'config-option-toggle': bool
|
2022-03-18 18:38:36 +04:00
|
|
|
}
|
2021-10-15 15:12:45 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
|
2021-10-15 15:12:45 +04:00
|
|
|
class AternosConfig:
|
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
"""Class for editing server settings
|
|
|
|
|
|
|
|
:param atserv: :class:`python_aternos.atserver.AternosServer` object
|
|
|
|
:type atserv: python_aternos.atserver.AternosServer
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, atserv: 'AternosServer') -> None:
|
2022-06-17 12:30:58 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
self.atserv = atserv
|
2021-10-15 15:12:45 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
def get_timezone(self) -> str:
|
2021-10-15 15:12:45 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
"""Parses timezone from options page
|
2022-01-11 18:08:59 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
:return: Area/Location
|
|
|
|
:rtype: str
|
|
|
|
"""
|
2022-06-17 12:30:58 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
optreq = self.atserv.atserver_request(
|
|
|
|
'https://aternos.org/options', 'GET'
|
|
|
|
)
|
|
|
|
opttree = lxml.html.fromstring(optreq)
|
2022-06-17 12:30:58 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
tzopt = opttree.xpath(
|
|
|
|
'//div[@class="options-other-input timezone-switch"]'
|
|
|
|
)[0]
|
|
|
|
tztext = tzopt.xpath('.//div[@class="option current"]')[0].text
|
|
|
|
return tztext.strip()
|
2021-10-15 15:12:45 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
def set_timezone(self, value: str) -> None:
|
2021-10-15 15:12:45 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
"""Sets new timezone
|
2022-01-11 18:08:59 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
:param value: New timezone
|
|
|
|
:type value: str
|
|
|
|
:raises ValueError: If given string
|
|
|
|
doesn't match Area/Location format
|
|
|
|
"""
|
2022-06-17 12:30:58 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
matches_tz = tzcheck.search(value)
|
|
|
|
if not matches_tz:
|
|
|
|
raise ValueError(
|
|
|
|
'Timezone must match zoneinfo format: Area/Location'
|
|
|
|
)
|
2022-06-17 12:30:58 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
self.atserv.atserver_request(
|
|
|
|
'https://aternos.org/panel/ajax/timezone.php',
|
|
|
|
'POST', data={'timezone': value},
|
|
|
|
sendtoken=True
|
|
|
|
)
|
2021-10-15 15:12:45 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
def get_java(self) -> int:
|
2021-10-15 15:12:45 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
"""Parses Java version from options page
|
2022-06-17 12:30:58 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
:return: Java image version
|
|
|
|
:rtype: int
|
|
|
|
"""
|
2022-06-17 12:30:58 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
optreq = self.atserv.atserver_request(
|
|
|
|
'https://aternos.org/options', 'GET'
|
|
|
|
)
|
|
|
|
opttree = lxml.html.fromstring(optreq)
|
|
|
|
imgopt = opttree.xpath(
|
|
|
|
'//div[@class="options-other-input image-switch"]'
|
|
|
|
)[0]
|
|
|
|
imgver = imgopt.xpath(
|
|
|
|
'.//div[@class="option current"]/@data-value'
|
|
|
|
)[0]
|
2022-01-11 18:08:59 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
jdkver = str(imgver or '').removeprefix('openjdk:')
|
|
|
|
return int(jdkver)
|
2021-10-15 15:12:45 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
def set_java(self, value: int) -> None:
|
|
|
|
|
|
|
|
"""Sets new Java version
|
|
|
|
|
|
|
|
:param value: New Java image version
|
|
|
|
:type value: int
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.atserv.atserver_request(
|
|
|
|
'https://aternos.org/panel/ajax/image.php',
|
|
|
|
'POST', data={'image': f'openjdk:{value}'},
|
|
|
|
sendtoken=True
|
|
|
|
)
|
|
|
|
|
|
|
|
#
|
|
|
|
# server.properties
|
|
|
|
#
|
|
|
|
def set_server_prop(self, option: str, value: Any) -> None:
|
|
|
|
|
|
|
|
"""Sets server.properties option
|
|
|
|
|
|
|
|
:param option: Option name
|
|
|
|
:type option: str
|
|
|
|
:param value: New value
|
|
|
|
:type value: Any
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.__set_prop(
|
|
|
|
'/server.properties',
|
|
|
|
option, value
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_server_props(self, proptyping: bool = True) -> Dict[str, Any]:
|
|
|
|
|
|
|
|
"""Parses all server.properties from options page
|
|
|
|
|
|
|
|
:param proptyping: If the returned dict should contain value
|
|
|
|
that matches property type (e.g. max-players will be int)
|
|
|
|
instead of string, defaults to True
|
|
|
|
:type proptyping: bool, optional
|
|
|
|
:return: Server.properties dict
|
|
|
|
:rtype: Dict[str,Any]
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self.__get_all_props('https://aternos.org/options', proptyping)
|
|
|
|
|
|
|
|
def set_server_props(self, props: Dict[str, Any]) -> None:
|
|
|
|
|
|
|
|
"""Updates server.properties options with the given dict
|
|
|
|
|
|
|
|
:param props: Dict with properties `{key:value}`
|
|
|
|
:type props: Dict[str,Any]
|
|
|
|
"""
|
|
|
|
|
|
|
|
for key in props:
|
|
|
|
self.set_server_prop(key, props[key])
|
|
|
|
|
|
|
|
#
|
|
|
|
# level.dat
|
|
|
|
#
|
|
|
|
def set_world_prop(
|
|
|
|
self, option: Union[WorldOpts, WorldRules],
|
|
|
|
value: Any, gamerule: bool = False,
|
|
|
|
world: str = 'world') -> None:
|
|
|
|
|
|
|
|
"""Sets level.dat option for specified world
|
|
|
|
|
|
|
|
:param option: Option name
|
|
|
|
:type option: Union[WorldOpts,WorldRules]
|
|
|
|
:param value: New value
|
|
|
|
:type value: Any
|
|
|
|
:param gamerule: If the option
|
|
|
|
is a gamerule, defaults to False
|
|
|
|
:type gamerule: bool, optional
|
|
|
|
:param world: Name of the world which
|
|
|
|
level.dat must be edited, defaults to 'world'
|
|
|
|
:type world: str, optional
|
|
|
|
"""
|
|
|
|
|
|
|
|
prefix = DAT_PREFIX
|
|
|
|
if gamerule:
|
|
|
|
prefix = DAT_GR_PREFIX
|
|
|
|
|
|
|
|
self.__set_prop(
|
|
|
|
f'/{world}/level.dat',
|
|
|
|
f'{prefix}{option}',
|
|
|
|
value
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_world_props(
|
|
|
|
self, world: str = 'world',
|
|
|
|
proptyping: bool = True) -> Dict[str, Any]:
|
|
|
|
|
|
|
|
"""Parses level.dat from specified world's options page
|
|
|
|
|
|
|
|
:param world: Name of the world, defaults to 'world'
|
|
|
|
:type world: str, optional
|
|
|
|
:param proptyping: If the returned dict should contain the value
|
|
|
|
that matches property type (e.g. randomTickSpeed will be bool)
|
|
|
|
instead of string, defaults to True
|
|
|
|
:type proptyping: bool, optional
|
|
|
|
:return: Level.dat dict
|
|
|
|
:rtype: Dict[str,Any]
|
|
|
|
"""
|
|
|
|
|
2022-07-01 09:36:52 +04:00
|
|
|
return self.__get_all_props(
|
2022-06-23 15:13:56 +04:00
|
|
|
f'https://aternos.org/files/{world}/level.dat',
|
|
|
|
proptyping, [DAT_PREFIX, DAT_GR_PREFIX]
|
|
|
|
)
|
|
|
|
|
2022-07-01 09:36:52 +04:00
|
|
|
def set_world_props(
|
|
|
|
self,
|
2022-07-01 14:28:39 +04:00
|
|
|
props: Dict[Union[WorldOpts, WorldRules], Any],
|
|
|
|
world: str = 'world') -> None:
|
|
|
|
|
|
|
|
"""Sets level.dat options from
|
|
|
|
the dictionary for the specified world
|
|
|
|
|
|
|
|
:param props: Level.dat options
|
|
|
|
:type props: Dict[Union[WorldOpts, WorldRules], Any]
|
|
|
|
:param world: name of the world which
|
|
|
|
level.dat must be edited, defaults to 'world'
|
|
|
|
:type world: str
|
|
|
|
"""
|
2022-07-01 09:36:52 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
for key in props:
|
2022-07-01 14:28:39 +04:00
|
|
|
self.set_world_prop(
|
|
|
|
option=key,
|
|
|
|
value=props[key],
|
|
|
|
world=world
|
|
|
|
)
|
2022-06-23 15:13:56 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# helpers
|
|
|
|
#
|
|
|
|
def __set_prop(self, file: str, option: str, value: Any) -> None:
|
|
|
|
|
|
|
|
self.atserv.atserver_request(
|
|
|
|
'https://aternos.org/panel/ajax/config.php',
|
|
|
|
'POST', data={
|
|
|
|
'file': file,
|
|
|
|
'option': option,
|
|
|
|
'value': value
|
|
|
|
}, sendtoken=True
|
|
|
|
)
|
|
|
|
|
|
|
|
def __get_all_props(
|
|
|
|
self, url: str, proptyping: bool = True,
|
|
|
|
prefixes: Optional[List[str]] = None) -> Dict[str, Any]:
|
|
|
|
|
|
|
|
optreq = self.atserv.atserver_request(url, 'GET')
|
|
|
|
opttree = lxml.html.fromstring(optreq.content)
|
|
|
|
configs = opttree.xpath('//div[@class="config-options"]')
|
|
|
|
|
|
|
|
for i, conf in enumerate(configs):
|
|
|
|
opts = conf.xpath('/div[contains(@class,"config-option ")]')
|
|
|
|
result = {}
|
|
|
|
|
|
|
|
for opt in opts:
|
|
|
|
key = opt.xpath(
|
|
|
|
'.//span[@class="config-option-output-key"]'
|
|
|
|
)[0].text
|
|
|
|
value = opt.xpath(
|
|
|
|
'.//span[@class="config-option-output-value"]'
|
|
|
|
)[0].text
|
|
|
|
|
|
|
|
if prefixes is not None:
|
|
|
|
key = f'{prefixes[i]}{key}'
|
|
|
|
|
|
|
|
opttype = opt.xpath('/@class').split(' ')[1]
|
|
|
|
if proptyping and opttype in convert:
|
|
|
|
value = convert[opttype](value)
|
|
|
|
|
|
|
|
result[key] = value
|
|
|
|
|
|
|
|
return result
|