mirror of
https://github.com/AppleDash/SaneEconomy.git
synced 2025-02-16 19:51:23 +01:00
Add indexedFormat() to MessageUtils.
This commit is contained in:
parent
3670ebaadd
commit
a0d8cb2d5d
@ -4,6 +4,9 @@ import org.appledash.saneeconomy.SaneEconomy;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.appledash.saneeconomy.utils.I18n._;
|
||||
|
||||
/**
|
||||
@ -23,4 +26,23 @@ public class MessageUtils {
|
||||
String prefix = ChatColor.translateAlternateColorCodes('&', SaneEconomy.getInstance().getConfig().getString("chat.prefix", ""));
|
||||
target.sendMessage(prefix + String.format(fmt, (Object[])args));
|
||||
}
|
||||
|
||||
public static String indexedFormat(String fmt, String... arguments) {
|
||||
Matcher m = Pattern.compile("\\{([0-9]+)\\}").matcher(fmt);
|
||||
StringBuffer formatted = new StringBuffer();
|
||||
|
||||
while (m.find()) {
|
||||
int index = Integer.valueOf(m.group(1)) - 1;
|
||||
|
||||
if (index > arguments.length - 1 || index < 0) {
|
||||
throw new IllegalArgumentException("Index must be within the range of the given arguments.");
|
||||
}
|
||||
|
||||
m.appendReplacement(formatted, arguments[index]);
|
||||
}
|
||||
|
||||
m.appendTail(formatted);
|
||||
|
||||
return formatted.toString();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,18 @@
|
||||
package org.appledash.saneeconomy.test;
|
||||
|
||||
import org.appledash.saneeconomy.utils.MessageUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Created by appledash on 12/15/16.
|
||||
* Blackjack is best pony.
|
||||
*/
|
||||
public class MessageUtilsTest {
|
||||
@Test
|
||||
public void testIndexedFormat() {
|
||||
Assert.assertEquals("Hello, world!", MessageUtils.indexedFormat("{1}, {2}!", "Hello", "world"));
|
||||
Assert.assertEquals("Hello, world!", MessageUtils.indexedFormat("Hello, {1}!", "world", "discarded"));
|
||||
Assert.assertEquals("Hello, world!", MessageUtils.indexedFormat("Hello, {2}!", "discarded", "world"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user