Skip to content

Commit

Permalink
v1.3.0: more detailed server info entry support
Browse files Browse the repository at this point in the history
  • Loading branch information
Fallen-Breath committed May 22, 2022
1 parent 736e726 commit 5c2be12
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
44 changes: 34 additions & 10 deletions join_motd/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import collections
import os
from datetime import datetime
from typing import List, Optional, Callable, Any, Union
from typing import List, Optional, Callable, Any, Union, Dict

from mcdreforged.api.all import *


class ServerInfo(Serializable):
name: str
description: Optional[str] = None
category: str = ''

@classmethod
def from_object(cls, obj) -> 'ServerInfo':
if isinstance(obj, cls):
return obj
return ServerInfo(name=str(obj))


class Config(Serializable):
serverName: str = 'Survival Server'
mainServerName: str = 'My Server'
serverList: List[str] = [
"survival",
"lobby"
serverList: List[Union[str, ServerInfo]] = [
'survival',
'lobby',
ServerInfo(name='creative1', description='CMP Server#1', category='CMP'),
ServerInfo(name='creative2', description='CMP Server#2', category='CMP'),
]
start_day: Optional[str] = None
daycount_plugin_ids: List[str] = [
Expand Down Expand Up @@ -45,15 +60,24 @@ def get_day(server: ServerInterface) -> str:


def display_motd(server: ServerInterface, reply: Callable[[Union[str, RTextBase]], Any]):
messages = []
for subServerName in config.serverList:
command = '/server {}'.format(subServerName)
messages.append(RText('[{}]'.format(subServerName)).h(command).c(RAction.run_command, command))

reply('§7=======§r Welcome back to §e{}§7 =======§r'.format(config.serverName))
reply('今天是§e{}§r开服的第§e{}§r天'.format(config.mainServerName, get_day(server)))
reply('§7-------§r Server List §7-------§r')
reply(RTextBase.join(' ', messages))

server_dict: Dict[str, List[ServerInfo]] = collections.defaultdict(list)
for entry in config.serverList:
info = ServerInfo.from_object(entry)
server_dict[info.category].append(info)
for category, server_list in server_dict.items():
header = RText('{}: '.format(category) if len(category) > 0 else '')
messages = []
for info in server_list:
command = '/server {}'.format(info.name)
hover_text = command
if info.description is not None:
hover_text = info.description + '\n' + hover_text
messages.append(RText('[{}]'.format(info.name)).h(hover_text).c(RAction.run_command, command))
reply(header + RTextBase.join(' ', messages))


def on_player_joined(server: ServerInterface, player, info):
Expand Down
2 changes: 1 addition & 1 deletion mcdreforged.plugin.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "join_motd",
"version": "1.2.0",
"version": "1.3.0",
"name": "Join MOTD",
"description": {
"en_us": "Send player a MOTD when he joins",
Expand Down

0 comments on commit 5c2be12

Please sign in to comment.