Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Markdown and Minecraft format encoding #50

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@
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.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import static org.comroid.mcsd.core.util.ApplicationContextProvider.bean;
Expand Down Expand Up @@ -89,18 +93,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
Expand Down Expand Up @@ -167,7 +174,7 @@ public DiscordConnection(ServerProcess srv) {
if (server.getConsoleMode() == Server.ConsoleMode.ScrollClean
&& !msg.getAuthor().equals(adapter.getJda().getSelfUser()))
msg.delete().queue();
//noinspection RedundantCast //ide error
//noinspection RedundantCast //todo wtf intellij
return (String) raw;
})
.filterData(cmd -> cmd.startsWith(">"))
Expand Down
40 changes: 40 additions & 0 deletions src/api/main/java/org/comroid/mcsd/util/Tellraw.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,6 +29,7 @@ public interface Tellraw {
class Command implements Tellraw {
ISelector selector;
@Singular
@NonFinal
List<Component> components;

@Override
Expand All @@ -29,6 +39,36 @@ public String toString() {
.map(Component::toString)
.collect(Collectors.joining(",","[","]"));
}

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)
.map(Pattern::compile)
.map(pattern -> pattern.matcher(txt))
.filter(Matcher::matches)
.flatMap(matcher -> {
final var ls = new ArrayList<Component>();
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 {}
Expand Down
3 changes: 2 additions & 1 deletion src/api/test/java/org/comroid/mcsd/util/TellrawTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down