TranslatableComponent extends BaseComponent { @Override protected void toPlainText(StringBuilder builder) { String trans; try { trans = locales.getString( translate ); } catch ( MissingResourceException ex ) { trans = translate; } 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; } } if ( trans.length() != position ) { builder.append( trans.substring( position, trans.length() ) ); } super.toPlainText( builder ); } TranslatableComponent(TranslatableComponent original);  TranslatableComponent(String translate, Object... with); @Override BaseComponent duplicate(); void setWith(List<BaseComponent> components); void addWith(String text); void addWith(BaseComponent component);  }
@Test public void testMissingPlaceholdersAdded() { TranslatableComponent testComponent = new TranslatableComponent( "Test string with %s placeholders: %s", "2", "aoeu" ); assertEquals( "Test string with 2 placeholders: aoeu", testComponent.toPlainText() ); assertEquals( "§fTest string with §f2§f placeholders: §faoeu", testComponent.toLegacyText() ); }
@Test public void testEscapedPercentInPlainText() { TranslatableComponent testComponent = new TranslatableComponent( "Test string with %% sign" ); assertEquals( "Test string with % sign", testComponent.toPlainText() ); assertEquals( "§fTest string with §f%§f sign", testComponent.toLegacyText() ); }