From 7e0dabe6b537cf8544d114442bd43223ee0b18bd Mon Sep 17 00:00:00 2001 From: "Tobias Burdow [Kaleidox]" Date: Wed, 9 Aug 2023 11:15:48 +0200 Subject: [PATCH 1/3] introduce md->mc converter --- .../mcsd/agent/discord/DiscordConnection.java | 34 ++++++++++------ .../java/org/comroid/mcsd/util/Tellraw.java | 40 +++++++++++++++++++ .../org/comroid/mcsd/util/TellrawTest.java | 3 +- 3 files changed, 63 insertions(+), 14 deletions(-) diff --git a/src/agent/main/java/org/comroid/mcsd/agent/discord/DiscordConnection.java b/src/agent/main/java/org/comroid/mcsd/agent/discord/DiscordConnection.java index d58ac68d..9807920c 100644 --- a/src/agent/main/java/org/comroid/mcsd/agent/discord/DiscordConnection.java +++ b/src/agent/main/java/org/comroid/mcsd/agent/discord/DiscordConnection.java @@ -19,14 +19,18 @@ import org.comroid.mcsd.util.McFormatCode; import org.comroid.mcsd.util.Tellraw; import org.comroid.util.Markdown; +import org.comroid.util.Pair; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.yaml.snakeyaml.error.Mark; import java.io.*; +import java.util.ArrayList; import java.util.Optional; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Stream; import static org.comroid.mcsd.core.util.ApplicationContextProvider.bean; @@ -87,18 +91,21 @@ public DiscordConnection(ServerProcess srv) { .filterData(e -> e.getChannel().getIdLong() == id) .mapData(MessageReceivedEvent::getMessage) .filterData(msg -> !msg.getAuthor().isBot()) - .mapData(msg -> Tellraw.Command.builder() - .selector(Tellraw.Selector.Base.ALL_PLAYERS) - .component(Gray.text("<").build()) - .component(Dark_Aqua.text(msg.getAuthor().getEffectiveName()) - .hoverEvent(show_text.value("Open in Discord")) - .clickEvent(open_url.value(msg.getJumpUrl())) - .build()) - .component(Gray.text(">").build()) - // todo convert markdown to tellraw data - .component(Reset.text(" " + msg.getContentStripped()).build()) - .build() - .toString()) + .mapData(msg -> { + // convert markdown to tellraw data + return Tellraw.Command.builder() + .selector(Tellraw.Selector.Base.ALL_PLAYERS) + .component(Gray.text("<").build()) + .component(Dark_Aqua.text(msg.getAuthor().getEffectiveName()) + .hoverEvent(show_text.value("Open in Discord")) + .clickEvent(open_url.value(msg.getJumpUrl())) + .build()) + .component(Gray.text("> ").build()) + //.component(Reset.text(" " + msg.getContentStripped()).build()) + .build() + .appendMarkdown(msg.getContentRaw()) + .toString(); + }) .peekData(log::fine) .subscribeData(srv.getIn()::println)).stream(), // minecraft -> public channel @@ -151,7 +158,8 @@ public DiscordConnection(ServerProcess srv) { if (server.getConsoleMode() == Server.ConsoleMode.ScrollClean && !msg.getAuthor().equals(adapter.getJda().getSelfUser())) msg.delete().queue(); - return raw; + //noinspection RedundantCast //todo wtf intellij + return (String) raw; }) .filterData(cmd -> cmd.startsWith(">")) .peekData(out::println) diff --git a/src/api/main/java/org/comroid/mcsd/util/Tellraw.java b/src/api/main/java/org/comroid/mcsd/util/Tellraw.java index ecd2e57a..99b6617c 100644 --- a/src/api/main/java/org/comroid/mcsd/util/Tellraw.java +++ b/src/api/main/java/org/comroid/mcsd/util/Tellraw.java @@ -1,17 +1,26 @@ package org.comroid.mcsd.util; import lombok.*; +import lombok.experimental.NonFinal; import org.comroid.abstr.DataNode; import org.comroid.api.Named; +import org.comroid.api.StringAttribute; +import org.comroid.api.TextDecoration; import org.comroid.api.Vector; import org.comroid.util.JSON; +import org.comroid.util.Markdown; +import org.comroid.util.Pair; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; +import java.util.stream.Stream; public interface Tellraw { @With @@ -20,6 +29,7 @@ public interface Tellraw { class Command implements Tellraw { ISelector selector; @Singular + @NonFinal List components; @Override @@ -29,6 +39,36 @@ public String toString() { .map(Component::toString) .collect(Collectors.joining(",","[","]")); } + + public Command appendMarkdown(String txt) { + return append(TextDecoration.replacers(Markdown.class, McFormatCode.class) + .map(Pair::getFirst) + .map(".*%s.*"::formatted) + .map(Pattern::compile) + .map(pattern -> pattern.matcher(txt)) + .filter(Matcher::matches) + .flatMap(matcher -> { + var ls = new ArrayList(); + var map = TextDecoration.styles(Markdown.class, McFormatCode.class); + var ignored = matcher.replaceAll(result -> { + var format = StringAttribute.valueOf(result.group(1), Markdown.class) + .orElse(Markdown.None); + var msg = result.group(2); + ls.add(Tellraw.Component.builder() + .format(map.get(format)) + .text(msg) + .build()); + return msg; + }); + return ls.stream(); + }) + .toArray(Tellraw.Component[]::new)); + } + + public Command append(Component... components) { + this.components = Stream.concat(this.components.stream(), Stream.of(components)).toList(); + return this; + } } interface ISelector extends Tellraw {} diff --git a/src/api/test/java/org/comroid/mcsd/util/TellrawTest.java b/src/api/test/java/org/comroid/mcsd/util/TellrawTest.java index 969a9efb..80153650 100644 --- a/src/api/test/java/org/comroid/mcsd/util/TellrawTest.java +++ b/src/api/test/java/org/comroid/mcsd/util/TellrawTest.java @@ -10,8 +10,9 @@ public static void main(String[] args) { .component(Gray.text("<").build()) .component(Dark_Aqua.text("kaleidox").build()) .component(Gray.text(">").build()) - .component(Reset.text(" hello world").build()) + .component(Reset.text("hello world").build()) .build() + .appendMarkdown("_kursive and **bold**_") .toString() ); } From 191db8dc7cd29d15cdc57d2687b5f35ba9c04735 Mon Sep 17 00:00:00 2001 From: burdoto Date: Wed, 9 Aug 2023 18:54:38 +0200 Subject: [PATCH 2/3] Update TextDecorationTest.java --- src/api/test/java/org/comroid/mcsd/util/TextDecorationTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/test/java/org/comroid/mcsd/util/TextDecorationTest.java b/src/api/test/java/org/comroid/mcsd/util/TextDecorationTest.java index 454c1d65..3c7c7b74 100644 --- a/src/api/test/java/org/comroid/mcsd/util/TextDecorationTest.java +++ b/src/api/test/java/org/comroid/mcsd/util/TextDecorationTest.java @@ -5,7 +5,7 @@ public class TextDecorationTest { public static void main(String[] args) { - final var input = "**Bold is extreme** and an _italics text may be weird_, what about a _second italics_, and I hate ~~strikethrough~~"; + final var input = "**Bold is extreme** and an _italics text may be weird_, what about a _second italics_, or a _stacked set of **format codes**?_ I hate ~~strikethrough~~"; final var output = TextDecoration.convert(input, Markdown.class, McFormatCode.class); final var backward = TextDecoration.convert(input, McFormatCode.class, Markdown.class); From 0f8c9fcec5e6065a2a7c0ff787c505fbb8ff98f2 Mon Sep 17 00:00:00 2001 From: "Tobias Burdow [Kaleidox]" Date: Sat, 12 Aug 2023 16:50:03 +0200 Subject: [PATCH 3/3] Update Tellraw.java --- src/api/main/java/org/comroid/mcsd/util/Tellraw.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api/main/java/org/comroid/mcsd/util/Tellraw.java b/src/api/main/java/org/comroid/mcsd/util/Tellraw.java index 99b6617c..2a069d06 100644 --- a/src/api/main/java/org/comroid/mcsd/util/Tellraw.java +++ b/src/api/main/java/org/comroid/mcsd/util/Tellraw.java @@ -41,6 +41,7 @@ public String toString() { } public Command appendMarkdown(String txt) { + final var map = TextDecoration.styles(Markdown.class, McFormatCode.class); return append(TextDecoration.replacers(Markdown.class, McFormatCode.class) .map(Pair::getFirst) .map(".*%s.*"::formatted) @@ -48,8 +49,7 @@ public Command appendMarkdown(String txt) { .map(pattern -> pattern.matcher(txt)) .filter(Matcher::matches) .flatMap(matcher -> { - var ls = new ArrayList(); - var map = TextDecoration.styles(Markdown.class, McFormatCode.class); + final var ls = new ArrayList(); var ignored = matcher.replaceAll(result -> { var format = StringAttribute.valueOf(result.group(1), Markdown.class) .orElse(Markdown.None);