#3430: Add new fallback field to TranslatableComponent

This commit is contained in:
Outfluencer 2023-01-28 11:32:59 +11:00 committed by md_5
parent 71ac9b34fa
commit 19424aba9d
No known key found for this signature in database
GPG Key ID: E8E901AC7C617C11
2 changed files with 18 additions and 1 deletions

View File

@ -30,6 +30,10 @@ public final class TranslatableComponent extends BaseComponent
* The components to substitute into the translation
*/
private List<BaseComponent> with;
/**
* The fallback, if the translation is not found
*/
private String fallback;
/**
* Creates a translatable component from the original to clone it.
@ -153,6 +157,11 @@ public final class TranslatableComponent extends BaseComponent
{
String trans = TranslationRegistry.INSTANCE.translate( translate );
if ( trans.equals( translate ) && fallback != null )
{
trans = fallback;
}
Matcher matcher = format.matcher( trans );
int position = 0;
int i = 0;

View File

@ -28,7 +28,11 @@ public class TranslatableComponentSerializer extends BaseComponentSerializer imp
component.setTranslate( object.get( "translate" ).getAsString() );
if ( object.has( "with" ) )
{
component.setWith( Arrays.asList( context.<BaseComponent[]>deserialize( object.get( "with" ), BaseComponent[].class ) ) );
component.setWith( Arrays.asList( context.deserialize( object.get( "with" ), BaseComponent[].class ) ) );
}
if ( object.has( "fallback" ) )
{
component.setFallback( object.get( "fallback" ).getAsString() );
}
return component;
}
@ -43,6 +47,10 @@ public class TranslatableComponentSerializer extends BaseComponentSerializer imp
{
object.add( "with", context.serialize( src.getWith() ) );
}
if ( src.getFallback() != null )
{
object.addProperty( "fallback", src.getFallback() );
}
return object;
}
}