This repository has been archived by the owner on Sep 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
174dd2e
commit 58f5c42
Showing
4 changed files
with
146 additions
and
24 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/main/java/io/github/darkkronicle/advancedchatcore/util/RawText.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/io/github/darkkronicle/advancedchatcore/util/TextBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters