diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/model/currency/Currency.java b/api/src/main/java/dev/xhyrom/lighteco/api/model/currency/Currency.java index 8f673cf..5011492 100644 --- a/api/src/main/java/dev/xhyrom/lighteco/api/model/currency/Currency.java +++ b/api/src/main/java/dev/xhyrom/lighteco/api/model/currency/Currency.java @@ -34,8 +34,8 @@ public abstract class Currency { * @param amount The amount to calculate the tax for * @return The tax */ - public double calculateTax(T amount) { - return 0; + public T calculateTax(T amount) { + return getDefaultBalance(); }; /** diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/model/user/User.java b/api/src/main/java/dev/xhyrom/lighteco/api/model/user/User.java index 9e52b9e..ac341cf 100644 --- a/api/src/main/java/dev/xhyrom/lighteco/api/model/user/User.java +++ b/api/src/main/java/dev/xhyrom/lighteco/api/model/user/User.java @@ -26,7 +26,7 @@ public interface User { * @param currency the currency * @return the balance */ - T getBalance(@NonNull Currency currency); + T getBalance(@NonNull Currency currency); /** * Set the balance of this user for the specified currency. @@ -34,5 +34,5 @@ public interface User { * @param currency the currency * @param balance the balance */ - void setBalance(@NonNull Currency currency, @NonNull T balance); + void setBalance(@NonNull Currency currency, @NonNull T balance); } diff --git a/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestCurrency.java b/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestCurrency.java index ad6bdbd..fe75ff7 100644 --- a/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestCurrency.java +++ b/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestCurrency.java @@ -26,4 +26,9 @@ public class TestCurrency extends Currency { public Integer getDefaultBalance() { return 0; } + + @Override + public Integer calculateTax(Integer amount) { + return 0; + } } diff --git a/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestCurrency2.java b/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestCurrency2.java new file mode 100644 index 0000000..0a341a6 --- /dev/null +++ b/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestCurrency2.java @@ -0,0 +1,34 @@ +package dev.xhyrom.lighteco.bukkittest; + +import dev.xhyrom.lighteco.api.model.currency.Currency; + +public class TestCurrency2 extends Currency { + public TestCurrency2() { + super(Double.class); + } + + @Override + public String getIdentifier() { + return "test2"; + } + + @Override + public Type getType() { + return Type.LOCAL; + } + + @Override + public boolean isPayable() { + return false; + } + + @Override + public Double getDefaultBalance() { + return 0.0; + } + + @Override + public Double calculateTax(Double amount) { + return 0.0; + } +} diff --git a/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestPlugin.java b/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestPlugin.java index 9086abb..22d72af 100644 --- a/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestPlugin.java +++ b/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestPlugin.java @@ -25,14 +25,12 @@ public class TestPlugin extends JavaPlugin implements Listener { CurrencyManager currencyManager = provider.getCurrencyManager(); currencyManager.registerCurrency(new TestCurrency()); + currencyManager.registerCurrency(new TestCurrency2()); getLogger().info("TestCurrency registered!"); currencyManager.getRegisteredCurrencies().forEach(currency -> { getLogger().info("Currency: " + currency.getIdentifier() + " (" + currency.getType() + ", " + currency.getValueType() + ", " + currency.isPayable() + ")"); - /*currencyManager.getTopUsers(currency, 5).forEach(user -> { - plugin.getLogger().info("User: " + user.getUniqueId() + " (" + user.getUsername() + ")"); - });*/ }); } @@ -42,17 +40,27 @@ public class TestPlugin extends JavaPlugin implements Listener { LightEco provider = LightEcoProvider.get(); CurrencyManager currencyManager = provider.getCurrencyManager(); - Currency currency = currencyManager.getCurrency("test"); User user = provider.getPlayerAdapter(Player.class).getUser(player); - switch (event.getMessage()) { + String message = event.getMessage(); + String command = message.split(" ")[0]; + String[] args = message.substring(command.length()).trim().split(" "); + + Currency currency = currencyManager.getCurrency(args[0]); + + switch (command) { case "balance": { player.sendMessage(user.getBalance(currency).toString()); break; } case "add": { - user.setBalance(currency, user.getBalance(currency) + 1); + if (currency.getValueType().equals(Integer.class)) { + user.setBalance(currency, Integer.parseInt(args[1])); + } else if (currency.getValueType().equals(Double.class)) { + user.setBalance(currency, Double.parseDouble(args[1])); + } + provider.getUserManager().saveUser(user).thenAccept(aVoid -> player.sendMessage("Saved!")); break; } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiUser.java b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiUser.java index f52a094..1a7d6fb 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiUser.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiUser.java @@ -24,8 +24,8 @@ public class ApiUser implements User { } @Override - public T getBalance(@NonNull Currency currency) { - dev.xhyrom.lighteco.common.model.currency.Currency internal = (dev.xhyrom.lighteco.common.model.currency.Currency) this.handler.getPlugin() + public T getBalance(@NonNull Currency currency) { + dev.xhyrom.lighteco.common.model.currency.Currency internal = this.handler.getPlugin() .getCurrencyManager() .getIfLoaded(currency.getIdentifier()); @@ -33,8 +33,8 @@ public class ApiUser implements User { } @Override - public void setBalance(@NonNull Currency currency, @NonNull T balance) { - dev.xhyrom.lighteco.common.model.currency.Currency internal = (dev.xhyrom.lighteco.common.model.currency.Currency) this.handler.getPlugin() + public void setBalance(@NonNull Currency currency, @NonNull T balance) { + dev.xhyrom.lighteco.common.model.currency.Currency internal = this.handler.getPlugin() .getCurrencyManager() .getIfLoaded(currency.getIdentifier()); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/cache/TypedMap.java b/common/src/main/java/dev/xhyrom/lighteco/common/cache/TypedMap.java new file mode 100644 index 0000000..d11d58f --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/cache/TypedMap.java @@ -0,0 +1,20 @@ +package dev.xhyrom.lighteco.common.cache; + +import java.util.HashMap; +import java.util.Map; + +public class TypedMap { + private final Map map = new HashMap<>(); + + public V get(T key) { + return (V) map.get(key); + } + + public V getOrDefault(T key, V defaultValue) { + return (V) map.getOrDefault(key, defaultValue); + } + + public void put(T key, V value) { + map.put(key, value); + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/model/user/User.java b/common/src/main/java/dev/xhyrom/lighteco/common/model/user/User.java index b5d5734..63495b6 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/model/user/User.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/model/user/User.java @@ -1,6 +1,7 @@ package dev.xhyrom.lighteco.common.model.user; import dev.xhyrom.lighteco.common.api.impl.ApiUser; +import dev.xhyrom.lighteco.common.cache.TypedMap; import dev.xhyrom.lighteco.common.model.currency.Currency; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; import lombok.Getter; @@ -18,19 +19,18 @@ public class User { @Getter private final UUID uniqueId; - private final Map, Number> balances = new HashMap<>(); + private final TypedMap> balances = new TypedMap<>(); public User(LightEcoPlugin plugin, UUID uniqueId) { this.plugin = plugin; this.uniqueId = uniqueId; } - public T getBalance(@NonNull Currency currency) { - T balance = (T) balances.get(currency); - return balance == null ? currency.getDefaultBalance() : balance; + public T getBalance(@NonNull Currency currency) { + return balances.getOrDefault(currency, (T) currency.getDefaultBalance()); } - public void setBalance(@NonNull Currency currency, @NonNull T balance) { - balances.put(currency, (Number) balance); + public void setBalance(@NonNull Currency currency, @NonNull T balance) { + balances.put(currency, balance); } }