Skip to content
Merged
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ allprojects {
dependencies {
// Fabric
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
mappings loom.officialMojangMappings()
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

// Code Quality
Expand Down
34 changes: 16 additions & 18 deletions ec-core/src/main/java/dev/jpcode/eccore/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.Style;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import net.minecraft.text.MutableText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

public abstract class Config<T extends Config<T>> {
static final Logger LOGGER = LogManager.getLogger("ec-core-config");

Expand Down Expand Up @@ -110,16 +108,16 @@ public void storeProperties() {

}

static final Style DEFAULT_STYLE = Style.EMPTY.withFormatting(Formatting.GOLD);
static final Style ACCENT_STYLE = Style.EMPTY.withFormatting(Formatting.GREEN);
static final Style DEFAULT_STYLE = Style.EMPTY.applyFormat(ChatFormatting.GOLD);
static final Style ACCENT_STYLE = Style.EMPTY.applyFormat(ChatFormatting.GREEN);

public @NotNull Text stateAsText() {
var result = Text.empty();
public @NotNull Component stateAsText() {
var result = Component.empty();
String newLine = "\n";//System.getProperty("line.separator");

result.append(Text.literal(displayName + " {").setStyle(DEFAULT_STYLE));
result.append(Component.literal(displayName + " {").setStyle(DEFAULT_STYLE));
result.append(newLine);
var propsText = Text.empty();
var propsText = Component.empty();
result.append(propsText);

//print field names paired with their values
Expand All @@ -132,7 +130,7 @@ public void storeProperties() {
ex.printStackTrace();
}
}
result.append(Text.literal("}").setStyle(ACCENT_STYLE));
result.append(Component.literal("}").setStyle(ACCENT_STYLE));

return result;

Expand All @@ -159,14 +157,14 @@ public List<String> getPublicFieldNames() {
return publicFieldNames;
}

private MutableText fieldAsText(Field field) throws IllegalAccessException {
private MutableComponent fieldAsText(Field field) throws IllegalAccessException {
var value = (Option<?>) field.get(this);
return Text.empty()
.append(Text.literal(field.getName() + ": ").setStyle(DEFAULT_STYLE))
.append(Text.literal(value.getValue().toString()));
return Component.empty()
.append(Component.literal(field.getName() + ": ").setStyle(DEFAULT_STYLE))
.append(Component.literal(value.getValue().toString()));
}

public @Nullable MutableText getFieldValueAsText(String fieldName) throws NoSuchFieldException {
public @Nullable MutableComponent getFieldValueAsText(String fieldName) throws NoSuchFieldException {
try {
return fieldAsText(this.getClass().getField(fieldName));
} catch (IllegalAccessException e) {
Expand Down
25 changes: 11 additions & 14 deletions ec-core/src/main/java/dev/jpcode/eccore/config/ConfigUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonNull;
import com.google.gson.JsonSyntaxException;
import org.apache.logging.log4j.Level;
Expand All @@ -13,12 +16,6 @@
import org.jetbrains.annotations.Nullable;

import com.mojang.serialization.JsonOps;

import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.JsonHelper;

import dev.jpcode.eccore.util.TimeUtil;

import static dev.jpcode.eccore.config.Config.LOGGER;
Expand Down Expand Up @@ -53,16 +50,16 @@ public static Style parseStyleOrDefault(String styleStr, String defaultStyleStr)
@Nullable
public static Style parseStyle(String styleStr) {
Style outStyle = null;
Formatting formatting = Formatting.byName(styleStr);
ChatFormatting formatting = ChatFormatting.getByName(styleStr);
if (formatting != null) {
outStyle = Style.EMPTY.withFormatting(formatting);
outStyle = Style.EMPTY.applyFormat(formatting);
}

if (outStyle == null) {
try {
outStyle = Style.Codecs.CODEC.parse(
outStyle = Style.Serializer.CODEC.parse(
JsonOps.INSTANCE,
JsonHelper.deserialize(styleStr)
GsonHelper.parse(styleStr)
).result().orElse(null);
} catch (JsonSyntaxException e) {
LOGGER.log(Level.ERROR, String.format(
Expand All @@ -74,8 +71,8 @@ public static Style parseStyle(String styleStr) {
return outStyle;
}

public static Text parseTextOrDefault(String textStr, String defaultTextStr) {
Text outText = null;
public static Component parseTextOrDefault(String textStr, String defaultTextStr) {
Component outText = null;
if (textStr != null) {
outText = parseText(textStr);
}
Expand Down Expand Up @@ -154,7 +151,7 @@ private static void logNumberParseError(String num, String type) {
}

public static String serializeStyle(Style style) {
return Style.Codecs.CODEC.encodeStart(JsonOps.INSTANCE, style).result().orElse(JsonNull.INSTANCE).toString();
return Style.Serializer.CODEC.encodeStart(JsonOps.INSTANCE, style).result().orElse(JsonNull.INSTANCE).toString();
}

public static int parseDurationToTicks(String str) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package dev.jpcode.eccore.util;

import net.minecraft.text.Text;
import net.minecraft.network.chat.Component;

@FunctionalInterface
public interface StringToTextParser {

Text parseText(String str);
Component parseText(String str);
}
79 changes: 40 additions & 39 deletions ec-core/src/main/java/dev/jpcode/eccore/util/TextUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@

import com.mojang.serialization.JsonOps;

import net.minecraft.text.*;
import net.minecraft.network.chat.*;
import net.minecraft.network.chat.contents.PlainTextContents;

public final class TextUtil {
private TextUtil() {}

public static MutableText concat(Text... arr) {
MutableText out = Text.empty();
for (Text text : arr) {
public static MutableComponent concat(Component... arr) {
MutableComponent out = Component.empty();
for (Component text : arr) {
out.append(text);
}
return out;
}

public static MutableText deepCopy(Text text) {
public static MutableComponent deepCopy(Component text) {
if (text.getSiblings().isEmpty()) {
return text.copy();
}
Expand All @@ -41,8 +42,8 @@ public static MutableText deepCopy(Text text) {
}

/**
* <p>Joins the elements of the provided array into a single Text
* containing the provided list of elements.</p>
* Joins the elements of the provided array into a single Text
* containing the provided list of elements.
*
* <p>No delimiter is added before or after the list.
* Null objects or empty strings within the array are represented by
Expand All @@ -62,26 +63,26 @@ public static MutableText deepCopy(Text text) {
* @return the joined String, <code>null</code> if null array input
* @since 2.0
*/
public static MutableText join(Text[] array, Text separator) {
public static MutableComponent join(Component[] array, Component separator) {
if (array == null) {
return null;
}
return join(array, separator, 0, array.length);
}

public static MutableText join(Collection<Text> textCollection, Text separator) {
public static MutableComponent join(Collection<Component> textCollection, Component separator) {
if (textCollection == null) {
return null;
}
return join(textCollection.toArray(new Text[0]), separator, 0, textCollection.size());
return join(textCollection.toArray(new Component[0]), separator, 0, textCollection.size());
}

public static MutableText join(Collection<String> stringCollection, Text separator, Style stringsFormatting) {
public static MutableComponent join(Collection<String> stringCollection, Component separator, Style stringsFormatting) {
if (stringCollection == null) {
return null;
}
return join(
stringCollection.stream().map(str -> Text.literal(str).setStyle(stringsFormatting)).toArray(Text[]::new),
stringCollection.stream().map(str -> Component.literal(str).setStyle(stringsFormatting)).toArray(Component[]::new),
separator, 0, stringCollection.size()
);
}
Expand Down Expand Up @@ -116,15 +117,15 @@ public static String joinStrings(String[] array, String separator, int startInde
return buf.toString();
}

public static MutableText join(Text[] array, Text separator, int startIndex, int endIndex) {
public static MutableComponent join(Component[] array, Component separator, int startIndex, int endIndex) {
if (array == null) {
return null;
}
int bufSize = (endIndex - startIndex);
if (bufSize <= 0) {
return null;
}
MutableText buf = Text.empty();
MutableComponent buf = Component.empty();
for (int i = startIndex; i < endIndex; i++) {
if (i > startIndex) {
buf.append(separator);
Expand All @@ -136,9 +137,9 @@ public static MutableText join(Text[] array, Text separator, int startIndex, int
return buf;
}

public static MutableText spaceBetween(Text[] array, int totalWidth, int padding) {
public static MutableComponent spaceBetween(Component[] array, int totalWidth, int padding) {
int totalTextSize = 0;
for (Text txt : array) {
for (Component txt : array) {
String str = txt.getString();
totalTextSize += str.length();
}
Expand All @@ -148,37 +149,37 @@ public static MutableText spaceBetween(Text[] array, int totalWidth, int padding
return concat(array);
}

MutableText outText = Text.empty();
MutableComponent outText = Component.empty();
String lrPadStr = " ".repeat(padding);
String spaceStr = " ".repeat((totalWidth - padding * 2 - totalTextSize) / (array.length - 1));
outText.append(Text.literal(lrPadStr));
outText.append(Component.literal(lrPadStr));

for (int i = 0; i < array.length; i++) {
outText.append(array[i]);
if (i != array.length - 1) {
outText.append(Text.literal(spaceStr));
outText.append(Component.literal(spaceStr));
}
}

outText.append(Text.literal(lrPadStr));
outText.append(Component.literal(lrPadStr));

return outText;
}

public static MutableText clickableTeleport(MutableText originalText, String destinationName, String commandBaseString) {
public static MutableComponent clickableTeleport(MutableComponent originalText, String destinationName, String commandBaseString) {
String teleportCommand = String.format("%s %s", commandBaseString, destinationName);

Style outStyle = originalText.getStyle()
.withClickEvent(new ClickEvent.RunCommand(teleportCommand))
.withHoverEvent(new HoverEvent.ShowText(Text.literal("Click to teleport to "
.withHoverEvent(new HoverEvent.ShowText(Component.literal("Click to teleport to "
+ destinationName
+ ".")));

return originalText.setStyle(outStyle);
}

public static String toJsonString(Text text) {
return TextCodecs.CODEC
public static String toJsonString(Component text) {
return ComponentSerialization.CODEC
.encodeStart(JsonOps.INSTANCE, text)
.getOrThrow()
.toString();
Expand All @@ -194,12 +195,12 @@ public static void registerTextParser(StringToTextParser parser) {
}

static {
registerTextParser(str -> TextCodecs.CODEC.parse(JsonOps.INSTANCE, JsonParser.parseString(str)).getOrThrow());
registerTextParser(str -> ComponentSerialization.CODEC.parse(JsonOps.INSTANCE, JsonParser.parseString(str)).getOrThrow());
registerTextParser(str -> TagParser.DEFAULT.parseText(str, ParserContext.of()));
}

public static Text parseText(String textStr) {
Text outText = null;
public static Component parseText(String textStr) {
Component outText = null;
for (StringToTextParser parser : TEXT_PARSERS) {
try {
outText = parser.parseText(textStr);
Expand All @@ -215,28 +216,28 @@ public static Text parseText(String textStr) {
throw new RuntimeException(String.format("Failed to parse string '%s' as MinecraftText using any parsing strategy", textStr));
}

public static Collector<Text, MutableText, MutableText> collect() {
public static Collector<Component, MutableComponent, MutableComponent> collect() {
return new Collector<>() {
@Override
public Supplier<MutableText> supplier() {
return Text::empty;
public Supplier<MutableComponent> supplier() {
return Component::empty;
}

@Override
public BiConsumer<MutableText, Text> accumulator() {
return MutableText::append;
public BiConsumer<MutableComponent, Component> accumulator() {
return MutableComponent::append;
}

@Override
public BinaryOperator<MutableText> combiner() {
public BinaryOperator<MutableComponent> combiner() {
return (r1, r2) -> {
r1.append(r2);
return r1;
};
}

@Override
public Function<MutableText, MutableText> finisher() {
public Function<MutableComponent, MutableComponent> finisher() {
return (a) -> a;
}

Expand All @@ -252,17 +253,17 @@ public Set<Characteristics> characteristics() {
*
* @return flattened text
*/
public static List<Text> flattenRoot(Text text) {
public static List<Component> flattenRoot(Component text) {
var siblings = text.getSiblings();
if (text.getContent().equals(PlainTextContent.EMPTY) && siblings.size() == 1) {
if (text.getContents().equals(PlainTextContents.EMPTY) && siblings.size() == 1) {
return siblings;
} else if (siblings.size() == 0) {
return List.of(text);
}

List<Text> content = new ArrayList<>(siblings.size() + 1);
if (!text.getContent().equals(PlainTextContent.EMPTY)) {
content.add(text.copyContentOnly().setStyle(text.getStyle()));
List<Component> content = new ArrayList<>(siblings.size() + 1);
if (!text.getContents().equals(PlainTextContents.EMPTY)) {
content.add(text.plainCopy().setStyle(text.getStyle()));
}
content.addAll(siblings);

Expand Down
Loading