1
0
Fork 0
mirror of https://github.com/xHyroM/lighteco.git synced 2024-11-14 11:28:06 +01:00

feat: save every currency as BigDecimal

This commit is contained in:
Jozef Steinhübl 2023-08-26 10:03:18 +02:00
parent d7b93e4cd7
commit 2ab1bdeb85
15 changed files with 80 additions and 90 deletions

View file

@ -7,5 +7,5 @@ import org.checkerframework.checker.nullness.qual.NonNull;
public interface CurrencyRegisterEvent extends LightEcoEvent { public interface CurrencyRegisterEvent extends LightEcoEvent {
@Position(0) @Position(0)
@NonNull Currency<?> getCurrency(); @NonNull Currency getCurrency();
} }

View file

@ -8,11 +8,11 @@ import java.util.Collection;
import java.util.List; import java.util.List;
public interface CurrencyManager { public interface CurrencyManager {
@NonNull Collection<Currency<?>> getRegisteredCurrencies(); @NonNull Collection<Currency> getRegisteredCurrencies();
<T> Currency<T> getCurrency(@NonNull String identifier); Currency getCurrency(@NonNull String identifier);
void registerCurrency(@NonNull Currency<?> currency); void registerCurrency(@NonNull Currency currency);
List<User> getTopUsers(@NonNull Currency<?> currency, int length); List<User> getTopUsers(@NonNull Currency currency, int length);
} }

View file

