1
0
Fork 0
mirror of https://github.com/xHyroM/lighteco.git synced 2024-11-10 01:18:07 +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 {
@Position(0)
@NonNull Currency<?> getCurrency();
@NonNull Currency getCurrency();
}

View file

@ -8,11 +8,11 @@ import java.util.Collection;
import java.util.List;
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;
import lombok.Getter;
public abstract class Currency<T> {
@Getter
private final Class<T> valueType;
public Currency(Class<T> valueType) {
this.valueType = valueType;
}
import java.math.BigDecimal;
public abstract class Currency {
public abstract String getIdentifier();
/**
* 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();
/**
* 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
*
* @return The users
*/
public abstract T getDefaultBalance();
public abstract BigDecimal getDefaultBalance();
/**
* 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
* @return The tax
*/
public T calculateTax(T amount) {
public BigDecimal calculateTax(BigDecimal amount) {
return getDefaultBalance();
};

View file

@ -3,6 +3,7 @@ package dev.xhyrom.lighteco.api.model.user;
import dev.xhyrom.lighteco.api.model.currency.Currency;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.math.BigDecimal;
import java.util.UUID;
public interface User {
@ -26,7 +27,7 @@ public interface User {
* @param currency the currency
* @return the balance
*/
<T> T getBalance(@NonNull Currency<?> currency);
BigDecimal getBalance(@NonNull Currency currency);
/**
* Set the balance of this user for the specified currency.
@ -34,5 +35,5 @@ public interface User {
* @param currency the currency
* @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.UUID;
@Getter
public class BukkitLightEcoBootstrap implements LightEcoBootstrap, LoaderBootstrap {
@Getter
private final JavaPlugin loader;
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.checkerframework.checker.nullness.qual.NonNull;
@Getter
public class BukkitLightEcoPlugin extends AbstractLightEcoPlugin {
@Getter
private final BukkitLightEcoBootstrap bootstrap;
@Getter

View file

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

View file

@ -2,11 +2,9 @@ package dev.xhyrom.lighteco.bukkittest;
import dev.xhyrom.lighteco.api.model.currency.Currency;
public class TestCurrency2 extends Currency<Double> {
public TestCurrency2() {
super(Double.class);
}
import java.math.BigDecimal;
public class TestCurrency2 extends Currency {
@Override
public String getIdentifier() {
return "test2";
@ -23,12 +21,17 @@ public class TestCurrency2 extends Currency<Double> {
}
@Override
public Double getDefaultBalance() {
return 0.0;
public int getDecimalPlaces() {
return 2;
}
@Override
public Double calculateTax(Double amount) {
return 0.0;
public BigDecimal getDefaultBalance() {
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.plugin.java.JavaPlugin;
import java.math.BigDecimal;
public class TestPlugin extends JavaPlugin implements Listener {
@Override
public void onEnable() {
@ -27,7 +29,7 @@ public class TestPlugin extends JavaPlugin implements Listener {
getLogger().info("TestCurrency registered!");
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[] args = message.substring(command.length()).trim().split(" ");
Currency<?> currency = currencyManager.getCurrency(args[0]);
Currency currency = currencyManager.getCurrency(args[0]);
switch (command) {
case "balance": {
@ -52,10 +54,10 @@ public class TestPlugin extends JavaPlugin implements Listener {
break;
}
case "add": {
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]));
if (currency.getDecimalPlaces() > 0) {
user.setBalance(currency, BigDecimal.valueOf(Integer.parseInt(args[1])));
} else {
user.setBalance(currency, BigDecimal.valueOf(Double.parseDouble(args[1])));
}
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);
}
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();
}
@Override
public @NonNull Collection<Currency<?>> getRegisteredCurrencies() {
public @NonNull Collection<Currency> getRegisteredCurrencies() {
return this.handler.values()
.stream().map(this::wrap)
.collect(Collectors.toList());
}
@Override
public <T> Currency<T> getCurrency(@NonNull String identifier) {
return (Currency<T>) wrap(this.handler.getIfLoaded(identifier));
public Currency getCurrency(@NonNull String identifier) {
return wrap(this.handler.getIfLoaded(identifier));
}
@Override
public void registerCurrency(@NonNull Currency<?> currency) {
dev.xhyrom.lighteco.common.model.currency.Currency<?> internal = new dev.xhyrom.lighteco.common.model.currency.Currency<>(plugin, 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);
this.handler.registerCurrency(internal);
}
@Override
public List<User> getTopUsers(@NonNull Currency<?> currency, int length) {
dev.xhyrom.lighteco.common.model.currency.Currency<?> internal = this.handler.getIfLoaded(currency.getIdentifier());
public List<User> getTopUsers(@NonNull Currency currency, int length) {
dev.xhyrom.lighteco.common.model.currency.Currency internal = this.handler.getIfLoaded(currency.getIdentifier());
return this.handler.getTopUsers(internal, length)
.stream().map(dev.xhyrom.lighteco.common.model.user.User::getProxy)
.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 org.checkerframework.checker.nullness.qual.NonNull;
import java.math.BigDecimal;
import java.util.UUID;
public class ApiUser implements User {
@ -24,8 +25,8 @@ public class ApiUser implements User {
}
@Override
public <T> T getBalance(@NonNull Currency<?> currency) {
dev.xhyrom.lighteco.common.model.currency.Currency<?> internal = this.handler.getPlugin()
public BigDecimal getBalance(@NonNull Currency currency) {
dev.xhyrom.lighteco.common.model.currency.Currency internal = this.handler.getPlugin()
.getCurrencyManager()
.getIfLoaded(currency.getIdentifier());
@ -33,8 +34,8 @@ public class ApiUser implements User {
}
@Override
public <T> void setBalance(@NonNull Currency<?> currency, @NonNull T balance) {
dev.xhyrom.lighteco.common.model.currency.Currency<?> internal = this.handler.getPlugin()
public void setBalance(@NonNull Currency currency, @NonNull BigDecimal balance) {
dev.xhyrom.lighteco.common.model.currency.Currency internal = this.handler.getPlugin()
.getCurrencyManager()
.getIfLoaded(currency.getIdentifier());

View file

@ -8,10 +8,10 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Collection;
import java.util.List;
public interface CurrencyManager extends Manager<String, Currency<?>> {
@NonNull Collection<Currency<?>> getRegisteredCurrencies();
public interface CurrencyManager extends Manager<String, Currency> {
@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;
public class StandardCurrencyManager extends AbstractManager<String, Currency<?>> implements CurrencyManager {
public class StandardCurrencyManager extends AbstractManager<String, Currency> implements CurrencyManager {
private final LightEcoPlugin plugin;
public StandardCurrencyManager(LightEcoPlugin plugin) {
@ -21,47 +21,31 @@ public class StandardCurrencyManager extends AbstractManager<String, Currency<?>
}
@Override
public Currency<?> apply(String identifier) {
public Currency apply(String identifier) {
return null;
}
@Override
public Currency<?> getOrMake(String identifier) {
public Currency getOrMake(String identifier) {
throw new UnsupportedOperationException();
}
@Override
public @NonNull Collection<Currency<?>> getRegisteredCurrencies() {
public @NonNull Collection<Currency> getRegisteredCurrencies() {
return this.values();
}
@Override
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());
public void registerCurrency(@NonNull Currency currency) {
if (this.isLoaded(currency.getIdentifier()))
throw new IllegalArgumentException("Currency with identifier " + currency.getIdentifier() + " already registered");
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
@Override
public List<User> getTopUsers(@NonNull Currency<?> currency, int length) {
public List<User> getTopUsers(@NonNull Currency currency, int length) {
return null;
}
}

View file

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

View file

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