diff --git a/API/src/main/java/info/preva1l/fadah/currency/CurrencyLoadException.java b/API/src/main/java/info/preva1l/fadah/currency/CurrencyLoadException.java deleted file mode 100644 index 4d316cd..0000000 --- a/API/src/main/java/info/preva1l/fadah/currency/CurrencyLoadException.java +++ /dev/null @@ -1,7 +0,0 @@ -package info.preva1l.fadah.currency; - -public class CurrencyLoadException extends RuntimeException { - public CurrencyLoadException(String currency, String reason) { - super(currency + " failed to load: " + reason); - } -} diff --git a/API/src/main/java/info/preva1l/fadah/currency/CurrencyRegistry.java b/API/src/main/java/info/preva1l/fadah/currency/CurrencyRegistry.java index 9a914c4..0e99e97 100644 --- a/API/src/main/java/info/preva1l/fadah/currency/CurrencyRegistry.java +++ b/API/src/main/java/info/preva1l/fadah/currency/CurrencyRegistry.java @@ -13,6 +13,29 @@ public final class CurrencyRegistry { public static final Currency VAULT = get("vault"); public static final Currency REDIS_ECONOMY = get("redis_economy"); + public static void registerMulti(MultiCurrency currency) { + if (!currency.getRequiredPlugin().isEmpty()) { + Plugin requiredPlugin = Bukkit.getPluginManager().getPlugin(currency.getRequiredPlugin()); + if (requiredPlugin == null || !requiredPlugin.isEnabled()) { + Logger.getLogger("Fadah") + .warning("Tried enabling currency %s but the required plugin %s is not found/enabled!" + .formatted(currency.getId().toLowerCase(), currency.getRequiredPlugin())); + return; + } + } + + if (!currency.preloadChecks()) { + Logger.getLogger("Fadah") + .severe("Tried enabling currency %s but the preload checks failed!" + .formatted(currency.getId().toLowerCase())); + return; + } + + for (Currency curr : currency.getCurrencies()) { + register(curr); + } + } + public static void register(Currency currency) { if (values == null) { values = new ConcurrentHashMap<>(); diff --git a/API/src/main/java/info/preva1l/fadah/currency/MultiCurrency.java b/API/src/main/java/info/preva1l/fadah/currency/MultiCurrency.java new file mode 100644 index 0000000..7680791 --- /dev/null +++ b/API/src/main/java/info/preva1l/fadah/currency/MultiCurrency.java @@ -0,0 +1,22 @@ +package info.preva1l.fadah.currency; + +import java.util.List; + +public interface MultiCurrency { + String getId(); + + String getName(); + + String getRequiredPlugin(); + + List getCurrencies(); + + /** + * Pre startup checks for the currency hook. + * + * @return true if the checks succeed false if they fail. + */ + default boolean preloadChecks() { + return true; + } +} diff --git a/Bukkit/src/main/java/info/preva1l/fadah/Fadah.java b/Bukkit/src/main/java/info/preva1l/fadah/Fadah.java index c7117ff..2d5766c 100644 --- a/Bukkit/src/main/java/info/preva1l/fadah/Fadah.java +++ b/Bukkit/src/main/java/info/preva1l/fadah/Fadah.java @@ -257,9 +257,11 @@ private void loadMigrators() { private void loadCurrencies() { getConsole().info("Loading currencies..."); Stream.of( - new VaultCurrency(), - new RedisEconomyCurrency() + new VaultCurrency() ).forEach(CurrencyRegistry::register); + Stream.of( + new RedisEconomyCurrency() + ).forEach(CurrencyRegistry::registerMulti); getConsole().info("Currencies Loaded!"); } diff --git a/Bukkit/src/main/java/info/preva1l/fadah/config/Config.java b/Bukkit/src/main/java/info/preva1l/fadah/config/Config.java index ea3544a..fb36549 100644 --- a/Bukkit/src/main/java/info/preva1l/fadah/config/Config.java +++ b/Bukkit/src/main/java/info/preva1l/fadah/config/Config.java @@ -137,7 +137,6 @@ public static class Currency { @NoArgsConstructor(access = AccessLevel.PRIVATE) public static class Vault { private String name = "Money"; - private String format = "$%amount%"; } private RedisEconomy redisEconomy = new RedisEconomy(); @@ -148,8 +147,9 @@ public static class Vault { public static class RedisEconomy { private String name = "Redis Economy"; @Comment("Which currency to use from redis economy, if your using the default currency use the vault currency instead.") - private String subCurrency = "dollar"; - private String format = "$%amount%"; + private List currencies = List.of( + new SubEconomy("dollar", "Dollar"), + new SubEconomy("euro", "Euro")); } } diff --git a/Bukkit/src/main/java/info/preva1l/fadah/config/SubEconomy.java b/Bukkit/src/main/java/info/preva1l/fadah/config/SubEconomy.java new file mode 100644 index 0000000..b062282 --- /dev/null +++ b/Bukkit/src/main/java/info/preva1l/fadah/config/SubEconomy.java @@ -0,0 +1,4 @@ +package info.preva1l.fadah.config; + +public record SubEconomy(String economy, String displayName) { +} diff --git a/Bukkit/src/main/java/info/preva1l/fadah/currency/RedisEconomyCurrency.java b/Bukkit/src/main/java/info/preva1l/fadah/currency/RedisEconomyCurrency.java index 24dbfbd..30b93e0 100644 --- a/Bukkit/src/main/java/info/preva1l/fadah/currency/RedisEconomyCurrency.java +++ b/Bukkit/src/main/java/info/preva1l/fadah/currency/RedisEconomyCurrency.java @@ -3,13 +3,18 @@ import dev.unnm3d.rediseconomy.api.RedisEconomyAPI; import info.preva1l.fadah.Fadah; import info.preva1l.fadah.config.Config; +import info.preva1l.fadah.config.SubEconomy; import lombok.Getter; import org.bukkit.OfflinePlayer; +import java.util.ArrayList; +import java.util.List; + @Getter -public class RedisEconomyCurrency implements Currency { +public class RedisEconomyCurrency implements MultiCurrency { private final String id = "redis_economy"; private final String requiredPlugin = "RedisEconomy"; + private final List currencies = new ArrayList<>(); private RedisEconomyAPI api; @@ -18,26 +23,6 @@ public String getName() { return Config.i().getCurrency().getRedisEconomy().getName(); } - private dev.unnm3d.rediseconomy.currency.Currency getCurrency() { - return api.getCurrencyByName(Config.i().getCurrency().getRedisEconomy().getSubCurrency()); - } - - @Override - public void withdraw(OfflinePlayer player, double amountToTake) { - getCurrency().withdrawPlayer(player, amountToTake); - } - - @Override - public void add(OfflinePlayer player, double amountToAdd) { - getCurrency().depositPlayer(player, amountToAdd); - } - - - @Override - public double getBalance(OfflinePlayer player) { - return getCurrency().getBalance(player); - } - @Override public boolean preloadChecks() { api = RedisEconomyAPI.getAPI(); @@ -48,12 +33,42 @@ public boolean preloadChecks() { Fadah.getConsole().severe("-------------------------------------"); return false; } - if (getCurrency() == null) { - Fadah.getConsole().severe("-------------------------------------"); - Fadah.getConsole().severe("Cannot enable redis economy currency!"); - Fadah.getConsole().severe("No currency with name : " + Config.i().getCurrency().getRedisEconomy().getSubCurrency()); - Fadah.getConsole().severe("-------------------------------------"); - return false; + for (SubEconomy eco : Config.i().getCurrency().getRedisEconomy().getCurrencies()) { + Currency subCur = new SubCurrency(eco.economy(), eco.displayName(), requiredPlugin) { + private final RedisEconomyAPI api = getApi(); + + @Override + public void withdraw(OfflinePlayer player, double amountToTake) { + getCurrency().withdrawPlayer(player, amountToTake); + } + + @Override + public void add(OfflinePlayer player, double amountToAdd) { + getCurrency().depositPlayer(player, amountToAdd); + } + + @Override + public double getBalance(OfflinePlayer player) { + return getCurrency().getBalance(player); + } + + private dev.unnm3d.rediseconomy.currency.Currency getCurrency() { + return api.getCurrencyByName(eco.economy()); + } + + @Override + public boolean preloadChecks() { + if (getCurrency() == null) { + Fadah.getConsole().severe("-------------------------------------"); + Fadah.getConsole().severe("Cannot enable redis economy currency!"); + Fadah.getConsole().severe("No currency with name: " + eco.economy()); + Fadah.getConsole().severe("-------------------------------------"); + return false; + } + return true; + } + }; + currencies.add(subCur); } return true; } diff --git a/Bukkit/src/main/java/info/preva1l/fadah/currency/SubCurrency.java b/Bukkit/src/main/java/info/preva1l/fadah/currency/SubCurrency.java new file mode 100644 index 0000000..fcd5ff0 --- /dev/null +++ b/Bukkit/src/main/java/info/preva1l/fadah/currency/SubCurrency.java @@ -0,0 +1,12 @@ +package info.preva1l.fadah.currency; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public abstract class SubCurrency implements Currency { + private final String id; + private final String name; + private final String requiredPlugin; +} diff --git a/Bukkit/src/main/java/info/preva1l/fadah/currency/VaultCurrency.java b/Bukkit/src/main/java/info/preva1l/fadah/currency/VaultCurrency.java index 54d9207..3188ab7 100644 --- a/Bukkit/src/main/java/info/preva1l/fadah/currency/VaultCurrency.java +++ b/Bukkit/src/main/java/info/preva1l/fadah/currency/VaultCurrency.java @@ -23,7 +23,7 @@ public String getName() { @Override public void withdraw(OfflinePlayer player, double amountToTake) { if (economy == null) { - throw new CurrencyLoadException(id, "Vault has no compatible economy plugin."); + throw new RuntimeException("Vault has no compatible economy plugin."); } economy.withdrawPlayer(player, amountToTake); } @@ -31,7 +31,7 @@ public void withdraw(OfflinePlayer player, double amountToTake) { @Override public void add(OfflinePlayer player, double amountToAdd) { if (economy == null) { - throw new CurrencyLoadException(id, "Vault has no compatible economy plugin."); + throw new RuntimeException("Vault has no compatible economy plugin."); } economy.depositPlayer(player, amountToAdd); } @@ -40,7 +40,7 @@ public void add(OfflinePlayer player, double amountToAdd) { @Override public double getBalance(OfflinePlayer player) { if (economy == null) { - throw new CurrencyLoadException(id, "Vault has no compatible economy plugin."); + throw new RuntimeException("Vault has no compatible economy plugin."); } return economy.getBalance(player); }