mirror of
https://github.com/AppleDash/SaneEconomy.git
synced 2024-09-27 06:02:45 +02:00
Merge
This commit is contained in:
commit
36ffc50fa1
@ -34,14 +34,14 @@ public abstract class SaneEconomyCommand implements CommandExecutor {
|
||||
/* Invalid usage in some way, print out exactly what went wrong along with the proper usage. */
|
||||
MessageUtils.sendMessage(sender, e.getMessage());
|
||||
|
||||
|
||||
for (String s : getUsage()) {
|
||||
MessageUtils.sendMessage(sender, String.format("Usage: %s", s.replace("<command>", label)));
|
||||
MessageUtils.sendMessage(sender, "Usage: {1}", s.replace("<command>", label));
|
||||
}
|
||||
} catch (CommandException e) {
|
||||
MessageUtils.sendMessage(sender, e.getMessage());
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -51,7 +51,7 @@ public class BalanceCommand extends SaneEconomyCommand {
|
||||
playerName = args[0];
|
||||
|
||||
if (!sender.hasPermission("saneeconomy.balance.other")) {
|
||||
MessageUtils.sendMessage(sender, "You don't have permission to check the balance of %s.", playerIdentifier);
|
||||
MessageUtils.sendMessage(sender, "You don't have permission to check the balance of {0}.", playerIdentifier);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package org.appledash.saneeconomy.economy;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
@ -32,6 +33,13 @@ public class Currency {
|
||||
} else {
|
||||
symbols.setGroupingSeparator(',');
|
||||
}
|
||||
|
||||
String groupingSeparator = config.getString("grouping-separator", null);
|
||||
|
||||
if (!Strings.isNullOrEmpty(groupingSeparator)) {
|
||||
symbols.setGroupingSeparator(groupingSeparator.charAt(0));
|
||||
}
|
||||
|
||||
format.setDecimalFormatSymbols(symbols);
|
||||
format.setGroupingUsed(true);
|
||||
format.setGroupingSize(3);
|
||||
|
@ -8,10 +8,9 @@ import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Created by AppleDash on 8/5/2016.
|
||||
@ -31,27 +30,50 @@ public class I18n {
|
||||
YamlConfiguration configJar = YamlConfiguration.loadConfiguration(new InputStreamReader(this.getClass().getResourceAsStream("/messages.yml")));
|
||||
|
||||
if (configFile.exists()) { // Attempt to merge any new keys from the JAR's messages.yml into the copy in the plugin's data folder
|
||||
YamlConfiguration configFileYaml = YamlConfiguration.loadConfiguration(configFile);
|
||||
for (Map jarMap : configJar.getMapList("messages")) {
|
||||
boolean has = false;
|
||||
String key = jarMap.get("message").toString();
|
||||
YamlConfiguration configDisk = YamlConfiguration.loadConfiguration(configFile);
|
||||
|
||||
for (Map fileMap : configFileYaml.getMapList("messages")) {
|
||||
if (fileMap.get("message").toString().equals(key)) {
|
||||
has = true;
|
||||
List<Map<?, ?>> finalKeys = configDisk.getMapList("messages");
|
||||
|
||||
for (Map jarObject : configJar.getMapList("messages")) { // For every translation in the template config in the JAR
|
||||
String jarMessage = String.valueOf(jarObject.get("message")); // Key for this translation
|
||||
Map equivalentOnDisk = null; // Equivalent of this translation in the config file on disk
|
||||
|
||||
for (Map diskMap : configDisk.getMapList("messages")) { // For every translation in the config on disk
|
||||
if (String.valueOf(diskMap.get("message")).equals(jarMessage)) { // If the translation key on this object on disk is the same as the current one in the JAR
|
||||
equivalentOnDisk = diskMap;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!has) { // Folder messages.yml does not have this key, add it.
|
||||
List<Map> map = new ArrayList<>(configFileYaml.getMapList("messages"));
|
||||
map.add(ImmutableMap.of("message", key));
|
||||
configFileYaml.set("messages", map);
|
||||
if (equivalentOnDisk == null) { // This one isn't on disk yet - add it.
|
||||
finalKeys.add(jarObject);
|
||||
} else {
|
||||
String currentKey = String.valueOf(equivalentOnDisk.get("message"));
|
||||
String convertedKey = convertOldTranslations(currentKey);
|
||||
|
||||
if (!currentKey.equals(convertedKey)) { // Key needs conversion
|
||||
String convertedValue = convertOldTranslations(String.valueOf(equivalentOnDisk.get("translation")));
|
||||
|
||||
// Remove current key from map of things to go to the disk
|
||||
Iterator<Map<?, ?>> iter = finalKeys.iterator();
|
||||
|
||||
while (iter.hasNext()) {
|
||||
if (String.valueOf(iter.next().get("message")).equals(equivalentOnDisk.get("message"))) {
|
||||
iter.remove();
|
||||
}
|
||||
}
|
||||
|
||||
// Add the converted one.
|
||||
finalKeys.add(ImmutableMap.of("message", convertedKey, "translation", convertedValue));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
configDisk.set("messages", finalKeys);
|
||||
|
||||
try {
|
||||
configFileYaml.save(configFile);
|
||||
configDisk.save(configFile);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to save translations file.", e);
|
||||
}
|
||||
@ -69,6 +91,21 @@ public class I18n {
|
||||
});
|
||||
}
|
||||
|
||||
private String convertOldTranslations(String input) {
|
||||
Matcher m = Pattern.compile("(%s)").matcher(input);
|
||||
StringBuffer converted = new StringBuffer();
|
||||
int index = 1;
|
||||
|
||||
while (m.find()) {
|
||||
m.appendReplacement(converted, String.format("{%d}", index));
|
||||
index++;
|
||||
}
|
||||
|
||||
m.appendTail(converted);
|
||||
|
||||
return converted.toString();
|
||||
}
|
||||
|
||||
private String translate(String input) {
|
||||
return translations.containsKey(input) ? ChatColor.translateAlternateColorCodes('&', translations.get(input)) : input;
|
||||
}
|
||||
|
@ -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._;
|
||||
|
||||
/**
|
||||
@ -20,7 +23,36 @@ public class MessageUtils {
|
||||
*/
|
||||
public static synchronized void sendMessage(CommandSender target, String fmt, Object... args) {
|
||||
fmt = _(fmt);
|
||||
|
||||
String prefix = ChatColor.translateAlternateColorCodes('&', SaneEconomy.getInstance().getConfig().getString("chat.prefix", ""));
|
||||
target.sendMessage(prefix + String.format(fmt, (Object[])args));
|
||||
|
||||
String formatted;
|
||||
|
||||
if (fmt.contains("%s")) { // Legacy support.
|
||||
formatted = String.format(fmt, (Object[]) args);
|
||||
} else {
|
||||
formatted = indexedFormat(fmt, (Object[]) args);
|
||||
}
|
||||
|
||||
target.sendMessage(prefix + formatted);
|
||||
}
|
||||
|
||||
public static String indexedFormat(String fmt, Object... 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, String.valueOf(arguments[index]));
|
||||
}
|
||||
|
||||
m.appendTail(formatted);
|
||||
|
||||
return formatted.toString();
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ currency:
|
||||
plural: dollars
|
||||
format: '0.00'
|
||||
grouping: 3
|
||||
grouping-separator: ','
|
||||
|
||||
chat:
|
||||
prefix: '&b[&9Economy&b]&r '
|
||||
|
@ -0,0 +1,24 @@
|
||||
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"));
|
||||
Assert.assertEquals("Hello, world!", MessageUtils.indexedFormat("Hello, world!", "this", "shouldn't", "change"));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testBadIndexedFormat() {
|
||||
MessageUtils.indexedFormat("Hello, {3}!", "world", "something");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user