Skip to content

Commit

Permalink
Implement basic serializer
Browse files Browse the repository at this point in the history
Reimplement Replacers
  • Loading branch information
imDaniX committed Apr 27, 2023
1 parent 5a0cd33 commit 9e9e073
Show file tree
Hide file tree
Showing 21 changed files with 478 additions and 281 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ Those are its possible tags:

## TODO (until full release)
- Placeholders (for keybinds, translations, selectors)
- Actually implement serializer (as all we have now is deserializer lol)
- Fix symbolic styles stacking on serialization
- Simplified MineDown-like style tags (e.g. `&[GitHub Link](https://github.com/)`, `&[Stop the server](/stop)`, `&[Bold text](bold)`)
- Refactor to not use special characters to detect nodes
- Closing colors(?)
- More tests

Expand All @@ -45,7 +46,7 @@ Add to dependencies
<dependency>
<groupId>ink.glowing</groupId>
<artifactId>inkymessage</artifactId>
<version>0.7.2</version> <!-- Check the version above -->
<version>0.8.0</version> <!-- Check the version above -->
</dependency>
```
### Gradle
Expand All @@ -57,6 +58,6 @@ repositories {
}

dependencies {
implementation("ink.glowing:inkymessage:0.7.2") // Check the version above
implementation("ink.glowing:inkymessage:0.8.0") // Check the version above
}
```
15 changes: 1 addition & 14 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>ink.glowing</groupId>
<artifactId>inkymessage</artifactId>
<version>0.7.3-SNAPSHOT</version>
<version>0.8.0</version>

<properties>
<adventure.version>4.13.1</adventure.version>
Expand Down Expand Up @@ -35,21 +35,8 @@
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.themoep</groupId>
<artifactId>minedown-adventure</artifactId>
<version>1.7.1-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
<repository>
<id>minebench-repo</id>
<url>https://repo.minebench.de/</url>
</repository>
</repositories>

<distributionManagement>
<repository>
<id>glowing-ink</id>
Expand Down
86 changes: 67 additions & 19 deletions src/main/java/ink/glowing/text/InkyMessage.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package ink.glowing.text;

import ink.glowing.text.rich.BuildContext;
import ink.glowing.text.rich.RichNode;
import ink.glowing.text.style.symbolic.SymbolicStyle;
import ink.glowing.text.utils.function.InstanceProvider;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentIteratorType;
import net.kyori.adventure.text.KeybindComponent;
import net.kyori.adventure.text.ScoreComponent;
import net.kyori.adventure.text.SelectorComponent;
Expand All @@ -14,7 +13,7 @@
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -50,17 +49,24 @@ private InkyMessage() {}
* @return converted text component
*/
public @NotNull Component deserialize(@NotNull String inputText, @NotNull InkyMessageResolver inkyResolver) {
List<RichNode> richNodes = new ArrayList<>();
BuildContext context = new BuildContext(richNodes, inkyResolver);
return deserialize(inputText, new BuildContext(new ArrayList<>(), inkyResolver));
}

/**
* Convert string into adventure text component
* @param inputText input string
* @param context context to build with
* @return converted text component
*/
public @NotNull Component deserialize(@NotNull String inputText, @NotNull BuildContext context) {
String oldText = inputText;
int minimalIndex = inputText.indexOf("](");
String newText = parseRich(oldText, minimalIndex, context);
while (!newText.equals(oldText)) {
oldText = newText;
newText = parseRich(oldText, 2, context);
}
return node(newText).render(new BuildContext(richNodes, inkyResolver)).compact();
return node(newText).render(context).compact();
}

private static @NotNull String parseRich(@NotNull String input, int fromIndex, @NotNull BuildContext context) {
Expand Down Expand Up @@ -100,30 +106,72 @@ private InkyMessage() {}
}

@Override
public @NotNull String serialize(@NotNull Component component) {
return serialize(component, standardInkyResolver());
public @NotNull String serialize(@NotNull Component text) {
return serialize(text, standardInkyResolver());
}
public @NotNull String serialize(@NotNull Component component, @NotNull InkyMessageResolver resolver) {

public @NotNull String serialize(@NotNull Component text, @NotNull InkyMessageResolver resolver) {
StringBuilder builder = new StringBuilder();
for (var child : component.iterable(ComponentIteratorType.BREADTH_FIRST)) {
serialize(builder, child);
}
serialize(builder, new TreeSet<>(), text, resolver, new boolean[]{false});
return builder.toString();
}

private void serialize(@NotNull StringBuilder builder, @NotNull Component component) {
private void serialize(
@NotNull StringBuilder builder,
final @NotNull TreeSet<SymbolicStyle> outerStyle,
@NotNull Component text,
@NotNull InkyMessageResolver resolver,
boolean @NotNull [] previousStyled
) {
var tags = resolver.readStyleTags(text);
if (!tags.isEmpty()) {
builder.append("&[");
}
var currentStyle = resolver.readSymbolics(text);
if (previousStyled[0] && (currentStyle.isEmpty() || !currentStyle.first().resets())) {
if (outerStyle.isEmpty()) {
builder.append(resolver.symbolicReset().asFormatted());
} else for (var symb : outerStyle) {
builder.append(symb.asFormatted());
}
}
if (currentStyle.isEmpty()) {
previousStyled[0] = false;
} else {
previousStyled[0] = true;
for (var symb : currentStyle) {
builder.append(symb.asFormatted());
}
}

builder.append(escape(asString(text)));
var children = text.children();
var newOuterStyle = new TreeSet<>(outerStyle);
newOuterStyle.addAll(currentStyle);
for (var child : children) {
serialize(builder, newOuterStyle, child, resolver, previousStyled);
}
if (!tags.isEmpty()) {
builder.append("]");
for (var tag : tags) {
builder.append(tag);
}
}
}

private static String asString(@NotNull Component component) {
if (component instanceof TextComponent text) {
builder.append(text.content());
return text.content();
} else if (component instanceof TranslatableComponent translatable) {
builder.append(translatable.key());
return "{tl:" + translatable.key() + "}"; // TODO implement; args, fallback
} else if (component instanceof KeybindComponent keybind) {
builder.append(keybind.keybind());
return "{keybind:" + keybind.keybind() + "}"; // TODO implement
} else if (component instanceof ScoreComponent score) {
builder.append(score.objective());
return score.objective(); // TODO implement
} else if (component instanceof SelectorComponent selector) {
builder.append(selector.pattern());
return selector.pattern(); // TODO implement
} else {
builder.append('?');
return "?";
}
}

Expand Down
Loading

0 comments on commit 9e9e073

Please sign in to comment.