Merge branch 'master' of github.com:AppleDash/SaneEconomy

This commit is contained in:
AppleDash 2016-12-30 21:11:04 -05:00
commit 0ecfe71938
16 changed files with 102 additions and 72 deletions

View File

@ -6,10 +6,10 @@
<parent>
<groupId>org.appledash</groupId>
<artifactId>SaneEconomy</artifactId>
<version>0.9.3-SNAPSHOT</version>
<version>0.10.0-SNAPSHOT</version>
</parent>
<artifactId>SaneEconomyCore</artifactId>
<version>0.9.3-SNAPSHOT</version>
<version>0.10.0-SNAPSHOT</version>
<dependencies>
<dependency>

View File

@ -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 {0}.", playerIdentifier);
MessageUtils.sendMessage(sender, "You don't have permission to check the balance of {1}.", playerIdentifier);
return;
}
}
@ -63,6 +63,6 @@ public class BalanceCommand extends SaneEconomyCommand {
return;
}
MessageUtils.sendMessage(sender, "Balance for %s is %s.", playerName, saneEconomy.getEconomyManager().getFormattedBalance(Economable.wrap(player)));
MessageUtils.sendMessage(sender, "Balance for {1} is {2}.", playerName, saneEconomy.getEconomyManager().getFormattedBalance(Economable.wrap(player)));
}
}

View File

@ -46,7 +46,7 @@ public class BalanceTopCommand extends SaneEconomyCommand {
try {
page = Math.abs(Integer.parseInt(args[0]));
} catch (NumberFormatException e) {
MessageUtils.sendMessage(sender, "%s is not a valid number.");
MessageUtils.sendMessage(sender, "{1} is not a valid number.");
return;
}
}
@ -62,7 +62,7 @@ public class BalanceTopCommand extends SaneEconomyCommand {
AtomicInteger index = new AtomicInteger(offset + 1); /* I know it's stupid, but you can't do some_int++ from within the lambda. */
MessageUtils.sendMessage(sender, "Top %d players on page %d:", topBalances.size(), page);
topBalances.forEach((player, balance) -> MessageUtils.sendMessage(sender, "[%02d] %s - %s", index.getAndIncrement(), player.getName(), SaneEconomy.getInstance().getEconomyManager().getCurrency().formatAmount(balance)));
MessageUtils.sendMessage(sender, "Top {1} players on page {2}:", topBalances.size(), page);
topBalances.forEach((player, balance) -> MessageUtils.sendMessage(sender, "[{1:02d}] {2} - {3}", index.getAndIncrement(), player.getName(), SaneEconomy.getInstance().getEconomyManager().getCurrency().formatAmount(balance)));
}
}

View File

