Skip to content

Commit

Permalink
Merge pull request #6 from LazyAlienServer/dev
Browse files Browse the repository at this point in the history
v0.2.0
  • Loading branch information
tanhHeng authored Jul 28, 2024
2 parents ad13e4e + 29b12bd commit c876e81
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 76 deletions.
63 changes: 52 additions & 11 deletions lang/en_us.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,61 @@ where2go:
display:

pos:
overworld: §2主世界 §a[{x}, {y}, {z}]§f
the_nether: §c地狱 §4[{x}, {y}, {z}]§f
the_end: §d末地 §5[{x}, {y}, {z}]§f
overworld: §2overworld §a[{x}, {y}, {z}§a]§f
the_nether: §4the_nether §c[{x}, {y}, {z}§c]§f
the_end: §5the_end §d[{x}, {y}, {z}§d]§f
hover_text:
xaero: 添加Xaero坐标点
temporary: 将临时坐标点添加至共享
xaero: add Xaero's waypoint
temporary: add temporary waypoint to sharing waypoints

show: §e@§f §l{name}§r §7-§f
temporary: §e# §7(临时坐标点)§f§l{name}§r §7-§f
error: §c错误的坐标点
show: §e@§f {name}§r §7-§f
temporary: §e# §7(temporary)§f{name}§r §7-§f
error: §4wrong waypoint
id: §eID§7:§f{id}

command:
add:
success: "坐标点§a添加成功§f §eID§7:§f{id}"
success: "§aSuccessfully add§f waypoint §eID§7:§f{id}"
fail:
waypoint_exist: §c已存在坐标相同的坐标点§f
waypoint_close: §c已存在距离接近的坐标点§f
forceadd: §7§n仍要添加
remove:
success: 坐标点§4已移除
fail: §4无效的坐标点
success: 坐标点§c已移除
fail: §4无效的坐标点
list:
page_error: §4页码应为整数
page_outofindex: §4错误的页码范围
nodata: §c无数据
page: §7[ §f第 §b{current}§f/§b{total}§f 页 §7]
left: "§8----- "
right: " §8-----"
pre: "§3<<"
pre_prompt: 上一页
next: "§3>>"
next_prompt: 下一页
end_prompt: 已经到头了
search:
nodata: §c没有找到有关'{name}'的坐标点
title: 找到有关§b'{name}'§f的§b{count}§f个§e坐标点§f:
info:
nodata: §c无效的坐标点ID
show: |
§eID§7:§f{id}
§e{creator} §f创建于 §b{create_time}
player_pos:
nodata: 该玩家不存在
closest: "§b附近 §f{distance}m "
fastsearch:
nodata: §c没有找到有关'{name}'的坐标点或玩家

