Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

Commit

Permalink
Fixing replacing strings
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkKronicle committed Jul 25, 2022
1 parent 174dd2e commit 58f5c42
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.github.darkkronicle.advancedchatcore.util;

import net.minecraft.text.*;
import net.minecraft.util.Language;

import java.util.ArrayList;
import java.util.List;

public record RawText(String content, Style style) implements Text {

@Override
public Style getStyle() {
return style;
}

@Override
public TextContent getContent() {
return new LiteralTextContent(content);
}

@Override
public String getString() {
return content;
}

@Override
public List<Text> getSiblings() {
return new ArrayList<>();
}

@Override
public OrderedText asOrderedText() {
Language language = Language.getInstance();
return language.reorder(this);
}

public static RawText of(String string) {
return RawText.of(string, Style.EMPTY);
}

public static RawText of(String string, Style style) {
return new RawText(string, style);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,27 @@ public static MutableText formatText(Text text) {
return t;
}

public static MutableText flattenText(Text text) {
List<Text> newSiblings = new ArrayList<>();
Style last = text.getStyle();
StringBuilder content = new StringBuilder(TextUtil.getContent(text.getContent()));
for (Text t : text.getSiblings()) {
if (t.getStyle().equals(last)) {
content.append(TextUtil.getContent(t.getContent()));
continue;
}
newSiblings.add(Text.literal(content.toString()).fillStyle(last));
content = new StringBuilder(TextUtil.getContent(t.getContent()));
last = t.getStyle();
}
newSiblings.add(Text.literal(content.toString()).fillStyle(last));
MutableText newText = Text.empty();
for (Text sibling : newSiblings) {
newText.append(sibling);
}
return newText;
}

/**
* Wraps text into multiple lines
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.github.darkkronicle.advancedchatcore.util;

import net.minecraft.text.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

public class TextBuilder {

private List<RawText> siblings = new ArrayList<>();

public TextBuilder append(String string) {
siblings.add(RawText.of(string));
return this;
}

public TextBuilder append(String string, Style style) {
siblings.add(RawText.of(string, style));
return this;
}

public List<RawText> getTexts() {
return siblings;
}

public TextBuilder append(Text text) {
AtomicReference<Style> last = new AtomicReference<>(null);
AtomicReference<StringBuilder> builder = new AtomicReference<>(new StringBuilder());
text.visit((style, asString) -> {
if (last.get() == null) {
last.set(style);
builder.get().append(asString);
return Optional.empty();
} else if (last.get().equals(style)) {
builder.get().append(asString);
return Optional.empty();
}
append(builder.get().toString(), last.get());
last.set(style);
builder.set(new StringBuilder(asString));
return Optional.empty();
}, Style.EMPTY);
if (!builder.get().isEmpty()) {
append(builder.get().toString(), last.get());
}
return this;
}

public MutableText build() {
MutableText newText = Text.empty();
for (RawText sib : siblings) {
newText.append(Text.literal(sib.content()).fillStyle(sib.style()));
}
return newText;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@
package io.github.darkkronicle.advancedchatcore.util;

import fi.dy.masa.malilib.util.StringUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import java.util.*;
import java.util.function.BiFunction;
import lombok.experimental.UtilityClass;
import net.minecraft.text.MutableText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.text.TextContent;

@UtilityClass
public class TextUtil {
Expand Down Expand Up @@ -152,37 +149,35 @@ public Text replaceStrings(Text input, Map<StringMatch, StringInsert> matches) {
return input;
}
// List of new RawText to form a new FluidText.
ArrayList<Text> newSiblings = new ArrayList<>();
TextBuilder newSiblings = new TextBuilder();
// What match this is currently on.
Map.Entry<StringMatch, StringInsert> match = sortedMatches.next();

// Total number of chars went through. Used to find where the match end and beginning is.
int totalchar = 0;
boolean inMatch = false;
List<Text> siblings = input.getSiblings();
siblings.add(0, MutableText.of(input.getContent()).fillStyle(input.getStyle()));
for (Text text : siblings) {
for (RawText text : new TextBuilder().append(input).getTexts()) {
if (text.getString() == null || text.getString().length() <= 0) {
continue;
}
if (match == null) {
// No more replacing...
newSiblings.add(text);
newSiblings.append(text);
continue;
}
int length = text.getString().length();
int last = 0;
while (true) {
if (length + totalchar <= match.getKey().start) {
newSiblings.add(Text.literal((text.getString().substring(last))).fillStyle(text.getStyle()));
newSiblings.append(text.getString().substring(last), text.getStyle());
break;
}
int start = match.getKey().start - totalchar;
int end = match.getKey().end - totalchar;
if (inMatch) {
if (end <= length) {
inMatch = false;
newSiblings.add(Text.literal((text.getString().substring(end))).fillStyle(text.getStyle()));
newSiblings.append(text.getString().substring(end), text.getStyle());
last = end;
if (!sortedMatches.hasNext()) {
match = null;
Expand All @@ -196,11 +191,10 @@ public Text replaceStrings(Text input, Map<StringMatch, StringInsert> matches) {
// End will go onto another string
if (start > 0) {
// Add previous string section
newSiblings.add(Text.literal(text.getString().substring(last, start)).fillStyle(text.getStyle()));
newSiblings.append(text.getString().substring(last, start), text.getStyle());
}
if (end >= length) {
newSiblings.addAll(
match.getValue().getText(text, match.getKey()).getSiblings());
newSiblings.append(match.getValue().getText(text, match.getKey()));
if (end == length) {
if (!sortedMatches.hasNext()) {
match = null;
Expand All @@ -212,16 +206,15 @@ public Text replaceStrings(Text input, Map<StringMatch, StringInsert> matches) {
}
break;
}
newSiblings.addAll(
match.getValue().getText(text, match.getKey()).getSiblings());
newSiblings.append(match.getValue().getText(text, match.getKey()));
if (!sortedMatches.hasNext()) {
match = null;
} else {
match = sortedMatches.next();
}
last = end;
if (match == null || match.getKey().start - totalchar > length) {
newSiblings.add(Text.literal(text.getString().substring(end)).fillStyle(text.getStyle()));
newSiblings.append(text.getString().substring(end), text.getStyle());
break;
}
} else {
Expand All @@ -234,11 +227,7 @@ public Text replaceStrings(Text input, Map<StringMatch, StringInsert> matches) {
totalchar = totalchar + length;
}

MutableText newtext = Text.empty();
for (Text sibling : newSiblings) {
newtext.append(sibling);
}
return newtext;
return newSiblings.build();
}

/**
Expand Down Expand Up @@ -347,4 +336,13 @@ public static boolean styleChanges(Text text, BiFunction<Style, Style, Boolean>
}
return false;
}

public static String getContent(TextContent content) {
StringBuilder builder = new StringBuilder();
content.visit((s) -> {
builder.append(s);
return Optional.empty();
});
return builder.toString();
}
}

0 comments on commit 58f5c42

Please sign in to comment.