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 );
{
with.get( i ).toPlainText( builder );
i++;
} else } else
{ {
builder.append( part ); Matcher matcher = format.matcher( trans );
int position = 0;
int i = 0;
while ( matcher.find( position ) )
{
int pos = matcher.start();
if ( pos != position )
{
builder.append( trans.substring( position, pos ) );
} }
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,15 +91,45 @@ 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 );
{
with.get( i ).toLegacyText( builder );
i++;
} else } else
{
Matcher matcher = format.matcher( trans );
int position = 0;
int i = 0;
while ( matcher.find( position ) )
{
int pos = matcher.start();
if ( pos != position )
{
addFormat( builder );
builder.append( trans.substring( position, pos ) );
}
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++ ).toLegacyText( builder );
break;
case '%':
addFormat( builder );
builder.append( '%' );
break;
}
}
}
super.toLegacyText( builder );
}
private void addFormat(StringBuilder builder)
{ {
builder.append( getColor() ); builder.append( getColor() );
if ( isBold() ) builder.append( ChatColor.BOLD ); if ( isBold() ) builder.append( ChatColor.BOLD );
@ -82,10 +137,6 @@ public class TranslatableComponent extends BaseComponent
if ( isUnderlined() ) builder.append( ChatColor.UNDERLINE ); if ( isUnderlined() ) builder.append( ChatColor.UNDERLINE );
if ( isStrikethrough() ) builder.append( ChatColor.STRIKETHROUGH ); if ( isStrikethrough() ) builder.append( ChatColor.STRIKETHROUGH );
if ( isObfuscated() ) builder.append( ChatColor.MAGIC ); if ( isObfuscated() ) builder.append( ChatColor.MAGIC );
builder.append( part );
}
}
super.toLegacyText( builder );
} }
@Override @Override