help:
wp: 功能强大的共享坐标点工具
add: 在§eXaero地图§f中§d分享坐标点§f后,插件自动识别,点击最右侧§a[+]§f即可添加
remove: 移除坐标点,鼠标悬浮在坐标点前的§e'@'§f上即可显示§e坐标点ID§f
list: 列出所有§e坐标点§f,按§3<<§f和§3>>§f翻页
search: 按名称搜索§e坐标点§f
info: 按§e坐标点ID§f查看该坐标点的详细信息
fastsearch: 聊天框输入§b{prompt}§f来快速搜索坐标点或玩家
here: 广播自己的位置
whereis: 查询某玩家的位置
7 changes: 6 additions & 1 deletion lang/zh_cn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ where2go:
pos:
overworld: §2主世界 §a[{x}, {y}, {z}§a]§f
the_nether: §4地狱 §c[{x}, {y}, {z}§c]§f
the_end: §d末地 §5[{x}, {y}, {z}§5]§f
the_end: §5末地 §d[{x}, {y}, {z}§d]§f
hover_text:
xaero: 添加Xaero坐标点
temporary: 将临时坐标点添加至共享
Expand Down Expand Up @@ -46,6 +46,11 @@ where2go:
show: |
§eID§7:§f{id}
§e{creator} §f创建于 §b{create_time}
§e名称 §f{name}
§e标题 §f{title}
§e颜色 §{color}{color}
§e维度 §f{dimension}
§e坐标 §f{pos}
player_pos:
nodata: 该玩家不存在
closest: "§b附近 §f{distance}m "
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": "where2go",
"version": "0.1.2",
"version": "0.2.0",
"name": "Where2go",
"description": {
"en_us": "An advanced 'location' plugin including sharing waypoints, search player pos, etc.",
Expand Down
30 changes: 16 additions & 14 deletions where2go/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
from mcdreforged.api.all import PluginServerInterface, PluginCommandSource, CommandSource, CommandContext, Info, new_thread, SimpleCommandBuilder, Text, Integer, RText, RTextList, RAction, RColor
from mcdreforged.api.all import PluginServerInterface, PluginCommandSource, PlayerCommandSource, CommandSource, CommandContext, Info, new_thread, SimpleCommandBuilder, Text, Integer, RText, RTextList, RAction, RColor
from where2go.utils.waypoints import WaypointManager, Waypoint, Display
from where2go.utils.api import PlayerAPI
from where2go.utils.display_utils import rtr, help_msg
from where2go.constants import DEFAULT_CONFIG, PLUGIN_ID
from where2go.constants import PLUGIN_ID
from where2go.config import CONFIG
import re


class Proxy:

def __init__(self, server: PluginServerInterface) -> None:
self.config = server.load_config_simple("config.json", default_config=DEFAULT_CONFIG)
self.waypoint_manager = WaypointManager(server)
self.api = PlayerAPI(self.config["player_api"])
self.config : CONFIG = server.load_config_simple("config.json", target_class=CONFIG)
self.waypoint_manager : WaypointManager = WaypointManager(server)
self.api = PlayerAPI(self.config.player_api)

prefix = self.config["command"]["waypoints"]
prefix = self.config.command.waypoints
self.prefix = prefix
builder = SimpleCommandBuilder()
builder.command(f"{prefix}", self.help_msg) # wp help
Expand All @@ -33,18 +34,19 @@ def __init__(self, server: PluginServerInterface) -> None:
builder.arg("name", Text) # wp search
builder.command(f"{prefix} search", lambda source, context: source.reply(help_msg("search", prefix)))
builder.command(f"{prefix} search <name>", self.search)
here_prefix = self.config['command']['here'] # here
here_prefix = self.config.command.here # here
builder.command(f"{here_prefix}", lambda source, context: self.player_pos(source, context, source.player) if source.is_player else None)
builder.arg("player", Text)
whereis_prefix = self.config['command']['whereis'] # vris
whereis_prefix = self.config.command.whereis # vris
builder.command(f"{whereis_prefix}", lambda source, context: source.reply(RTextList(RText(f"§7{whereis_prefix} <player>").c(RAction.suggest_command, f"{whereis_prefix}"), " ", rtr(f"help.whereis"))))
builder.command(f"{whereis_prefix} <player>", lambda source, context: self.player_pos(source, context, context["player"]))
builder.register(server)

server.register_help_message(prefix, rtr("help.wp"))
server.register_help_message(here_prefix, rtr("help.here"))
server.register_help_message(whereis_prefix, rtr("help.whereis"))
server.register_help_message(self.config["command"]["fastsearch_prompt"], rtr("help.fastsearch", prompt=self.config["command"]["fastsearch_prompt"]))
fastsearch_prompt = self.config.command.fastsearch_prompt
server.register_help_message(fastsearch_prompt, rtr("help.fastsearch", prompt=fastsearch_prompt))


def help_msg(self, source: CommandSource, context: CommandContext):
Expand Down Expand Up @@ -129,8 +131,7 @@ def info(self, source: CommandSource, context: CommandContext):
waypoint = self.waypoint_manager.search_id(id)
if not waypoint:
source.reply(rtr("command.info.nodata"))
source.reply(rtr("command.info.show", id=waypoint["id"], creator=waypoint["creator"], create_time=waypoint["create_time"]))
source.reply(Display.show(waypoint["waypoint"], waypoint["id"]))
source.reply(rtr("command.info.show", id=waypoint["id"], creator=waypoint["creator"], create_time=waypoint["create_time"], **waypoint["waypoint"].to_dict()))


@new_thread(f"{PLUGIN_ID}-player_pos")
Expand All @@ -145,6 +146,7 @@ def player_pos(self, source: CommandSource, context: CommandContext, player: str
waypoint = Waypoint(player_pos["pos"], player_pos["dimension"], player)

server.say(Display.show(waypoint))
source.get_server().execute(self.config.player_api.highlight_command.format(player=player))
closest = self.waypoint_manager.search_closest(player_pos["pos"], player_pos["dimension"], 128)
if closest:
server.say(RTextList(rtr("command.player_pos.closest", distance="%.1f"%closest[1]), Display.show(closest[0]["waypoint"], closest[0]["id"])))
Expand All @@ -154,9 +156,9 @@ def player_pos(self, source: CommandSource, context: CommandContext, player: str
def on_user_info(self, server: PluginServerInterface, info: Info):
waypoint = Waypoint.transform_xaero_waypoint(info.content)
if waypoint:
server.say(Display.temporary(waypoint, self.config["command"]["waypoints"]))
server.say(Display.temporary(waypoint, self.config.command.waypoints))
return
fastsearch = re.match(self.config["command"]["fastsearch_regex"], info.content)
fastsearch = re.match(self.config.command.fastsearch_regex, info.content)
if not fastsearch:
return
name = fastsearch.groups()[0]
Expand All @@ -176,6 +178,7 @@ def on_user_info(self, server: PluginServerInterface, info: Info):
return
waypoint = Waypoint(player_pos["pos"], player_pos["dimension"], name)
server.say(Display.show(waypoint))
server.execute(self.config.player_api.highlight_command.format(player=name))
closest = self.waypoint_manager.search_closest(player_pos["pos"], player_pos["dimension"], 64)
if closest:
server.say(RTextList(rtr("command.player_pos.closest", distance="%.1f"%closest[1]), Display.show(closest[0]["waypoint"])))
Expand All @@ -187,7 +190,6 @@ def on_load(server: PluginCommandSource, prev_module):
proxy = Proxy(server)

def on_user_info(server: PluginServerInterface, info: Info):
global proxy
proxy.on_user_info(server, info)

def on_info(server: PluginServerInterface, info: Info):
Expand Down
24 changes: 24 additions & 0 deletions where2go/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from mcdreforged.api.all import Serializable

class COMMAND_CONFIG(Serializable):
waypoints: str = "!!wp"
whereis: str = "!!vris"
here: str = "!!here"
fastsearch_regex: str = "^(\w+)在哪??"
fastsearch_prompt: str = "XXX在哪"

class PLAYER_API_CONFIG(Serializable):
prefix_regex: str = "(?:\[.+\])?",
player_pos_command: str = "data get entity {player} Pos",
player_dimension_command: str = "data get entity {player} Dimension",
player_pos_regex: str = "^{prefix_regex}(\w+) has the following entity data: \[(-?[0-9.]+)d, (-?[0-9.]+)d, (-?[0-9.]+)d\]",
player_dimension_regex: str = '^{prefix_regex}(\w+) has the following entity data: "minecraft:(\w+)"',
player_notfound_regex: str = '^No entity was found',
player_list_command: str = "list",
player_list_sep: str = ", ",
player_list_regex: str = '^There are [0-9]+ of a max of [0-9]+ players online: (.+)*'
highlight_command: str = "effect give {player} minecraft:glowing 15 0 true"

class CONFIG(Serializable):
command: COMMAND_CONFIG = COMMAND_CONFIG()
player_api: PLAYER_API_CONFIG = PLAYER_API_CONFIG()
21 changes: 0 additions & 21 deletions where2go/constants.py
Original file line number Diff line number Diff line change
@@ -1,22 +1 @@
DEFAULT_CONFIG = {
"command": {
"waypoints": "!!wp",
"whereis": "!!vris",
"here": "!!here",
"fastsearch_regex": "^(\w+)在哪??",
"fastsearch_prompt": "XXX在哪"
},
"player_api": {
"prefix_regex": "(?:\[.+\])?",
"player_pos_command": "data get entity {player} Pos",
"player_dimension_command": "data get entity {player} Dimension",
"player_pos_regex": "^{prefix_regex}(\w+) has the following entity data: \[(-?[0-9.]+)d, (-?[0-9.]+)d, (-?[0-9.]+)d\]",
"player_dimension_regex": '^{prefix_regex}(\w+) has the following entity data: "minecraft:(\w+)"',
"player_notfound_regex": '^No entity was found',
"player_list_command": "list",
"player_list_sep": ", ",
"player_list_regex": '^There are [0-9]+ of a max of [0-9]+ players online: (.+)*'
}
}

PLUGIN_ID = "where2go"
30 changes: 10 additions & 20 deletions where2go/utils/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,7 @@
from threading import Event
from typing import TypedDict, List, Union
import re


class PlayerAPIConfig(TypedDict):
prefix_regex: str
player_pos_command: str
player_dimension_command: str
player_pos_regex: str
player_dimension_regex: str
player_list_command: str
player_list_sep: str
player_list_regex: str
from where2go.config import PLAYER_API_CONFIG


class PlayerPos(TypedDict):
Expand All @@ -22,7 +12,7 @@ class PlayerPos(TypedDict):

class PlayerAPI:

def __init__(self, config: PlayerAPIConfig) -> None:
def __init__(self, config: PLAYER_API_CONFIG) -> None:
self.player_pos: dict = {}
self.player_list: dict = {}
self.config = config
Expand All @@ -40,8 +30,8 @@ def get_player_pos(self, player: str, timeout: int = 5) -> Union[None, PlayerPos
"dimension": None
}
server = ServerInterface.si()
server.execute(self.config["player_pos_command"].format(player=player))
server.execute(self.config["player_dimension_command"].format(player=player))
server.execute(self.config.player_pos_command.format(player=player))
server.execute(self.config.player_dimension_command.format(player=player))
event.wait(timeout=timeout)
pos = player_pos[player]
player_pos.pop(player)
Expand All @@ -58,9 +48,9 @@ def get_player_list(self, timeout: int = 5) -> Union[List[str], None]:
"list": None
}
event.clear()
ServerInterface.si().execute(self.config["player_list_command"])
ServerInterface.si().execute(self.config.player_list_command)
event.wait(timeout=timeout)
result = [re.fullmatch(f"{self.config['prefix_regex']}(.+)", i).groups()[-1] for i in self.player_list["list"]] if self.player_list["list"] else None
result = [re.fullmatch(f"{self.config.prefix_regex}(.+)", i).groups()[-1] for i in self.player_list["list"]] if self.player_list["list"] else None
self.player_list = {}
return result

Expand All @@ -69,8 +59,8 @@ def on_info(self, server, info: Info):
if not info.is_from_server:
return
if self.player_pos:
pos = re.match(self.config["player_pos_regex"].format(prefix_regex = self.config["prefix_regex"]), info.content)
dimension = re.match(self.config["player_dimension_regex"].format(prefix_regex = self.config["prefix_regex"]), info.content)
pos = re.match(self.config.player_pos_regex.format(prefix_regex = self.config.prefix_regex), info.content)
dimension = re.match(self.config.player_dimension_regex.format(prefix_regex = self.config.prefix_regex), info.content)
if not pos and not dimension:
return
if pos:
Expand All @@ -90,7 +80,7 @@ def on_info(self, server, info: Info):
if player_pos["pos"]:
player_pos["event"].set()
if self.player_list:
player_list = re.match(self.config["player_list_regex"], info.content)
player_list = re.match(self.config.player_list_regex, info.content)
if player_list:
self.player_list["list"] = player_list.groups()[-1].split(self.config["player_list_sep"])
self.player_list["list"] = player_list.groups()[-1].split(self.config.player_list_sep)
self.player_list["event"].set()
17 changes: 9 additions & 8 deletions where2go/utils/waypoints/waypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import TypedDict
from mcdreforged.api.all import ServerInterface

formatting_codes = "0123456789abcdef"

class WaypointDict(TypedDict):
pos: tuple
Expand All @@ -14,7 +15,7 @@ class WaypointDict(TypedDict):

class Waypoint:

def __init__(self, pos: tuple, dimension: str, name: str, title: str = None, color: Union[int,str] = None) -> None:
def __init__(self, pos: tuple, dimension: str, name: str, title: str = None, color: Union[int, str] = None) -> None:
'''Create a waypoint
Parameters
Expand All @@ -38,12 +39,12 @@ def __init__(self, pos: tuple, dimension: str, name: str, title: str = None, col
if not title:
title = name[0] if len(name) > 0 else ""
self.title: str = title
color = str(color)
formatting_codes = "0123456789abcdef"
if len(color) != 1 or color not in formatting_codes:
if type(color) == int:
color = str(formatting_codes[color]) if color < len(formatting_codes) else random.choice(formatting_codes)
elif type(color) != str or color not in formatting_codes:
color = random.choice(formatting_codes)
self.color: str = color


def __eq__(self, __value: object) -> bool:
return isinstance(__value, Waypoint) and self.pos == __value.pos
Expand Down Expand Up @@ -74,11 +75,11 @@ def transform_xaero_waypoint(content: str):
return
name, title, x, y, z, color, dimension = result.groups()
dimension = dimension.replace("-","_")
return Waypoint((int(x), int(y), int(z)), dimension, name, title, color)
return Waypoint((int(x), int(y), int(z)), dimension, name, title, int(color))


def get_xaero_waypoint(self, dimensions_map = {"overworld": "Internal-overworld-waypoints", "the_nether": "Internal-the-nether-waypoints", "the_end": "Internal-the-end-waypoints"}):
return f"xaero-waypoint:{self.name}:{self.title}:{':'.join(map(str,self.pos))}:{self.color}:false:0:{dimensions_map[self.dimension]}"
return f"xaero-waypoint:{self.name}:{self.title}:{':'.join(map(str,self.pos))}:{formatting_codes.index(self.color)}:false:0:{dimensions_map[self.dimension]}"

def get_xaero_waypoint_add(self):
return f"xaero_waypoint_add:{self.name}:{self.title}:{':'.join(map(str,self.pos))}:{self.color}:false:0:Internal_{self.dimension}_waypoints"
return f"xaero_waypoint_add:{self.name}:{self.title}:{':'.join(map(str,self.pos))}:{formatting_codes.index(self.color)}:false:0:Internal_{self.dimension}_waypoints"

0 comments on commit c876e81

Please sign in to comment.