Skip to content

Commit

Permalink
fix: fix \& escapes in messages not working
Browse files Browse the repository at this point in the history
Fix to unicode translation in previous commit; now specifically only
look for for \u unicode escapes to unescape, not anything else. Also
move unescaping from loading time to raw-parsing time.

FTBTeam/FTB-Mods-Issues#1480
  • Loading branch information
desht committed Jan 27, 2025
1 parent d7bd2c0 commit 935493d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.StringTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import org.apache.commons.lang3.StringEscapeUtils;

import java.util.HashMap;
import java.util.List;
Expand All @@ -33,14 +33,12 @@ public TranslationTable() {
this.map = new HashMap<>();
}

// deprecated StringEscapeUtils, but we don't have Apache Commons Text available to us, so whatever...
@SuppressWarnings("deprecation")
public static TranslationTable fromNBT(CompoundTag tag) {
Map<String, Either<String, List<String>>> map = new HashMap<>();
tag.getAllKeys().forEach(k -> {
switch (tag.get(k)) {
case StringTag str -> map.put(k, Either.left(StringEscapeUtils.unescapeJava(str.getAsString())));
case ListTag list -> map.put(k, Either.right(list.stream().map(tag1 -> StringEscapeUtils.unescapeJava(tag1.getAsString())).toList()));
case StringTag str -> map.put(k, Either.left(str.getAsString()));
case ListTag list -> map.put(k, Either.right(list.stream().map(Tag::getAsString).toList()));
case null, default -> { }
}
});
Expand Down
10 changes: 7 additions & 3 deletions common/src/main/java/dev/ftb/mods/ftbquests/util/TextUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.contents.PlainTextContents;
import org.apache.commons.lang3.text.translate.UnicodeUnescaper;

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

public class TextUtils {
// deprecated in apache commons, but we don't have apache commons text available here
@SuppressWarnings("deprecation")
private static final UnicodeUnescaper UNESCAPER = new UnicodeUnescaper();

/**
* Parse some rich text into a Component. Use vanilla-style raw JSON if applicable, fall back to old-style FTB
* Quests rich text otherwise. (FTB Quests rich text is more concise, raw JSON is much more powerful)
Expand All @@ -25,15 +30,14 @@ public static Component parseRawText(String str, HolderLookup.Provider provider)
if (str2.startsWith("[") && str2.endsWith("]") || str2.startsWith("{") && str2.endsWith("}")) {
// could be JSON raw text, but not for definite...
try {
MutableComponent res = Component.Serializer.fromJson(str2, provider);
MutableComponent res = Component.Serializer.fromJson(UNESCAPER.translate(str2), provider);
if (res != null) {
return res;
}
} catch (JsonParseException ignored) {

}
}
return ClientTextComponentUtils.parse(str);
return ClientTextComponentUtils.parse(UNESCAPER.translate(str));
}

public static List<String> fromListTag(ListTag tag) {
Expand Down

0 comments on commit 935493d

Please sign in to comment.