From f937943bdd081211826a2cdc449a57c2846472de Mon Sep 17 00:00:00 2001 From: xHyroM Date: Sun, 27 Aug 2023 23:10:15 +0200 Subject: [PATCH] feat(currency-money): vault support, feat(common): messages --- .../bukkit/manager/BukkitCommandManager.java | 3 +- .../xhyrom/lighteco/common/config/Config.java | 4 + .../config/message/CurrencyMessageConfig.java | 16 ++ .../common/config/message/MessageConfig.java | 10 + .../command/AbstractCommandManager.java | 30 ++- currency-money/build.gradle.kts | 3 +- .../currency/money/bukkit/BukkitMCLoader.java | 6 + .../currency/money/bukkit/hooks/Vault.java | 4 - .../money/bukkit/hooks/vault/Vault.java | 205 ++++++++++++++++++ .../bukkit/hooks/vault/VaultFactory.java | 24 ++ .../listener/BukkitServiceListener.java | 15 ++ 11 files changed, 311 insertions(+), 9 deletions(-) create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/config/message/CurrencyMessageConfig.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/config/message/MessageConfig.java delete mode 100644 currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/Vault.java create mode 100644 currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/Vault.java create mode 100644 currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/VaultFactory.java create mode 100644 currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/listener/BukkitServiceListener.java diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/manager/BukkitCommandManager.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/manager/BukkitCommandManager.java index 6bd9651..2e68e7e 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/manager/BukkitCommandManager.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/manager/BukkitCommandManager.java @@ -54,7 +54,8 @@ public class BukkitCommandManager extends AbstractCommandManager { new CommandAPICommand(currency.getIdentifier()) .withSubcommand(new SetCommand(this, currency, permissionBase).build()) .withSubcommand(new GiveCommand(this, currency, permissionBase).build()) - .withSubcommand(new TakeCommand(this, currency, permissionBase).build()); + .withSubcommand(new TakeCommand(this, currency, permissionBase).build()) + .register(); new PayCommand(this, currency, permissionBase).build().register(); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/config/Config.java b/common/src/main/java/dev/xhyrom/lighteco/common/config/Config.java index bac6b18..6492692 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/config/Config.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/config/Config.java @@ -1,5 +1,6 @@ package dev.xhyrom.lighteco.common.config; +import dev.xhyrom.lighteco.common.config.message.MessageConfig; import dev.xhyrom.lighteco.common.config.storage.StorageConfig; import eu.okaeri.configs.OkaeriConfig; import eu.okaeri.configs.annotation.Comment; @@ -15,4 +16,7 @@ public class Config extends OkaeriConfig { @Comment("Storage settings.") public StorageConfig storage = new StorageConfig(); + + @Comment("Messages") + public MessageConfig messages = new MessageConfig(); } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/config/message/CurrencyMessageConfig.java b/common/src/main/java/dev/xhyrom/lighteco/common/config/message/CurrencyMessageConfig.java new file mode 100644 index 0000000..eaa0158 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/config/message/CurrencyMessageConfig.java @@ -0,0 +1,16 @@ +package dev.xhyrom.lighteco.common.config.message; + +import eu.okaeri.configs.OkaeriConfig; +import eu.okaeri.configs.annotation.Variable; + +public class CurrencyMessageConfig extends OkaeriConfig { + public String balance = " | Your balance: "; + @Variable("balance-others") + public String balanceOthers = " | Balance of | "; + + public String set = " | Set balance of | "; + public String give = " | Gave | "; + public String take = " | Took | "; + + public String pay = " | Paid to ( after tax)"; +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/config/message/MessageConfig.java b/common/src/main/java/dev/xhyrom/lighteco/common/config/message/MessageConfig.java new file mode 100644 index 0000000..88be91a --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/config/message/MessageConfig.java @@ -0,0 +1,10 @@ +package dev.xhyrom.lighteco.common.config.message; + +import eu.okaeri.configs.OkaeriConfig; + +import java.util.Collections; +import java.util.Map; + +public class MessageConfig extends OkaeriConfig { + public Map currency = Collections.singletonMap("default", new CurrencyMessageConfig()); +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/manager/command/AbstractCommandManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/manager/command/AbstractCommandManager.java index 18141d3..574b33e 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/manager/command/AbstractCommandManager.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/manager/command/AbstractCommandManager.java @@ -1,25 +1,29 @@ package dev.xhyrom.lighteco.common.manager.command; +import dev.xhyrom.lighteco.common.config.message.CurrencyMessageConfig; import dev.xhyrom.lighteco.common.model.chat.CommandSender; import dev.xhyrom.lighteco.common.model.currency.Currency; import dev.xhyrom.lighteco.common.model.user.User; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayList; -import java.util.HashMap; +import java.util.Map; import java.util.UUID; import java.util.concurrent.CompletableFuture; public abstract class AbstractCommandManager implements CommandManager { public final LightEcoPlugin plugin; private final MiniMessage miniMessage = MiniMessage.miniMessage(); + private final Map config; private final ArrayList mustWait = new ArrayList<>(); public AbstractCommandManager(LightEcoPlugin plugin) { this.plugin = plugin; + this.config = this.plugin.getConfig().messages.currency; } @Override @@ -52,13 +56,27 @@ public abstract class AbstractCommandManager implements CommandManager { } } + private CurrencyMessageConfig getConfig(Currency currency) { + CurrencyMessageConfig config = this.config.get(currency.getIdentifier()); + + if (config == null) { + return this.config.get("default"); + } + + return config; + } + @Override public void onBalance(CommandSender sender, Currency currency) { User user = this.plugin.getUserManager().getIfLoaded(sender.getUniqueId()); BigDecimal balance = user.getBalance(currency); sender.sendMessage( - miniMessage.deserialize("Your balance: " + balance.toPlainString() + " " + currency.getIdentifier()) + miniMessage.deserialize( + getConfig(currency).balance, + Placeholder.parsed("currency", currency.getIdentifier()), + Placeholder.parsed("balance", balance.toPlainString()) + ) ); } @@ -67,7 +85,13 @@ public abstract class AbstractCommandManager implements CommandManager { BigDecimal balance = target.getBalance(currency); sender.sendMessage( - miniMessage.deserialize("" + target.getUsername() + "'s balance: " + balance.toPlainString() + " " + currency.getIdentifier()) + miniMessage.deserialize( + getConfig(currency).balanceOthers, + Placeholder.parsed("currency", currency.getIdentifier()), + Placeholder.parsed("target", target.getUsername()), + Placeholder.parsed("balance", balance.toPlainString()) + ) + ); } diff --git a/currency-money/build.gradle.kts b/currency-money/build.gradle.kts index e03d77b..0b06a06 100644 --- a/currency-money/build.gradle.kts +++ b/currency-money/build.gradle.kts @@ -4,12 +4,13 @@ plugins { repositories { maven("https://repo.papermc.io/repository/maven-public/") + maven("https://jitpack.io") } dependencies { compileOnly(project(":lighteco-api")) - compileOnly("io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT") + compileOnly("com.github.MilkBowl:VaultAPI:1.7") compileOnly("org.projectlombok:lombok:1.18.28") annotationProcessor("org.projectlombok:lombok:1.18.28") diff --git a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/BukkitMCLoader.java b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/BukkitMCLoader.java index f79f8c9..ff43119 100644 --- a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/BukkitMCLoader.java +++ b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/BukkitMCLoader.java @@ -5,6 +5,7 @@ import dev.xhyrom.lighteco.api.LightEcoProvider; import dev.xhyrom.lighteco.api.manager.CommandManager; import dev.xhyrom.lighteco.api.manager.CurrencyManager; import dev.xhyrom.lighteco.api.model.currency.Currency; +import dev.xhyrom.lighteco.currency.money.bukkit.hooks.vault.VaultFactory; import dev.xhyrom.lighteco.currency.money.common.MoneyCurrency; import org.bukkit.plugin.java.JavaPlugin; @@ -19,6 +20,11 @@ public class BukkitMCLoader extends JavaPlugin { currencyManager.registerCurrency(currency); commandManager.registerCurrencyCommand(currency, true); + + if (getServer().getPluginManager().getPlugin("Vault") != null) { + getSLF4JLogger().info("Vault found, hooking..."); + new VaultFactory(this).hook(); + } } } diff --git a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/Vault.java b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/Vault.java deleted file mode 100644 index 8dbf28c..0000000 --- a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/Vault.java +++ /dev/null @@ -1,4 +0,0 @@ -package dev.xhyrom.lighteco.currency.money.bukkit.hooks; - -public class Vault { -} diff --git a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/Vault.java b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/Vault.java new file mode 100644 index 0000000..4eaafb7 --- /dev/null +++ b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/Vault.java @@ -0,0 +1,205 @@ +package dev.xhyrom.lighteco.currency.money.bukkit.hooks.vault; + +import dev.xhyrom.lighteco.api.LightEco; +import dev.xhyrom.lighteco.api.LightEcoProvider; +import dev.xhyrom.lighteco.api.model.currency.Currency; +import dev.xhyrom.lighteco.api.model.user.User; +import net.milkbowl.vault.economy.AbstractEconomy; +import net.milkbowl.vault.economy.EconomyResponse; +import org.bukkit.Bukkit; + +import java.math.BigDecimal; +import java.util.List; +import java.util.UUID; + +public class Vault extends AbstractEconomy { + private final LightEco provider; + private final Currency currency; + + public Vault() { + this.provider = LightEcoProvider.get(); + this.currency = this.provider.getCurrencyManager().getCurrency("money"); + } + + @Override + public boolean isEnabled() { + return true; + } + + @Override + public String getName() { + return "LightEco"; + } + + @Override + public boolean hasBankSupport() { + return false; + } + + @Override + public int fractionalDigits() { + return currency.fractionalDigits(); + } + + @Override + public String format(double amount) { + return null; + } + + @Override + public String currencyNamePlural() { + return null; + } + + @Override + public String currencyNameSingular() { + return null; + } + + @Override + public boolean hasAccount(String playerName) { + return hasAccount(playerName, null); + } + + @Override + public boolean hasAccount(String playerName, String worldName) { + UUID uniqueId = Bukkit.getPlayerUniqueId(playerName); + assert uniqueId != null; + + return provider.getUserManager().isLoaded(uniqueId); + } + + @Override + public double getBalance(String playerName) { + return getBalance(playerName, null); + } + + @Override + public double getBalance(String playerName, String world) { + UUID uniqueId = Bukkit.getPlayerUniqueId(playerName); + User user = provider.getUserManager().loadUser(uniqueId).join(); + + return bigDecimalToDouble(user.getBalance(currency)); + } + + @Override + public boolean has(String playerName, double amount) { + return has(playerName, null, amount); + } + + @Override + public boolean has(String playerName, String worldName, double amount) { + UUID uniqueId = Bukkit.getPlayerUniqueId(playerName); + User user = provider.getUserManager().loadUser(uniqueId).join(); + + return user.getBalance(currency).compareTo(BigDecimal.valueOf(amount)) >= 0; + } + + @Override + public EconomyResponse withdrawPlayer(String playerName, double amount) { + return withdrawPlayer(playerName, null, amount); + } + + @Override + public EconomyResponse withdrawPlayer(String playerName, String worldName, double amount) { + UUID uniqueId = Bukkit.getPlayerUniqueId(playerName); + User user = provider.getUserManager().loadUser(uniqueId).join(); + + BigDecimal balance = user.getBalance(currency); + BigDecimal newBalance = balance.subtract(BigDecimal.valueOf(amount)); + + user.setBalance(currency, newBalance); + + provider.getUserManager().saveUser(user); + + return new EconomyResponse(amount, bigDecimalToDouble(newBalance), EconomyResponse.ResponseType.SUCCESS, ""); + } + + @Override + public EconomyResponse depositPlayer(String playerName, double amount) { + return depositPlayer(playerName, null, amount); + } + + @Override + public EconomyResponse depositPlayer(String playerName, String worldName, double amount) { + UUID uniqueId = Bukkit.getPlayerUniqueId(playerName); + User user = provider.getUserManager().loadUser(uniqueId).join(); + + BigDecimal balance = user.getBalance(currency); + BigDecimal newBalance = balance.add(BigDecimal.valueOf(amount)); + + user.setBalance(currency, newBalance); + + provider.getUserManager().saveUser(user); + + return new EconomyResponse(amount, bigDecimalToDouble(newBalance), EconomyResponse.ResponseType.SUCCESS, ""); + } + + @Override + public EconomyResponse createBank(String name, String player) { + return null; + } + + @Override + public EconomyResponse deleteBank(String name) { + return null; + } + + @Override + public EconomyResponse bankBalance(String name) { + return null; + } + + @Override + public EconomyResponse bankHas(String name, double amount) { + return null; + } + + @Override + public EconomyResponse bankWithdraw(String name, double amount) { + return null; + } + + @Override + public EconomyResponse bankDeposit(String name, double amount) { + return null; + } + + @Override + public EconomyResponse isBankOwner(String name, String playerName) { + return null; + } + + @Override + public EconomyResponse isBankMember(String name, String playerName) { + return null; + } + + @Override + public List getBanks() { + return null; + } + + @Override + public boolean createPlayerAccount(String playerName) { + return createPlayerAccount(playerName, null); + } + + @Override + public boolean createPlayerAccount(String playerName, String worldName) { + UUID uniqueId = Bukkit.getPlayerUniqueId(playerName); + provider.getUserManager().loadUser(uniqueId).join(); + + return true; + } + + private double bigDecimalToDouble(final BigDecimal value) { + double amount = value.doubleValue(); + + if (new BigDecimal(amount).compareTo(value) > 0) { + amount = Math.nextAfter(amount, Double.NEGATIVE_INFINITY); + } + + return amount; + } +} diff --git a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/VaultFactory.java b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/VaultFactory.java new file mode 100644 index 0000000..5270eea --- /dev/null +++ b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/VaultFactory.java @@ -0,0 +1,24 @@ +package dev.xhyrom.lighteco.currency.money.bukkit.hooks.vault; + +import dev.xhyrom.lighteco.currency.money.bukkit.BukkitMCLoader; +import net.milkbowl.vault.economy.Economy; +import org.bukkit.Bukkit; +import org.bukkit.plugin.ServicePriority; +import org.bukkit.plugin.ServicesManager; + +public class VaultFactory { + private final BukkitMCLoader plugin; + private Vault vault; + + public VaultFactory(BukkitMCLoader plugin) { + this.plugin = plugin; + } + + public void hook() { + if (this.vault == null) + vault = new Vault(); + + ServicesManager manager = Bukkit.getServicesManager(); + manager.register(Economy.class, vault, this.plugin, ServicePriority.Highest); + } +} diff --git a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/listener/BukkitServiceListener.java b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/listener/BukkitServiceListener.java new file mode 100644 index 0000000..1bd6dae --- /dev/null +++ b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/listener/BukkitServiceListener.java @@ -0,0 +1,15 @@ +package dev.xhyrom.lighteco.currency.money.bukkit.listener; + +import net.milkbowl.vault.economy.Economy; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.server.ServiceRegisterEvent; + +public class BukkitServiceListener implements Listener { + @EventHandler + public void onServiceRegister(ServiceRegisterEvent event) { + if (!(event.getProvider().getProvider() instanceof Economy)) return; + + + } +}