@ -18,8 +18,6 @@ import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import static org.appledash.saneeconomy.utils.I18n._;
/**
* Created by AppleDash on 6/13/2016.
* Blackjack is still best pony.
@ -66,7 +64,7 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
OfflinePlayer targetPlayer = PlayerUtils.getOfflinePlayer(sTargetPlayer);
if (targetPlayer == null) {
MessageUtils.sendMessage(sender, _("That player does not exist."));
MessageUtils.sendMessage(sender, "That player does not exist.");
return;
}
@ -76,7 +74,7 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
double amount = NumberUtils.parseAndFilter(ecoMan.getCurrency(), sAmount);
if (amount <= 0) {
MessageUtils.sendMessage(sender, _("%s is not a positive number."), ((amount == -1) ? sAmount : String.valueOf(amount)));
MessageUtils.sendMessage(sender, "{1} is not a positive number.", ((amount == -1) ? sAmount : String.valueOf(amount)));
return;
}
@ -86,7 +84,7 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
double newAmount = result.getToBalance();
MessageUtils.sendMessage(sender, _("Added %s to %s. Their balance is now %s."),
MessageUtils.sendMessage(sender, "Added {1} to {2}. Their balance is now {3}.",
ecoMan.getCurrency().formatAmount(amount),
sTargetPlayer,
ecoMan.getCurrency().formatAmount(newAmount)
@ -100,7 +98,7 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
double newAmount = result.getFromBalance();
MessageUtils.sendMessage(sender, _("Took %s from %s. Their balance is now %s."),
MessageUtils.sendMessage(sender, "Took {1} from {2}. Their balance is now {3}.",
ecoMan.getCurrency().formatAmount(amount),
sTargetPlayer,
ecoMan.getCurrency().formatAmount(newAmount)
@ -111,7 +109,7 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
if (subCommand.equalsIgnoreCase("set")) {
double oldBal = ecoMan.getBalance(economable);
ecoMan.setBalance(economable, amount);
MessageUtils.sendMessage(sender, _("Balance for %s set to %s."), sTargetPlayer, ecoMan.getCurrency().formatAmount(amount));
MessageUtils.sendMessage(sender, "Balance for {1} set to {2}.", sTargetPlayer, ecoMan.getCurrency().formatAmount(amount));
if (saneEconomy.shouldLogTransactions()) {
// FIXME: This is a silly hack to get it to log.

View File

@ -69,7 +69,7 @@ public class PayCommand extends SaneEconomyCommand {
double amount = NumberUtils.parseAndFilter(ecoMan.getCurrency(), sAmount);
if (amount <= 0) {
MessageUtils.sendMessage(sender, "%s is not a positive number.", ((amount == -1) ? sAmount : String.valueOf(amount)));
MessageUtils.sendMessage(sender, "{1} is not a positive number.", ((amount == -1) ? sAmount : String.valueOf(amount)));
return;
}
@ -78,7 +78,7 @@ public class PayCommand extends SaneEconomyCommand {
TransactionResult result = ecoMan.transact(transaction);
if (result.getStatus() != TransactionResult.Status.SUCCESS) {
MessageUtils.sendMessage(sender, "You do not have enough money to transfer %s to %s.",
MessageUtils.sendMessage(sender, "You do not have enough money to transfer {1} to {2}.",
ecoMan.getCurrency().formatAmount(amount),
sToPlayer
);
@ -88,12 +88,12 @@ public class PayCommand extends SaneEconomyCommand {
/* Inform the relevant parties. */
MessageUtils.sendMessage(sender, "You have transferred %s to %s.",
MessageUtils.sendMessage(sender, "You have transferred {1} to {2}.",
ecoMan.getCurrency().formatAmount(amount),
sToPlayer
);
MessageUtils.sendMessage(toPlayer, "You have received %s from %s.",
MessageUtils.sendMessage(toPlayer, "You have received {1} from {2}.",
ecoMan.getCurrency().formatAmount(amount),
fromPlayer.getDisplayName()
);

View File

@ -36,13 +36,13 @@ public class JoinQuitListener implements Listener {
Economable.CONSOLE, economable, startBalance, TransactionReason.STARTING_BALANCE
));
if (plugin.getConfig().getBoolean("economy.notify-start-balance", true)) {
MessageUtils.sendMessage(player, "You've been issued a starting balance of %s!", plugin.getEconomyManager().getCurrency().formatAmount(startBalance));
MessageUtils.sendMessage(player, "You've been issued a starting balance of {1}!", plugin.getEconomyManager().getCurrency().formatAmount(startBalance));
}
}
/* Update notification */
if (player.hasPermission("saneeconomy.update-notify") && plugin.getVersionChecker().isUpdateAvailable()) {
MessageUtils.sendMessage(player, "An update is available! The currently-installed version is %s, but the newest available is %s. Please go to %s to update!", plugin.getDescription().getVersion(), plugin.getVersionChecker().getNewestVersion(), GithubVersionChecker.DOWNLOAD_URL);
MessageUtils.sendMessage(player, "An update is available! The currently-installed version is {1}, but the newest available is {2}. Please go to %s to update!", plugin.getDescription().getVersion(), plugin.getVersionChecker().getNewestVersion(), GithubVersionChecker.DOWNLOAD_URL);
}
}

View File

