Skip to content

Commit

Permalink
refactor: currency, support redis economy multi currency
Browse files Browse the repository at this point in the history
  • Loading branch information
ProdPreva1l committed Oct 24, 2024
1 parent 3bc093e commit 24900c8
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 42 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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<>();
Expand Down
22 changes: 22 additions & 0 deletions API/src/main/java/info/preva1l/fadah/currency/MultiCurrency.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package info.preva1l.fadah.currency;

import java.util.List;

public interface MultiCurrency {
String getId();

String getName();

String getRequiredPlugin();

List<Currency> getCurrencies();

/**
* Pre startup checks for the currency hook.
*
* @return true if the checks succeed false if they fail.
*/
default boolean preloadChecks() {
return true;
}
}
6 changes: 4 additions & 2 deletions Bukkit/src/main/java/info/preva1l/fadah/Fadah.java
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}

Expand Down
6 changes: 3 additions & 3 deletions Bukkit/src/main/java/info/preva1l/fadah/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<SubEconomy> currencies = List.of(
new SubEconomy("dollar", "Dollar"),
new SubEconomy("euro", "Euro"));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package info.preva1l.fadah.config;

public record SubEconomy(String economy, String displayName) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Currency> currencies = new ArrayList<>();

private RedisEconomyAPI api;

Expand All @@ -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();
Expand All @@ -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;
}
Expand Down
12 changes: 12 additions & 0 deletions Bukkit/src/main/java/info/preva1l/fadah/currency/SubCurrency.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ 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);
}

@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);
}
Expand All @@ -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);
}
Expand Down

0 comments on commit 24900c8

Please sign in to comment.