Skip to content

Commit

Permalink
Merge pull request #934 from booky10/fix/translation-fallback-adventure
Browse files Browse the repository at this point in the history
Fix compatibility with adventure versions older than 4.13.0
  • Loading branch information
retrooper authored Aug 12, 2024
2 parents 3fa5eaf + 9ccd851 commit 048903f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,17 @@ public AdventureNBTSerializer(boolean downsampleColor) {
if (text != null) {
builder = Component.text().content(text);
} else if (translate != null) {
TranslatableComponent.Builder i18nBuilder;
builder = i18nBuilder = Component.translatable().key(translate);
if (translateWith != null) {
if (BackwardCompatUtil.IS_4_15_0_OR_NEWER) {
builder = Component.translatable().key(translate).fallback(translateFallback).arguments(translateWith);
i18nBuilder.arguments(translateWith);
} else {
builder = Component.translatable().key(translate).fallback(translateFallback).args(translateWith);
i18nBuilder.args(translateWith);
}
} else {
builder = Component.translatable().key(translate).fallback(translateFallback);
}
if (BackwardCompatUtil.IS_4_13_0_OR_NEWER) {
i18nBuilder.fallback(translateFallback);
}
} else if (score != null) {
builder = Component.score()
Expand Down Expand Up @@ -230,9 +233,11 @@ public AdventureNBTSerializer(boolean downsampleColor) {
writer.writeUTF("translate", ((TranslatableComponent) component).key());

// translation fallback
String fallback = ((TranslatableComponent) component).fallback();
if (fallback != null) {
writer.writeUTF("fallback", fallback);
if (BackwardCompatUtil.IS_4_13_0_OR_NEWER) {
String fallback = ((TranslatableComponent) component).fallback();
if (fallback != null) {
writer.writeUTF("fallback", fallback);
}
}

// translation arguments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,20 @@

public final class BackwardCompatUtil {

public static final boolean IS_4_13_0_OR_NEWER;
public static final boolean IS_4_15_0_OR_NEWER;
public static final boolean IS_4_17_0_OR_NEWER;

static {
boolean is4_13_0OrNewer = false;
try {
// translatable fallback support was added in 4.13.0
Component.translatable().fallback("");
is4_13_0OrNewer = true;
} catch (Throwable ignored) {
}
IS_4_13_0_OR_NEWER = is4_13_0OrNewer;

boolean is4_15_0OrNewer = false;
try {
Component.translatable().arguments(Component.empty()); // TranslatableComponent#arguments method was added in 4.15.0
Expand Down

0 comments on commit 048903f

Please sign in to comment.