Added support for localization and localization for Portuguese.
Added support for item name localization.
This commit is contained in:
parent
398bfb6deb
commit
dfbf218aba
10 changed files with 176 additions and 25 deletions
|
|
@ -1,10 +1,10 @@
|
||||||
package io.github.adrianvic.itemeconomy;
|
package io.github.adrianvic.itemeconomy;
|
||||||
|
|
||||||
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.*;
|
||||||
import java.util.Locale;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class Config {
|
public class Config {
|
||||||
private static Map<String, String> entries = new HashMap<>();
|
private static Map<String, String> entries = new HashMap<>();
|
||||||
|
|
@ -18,6 +18,10 @@ public class Config {
|
||||||
entries.put("singular", "diamond");
|
entries.put("singular", "diamond");
|
||||||
entries.put("ender_chest", "balance");
|
entries.put("ender_chest", "balance");
|
||||||
entries.put("commands", "true");
|
entries.put("commands", "true");
|
||||||
|
entries.put("player", "&a{}");
|
||||||
|
entries.put("localization", "default");
|
||||||
|
getAvailableLocales().forEach(l -> entries.put("plural_%s".formatted(l.getLanguage()), entries.get("plural")));
|
||||||
|
getAvailableLocales().forEach(l -> entries.put("singular_%s".formatted(l.getLanguage()), entries.get("singular")));
|
||||||
|
|
||||||
Map<String, String> missingValues = new HashMap<>();
|
Map<String, String> missingValues = new HashMap<>();
|
||||||
|
|
||||||
|
|
@ -50,10 +54,38 @@ public class Config {
|
||||||
return is(entry.toLowerCase(Locale.ROOT), value.toLowerCase(Locale.ROOT));
|
return is(entry.toLowerCase(Locale.ROOT), value.toLowerCase(Locale.ROOT));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getCurrencyText(int amount) {
|
public static String getCurrencyText(int amount, String lang) {
|
||||||
return entries.get("format")
|
String plural = entries.get("plural_%s".formatted(lang));
|
||||||
|
String singular = entries.get("singular_%s".formatted(lang));
|
||||||
|
|
||||||
|
if (plural == null || singular == null) {
|
||||||
|
plural = entries.get("plural");
|
||||||
|
singular = entries.get("singular");
|
||||||
|
}
|
||||||
|
|
||||||
|
return ChatColor.translateAlternateColorCodes('&', entries.get("format")
|
||||||
.replace("{}", String.valueOf(amount))
|
.replace("{}", String.valueOf(amount))
|
||||||
.replace("$", (amount != 1) ? entries.get("plural") : entries.get("singular"));
|
.replace("$", (amount != 1) ? plural : singular)
|
||||||
|
+ ChatColor.RESET
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getCurrencyText(int amount, Locale locale) {
|
||||||
|
return getCurrencyText(amount, locale.getLanguage());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getCurrencyText(int amount) {
|
||||||
|
return getCurrencyText(amount, getServerLocale().getLanguage());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static Locale getServerLocale() {
|
||||||
|
Locale locale = Locale.forLanguageTag(entries.get("localization"));
|
||||||
|
if (locale.getCountry().isEmpty()) {
|
||||||
|
locale = Locale.getDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
return locale;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UnrealConfig getuConf() {
|
public static UnrealConfig getuConf() {
|
||||||
|
|
@ -70,4 +102,19 @@ public class Config {
|
||||||
|
|
||||||
return Material.DIAMOND;
|
return Material.DIAMOND;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String playerPrefix(String playerName) {
|
||||||
|
return ChatColor.translateAlternateColorCodes('&', entries.get("player").replace("{}", playerName)) + ChatColor.RESET;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String playerPrefix(Player player) {
|
||||||
|
return playerPrefix(player.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Set<Locale> getAvailableLocales() {
|
||||||
|
return Set.of(
|
||||||
|
Locale.forLanguageTag("en"),
|
||||||
|
Locale.forLanguageTag("pt")
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,11 @@ public class Main extends JavaPlugin {
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
instance = this;
|
instance = this;
|
||||||
Config.loadConfig(new UnrealConfig(this, this.getDataFolder(), "config.yml"));
|
Config.loadConfig(new UnrealConfig(this, this.getDataFolder(), "config.yml"));
|
||||||
|
getLogger().info(Messages.ENABLING.get(
|
||||||
|
"ItemEconomy",
|
||||||
|
getDescription().getVersion(),
|
||||||
|
Config.getServerLocale()
|
||||||
|
));
|
||||||
economy = new VaultLayer();
|
economy = new VaultLayer();
|
||||||
Bukkit.getServicesManager().register(Economy.class, economy, this, ServicePriority.High);
|
Bukkit.getServicesManager().register(Economy.class, economy, this, ServicePriority.High);
|
||||||
|
|
||||||
|
|
|
||||||
47
src/main/java/io/github/adrianvic/itemeconomy/Messages.java
Normal file
47
src/main/java/io/github/adrianvic/itemeconomy/Messages.java
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
package io.github.adrianvic.itemeconomy;
|
||||||
|
|
||||||
|
import io.github.adrianvic.itemeconomy.commands.Utils;
|
||||||
|
import org.bukkit.ChatColor;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
|
public enum Messages {
|
||||||
|
ENABLING("enabling"),
|
||||||
|
BALANCE_SUCCESSFUL("balance-successful"),
|
||||||
|
BALANCE_OTHER_SUCCESSFUL("balance-other-successful"),
|
||||||
|
MUST_BE_PLAYER_TO_ISSUE_COMMAND("must-be-player-to-issue-command"),
|
||||||
|
PAY_SUCCESSFUL("pay-successful"),
|
||||||
|
PAY_RECEIVED("pay-received"),
|
||||||
|
PAY_COULD_NOT_REALIZE_TRANSACTION("pay-could-not-realize-transaction"),
|
||||||
|
PAY_COULD_NOT_FIND_TARGET("pay-could-not-find-target"),
|
||||||
|
PAY_NOT_ENOUGH_MONEY("pay-not-enough-money"),
|
||||||
|
PAY_INVALID_AMOUNT("pay-invalid-amount");
|
||||||
|
|
||||||
|
private final String path;
|
||||||
|
Messages(String path) { this.path = path; }
|
||||||
|
|
||||||
|
private String raw(Locale locale) {
|
||||||
|
ResourceBundle bundle = ResourceBundle.getBundle("messages", locale);
|
||||||
|
return bundle.getString(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String get(Locale locale, Object... args) {
|
||||||
|
String pattern = raw(locale);
|
||||||
|
try {
|
||||||
|
return ChatColor.translateAlternateColorCodes('&', args.length == 0 ? pattern : String.format(pattern, args));
|
||||||
|
} catch (Exception e) {
|
||||||
|
return pattern;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String get(Player player, Object... args) {
|
||||||
|
Locale locale = Utils.localeOrDefault(player);
|
||||||
|
return get(locale, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String get(Object... args) {
|
||||||
|
return get(Config.getServerLocale(), args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,6 +2,7 @@ package io.github.adrianvic.itemeconomy.commands;
|
||||||
|
|
||||||
import io.github.adrianvic.itemeconomy.Config;
|
import io.github.adrianvic.itemeconomy.Config;
|
||||||
import io.github.adrianvic.itemeconomy.Main;
|
import io.github.adrianvic.itemeconomy.Main;
|
||||||
|
import io.github.adrianvic.itemeconomy.Messages;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandExecutor;
|
import org.bukkit.command.CommandExecutor;
|
||||||
|
|
@ -19,18 +20,17 @@ public class Balance implements CommandExecutor, TabCompleter {
|
||||||
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String @NotNull [] strings) {
|
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String @NotNull [] strings) {
|
||||||
if ((commandSender.isOp() || commandSender.hasPermission("iteco.balance.others")) && strings.length > 0) {
|
if ((commandSender.isOp() || commandSender.hasPermission("iteco.balance.others")) && strings.length > 0) {
|
||||||
double amount = Main.getEconomy().getBalance(strings[0]);
|
double amount = Main.getEconomy().getBalance(strings[0]);
|
||||||
commandSender.sendMessage("%s has %s.".formatted(
|
commandSender.sendMessage(Messages.BALANCE_OTHER_SUCCESSFUL.get(
|
||||||
strings[0],
|
Utils.localeOrDefault(commandSender),
|
||||||
Config.getCurrencyText((int) amount)
|
Config.playerPrefix(strings[0]),
|
||||||
));
|
Config.getCurrencyText((int) amount, Utils.localeOrDefault(commandSender))
|
||||||
|
));
|
||||||
} else {
|
} else {
|
||||||
if (commandSender instanceof Player player) {
|
if (commandSender instanceof Player player) {
|
||||||
double amount = Main.getEconomy().getBalance(player);
|
double amount = Main.getEconomy().getBalance(player);
|
||||||
commandSender.sendMessage("You have %s.".formatted(
|
commandSender.sendMessage(Messages.BALANCE_SUCCESSFUL.get(player, Config.getCurrencyText((int) amount, Utils.localeOrDefault(commandSender))));
|
||||||
Config.getCurrencyText((int) amount)
|
|
||||||
));
|
|
||||||
} else {
|
} else {
|
||||||
commandSender.sendMessage("One must be a player to have a balance.");
|
commandSender.sendMessage(Messages.MUST_BE_PLAYER_TO_ISSUE_COMMAND.get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package io.github.adrianvic.itemeconomy.commands;
|
||||||
|
|
||||||
import io.github.adrianvic.itemeconomy.Config;
|
import io.github.adrianvic.itemeconomy.Config;
|
||||||
import io.github.adrianvic.itemeconomy.Main;
|
import io.github.adrianvic.itemeconomy.Main;
|
||||||
|
import io.github.adrianvic.itemeconomy.Messages;
|
||||||
import net.milkbowl.vault.economy.EconomyResponse;
|
import net.milkbowl.vault.economy.EconomyResponse;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
|
|
@ -13,6 +14,7 @@ import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class Pay implements CommandExecutor, TabCompleter {
|
public class Pay implements CommandExecutor, TabCompleter {
|
||||||
|
|
@ -27,7 +29,7 @@ public class Pay implements CommandExecutor, TabCompleter {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
amount = Integer.parseInt(strings[1]);
|
amount = Integer.parseInt(strings[1]);
|
||||||
String amountString = Config.getCurrencyText(amount);
|
String amountString = Config.getCurrencyText(amount, Utils.localeOrDefault(commandSender));
|
||||||
|
|
||||||
if (commandSender instanceof Player player && Main.getEconomy().has(player, amount)) {
|
if (commandSender instanceof Player player && Main.getEconomy().has(player, amount)) {
|
||||||
if (Bukkit.getPlayer(strings[0]) instanceof Player target) {
|
if (Bukkit.getPlayer(strings[0]) instanceof Player target) {
|
||||||
|
|
@ -35,23 +37,26 @@ public class Pay implements CommandExecutor, TabCompleter {
|
||||||
if (withdrawResponse.transactionSuccess()) {
|
if (withdrawResponse.transactionSuccess()) {
|
||||||
EconomyResponse depositResponse = Main.getEconomy().depositPlayer(target.getName(), amount);
|
EconomyResponse depositResponse = Main.getEconomy().depositPlayer(target.getName(), amount);
|
||||||
if (depositResponse.transactionSuccess()) {
|
if (depositResponse.transactionSuccess()) {
|
||||||
commandSender.sendMessage("Transaction of %s to %s was successfully realized.".formatted(amountString, target.getName()));
|
commandSender.sendMessage(Messages.PAY_SUCCESSFUL.get(
|
||||||
target.sendMessage("You received %s from %s.".formatted(amountString, player.getName()));
|
Utils.localeOrDefault(commandSender),
|
||||||
|
amountString,
|
||||||
|
Config.playerPrefix(target)));
|
||||||
|
target.sendMessage(Messages.PAY_RECEIVED.get(target, amountString, Config.playerPrefix(player)));
|
||||||
} else {
|
} else {
|
||||||
commandSender.sendMessage("Could not realize transaction: %s".formatted(depositResponse.errorMessage));
|
commandSender.sendMessage(Messages.PAY_COULD_NOT_REALIZE_TRANSACTION.get(Utils.localeOrDefault(commandSender), depositResponse.errorMessage));
|
||||||
Main.getEconomy().depositPlayer(player.getName(), amount);
|
Main.getEconomy().depositPlayer(player.getName(), amount);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
commandSender.sendMessage("Could not realize transaction: %s".formatted(withdrawResponse.errorMessage));
|
commandSender.sendMessage(Messages.PAY_COULD_NOT_REALIZE_TRANSACTION.get(Utils.localeOrDefault(commandSender), withdrawResponse.errorMessage));;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
commandSender.sendMessage("Could not find target player.");
|
commandSender.sendMessage(Messages.PAY_COULD_NOT_FIND_TARGET.get(Utils.localeOrDefault(commandSender)));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
commandSender.sendMessage("You don't have enough money.");
|
commandSender.sendMessage(Messages.PAY_NOT_ENOUGH_MONEY.get(Utils.localeOrDefault(commandSender)));
|
||||||
}
|
}
|
||||||
} catch (NumberFormatException nfe) {
|
} catch (NumberFormatException nfe) {
|
||||||
commandSender.sendMessage("The amount you tried to pay is not valid.");
|
commandSender.sendMessage(Messages.PAY_INVALID_AMOUNT.get(Utils.localeOrDefault(commandSender)));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
package io.github.adrianvic.itemeconomy.commands;
|
||||||
|
|
||||||
|
import io.github.adrianvic.itemeconomy.Config;
|
||||||
|
import io.github.adrianvic.itemeconomy.Main;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
public class Utils {
|
||||||
|
public static Locale localeOrDefault(CommandSender sender) {
|
||||||
|
return (sender instanceof Player p) ? Locale.forLanguageTag(p.getLocale().replace('_', '-')) : Config.getServerLocale();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
item: "diamond"
|
item: "diamond" # MUST be a valid item.
|
||||||
singular: "diamond"
|
singular: "diamond"
|
||||||
plural: "diamonds"
|
plural: "diamonds"
|
||||||
format: "{} $"
|
format: "&b{} $" # {} unfolds to amount and $ to singular or plural.
|
||||||
commands: "true"
|
commands: "true" # Enables or disables plugin commands.
|
||||||
|
player: "&b{}" # {} unfolds to player name.
|
||||||
|
localization: "default" # can be "en" and "pt" too, if set to anything else uses system locale.
|
||||||
10
src/main/resources/messages.properties
Normal file
10
src/main/resources/messages.properties
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
enabling=enabling
|
||||||
|
balance-successful=balance-successful
|
||||||
|
balance-other-successful=balance-other-successful
|
||||||
|
must-be-player-to-issue-command=must-be-player-to-issue-command
|
||||||
|
pay-successful=pay-successful
|
||||||
|
pay-received=pay-received
|
||||||
|
pay-could-not-realize-transaction=pay-could-not-realize-transaction
|
||||||
|
pay-could-not-find-target=pay-could-not-find-target
|
||||||
|
pay-not-enough-money=pay-not-enough-money
|
||||||
|
pay-invalid-amount=pay-invalid-amount
|
||||||
10
src/main/resources/messages_en.properties
Normal file
10
src/main/resources/messages_en.properties
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
enabling=Starting %s version %s with locale '%s'.
|
||||||
|
balance-successful=You have %s.
|
||||||
|
balance-other-successful=%s has %s.
|
||||||
|
must-be-player-to-issue-command=You must be a player to issue this command.
|
||||||
|
pay-successful=Transaction of %s to %s was successfully realized.
|
||||||
|
pay-received=You received %s from %s.
|
||||||
|
pay-could-not-realize-transaction=Could not realize transaction: %s
|
||||||
|
pay-could-not-find-target=Could not find target player.
|
||||||
|
pay-not-enough-money=You don't have enough money.
|
||||||
|
pay-invalid-amount=The amount you tried to pay is not valid.
|
||||||
10
src/main/resources/messages_pt.properties
Normal file
10
src/main/resources/messages_pt.properties
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
enabling=Iniciando %s versão %s com localização para %s.
|
||||||
|
balance-successful=Você tem %s.
|
||||||
|
balance-other-successful=%s possui %s.
|
||||||
|
must-be-player-to-issue-command=Você precisa ser um jogador para executar esse comando.
|
||||||
|
pay-successful=Transação de %s para %s realizada com sucesso.
|
||||||
|
pay-received=Você recebeu %s de %s.
|
||||||
|
pay-could-not-realize-transaction=Não foi possível realizar a transação: %s
|
||||||
|
pay-could-not-find-target=Não foi possível encontrar o jogador-alvo.
|
||||||
|
pay-not-enough-money=Você não tem dinheiro suficiente.
|
||||||
|
pay-invalid-amount=A quantidade que você tentou enviar é inválida.
|
||||||
Loading…
Add table
Add a link
Reference in a new issue