Skip to content

Commit

Permalink
🐛 fix adapter Minecraft
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Aug 13, 2024
1 parent ec9fa4a commit 01abebf
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 45 deletions.
4 changes: 4 additions & 0 deletions src/nonebot_plugin_alconna/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ def source(self) -> Alconna:
def matched(self) -> bool:
return self.result.matched

@property
def context(self):
return self.result.context

if PYDANTIC_V2:
model_config = ConfigDict(frozen=True, arbitrary_types_allowed=True) # type: ignore
else:
Expand Down
46 changes: 19 additions & 27 deletions src/nonebot_plugin_alconna/uniseg/adapters/minecraft/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,38 @@ class MinecraftMessageBuilder(MessageBuilder):
def get_adapter(cls) -> SupportAdapter:
return SupportAdapter.minecraft

@build("text")
def text(self, seg: MessageSegment):
text = Text(seg.data["text"])
_len = len(seg.data["text"])
def get_styles(self, data: dict):
styles = []
if "color" in seg.data and seg.data["color"] != TextColor.WHITE:
styles.append(seg.data["color"])
if "bold" in seg.data and seg.data["bold"]:
if "color" in data and data["color"] and data["color"] != TextColor.WHITE:
styles.append(data["color"])
if "bold" in data and data["bold"]:
styles.append("bold")
if "italic" in seg.data and seg.data["italic"]:
if "italic" in data and data["italic"]:
styles.append("italic")
if "underlined" in seg.data and seg.data["underlined"]:
if "underlined" in data and data["underlined"]:
styles.append("underlined")
if "strikethrough" in seg.data and seg.data["strikethrough"]:
if "strikethrough" in data and data["strikethrough"]:
styles.append("strikethrough")
if "obfuscated" in seg.data and seg.data["obfuscated"]:
if "obfuscated" in data and data["obfuscated"]:
styles.append("obfuscated")
text.mark(0, _len, *styles)
return styles

@build("text")
def text(self, seg: MessageSegment):
text = Text(seg.data["text"])
_len = len(seg.data["text"])
text.mark(0, _len, *self.get_styles(seg.data))
return text

@build("title")
def title(self, seg: MessageSegment):
return Text(seg.data["title"]).mark(0, len(seg.data["title"]), "title")
text = Text(seg.data["title"]["text"])
_len = len(seg.data["title"]["text"])
return text.mark(0, _len, "title", *self.get_styles(seg.data["title"]))

@build("actionbar")
def actionbar(self, seg: MessageSegment):
text = Text(seg.data["text"])
_len = len(seg.data["text"])
styles = ["actionbar"]
if "color" in seg.data and seg.data["color"] != TextColor.WHITE:
styles.append(seg.data["color"])
if "bold" in seg.data and seg.data["bold"]:
styles.append("bold")
if "italic" in seg.data and seg.data["italic"]:
styles.append("italic")
if "underlined" in seg.data and seg.data["underlined"]:
styles.append("underlined")
if "strikethrough" in seg.data and seg.data["strikethrough"]:
styles.append("strikethrough")
if "obfuscated" in seg.data and seg.data["obfuscated"]:
styles.append("obfuscated")
text.mark(0, _len, *styles)
text.mark(0, _len, "actionbar", *self.get_styles(seg.data))
return text
40 changes: 23 additions & 17 deletions src/nonebot_plugin_alconna/uniseg/adapters/minecraft/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ def get_target(self, event: Event, bot: Union[Bot, None] = None) -> Target:

@export
async def text(self, seg: Text, bot: Union[Bot, None]) -> "MessageSegment":
styles = [STYLE_TYPE_MAP[s] for s in seg.styles[(0, len(seg.text))] if s in STYLE_TYPE_MAP]
styles = seg.extract_most_styles()
kwargs = {}
for style in styles:
if style not in STYLE_TYPE_MAP:
continue
style = STYLE_TYPE_MAP[style]
if style == "bold":
kwargs["bold"] = True
elif style == "italic":
Expand All @@ -70,30 +73,33 @@ async def text(self, seg: Text, bot: Union[Bot, None]) -> "MessageSegment":
kwargs["obfuscated"] = True
else:
kwargs["color"] = style
if seg.extract_most_style() == "actionbar":
if "actionbar" in styles:
return MessageSegment.actionbar(seg.text, **kwargs)
if seg.extract_most_style() == "title":
if "title" in styles:
return MessageSegment.title(BaseComponent(text=seg.text, **kwargs))
return MessageSegment.text(seg.text, **kwargs)

@export
async def button(self, seg: Button, bot: Union[Bot, None]):
label = Text(seg.label) if isinstance(seg.label, str) else seg.label
styles = [STYLE_TYPE_MAP[s] for s in label.styles[(0, len(label.text))] if s in STYLE_TYPE_MAP]
kwargs = {}
for style in styles:
if style == "bold":
kwargs["bold"] = True
elif style == "italic":
kwargs["italic"] = True
elif style == "underline":
kwargs["underlined"] = True
elif style == "strikethrough":
kwargs["strikethrough"] = True
elif style == "obfuscated":
kwargs["obfuscated"] = True
else:
kwargs["color"] = style
if label.styles:
for style in label.extract_most_styles():
if style not in STYLE_TYPE_MAP:
continue
style = STYLE_TYPE_MAP[style]
if style == "bold":
kwargs["bold"] = True
elif style == "italic":
kwargs["italic"] = True
elif style == "underline":
kwargs["underlined"] = True
elif style == "strikethrough":
kwargs["strikethrough"] = True
elif style == "obfuscated":
kwargs["obfuscated"] = True
else:
kwargs["color"] = style
if seg.clicked_label:
kwargs["hover_event"] = HoverEvent(
action=HoverAction.SHOW_TEXT, base_component_list=[BaseComponent(text=seg.clicked_label)]
Expand Down
2 changes: 1 addition & 1 deletion src/nonebot_plugin_alconna/uniseg/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def merge_text(msg: Message) -> Message:
result = []
last = list.__getitem__(msg, 0)
for seg in list.__getitem__(msg, slice(1, None)):
if seg.is_text() and last.is_text():
if seg.is_text() and last.is_text() and seg.type == "text" and len(seg.data) == 1:
last.data["text"] += seg.data["text"]
else:
result.append(last)
Expand Down

0 comments on commit 01abebf

Please sign in to comment.