Skip to content

Commit

Permalink
added plugins, mods implementation and removed get_software() for now
Browse files Browse the repository at this point in the history
  • Loading branch information
hitblast committed Dec 5, 2023
1 parent 8740676 commit 17d3caa
Showing 1 changed file with 62 additions and 85 deletions.
147 changes: 62 additions & 85 deletions mcsrvstat/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,27 @@ def hostname(self) -> str:

return self.data['hostname']

@precheck
def get_debug_values(self) -> ServerDebugInfo:
"""
Gives out a `ServerDebugValue` object containing all the accessible debug values of the given server.
"""

debug_values = self.data['debug']
return ServerDebugInfo(
ping=debug_values['ping'],
query=debug_values['query'],
srv=debug_values['srv'],
querymismatch=debug_values['querymismatch'],
ipinsrv=debug_values['ipinsrv'],
cnameinsrv=debug_values['cnameinsrv'],
animatedmotd=debug_values['animatedmotd'],
cachehit=debug_values['cachehit'],
cachetime=debug_values['cachetime'],
cacheexpire=debug_values['cacheexpire'],
apiversion=debug_values['apiversion'],
)

@property
@precheck
def id(self) -> str:
Expand Down Expand Up @@ -164,7 +185,7 @@ def gamemode(self) -> str:
@precheck
def get_motd(self) -> ServerMOTD:
"""
Gives out a `ServerMOTD` object containing the server's MOTD in different types (clean, raw, HTML).
Gives out a `ServerMOTD` object containing the server's MOTD in different string types (clean, raw, HTML).
Exceptions:
`DataNotFoundError` - If the MOTD of the server is not found.
Expand All @@ -178,130 +199,86 @@ def get_motd(self) -> ServerMOTD:
return ServerMOTD(raw=motd['raw'], clean=motd['clean'], html=motd['html'])

@precheck
def get_info(self) -> ServerInfo:
def get_player_by_name(self, player_name: str) -> Player:
"""
Gives out a `ServerInfo` object containing the server's base information (if any).
Gives out a `Player` object representing a player currently playing on the Minecraft server.
Parameters:
`player_name: str` - The name of the player you wish to fetch.
Exceptions:
`DataNotFoundError` - If the server information data is not found.
`DataNotFoundError` - If the player data is not found.
"""

try:
info = self.data['info']
except KeyError:
raise DataNotFoundError('Failed to fetch server base information.')
player = next((p for p in self.data['players']['list'] if p['name'] == player_name), None)

if not player:
raise DataNotFoundError('Failed to fetch player data.')
else:
return ServerInfo(raw=info['raw'], clean=info['clean'], html=info['html'])
return Player(name=player['name'], uuid=player['uuid'])

@precheck
def get_plugins(self) -> ServerPlugins:
def get_player_count(self) -> PlayerCount:
"""
Gives out a `ServerPlugins` object containing the names of the plugins
that have been used in the development of the server.
Gives out a `PlayerCount` object, representing the active player count of the Minecraft server.
Exceptions:
`DataNotFoundError` - If the data for installed plugins is not found.
`DataNotFoundError` - If the player count data is not found.
"""

try:
plugins = self.data['plugins']
return PlayerCount(online=self.data['players']['online'], max=self.data['players']['max'])
except KeyError:
raise DataNotFoundError('Failed to fetch server plugin data.')
else:
return ServerPlugins(names=plugins['names'], raw=plugins['raw'])
raise DataNotFoundError('Failed to fetch player count data.')

@precheck
def get_mods(self) -> ServerMods:
def get_players(self) -> Optional[List[Player]]:
"""
Gives out a `ServerMods` object containing the names of active mods that are being used the server.
Exceptions:
`DataNotFoundError` - If the data for installed mods is not found.
Gives out a list containing `Player` objects, each indicating an online player.\n
Returns `None` if no players are found.
"""

try:
mods = self.data['mods']
return [Player(name=name, uuid=uuid) for name, uuid in self.data['players']['list'].items()]
except KeyError:
raise DataNotFoundError('Failed to fetch server mods data.')
else:
return ServerPlugins(names=mods['names'], raw=mods['raw'])
return None

@precheck
def get_software(self) -> ServerSoftware:
def get_plugins(self) -> Optional[List[ServerPlugin]]:
"""
Gives out a `ServerSoftware` object containing the version and software information of the given server.
Exceptions:
`DataNotFoundError` - If the server software data is not found.
Gives out a list of `ServerPlugin` objects, each representing a plugin used on the Minecraft server.
Returns `None` if not detected.
"""

try:
return ServerSoftware(version=self.data['version'], software=self.data['software'])
return [ServerPlugin(name=name, version=version) for name, version in self.data['plugins'].items()]
except KeyError:
raise DataNotFoundError('Failed to fetch server software data.')

@precheck
def get_debug_values(self) -> ServerDebugInfo:
"""
Gives out a `ServerDebugValue` object containing all the accessible debug values of the given server.
"""

debug_values = self.data['debug']
return ServerDebugInfo(
ping=debug_values['ping'],
query=debug_values['query'],
srv=debug_values['srv'],
querymismatch=debug_values['querymismatch'],
ipinsrv=debug_values['ipinsrv'],
cnameinsrv=debug_values['cnameinsrv'],
animatedmotd=debug_values['animatedmotd'],
cachehit=debug_values['cachehit'],
cachetime=debug_values['cachetime'],
cacheexpire=debug_values['cacheexpire'],
apiversion=debug_values['apiversion'],
)

@precheck
def get_player_by_name(self, player_name: str) -> Player:
"""
Gives out a `Player` object if a player is found active / online by the given name.
Parameters:
`player_name: str` - The name of the player you wish to fetch.
Exceptions:
`DataNotFoundError` - If the player data is not found.
"""

player = next((p for p in self.data['players']['list'] if p['name'] == player_name), None)

if not player:
raise DataNotFoundError('Failed to fetch player data.')
else:
return Player(name=player['name'], uuid=player['uuid'])
return None

@precheck
def get_player_count(self) -> ServerPlayerCount:
def get_mods(self) -> Optional[List[ServerMod]]:
"""
Gives out a `ServerPlayerCount` object containing both the online and the max player count.
Exceptions:
`DataNotFoundError` - If the player count data is not found.
Gives out a list of `ServerMod` objects, each representing a mod used on the Minecraft server.
Returns `None` if not detected.
"""

try:
return ServerPlayerCount(online=self.data['players']['online'], max=self.data['players']['max'])
return [ServerMod(name=name, version=version) for name, version in self.data['mods'].items()]
except KeyError:
raise DataNotFoundError('Failed to fetch player count data.')
return None

@precheck
def get_players(self) -> Optional[List[Player]]:
def get_info(self) -> ServerInfo:
"""
Gives out a list containing `Player` objects, each indicating an online player.\n
Returns `None` if no players are found.
Gives out a `ServerInfo` object containing the server's base information (if any).
Exceptions:
`DataNotFoundError` - If the server information data is not found.
"""

try:
return [Player(name=name, uuid=uuid) for name, uuid in self.data['players']['list'].items()]
info = self.data['info']
except KeyError:
return None
raise DataNotFoundError('Failed to fetch server base information.')
else:
return ServerInfo(raw=info['raw'], clean=info['clean'], html=info['html'])

0 comments on commit 17d3caa

Please sign in to comment.