@ -1,14 +1,8 @@
package dev.xhyrom.lighteco.api.model.currency; package dev.xhyrom.lighteco.api.model.currency;
import lombok.Getter; import java.math.BigDecimal;
public abstract class Currency<T> {
@Getter
private final Class<T> valueType;
public Currency(Class<T> valueType) {
this.valueType = valueType;
}
public abstract class Currency {
public abstract String getIdentifier(); public abstract String getIdentifier();
/** /**
* Get the type of the currency, either {@link Type#LOCAL} or {@link Type#GLOBAL} * Get the type of the currency, either {@link Type#LOCAL} or {@link Type#GLOBAL}
@ -20,12 +14,22 @@ public abstract class Currency<T> {
public abstract boolean isPayable(); public abstract boolean isPayable();
/**
* Get the number of decimal places this currency has
* If zero, the currency is considered to be a whole number (integer)
*
* @return The number of decimal places
*/
public int getDecimalPlaces() {
return 0;
};
/** /**
* Get the users that have a balance in this currency * Get the users that have a balance in this currency
* *
* @return The users * @return The users
*/ */
public abstract T getDefaultBalance(); public abstract BigDecimal getDefaultBalance();
/** /**
* Calculate the tax for the given amount * Calculate the tax for the given amount
@ -34,7 +38,7 @@ public abstract class Currency<T> {
* @param amount The amount to calculate the tax for * @param amount The amount to calculate the tax for
* @return The tax * @return The tax
*/ */
public T calculateTax(T amount) { public BigDecimal calculateTax(BigDecimal amount) {
return getDefaultBalance(); return getDefaultBalance();
}; };

View file

@ -3,6 +3,7 @@ package dev.xhyrom.lighteco.api.model.user;
import dev.xhyrom.lighteco.api.model.currency.Currency; import dev.xhyrom.lighteco.api.model.currency.Currency;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import java.math.BigDecimal;
import java.util.UUID; import java.util.UUID;
public interface User { public interface User {
@ -26,7 +27,7 @@ public interface User {
* @param currency the currency * @param currency the currency
* @return the balance * @return the balance
*/ */
<T> T getBalance(@NonNull Currency<?> currency); BigDecimal getBalance(@NonNull Currency currency);
/** /**
* Set the balance of this user for the specified currency. * Set the balance of this user for the specified currency.
@ -34,5 +35,5 @@ public interface User {
* @param currency the currency * @param currency the currency
* @param balance the balance * @param balance the balance
*/ */
<T> void setBalance(@NonNull Currency<?> currency, @NonNull T balance); void setBalance(@NonNull Currency currency, @NonNull BigDecimal balance);
} }

View file

@ -12,8 +12,8 @@ import org.bukkit.plugin.java.JavaPlugin;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@Getter
public class BukkitLightEcoBootstrap implements LightEcoBootstrap, LoaderBootstrap { public class BukkitLightEcoBootstrap implements LightEcoBootstrap, LoaderBootstrap {
@Getter
private final JavaPlugin loader; private final JavaPlugin loader;
private final BukkitLightEcoPlugin plugin = new BukkitLightEcoPlugin(this); private final BukkitLightEcoPlugin plugin = new BukkitLightEcoPlugin(this);

View file

@ -13,8 +13,8 @@ import org.bukkit.entity.Player;
import org.bukkit.plugin.ServicePriority; import org.bukkit.plugin.ServicePriority;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
@Getter
public class BukkitLightEcoPlugin extends AbstractLightEcoPlugin { public class BukkitLightEcoPlugin extends AbstractLightEcoPlugin {
@Getter
private final BukkitLightEcoBootstrap bootstrap; private final BukkitLightEcoBootstrap bootstrap;
@Getter @Getter

View file

@ -2,11 +2,9 @@ package dev.xhyrom.lighteco.bukkittest;
import dev.xhyrom.lighteco.api.model.currency.Currency; import dev.xhyrom.lighteco.api.model.currency.Currency;
public class TestCurrency extends Currency<Integer> { import java.math.BigDecimal;
public TestCurrency() {
super(Integer.class);
}
public class TestCurrency extends Currency {
@Override @Override
public String getIdentifier() { public String getIdentifier() {
return "test"; return "test";
@ -23,12 +21,12 @@ public class TestCurrency extends Currency<Integer> {
} }
@Override @Override
public Integer getDefaultBalance() { public BigDecimal getDefaultBalance() {
return 0; return BigDecimal.ZERO;
} }
@Override @Override
public Integer calculateTax(Integer amount) { public BigDecimal calculateTax(BigDecimal amount) {
return 0; return BigDecimal.ZERO;
} }
} }

View file

@ -2,11 +2,9 @@ package dev.xhyrom.lighteco.bukkittest;
import dev.xhyrom.lighteco.api.model.currency.Currency; import dev.xhyrom.lighteco.api.model.currency.Currency;
public class TestCurrency2 extends Currency<Double> { import java.math.BigDecimal;
public TestCurrency2() {
super(Double.class);
}
public class TestCurrency2 extends Currency {
@Override @Override
public String getIdentifier() { public String getIdentifier() {
return "test2"; return "test2";
@ -23,12 +21,17 @@ public class TestCurrency2 extends Currency<Double> {
} }
@Override @Override
public Double getDefaultBalance() { public int getDecimalPlaces() {
return 0.0; return 2;
} }
@Override @Override
public Double calculateTax(Double amount) { public BigDecimal getDefaultBalance() {
return 0.0; return BigDecimal.ZERO;
}
@Override
public BigDecimal calculateTax(BigDecimal amount) {
return BigDecimal.ZERO;
} }
} }

View file

@ -11,6 +11,8 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.math.BigDecimal;
public class TestPlugin extends JavaPlugin implements Listener { public class TestPlugin extends JavaPlugin implements Listener {
@Override @Override
public void onEnable() { public void onEnable() {
@ -27,7 +29,7 @@ public class TestPlugin extends JavaPlugin implements Listener {
getLogger().info("TestCurrency registered!"); getLogger().info("TestCurrency registered!");
currencyManager.getRegisteredCurrencies().forEach(currency -> { currencyManager.getRegisteredCurrencies().forEach(currency -> {
getLogger().info("Currency: " + currency.getIdentifier() + " (" + currency.getType() + ", " + currency.getValueType() + ", " + currency.isPayable() + ")"); getLogger().info("Currency: " + currency.getIdentifier() + " (" + currency.getType() + ", " + currency.getDecimalPlaces() + ", " + currency.isPayable() + ")");
}); });
} }
@ -44,7 +46,7 @@ public class TestPlugin extends JavaPlugin implements Listener {
String command = message.split(" ")[0]; String command = message.split(" ")[0];
String[] args = message.substring(command.length()).trim().split(" "); String[] args = message.substring(command.length()).trim().split(" ");
Currency<?> currency = currencyManager.getCurrency(args[0]); Currency currency = currencyManager.getCurrency(args[0]);
switch (command) { switch (command) {
case "balance": { case "balance": {
@ -52,10 +54,10 @@ public class TestPlugin extends JavaPlugin implements Listener {
break; break;
} }
case "add": { case "add": {
if (currency.getValueType().equals(Integer.class)) { if (currency.getDecimalPlaces() > 0) {
user.setBalance(currency, Integer.parseInt(args[1])); user.setBalance(currency, BigDecimal.valueOf(Integer.parseInt(args[1])));
} else if (currency.getValueType().equals(Double.class)) { } else {
user.setBalance(currency, Double.parseDouble(args[1])); user.setBalance(currency, BigDecimal.valueOf(Double.parseDouble(args[1])));
} }
provider.getUserManager().saveUser(user).thenAccept(aVoid -> player.sendMessage("Saved!")); provider.getUserManager().saveUser(user).thenAccept(aVoid -> player.sendMessage("Saved!"));

View file

@ -15,31 +15,31 @@ public class ApiCurrencyManager extends ApiAbstractManager<dev.xhyrom.lighteco.c
super(plugin, handler); super(plugin, handler);
} }
private Currency<?> wrap(dev.xhyrom.lighteco.common.model.currency.Currency<?> handler) { private Currency wrap(dev.xhyrom.lighteco.common.model.currency.Currency handler) {
return handler.getProxy(); return handler.getProxy();
} }
@Override @Override
public @NonNull Collection<Currency<?>> getRegisteredCurrencies() { public @NonNull Collection<Currency> getRegisteredCurrencies() {
return this.handler.values() return this.handler.values()
.stream().map(this::wrap) .stream().map(this::wrap)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@Override @Override
public <T> Currency<T> getCurrency(@NonNull String identifier) { public Currency getCurrency(@NonNull String identifier) {
return (Currency<T>) wrap(this.handler.getIfLoaded(identifier)); return wrap(this.handler.getIfLoaded(identifier));
} }
@Override @Override
public void registerCurrency(@NonNull Currency<?> currency) { public void registerCurrency(@NonNull Currency currency) {
dev.xhyrom.lighteco.common.model.currency.Currency<?> internal = new dev.xhyrom.lighteco.common.model.currency.Currency<>(plugin, currency); dev.xhyrom.lighteco.common.model.currency.Currency internal = new dev.xhyrom.lighteco.common.model.currency.Currency(plugin, currency);
this.handler.registerCurrency(internal); this.handler.registerCurrency(internal);
} }
@Override @Override
public List<User> getTopUsers(@NonNull Currency<?> currency, int length) { public List<User> getTopUsers(@NonNull Currency currency, int length) {
dev.xhyrom.lighteco.common.model.currency.Currency<?> internal = this.handler.getIfLoaded(currency.getIdentifier()); dev.xhyrom.lighteco.common.model.currency.Currency internal = this.handler.getIfLoaded(currency.getIdentifier());
return this.handler.getTopUsers(internal, length) return this.handler.getTopUsers(internal, length)
.stream().map(dev.xhyrom.lighteco.common.model.user.User::getProxy) .stream().map(dev.xhyrom.lighteco.common.model.user.User::getProxy)
.collect(Collectors.toList()); .collect(Collectors.toList());

View file

@ -4,6 +4,7 @@ import dev.xhyrom.lighteco.api.model.currency.Currency;
import dev.xhyrom.lighteco.api.model.user.User; import dev.xhyrom.lighteco.api.model.user.User;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import java.math.BigDecimal;
import java.util.UUID; import java.util.UUID;
public class ApiUser implements User { public class ApiUser implements User {
@ -24,8 +25,8 @@ public class ApiUser implements User {
} }
@Override @Override
public <T> T getBalance(@NonNull Currency<?> currency) { public BigDecimal getBalance(@NonNull Currency currency) {
dev.xhyrom.lighteco.common.model.currency.Currency<?> internal = this.handler.getPlugin() dev.xhyrom.lighteco.common.model.currency.Currency internal = this.handler.getPlugin()
.getCurrencyManager() .getCurrencyManager()
.getIfLoaded(currency.getIdentifier()); .getIfLoaded(currency.getIdentifier());
@ -33,8 +34,8 @@ public class ApiUser implements User {
} }
@Override @Override
public <T> void setBalance(@NonNull Currency<?> currency, @NonNull T balance) { public void setBalance(@NonNull Currency currency, @NonNull BigDecimal balance) {
dev.xhyrom.lighteco.common.model.currency.Currency<?> internal = this.handler.getPlugin() dev.xhyrom.lighteco.common.model.currency.Currency internal = this.handler.getPlugin()
.getCurrencyManager() .getCurrencyManager()
.getIfLoaded(currency.getIdentifier()); .getIfLoaded(currency.getIdentifier());

View file

@ -8,10 +8,10 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
public interface CurrencyManager extends Manager<String, Currency<?>> { public interface CurrencyManager extends Manager<String, Currency> {
@NonNull Collection<Currency<?>> getRegisteredCurrencies(); @NonNull Collection<Currency> getRegisteredCurrencies();
void registerCurrency(@NonNull Currency<?> currency); void registerCurrency(@NonNull Currency currency);
List<User> getTopUsers(@NonNull Currency<?> currency, int length); List<User> getTopUsers(@NonNull Currency currency, int length);
} }

View file

@ -13,7 +13,7 @@ import java.util.List;
import static dev.xhyrom.lighteco.api.model.currency.Currency.Type; import static dev.xhyrom.lighteco.api.model.currency.Currency.Type;
public class StandardCurrencyManager extends AbstractManager<String, Currency<?>> implements CurrencyManager { public class StandardCurrencyManager extends AbstractManager<String, Currency> implements CurrencyManager {
private final LightEcoPlugin plugin; private final LightEcoPlugin plugin;
public StandardCurrencyManager(LightEcoPlugin plugin) { public StandardCurrencyManager(LightEcoPlugin plugin) {
@ -21,47 +21,31 @@ public class StandardCurrencyManager extends AbstractManager<String, Currency<?>
} }
@Override @Override
public Currency<?> apply(String identifier) { public Currency apply(String identifier) {
return null; return null;
} }
@Override @Override
public Currency<?> getOrMake(String identifier) { public Currency getOrMake(String identifier) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override @Override
public @NonNull Collection<Currency<?>> getRegisteredCurrencies() { public @NonNull Collection<Currency> getRegisteredCurrencies() {
return this.values(); return this.values();
} }
@Override @Override
public void registerCurrency(@NonNull Currency<?> currency) { public void registerCurrency(@NonNull Currency currency) {
if (currency.getType() == Type.GLOBAL && this.plugin.getPlatformType().isLocal())
throw new IllegalArgumentException("Cannot register global currency on local platform");
if (currency.getType() == Type.LOCAL && this.plugin.getPlatformType().isProxy())
throw new IllegalArgumentException("Cannot register local currency on proxy platform");
if (!isValidValueType(currency.getValueType()))
throw new IllegalArgumentException("Invalid value type " + currency.getValueType().getName() + " for currency " + currency.getIdentifier());
if (this.isLoaded(currency.getIdentifier())) if (this.isLoaded(currency.getIdentifier()))
throw new IllegalArgumentException("Currency with identifier " + currency.getIdentifier() + " already registered"); throw new IllegalArgumentException("Currency with identifier " + currency.getIdentifier() + " already registered");
this.map.put(currency.getIdentifier(), currency); this.map.put(currency.getIdentifier(), currency);
} }
private boolean isValidValueType(Class<?> clazz) {
return clazz == Integer.class || clazz == Long.class ||
clazz == Double.class || clazz == Float.class ||
clazz == Short.class || clazz == BigInteger.class ||
clazz == BigDecimal.class;
}
// TODO: finish // TODO: finish
@Override @Override
public List<User> getTopUsers(@NonNull Currency<?> currency, int length) { public List<User> getTopUsers(@NonNull Currency currency, int length) {
return null; return null;
} }
} }

View file

@ -4,17 +4,18 @@ import dev.xhyrom.lighteco.common.model.user.User;
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
import lombok.Getter; import lombok.Getter;
import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
public class Currency<T> { public class Currency {
private final LightEcoPlugin plugin; private final LightEcoPlugin plugin;
@Getter @Getter
private final dev.xhyrom.lighteco.api.model.currency.Currency<T> proxy; private final dev.xhyrom.lighteco.api.model.currency.Currency proxy;
public Currency(LightEcoPlugin plugin, dev.xhyrom.lighteco.api.model.currency.Currency<T> proxy) { public Currency(LightEcoPlugin plugin, dev.xhyrom.lighteco.api.model.currency.Currency proxy) {
this.plugin = plugin; this.plugin = plugin;
this.proxy = proxy; this.proxy = proxy;
} }
@ -27,11 +28,7 @@ public class Currency<T> {
return proxy.getType(); return proxy.getType();
} }
public Class<T> getValueType() { public BigDecimal getDefaultBalance() {
return proxy.getValueType();
}
public T getDefaultBalance() {
return proxy.getDefaultBalance(); return proxy.getDefaultBalance();
} }

View file

@ -17,18 +17,18 @@ public class User {
@Getter @Getter
private final UUID uniqueId; private final UUID uniqueId;
private final TypedMap<Currency<?>> balances = new TypedMap<>(); private final TypedMap<Currency> balances = new TypedMap<>();
public User(LightEcoPlugin plugin, UUID uniqueId) { public User(LightEcoPlugin plugin, UUID uniqueId) {
this.plugin = plugin; this.plugin = plugin;
this.uniqueId = uniqueId; this.uniqueId = uniqueId;
} }
public <T> T getBalance(@NonNull Currency<?> currency) { public <T> T getBalance(@NonNull Currency currency) {
return balances.<T>getOrDefault(currency, (T) currency.getDefaultBalance()); return balances.<T>getOrDefault(currency, (T) currency.getDefaultBalance());
} }
public <T> void setBalance(@NonNull Currency<?> currency, @NonNull T balance) { public <T> void setBalance(@NonNull Currency currency, @NonNull T balance) {
balances.put(currency, balance); balances.put(currency, balance);
} }