2022-01-11 18:08:59 +04:00
|
|
|
import enum
|
2021-10-15 15:12:45 +04:00
|
|
|
import re
|
|
|
|
import lxml.html
|
2021-10-15 19:31:47 +04:00
|
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
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-03-18 18:38:36 +04:00
|
|
|
from .atserver import AternosServer
|
2021-10-15 19:31:47 +04:00
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
#
|
|
|
|
# server.options
|
2022-01-11 18:08:59 +04:00
|
|
|
class ServerOpts(enum.Enum):
|
|
|
|
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'
|
2022-03-25 16:45:38 +04:00
|
|
|
packreq = 'require-resource-pack'
|
2022-01-11 18:08:59 +04:00
|
|
|
pack = 'resource-pack'
|
2021-10-15 15:12:45 +04:00
|
|
|
|
|
|
|
DAT_PREFIX = 'Data:'
|
2022-03-18 18:38:36 +04:00
|
|
|
DAT_GR_PREFIX = 'Data:GameRules:'
|
2022-01-11 18:08:59 +04:00
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
# level.dat
|
2022-01-11 18:08:59 +04:00
|
|
|
class WorldOpts(enum.Enum):
|
2022-03-25 16:45:38 +04:00
|
|
|
seed12 = 'randomseed'
|
|
|
|
seed = 'seed'
|
2022-01-11 18:08:59 +04:00
|
|
|
hardcore = 'hardcore'
|
2022-03-25 16:45:38 +04:00
|
|
|
difficulty = 'Difficulty'
|
2021-10-15 15:12:45 +04:00
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
# /gamerule
|
2022-01-11 18:08:59 +04:00
|
|
|
class WorldRules(enum.Enum):
|
2022-03-18 18:38:36 +04:00
|
|
|
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'
|
2022-03-25 16:45:38 +04:00
|
|
|
snowdmg = 'freezeDamage'
|
2022-03-18 18:38:36 +04:00
|
|
|
forgive = 'forgiveDeadPlayers'
|
|
|
|
keepinv = 'keepInventory'
|
|
|
|
deathmsg = 'showDeathMessages'
|
|
|
|
admincmdlog = 'logAdminCommands'
|
|
|
|
cmdlen = 'maxCommandChainLength'
|
|
|
|
entcram = 'maxEntityCramming'
|
|
|
|
mobgrief = 'mobGriefing'
|
|
|
|
regen = 'naturalRegeneration'
|
2022-03-25 16:45:38 +04:00
|
|
|
sleeppct = 'playersSleepingPercentage'
|
|
|
|
rndtick = 'randomTickSpeed'
|
2022-03-18 18:38:36 +04:00
|
|
|
spawnradius = 'spawnRadius'
|
|
|
|
reducedf3 = 'reducedDebugInfo'
|
|
|
|
spectchunkgen = 'spectatorsGenerateChunks'
|
|
|
|
cmdfb = 'sendCommandFeedback'
|
2021-10-15 15:12:45 +04:00
|
|
|
|
|
|
|
DAT_TYPE_WORLD = 0
|
|
|
|
DAT_TYPE_GR = 1
|
|
|
|
|
2022-01-11 18:08:59 +04:00
|
|
|
class Gamemode(enum.IntEnum):
|
|
|
|
survival = 0
|
|
|
|
creative = 1
|
|
|
|
adventure = 2
|
|
|
|
spectator = 3
|
2021-10-15 15:12:45 +04:00
|
|
|
|
2022-01-11 18:08:59 +04:00
|
|
|
class Difficulty(enum.IntEnum):
|
|
|
|
peaceful = 0
|
|
|
|
easy = 1
|
|
|
|
normal = 2
|
|
|
|
hard = 3
|
2021-10-15 15:12:45 +04:00
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
#
|
|
|
|
# jre types for set_java
|
|
|
|
javatype = {
|
|
|
|
'jdk': 'openjdk:{ver}',
|
|
|
|
'openj9-1': 'adoptopenjdk:{ver}-jre-openj9-bionic',
|
|
|
|
'openj9-2': 'ibm-semeru-runtimes:open-{ver}-jre'
|
|
|
|
}
|
|
|
|
# checking java version format
|
|
|
|
javacheck = re.compile(
|
|
|
|
''.join(
|
|
|
|
list(
|
|
|
|
map(
|
|
|
|
# create a regexp for each jre type,
|
|
|
|
# e.g.: (^openjdk:\d+$)|
|
|
|
|
lambda i: '(^' + javatype[i].format(ver=r'\d+') + '$)|',
|
|
|
|
javatype
|
|
|
|
)
|
|
|
|
)
|
|
|
|
).rstrip('|')
|
|
|
|
)
|
|
|
|
# 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 = {
|
|
|
|
'config-option-number': int,
|
|
|
|
'config-option-select': int,
|
|
|
|
'config-option-toggle': bool
|
|
|
|
}
|
2021-10-15 15:12:45 +04:00
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
# MAIN CLASS
|
2021-10-15 15:12:45 +04:00
|
|
|
class AternosConfig:
|
|
|
|
|
2021-10-15 19:31:47 +04:00
|
|
|
def __init__(self, atserv:'AternosServer') -> None:
|
2021-10-15 15:12:45 +04:00
|
|
|
|
|
|
|
self.atserv = atserv
|
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
def get_timezone(self) -> str:
|
2022-01-11 18:08:59 +04:00
|
|
|
|
2021-10-15 15:12:45 +04:00
|
|
|
optreq = self.atserv.atserver_request(
|
2022-03-18 18:38:36 +04:00
|
|
|
'https://aternos.org/options', 'GET'
|
2021-10-15 15:12:45 +04:00
|
|
|
)
|
|
|
|
opttree = lxml.html.fromstring(optreq)
|
|
|
|
|
|
|
|
tzopt = opttree.xpath('//div[@class="options-other-input timezone-switch"]')[0]
|
|
|
|
tztext = tzopt.xpath('.//div[@class="option current"]')[0].text
|
|
|
|
return tztext.strip()
|
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
def set_timezone(self, value:str) -> None:
|
2022-01-11 18:08:59 +04:00
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
matches_tz = tzcheck.search(value)
|
|
|
|
if not matches_tz:
|
|
|
|
raise ValueError('Timezone must match zoneinfo format: Area/Location')
|
2021-10-15 15:12:45 +04:00
|
|
|
|
|
|
|
self.atserv.atserver_request(
|
|
|
|
'https://aternos.org/panel/ajax/timezone.php',
|
2022-03-18 18:38:36 +04:00
|
|
|
'POST', data={'timezone': value},
|
2021-10-15 15:12:45 +04:00
|
|
|
sendtoken=True
|
|
|
|
)
|
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
def get_java(self) -> str:
|
2022-01-11 18:08:59 +04:00
|
|
|
|
2021-10-15 15:12:45 +04:00
|
|
|
optreq = self.atserv.atserver_request(
|
2022-03-25 16:45:38 +04:00
|
|
|
'https://aternos.org/options', 'GET'
|
2021-10-15 15:12:45 +04:00
|
|
|
)
|
|
|
|
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]
|
|
|
|
return imgver
|
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
def set_java(self, value:str) -> None:
|
|
|
|
|
|
|
|
matches_jdkver = javacheck.search(value)
|
|
|
|
if not matches_jdkver:
|
|
|
|
raise ValueError('Incorrect Java image version format!')
|
2021-10-15 15:12:45 +04:00
|
|
|
|
|
|
|
self.atserv.atserver_request(
|
|
|
|
'https://aternos.org/panel/ajax/image.php',
|
2022-03-18 18:38:36 +04:00
|
|
|
'POST', data={'image': value},
|
2021-10-15 15:12:45 +04:00
|
|
|
sendtoken=True
|
|
|
|
)
|
|
|
|
|
|
|
|
#
|
|
|
|
# server.properties
|
|
|
|
#
|
|
|
|
def set_server_prop(self, option:str, value:Any) -> None:
|
|
|
|
self.__set_prop(
|
|
|
|
'/server.properties',
|
|
|
|
option, value
|
|
|
|
)
|
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
def get_server_props(self, proptyping:bool=True) -> Dict[str,Any]:
|
|
|
|
return self.__get_all_props('https://aternos.org/options', proptyping)
|
2021-10-15 15:12:45 +04:00
|
|
|
|
|
|
|
def set_server_props(self, props:Dict[str,Any]) -> None:
|
|
|
|
for key in props:
|
2022-03-18 18:38:36 +04:00
|
|
|
self.set_server_prop(key, props[key])
|
2021-10-15 15:12:45 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# level.dat
|
|
|
|
#
|
|
|
|
def set_world_prop(
|
|
|
|
self, option:str, value:Any,
|
|
|
|
proptype:int, world:str='world') -> None:
|
2022-01-11 18:08:59 +04:00
|
|
|
|
2021-10-15 15:12:45 +04:00
|
|
|
prefix = DAT_PREFIX
|
|
|
|
if proptype == DAT_TYPE_GR:
|
|
|
|
prefix = DAT_GR_PREFIX
|
|
|
|
|
|
|
|
self.__set_prop(
|
|
|
|
f'/{world}/level.dat',
|
|
|
|
f'{prefix}{option}',
|
|
|
|
value
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_world_props(
|
|
|
|
self, world:str='world',
|
2022-03-25 16:45:38 +04:00
|
|
|
proptyping:bool=True) -> Dict[str,Any]:
|
2022-01-11 18:08:59 +04:00
|
|
|
|
2021-10-15 15:12:45 +04:00
|
|
|
self.__get_all_props(
|
|
|
|
f'https://aternos.org/files/{world}/level.dat',
|
2022-03-25 16:45:38 +04:00
|
|
|
proptyping, [DAT_PREFIX, DAT_GR_PREFIX]
|
2021-10-15 15:12:45 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
def set_world_props(self, props:Dict[str,Any]) -> None:
|
|
|
|
for key in props:
|
2022-03-18 18:38:36 +04:00
|
|
|
self.set_world_prop(key, props[key])
|
2021-10-15 15:12:45 +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',
|
2022-03-18 18:38:36 +04:00
|
|
|
'POST', data={
|
2021-10-15 15:12:45 +04:00
|
|
|
'file': file,
|
|
|
|
'option': option,
|
|
|
|
'value': value
|
|
|
|
}, sendtoken=True
|
|
|
|
)
|
|
|
|
|
|
|
|
def __get_all_props(
|
2022-03-25 16:45:38 +04:00
|
|
|
self, url:str, proptyping:bool=True,
|
2021-10-15 15:12:45 +04:00
|
|
|
prefixes:Optional[List[str]]=None) -> Dict[str,Any]:
|
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
optreq = self.atserv.atserver_request(url, 'GET')
|
2021-10-15 15:12:45 +04:00
|
|
|
opttree = lxml.html.fromstring(optreq.content)
|
|
|
|
configs = opttree.xpath('//div[@class="config-options"]')
|
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
for i, conf in enumerate(configs):
|
2021-10-15 15:12:45 +04:00
|
|
|
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
|
2022-03-25 16:45:38 +04:00
|
|
|
|
2021-10-15 15:12:45 +04:00
|
|
|
if prefixes != None:
|
|
|
|
key = f'{prefixes[i]}{key}'
|
|
|
|
|
|
|
|
opttype = opt.xpath('/@class').split(' ')[1]
|
2022-03-25 16:45:38 +04:00
|
|
|
if proptyping and opttype in convert:
|
|
|
|
value = convert[opttype](value)
|
2021-10-15 15:12:45 +04:00
|
|
|
|
|
|
|
result[key] = value
|
2022-03-25 16:45:38 +04:00
|
|
|
|
2021-10-15 15:12:45 +04:00
|
|
|
return result
|