@ -49,27 +49,16 @@ public class I18n {
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));
}
String currentTranslation = String.valueOf(equivalentOnDisk.get("translation"));
convertKey(finalKeys, currentKey, currentTranslation);
}
}
for (Map diskObject : configDisk.getMapList("messages")) {
convertKey(finalKeys, String.valueOf(diskObject.get("message")), String.valueOf(diskObject.get("translation")));
}
configDisk.set("messages", finalKeys);
try {
@ -91,6 +80,30 @@ public class I18n {
});
}
private void convertKey(List<Map<?, ?>> finalKeys, String currentKey, String currentTranslation) {
String convertedKey = convertOldTranslations(currentKey);
if (!currentKey.equals(convertedKey)) { // Key needs conversion
String convertedValue = convertOldTranslations(String.valueOf(currentTranslation));
// 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(currentKey)) {
iter.remove();
}
}
// Add the converted one.
if (convertedValue.equals("null")) {
finalKeys.add(ImmutableMap.of("message", convertedKey));
} else {
finalKeys.add(ImmutableMap.of("message", convertedKey, "translation", convertedValue));
}
}
}
private String convertOldTranslations(String input) {
Matcher m = Pattern.compile("(%s)").matcher(input);
StringBuffer converted = new StringBuffer();

View File

@ -1,5 +1,6 @@
package org.appledash.saneeconomy.utils;
import com.google.common.base.Strings;
import org.appledash.saneeconomy.SaneEconomy;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
@ -38,7 +39,7 @@ public class MessageUtils {
}
public static String indexedFormat(String fmt, Object... arguments) {
Matcher m = Pattern.compile("\\{([0-9]+)\\}").matcher(fmt);
Matcher m = Pattern.compile("\\{([0-9]+)(:[^}]+)?\\}").matcher(fmt);
StringBuffer formatted = new StringBuffer();
while (m.find()) {
@ -48,7 +49,15 @@ public class MessageUtils {
throw new IllegalArgumentException("Index must be within the range of the given arguments.");
}
m.appendReplacement(formatted, String.valueOf(arguments[index]));
String stringRep;
if (!Strings.isNullOrEmpty(m.group(2))) {
stringRep = String.format(String.format("%%%s", m.group(2).substring(1)), arguments[index]);
} else {
stringRep = String.valueOf(arguments[index]);
}
m.appendReplacement(formatted, stringRep);
}
m.appendTail(formatted);

View File

@ -3,22 +3,26 @@
# - message: That player is not online.
# translation: Ce joueur n'est pas en ligne.
# Colors can also be used, by means of prefixing color codes with an '&' symbol.
# The order of placeholders can be changed. Anything after the :, like the '02d' in {1:02d} is a Java String.format specifier. If you don't know what that is, I recommend leaving it as-is.
messages:
- message: You don't have permission to check the balance of %s.
- message: You don't have permission to check the balance of {1}.
- message: That player is not online.
- message: You cannot pay yourself.
- message: "Usage: %s"
- message: "Usage: {1}"
- message: That player does not exist.
- message: Balance for %s is %s.
- message: "Top %d players:"
- message: Balance for {1} is {2}.
- message: "Top {1} players on page {2}:"
- message: That player does not exist.
- message: "%s is not a positive number."
- message: Added %s to %s. Their balance is now %s.
- message: Took %s from %s. Their balance is now %s.
- message: Balance for %s set to %s
- message: You do not have enough money to transfer %s to %s.
- message: You have transferred %s to %s.
- message: You have received %s from %s.
- message: "{1} is not a positive number."
- message: Added {1} to {2}. Their balance is now {3}.
- message: Took {1} from {2}. Their balance is now {3}.
- message: Balance for {1} set to {2}.
- message: You do not have enough money to transfer {1} to {2}.
- message: You have transferred {1} to {2}.
- message: You have received {1} from {2}.
- message: Reloading database...
- message: Database reloaded.
- message: You've been issued a starting balance of %s!
- message: You've been issued a starting balance of {1}!
- message: "{1} is not a valid number."
- message: "There aren't enough players to display that page."
- message: "[{1:02d}] {2} - {3}"

View File

@ -1,7 +1,7 @@
name: SaneEconomy
author: AppleDash
main: org.appledash.saneeconomy.SaneEconomy
version: 0.9.3
version: 0.10.0
loadbefore: [Vault]
commands:
balance:

View File

@ -17,6 +17,12 @@ public class MessageUtilsTest {
Assert.assertEquals("Hello, world!", MessageUtils.indexedFormat("Hello, world!", "this", "shouldn't", "change"));
}
@Test
public void testAdvancedIndexFormat() {
Assert.assertEquals("Temperature: 20.01 degrees", MessageUtils.indexedFormat("Temperature: {1:.2f} degrees", 20.01f));
Assert.assertEquals("Index: 01", MessageUtils.indexedFormat("Index: {1:02d}", 1));
}
@Test(expected = IllegalArgumentException.class)
public void testBadIndexedFormat() {
MessageUtils.indexedFormat("Hello, {3}!", "world", "something");

View File

@ -5,18 +5,18 @@
<parent>
<artifactId>SaneEconomy</artifactId>
<groupId>org.appledash</groupId>
<version>0.9.3-SNAPSHOT</version>
<version>0.10.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>SaneEconomySignShop</artifactId>
<version>0.1.1-SNAPSHOT</version>
<version>0.1.2-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.appledash</groupId>
<artifactId>SaneEconomyCore</artifactId>
<version>0.9.3-SNAPSHOT</version>
<version>0.10.0-SNAPSHOT</version>
</dependency>
</dependencies>

View File

@ -18,12 +18,14 @@ import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import java.util.Optional;
import java.util.logging.Logger;
/**
* Created by appledash on 10/3/16.
* Blackjack is best pony.
*/
public class InteractListener implements Listener {
private static final Logger LOGGER = Logger.getLogger("SignShop");
private final SaneEconomySignShop plugin;
public InteractListener(SaneEconomySignShop plugin) {
@ -85,14 +87,14 @@ public class InteractListener implements Listener {
double price = shop.getBuyPrice(quantity);
if (!ecoMan.hasBalance(Economable.wrap(player), price)) {
MessageUtils.sendMessage(player, String.format("You do not have enough money to buy %d %s.", quantity, shop.getItem()));
MessageUtils.sendMessage(player, "You do not have enough money to buy {1} {2}.", quantity, shop.getItem());
return;
}
TransactionResult result = ecoMan.transact(new Transaction(Economable.wrap(player), Economable.PLUGIN, price, TransactionReason.PLUGIN_TAKE));
if (result.getStatus() != TransactionResult.Status.SUCCESS) {
MessageUtils.sendMessage(player, String.format("An error occurred attempting to perform that transaction: %s", result.getStatus()));
MessageUtils.sendMessage(player, "An error occurred attempting to perform that transaction: {1}", result.getStatus());
return;
}
@ -100,7 +102,8 @@ public class InteractListener implements Listener {
stack.setAmount(quantity);
player.getInventory().addItem(stack);
MessageUtils.sendMessage(player, String.format("You have bought %d %s for %s.", quantity, shop.getItem(), ecoMan.getCurrency().formatAmount(price)));
MessageUtils.sendMessage(player, "You have bought {1} {2} for {3}.", quantity, shop.getItem(), ecoMan.getCurrency().formatAmount(price));
LOGGER.info(String.format("%s just bought %s for %s.", player.getName(), shop.getItem(), ecoMan.getCurrency().formatAmount(price)));
}
private void doSell(SignShop shop, Player player) { // TODO: Selling enchanted items
@ -109,7 +112,7 @@ public class InteractListener implements Listener {
double price = shop.getSellPrice(quantity);
if (!player.getInventory().containsAtLeast(new ItemStack(shop.getItem()), quantity)) {
MessageUtils.sendMessage(player, String.format("You do not have %d %s!", quantity, shop.getItem()));
MessageUtils.sendMessage(player, "You do not have {1} {2}!", quantity, shop.getItem());
return;
}
@ -118,6 +121,7 @@ public class InteractListener implements Listener {
player.getInventory().removeItem(stack); // FIXME: This does not remove items with damage values that were detected by contains()
ecoMan.transact(new Transaction(Economable.PLUGIN, Economable.wrap(player), price, TransactionReason.PLUGIN_GIVE));
MessageUtils.sendMessage(player, String.format("You have sold %d %s for %s.", quantity, shop.getItem(), ecoMan.getCurrency().formatAmount(price)));
MessageUtils.sendMessage(player, "You have sold {1} {2} for {3}.", quantity, shop.getItem(), ecoMan.getCurrency().formatAmount(price));
LOGGER.info(String.format("%s just sold %s for %s.", player.getName(), shop.getItem(), ecoMan.getCurrency().formatAmount(price)));
}
}

View File

@ -38,7 +38,7 @@ public class SignChangeListener implements Listener {
ParsedSignShop pss = parseSignShop(evt);
if (pss.error != null) {
MessageUtils.sendMessage(evt.getPlayer(), String.format("Cannot create shop: %s", pss.error));
MessageUtils.sendMessage(evt.getPlayer(), "Cannot create shop: {1}", pss.error);
return;
}
@ -50,18 +50,18 @@ public class SignChangeListener implements Listener {
plugin.getSignShopManager().addSignShop(signShop);
evt.setLine(0, ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("admin-shop-title")));
MessageUtils.sendMessage(evt.getPlayer(), "Sign shop created!");
MessageUtils.sendMessage(evt.getPlayer(), String.format("Item: %d x %s", signShop.getQuantity(), signShop.getItem()));
MessageUtils.sendMessage(evt.getPlayer(), "Item: {1} x {2}", signShop.getQuantity(), signShop.getItem());
if (signShop.canBuy()) { // The player be buying from the shop, not the other way around.
MessageUtils.sendMessage(evt.getPlayer(), String.format("Will sell too players for %s.",
MessageUtils.sendMessage(evt.getPlayer(), "Will sell to players for {!}.",
plugin.getSaneEconomy().getEconomyManager().getCurrency().formatAmount(signShop.getBuyPrice())
));
);
}
if (signShop.canSell()) { // The player be selling to the shop, not the other way around.
MessageUtils.sendMessage(evt.getPlayer(), String.format("Will buy from players for %s.",
MessageUtils.sendMessage(evt.getPlayer(), "Will buy from players for {1}.",
plugin.getSaneEconomy().getEconomyManager().getCurrency().formatAmount(signShop.getSellPrice())
));
);
}
}
@ -202,8 +202,4 @@ public class SignChangeListener implements Listener {
super(message);
}
}
}

View File

@ -1,5 +1,5 @@
name: SaneEconomySignShop
main: org.appledash.saneeconomysignshop.SaneEconomySignShop
author: AppleDash
version: 0.1.1
version: 0.1.2
depend: [SaneEconomy]

View File

@ -6,7 +6,7 @@
<groupId>org.appledash</groupId>
<artifactId>SaneEconomy</artifactId>
<version>0.9.3-SNAPSHOT</version>
<version>0.10.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>