diff --git a/.gitignore b/.gitignore index 4c59a71..9ede845 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,3 @@ build/ out/ .idea/ libs/ -run/ \ No newline at end of file diff --git a/README.md b/README.md index 0af72de..341faa1 100644 --- a/README.md +++ b/README.md @@ -1,37 +1,30 @@ # ItemEconomy II -

-image -

- -**ItemEconomy II** is a fork of [ItemEconomy](https://modrinth.com/plugin/itemeconomy), keeping it updated to later versions of Minecraft and adding new features. +ItemEconomy II is a fork of [ItemEconomy](https://modrinth.com/plugin/itemeconomy), keeping it updated to later versions of Minecraft and adding new features. This PaperMC plugin integrates with VaultUnlocked to provide a unique, item-based economy system for your Minecraft server. Instead of relying solely on virtual balances, players use in-game items as physical currency, adding a layer of immersion and realism to your economy. +Features: -ItemEconomy is as powerful as you need and as simple as you want, with every feature being optional. - -## Features -- **Item-Based Currency:** Set any Minecraft item as your server's currency (diamonds by default). +- **Item-Based Currency:** Set any Minecraft item as your server's currency (default: diamonds). - **VaultUnlocked Integration:** Fully compatible with VaultUnlocked, enabling seamless interaction with other economy-based plugins. - **Simple logic:** Just checks if the user has the item/how many when queried. - **Customizable Formatting:** Define how your currency is displayed, including singular and plural forms. - **Ender Chest support:** Items on Ender Chests are counted in the user balance. - **Built-int optional balance and pay commands** with support for permissions. -- **Translation support** with per-user language and per-language currency name. -## Configuration -An updated example configuration file is available [here](https://github.com/adrianvic/ItemEconomy/blob/main/src/main/resources/config.yml). +## Configuration Example: +```yaml +item: diamond # Define the item to be used as currency. +singular: diamond # Singular form of the currency. +plural: diamonds # Plural form of the currency. +format: "{} $" # {} will be replaced with the amount and $ either with singular or plural +ender_chest: balance # Either none or balance +commands: true # Disabling this will disable /balance and /pay +``` + +This configuration will use diamonds as the currency, displayed as {amount}$, e.g., "5 diamonds" or "1 diamond". + +## Usage: -## Usage - Players can earn, trade, and store the configured item as physical currency. -- Once they spend the currency, that amount of the item will be subtracted from their inventory. +- Integrates seamlessly with Vault-compatible plugins for shops, auctions, and more. - Administrators can customize the item and formatting to match their server's theme. - -## Commands -
-
/balance
-
Prints player balance, permission is `iteco.balance` but is allowed by default. Requires permission `iteco.balance.others` to specify other user.
-
/pay
-
Transfers money from your account to another player's, permission is `iteco.pay` but is allowed by default.
-
/itecoreload
-
Reloads the plugin configuration, requires permission `iteco.reload`.
-
diff --git a/build.gradle.kts b/build.gradle.kts index 35e0edf..bbc19a2 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -106,4 +106,4 @@ tasks.withType { tasks.runServer { minecraftVersion("1.21") -} \ No newline at end of file +} diff --git a/src/main/java/io/github/adrianvic/itemeconomy/Config.java b/src/main/java/io/github/adrianvic/itemeconomy/Config.java index b5dac25..4b57e6f 100644 --- a/src/main/java/io/github/adrianvic/itemeconomy/Config.java +++ b/src/main/java/io/github/adrianvic/itemeconomy/Config.java @@ -1,10 +1,10 @@ package io.github.adrianvic.itemeconomy; -import org.bukkit.ChatColor; import org.bukkit.Material; -import org.bukkit.entity.Player; -import java.util.*; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; public class Config { private static Map entries = new HashMap<>(); @@ -18,10 +18,6 @@ public class Config { entries.put("singular", "diamond"); entries.put("ender_chest", "balance"); 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 missingValues = new HashMap<>(); @@ -54,38 +50,10 @@ public class Config { return is(entry.toLowerCase(Locale.ROOT), value.toLowerCase(Locale.ROOT)); } - public static String getCurrencyText(int amount, String lang) { - 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("$", (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; + return entries.get("format") + .replace("{}", String.valueOf(amount)) + .replace("$", (amount != 1) ? entries.get("plural") : entries.get("singular")); } public static UnrealConfig getuConf() { @@ -102,19 +70,4 @@ public class Config { 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 getAvailableLocales() { - return Set.of( - Locale.forLanguageTag("en"), - Locale.forLanguageTag("pt") - ); - } } diff --git a/src/main/java/io/github/adrianvic/itemeconomy/Main.java b/src/main/java/io/github/adrianvic/itemeconomy/Main.java index 3b999ee..18dc263 100644 --- a/src/main/java/io/github/adrianvic/itemeconomy/Main.java +++ b/src/main/java/io/github/adrianvic/itemeconomy/Main.java @@ -21,11 +21,6 @@ public class Main extends JavaPlugin { public void onEnable() { instance = this; Config.loadConfig(new UnrealConfig(this, this.getDataFolder(), "config.yml")); - getLogger().info(Messages.ENABLING.get( - "ItemEconomy", - getDescription().getVersion(), - Config.getServerLocale() - )); economy = new VaultLayer(); Bukkit.getServicesManager().register(Economy.class, economy, this, ServicePriority.High); @@ -122,24 +117,12 @@ public class Main extends JavaPlugin { } public static void addItems(Player player, Material type, int amount) { - if (amount <= 0) return; - - HashMap invOverflow = - getInventory(player, InventoryID.INVENTORY) - .addItem(new ItemStack(type, amount)); - - int overflowAmount = invOverflow.values() + HashMap invOverflow = getInventory(player, InventoryID.INVENTORY).addItem(new ItemStack(type, amount)); + HashMap echestOverflow = getInventory(player, InventoryID.ENDER_CHEST).addItem(new ItemStack(type, invOverflow.values() .stream() .mapToInt(ItemStack::getAmount) - .sum(); + .sum())); - if (overflowAmount <= 0) { - return; - } - - HashMap echestOverflow = - getInventory(player, InventoryID.ENDER_CHEST) - .addItem(new ItemStack(type, overflowAmount)); for (ItemStack overflow : echestOverflow.values()) { player.getWorld().dropItemNaturally(player.getLocation(), overflow); diff --git a/src/main/java/io/github/adrianvic/itemeconomy/Messages.java b/src/main/java/io/github/adrianvic/itemeconomy/Messages.java deleted file mode 100644 index 33102ce..0000000 --- a/src/main/java/io/github/adrianvic/itemeconomy/Messages.java +++ /dev/null @@ -1,51 +0,0 @@ -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"), - RELOADING("reloading"), - RELOAD_ERROR("reload-error"), - RELOAD_FINISHED("reload-finished"); - - - 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); - } -} diff --git a/src/main/java/io/github/adrianvic/itemeconomy/commands/Balance.java b/src/main/java/io/github/adrianvic/itemeconomy/commands/Balance.java index e5e93f7..e42b456 100644 --- a/src/main/java/io/github/adrianvic/itemeconomy/commands/Balance.java +++ b/src/main/java/io/github/adrianvic/itemeconomy/commands/Balance.java @@ -2,7 +2,6 @@ package io.github.adrianvic.itemeconomy.commands; import io.github.adrianvic.itemeconomy.Config; import io.github.adrianvic.itemeconomy.Main; -import io.github.adrianvic.itemeconomy.Messages; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -20,17 +19,18 @@ public class Balance implements CommandExecutor, TabCompleter { 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) { double amount = Main.getEconomy().getBalance(strings[0]); - commandSender.sendMessage(Messages.BALANCE_OTHER_SUCCESSFUL.get( - Utils.localeOrDefault(commandSender), - Config.playerPrefix(strings[0]), - Config.getCurrencyText((int) amount, Utils.localeOrDefault(commandSender)) - )); + commandSender.sendMessage("%s has %s.".formatted( + strings[0], + Config.getCurrencyText((int) amount) + )); } else { if (commandSender instanceof Player player) { double amount = Main.getEconomy().getBalance(player); - commandSender.sendMessage(Messages.BALANCE_SUCCESSFUL.get(player, Config.getCurrencyText((int) amount, Utils.localeOrDefault(commandSender)))); + commandSender.sendMessage("You have %s.".formatted( + Config.getCurrencyText((int) amount) + )); } else { - commandSender.sendMessage(Messages.MUST_BE_PLAYER_TO_ISSUE_COMMAND.get()); + commandSender.sendMessage("One must be a player to have a balance."); } } return true; diff --git a/src/main/java/io/github/adrianvic/itemeconomy/commands/Pay.java b/src/main/java/io/github/adrianvic/itemeconomy/commands/Pay.java index f87cb38..f71a679 100644 --- a/src/main/java/io/github/adrianvic/itemeconomy/commands/Pay.java +++ b/src/main/java/io/github/adrianvic/itemeconomy/commands/Pay.java @@ -2,7 +2,6 @@ package io.github.adrianvic.itemeconomy.commands; import io.github.adrianvic.itemeconomy.Config; import io.github.adrianvic.itemeconomy.Main; -import io.github.adrianvic.itemeconomy.Messages; import net.milkbowl.vault.economy.EconomyResponse; import org.bukkit.Bukkit; import org.bukkit.command.Command; @@ -14,7 +13,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.List; -import java.util.Locale; import java.util.stream.Collectors; public class Pay implements CommandExecutor, TabCompleter { @@ -29,7 +27,7 @@ public class Pay implements CommandExecutor, TabCompleter { try { amount = Integer.parseInt(strings[1]); - String amountString = Config.getCurrencyText(amount, Utils.localeOrDefault(commandSender)); + String amountString = Config.getCurrencyText(amount); if (commandSender instanceof Player player && Main.getEconomy().has(player, amount)) { if (Bukkit.getPlayer(strings[0]) instanceof Player target) { @@ -37,26 +35,23 @@ public class Pay implements CommandExecutor, TabCompleter { if (withdrawResponse.transactionSuccess()) { EconomyResponse depositResponse = Main.getEconomy().depositPlayer(target.getName(), amount); if (depositResponse.transactionSuccess()) { - commandSender.sendMessage(Messages.PAY_SUCCESSFUL.get( - Utils.localeOrDefault(commandSender), - amountString, - Config.playerPrefix(target))); - target.sendMessage(Messages.PAY_RECEIVED.get(target, amountString, Config.playerPrefix(player))); + commandSender.sendMessage("Transaction of %s to %s was successfully realized.".formatted(amountString, target.getName())); + target.sendMessage("You received %s from %s.".formatted(amountString, player.getName())); } else { - commandSender.sendMessage(Messages.PAY_COULD_NOT_REALIZE_TRANSACTION.get(Utils.localeOrDefault(commandSender), depositResponse.errorMessage)); + commandSender.sendMessage("Could not realize transaction: %s".formatted(depositResponse.errorMessage)); Main.getEconomy().depositPlayer(player.getName(), amount); } } else { - commandSender.sendMessage(Messages.PAY_COULD_NOT_REALIZE_TRANSACTION.get(Utils.localeOrDefault(commandSender), withdrawResponse.errorMessage));; + commandSender.sendMessage("Could not realize transaction: %s".formatted(withdrawResponse.errorMessage)); } } else { - commandSender.sendMessage(Messages.PAY_COULD_NOT_FIND_TARGET.get(Utils.localeOrDefault(commandSender))); + commandSender.sendMessage("Could not find target player."); } } else { - commandSender.sendMessage(Messages.PAY_NOT_ENOUGH_MONEY.get(Utils.localeOrDefault(commandSender))); + commandSender.sendMessage("You don't have enough money."); } } catch (NumberFormatException nfe) { - commandSender.sendMessage(Messages.PAY_INVALID_AMOUNT.get(Utils.localeOrDefault(commandSender))); + commandSender.sendMessage("The amount you tried to pay is not valid."); return true; } diff --git a/src/main/java/io/github/adrianvic/itemeconomy/commands/Reload.java b/src/main/java/io/github/adrianvic/itemeconomy/commands/Reload.java index b0d8514..804e6ed 100644 --- a/src/main/java/io/github/adrianvic/itemeconomy/commands/Reload.java +++ b/src/main/java/io/github/adrianvic/itemeconomy/commands/Reload.java @@ -2,28 +2,20 @@ package io.github.adrianvic.itemeconomy.commands; import io.github.adrianvic.itemeconomy.Config; import io.github.adrianvic.itemeconomy.Main; -import io.github.adrianvic.itemeconomy.Messages; import io.github.adrianvic.itemeconomy.UnrealConfig; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; -import java.util.Locale; - public class Reload implements CommandExecutor { @Override public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String @NotNull [] strings) { - Locale locale = Utils.localeOrDefault(commandSender); try { - commandSender.sendMessage("[ECONOMY] %s".formatted(Messages.RELOADING.get(locale))); Config.loadConfig(new UnrealConfig(Main.getInstance(), Main.getInstance().getDataFolder(), "config.yml")); - commandSender.sendMessage("[ECONOMY] %s".formatted(Messages.RELOAD_FINISHED.get(locale))); return true; } catch (Exception e) { - commandSender.sendMessage("[ECONOMY] %s".formatted(Messages.RELOAD_ERROR.get(locale))); - e.printStackTrace(); return false; } } diff --git a/src/main/java/io/github/adrianvic/itemeconomy/commands/Utils.java b/src/main/java/io/github/adrianvic/itemeconomy/commands/Utils.java deleted file mode 100644 index aceb749..0000000 --- a/src/main/java/io/github/adrianvic/itemeconomy/commands/Utils.java +++ /dev/null @@ -1,15 +0,0 @@ -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(); - } - -} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 1b2d06d..49262a7 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,7 +1,5 @@ -item: "diamond" # MUST be a valid item. +item: "diamond" singular: "diamond" plural: "diamonds" -format: "&b{} $" # {} unfolds to amount and $ to singular or plural. -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. \ No newline at end of file +format: "{} $" +commands: "true" \ No newline at end of file diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties deleted file mode 100644 index 77e17f5..0000000 --- a/src/main/resources/messages.properties +++ /dev/null @@ -1,13 +0,0 @@ -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 -reloading=reloading -reload-error=reload-error -reload-finished=reload-finished \ No newline at end of file diff --git a/src/main/resources/messages_en.properties b/src/main/resources/messages_en.properties deleted file mode 100644 index 95f9873..0000000 --- a/src/main/resources/messages_en.properties +++ /dev/null @@ -1,13 +0,0 @@ -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. -reloading=Reloading... -reload-error=An error occurred while reloading the config, please check your logs. -reload-finished=Reload complete. \ No newline at end of file diff --git a/src/main/resources/messages_pt.properties b/src/main/resources/messages_pt.properties deleted file mode 100644 index e11dc4c..0000000 --- a/src/main/resources/messages_pt.properties +++ /dev/null @@ -1,13 +0,0 @@ -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. -reloading=Recarregando... -reload-error=Ocorreu um erro ao recarregar, por favor, verifique seu registro. -reload-finished=Carregamento completo. \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 75cefe8..e78d2d1 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: ItemEconomy main: io.github.adrianvic.itemeconomy.Main -version: 1.3.1 +version: 1.2 depend: [Vault] api-version: '1.21' commands: