Fix TranslatableComponent's toPlainText and toLegacyText not handling %1$s and %d

This commit is contained in:
Thinkofdeath 2013-12-07 13:16:10 +00:00
parent c7e590e286
commit 2cb3b6f934

View File

@ -6,8 +6,11 @@ import lombok.Setter;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Getter @Getter
@Setter @Setter
@ -15,6 +18,7 @@ import java.util.ResourceBundle;
public class TranslatableComponent extends BaseComponent public class TranslatableComponent extends BaseComponent
{ {
public final ResourceBundle locales = ResourceBundle.getBundle( "mojang-translations/en_US" ); public final ResourceBundle locales = ResourceBundle.getBundle( "mojang-translations/en_US" );
private final Pattern format = Pattern.compile( "%(?:(\\d+)\\$)?([A-Za-z%]|$)" );
private String translate; private String translate;
private List<BaseComponent> with; private List<BaseComponent> with;
@ -22,17 +26,18 @@ public class TranslatableComponent extends BaseComponent
public TranslatableComponent(String translate, Object... with) public TranslatableComponent(String translate, Object... with)
{ {
setTranslate( translate ); setTranslate( translate );
this.with = new ArrayList<>(); List<BaseComponent> temp = new ArrayList<>();
for ( Object w : with ) for ( Object w : with )
{ {
if ( w instanceof String ) if ( w instanceof String )
{ {
this.with.add( new TextComponent( (String) w ) ); temp.add( new TextComponent( (String) w ) );
} else } else
{ {
this.with.add( (BaseComponent) w ); temp.add( (BaseComponent) w );
} }
} }
setWith( temp );
} }
public void setWith(List<BaseComponent> components) public void setWith(List<BaseComponent> components)
@ -47,18 +52,38 @@ public class TranslatableComponent extends BaseComponent
@Override @Override
protected void toPlainText(StringBuilder builder) protected void toPlainText(StringBuilder builder)
{ {
String[] parts = translate.split( "((?<=%s)|(?=%s))" ); String trans = locales.getString( translate );
int i = 0; if ( trans == null )
for ( String part : parts )
{ {
if ( part.equals( "%s" ) ) builder.append( translate );
} else
{
Matcher matcher = format.matcher( trans );
int position = 0;
int i = 0;
while ( matcher.find( position ) )
{ {
with.get( i ).toPlainText( builder ); int pos = matcher.start();
i++; if ( pos != position )
} else {
{ builder.append( trans.substring( position, pos ) );
builder.append( part ); }
position = matcher.end();
String formatCode = matcher.group( 2 );
switch ( formatCode.charAt( 0 ) )
{
case 's':
case 'd':
String withIndex = matcher.group( 1 );
with.get( withIndex != null ? Integer.parseInt( withIndex ) - 1 : i++ ).toPlainText( builder );
break;
case '%':
builder.append( '%' );
break;
}
} }
} }
super.toPlainText( builder ); super.toPlainText( builder );
} }
@ -66,28 +91,54 @@ public class TranslatableComponent extends BaseComponent
@Override @Override
protected void toLegacyText(StringBuilder builder) protected void toLegacyText(StringBuilder builder)
{ {
String[] parts = locales.getString( translate ).split( "((?<=%s)|(?=%s))" ); String trans = locales.getString( translate );
int i = 0; if ( trans == null )
for ( String part : parts )
{ {
if ( part.equals( "%s" ) ) builder.append( translate );
} else
{
Matcher matcher = format.matcher( trans );
int position = 0;
int i = 0;
while ( matcher.find( position ) )
{ {
with.get( i ).toLegacyText( builder ); int pos = matcher.start();
i++; if ( pos != position )
} else {
{ addFormat( builder );
builder.append( getColor() ); builder.append( trans.substring( position, pos ) );
if ( isBold() ) builder.append( ChatColor.BOLD ); }
if ( isItalic() ) builder.append( ChatColor.ITALIC ); position = matcher.end();
if ( isUnderlined() ) builder.append( ChatColor.UNDERLINE );
if ( isStrikethrough() ) builder.append( ChatColor.STRIKETHROUGH ); String formatCode = matcher.group( 2 );
if ( isObfuscated() ) builder.append( ChatColor.MAGIC ); switch ( formatCode.charAt( 0 ) )
builder.append( part ); {
case 's':
case 'd':
String withIndex = matcher.group( 1 );
with.get( withIndex != null ? Integer.parseInt( withIndex ) - 1 : i++ ).toLegacyText( builder );
break;
case '%':
addFormat( builder );
builder.append( '%' );
break;
}
} }
} }
super.toLegacyText( builder ); super.toLegacyText( builder );
} }
private void addFormat(StringBuilder builder)
{
builder.append( getColor() );
if ( isBold() ) builder.append( ChatColor.BOLD );
if ( isItalic() ) builder.append( ChatColor.ITALIC );
if ( isUnderlined() ) builder.append( ChatColor.UNDERLINE );
if ( isStrikethrough() ) builder.append( ChatColor.STRIKETHROUGH );
if ( isObfuscated() ) builder.append( ChatColor.MAGIC );
}
@Override @Override
public String toString() public String toString()
{ {