mirror of
https://github.com/xHyroM/lighteco.git
synced 2024-11-25 00:01:05 +01:00
fix: handle franctional digits
This commit is contained in:
parent
c902a6a989
commit
c82b04970b
10 changed files with 24 additions and 15 deletions
|
@ -17,12 +17,11 @@ public abstract class Currency {
|
|||
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)
|
||||
* Get the number of fractional digits this currency has
|
||||
*
|
||||
* @return The number of decimal places
|
||||
* @return The number of fractional digits
|
||||
*/
|
||||
public int getDecimalPlaces() {
|
||||
public int fractionalDigits() {
|
||||
return 0;
|
||||
};
|
||||
|
||||
|
|
|
@ -10,8 +10,6 @@ import lombok.RequiredArgsConstructor;
|
|||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class BalanceCommand implements Command {
|
||||
private final BukkitCommandManager manager;
|
||||
|
|
|
@ -26,7 +26,7 @@ public class GiveCommand implements Command {
|
|||
.withPermission(permissionBase + "give")
|
||||
.withArguments(
|
||||
new OfflinePlayerArgument("target"),
|
||||
currency.getProxy().getDecimalPlaces() > 0
|
||||
currency.getProxy().fractionalDigits() > 0
|
||||
? new DoubleArgument("amount", 1)
|
||||
: new IntegerArgument("amount", 1)
|
||||
)
|
||||
|
|
|
@ -26,7 +26,7 @@ public class PayCommand implements Command {
|
|||
.withPermission(permissionBase + "pay")
|
||||
.withArguments(
|
||||
new OfflinePlayerArgument("target"),
|
||||
currency.getProxy().getDecimalPlaces() > 0
|
||||
currency.getProxy().fractionalDigits() > 0
|
||||
? new DoubleArgument("amount", 1)
|
||||
: new IntegerArgument("amount", 1)
|
||||
)
|
||||
|
|
|
@ -26,7 +26,7 @@ public class SetCommand implements Command {
|
|||
.withPermission(permissionBase + "set")
|
||||
.withArguments(
|
||||
new OfflinePlayerArgument("target"),
|
||||
currency.getProxy().getDecimalPlaces() > 0
|
||||
currency.getProxy().fractionalDigits() > 0
|
||||
? new DoubleArgument("amount", 0)
|
||||
: new IntegerArgument("amount", 0)
|
||||
)
|
||||
|
|
|
@ -26,7 +26,7 @@ public class TakeCommand implements Command {
|
|||
.withPermission(permissionBase + "take")
|
||||
.withArguments(
|
||||
new OfflinePlayerArgument("target"),
|
||||
currency.getProxy().getDecimalPlaces() > 0
|
||||
currency.getProxy().fractionalDigits() > 0
|
||||
? new DoubleArgument("amount", 1)
|
||||
: new IntegerArgument("amount", 1)
|
||||
)
|
||||
|
|
|
@ -22,7 +22,7 @@ public class TestCurrency2 extends Currency {
|
|||
}
|
||||
|
||||
@Override
|
||||
public int getDecimalPlaces() {
|
||||
public int fractionalDigits() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ public class TestPlugin extends JavaPlugin {
|
|||
getLogger().info("TestCurrency registered!");
|
||||
|
||||
currencyManager.getRegisteredCurrencies().forEach(currency -> {
|
||||
getLogger().info("Currency: " + currency.getIdentifier() + " (" + currency.getType() + ", " + currency.getDecimalPlaces() + ", " + currency.isPayable() + ")");
|
||||
getLogger().info("Currency: " + currency.getIdentifier() + " (" + currency.getType() + ", " + currency.fractionalDigits() + ", " + currency.isPayable() + ")");
|
||||
});
|
||||
|
||||
provider.getCommandManager().registerCurrencyCommand(currencyManager.getCurrency("test"));
|
||||
|
|
|
@ -7,6 +7,7 @@ import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
|||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
|
@ -73,6 +74,7 @@ public abstract class AbstractCommandManager implements CommandManager {
|
|||
@Override
|
||||
public void onSet(CommandSender sender, Currency currency, User target, BigDecimal amount) {
|
||||
addToMustWait(sender.getUniqueId(), target.getUniqueId());
|
||||
amount = amount.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN);
|
||||
|
||||
target.setBalance(currency, amount);
|
||||
|
||||
|
@ -87,6 +89,7 @@ public abstract class AbstractCommandManager implements CommandManager {
|
|||
@Override
|
||||
public void onGive(CommandSender sender, Currency currency, User target, BigDecimal amount) {
|
||||
addToMustWait(sender.getUniqueId(), target.getUniqueId());
|
||||
amount = amount.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN);
|
||||
|
||||
target.setBalance(currency, target.getBalance(currency).add(amount));
|
||||
|
||||
|
@ -101,6 +104,7 @@ public abstract class AbstractCommandManager implements CommandManager {
|
|||
@Override
|
||||
public void onTake(CommandSender sender, Currency currency, User target, BigDecimal amount) {
|
||||
addToMustWait(sender.getUniqueId(), target.getUniqueId());
|
||||
amount = amount.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN);
|
||||
|
||||
target.setBalance(currency, target.getBalance(currency).subtract(amount));
|
||||
|
||||
|
@ -122,11 +126,19 @@ public abstract class AbstractCommandManager implements CommandManager {
|
|||
return;
|
||||
}
|
||||
|
||||
addToMustWait(sender.getUniqueId(), target.getUniqueId());
|
||||
amount = amount.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN);
|
||||
|
||||
User user = this.plugin.getUserManager().getIfLoaded(sender.getUniqueId());
|
||||
|
||||
// TODO: ADD CHECKS FOR IF USER HAS ENOUGH MONEY
|
||||
if (user.getBalance(currency).compareTo(amount) < 0) {
|
||||
sender.sendMessage(
|
||||
miniMessage.deserialize("<red>You do not have enough money!")
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
addToMustWait(sender.getUniqueId(), target.getUniqueId());
|
||||
|
||||
// calculate tax using Currency#calculateTax
|
||||
BigDecimal tax = currency.getProxy().calculateTax(user.getProxy(), amount);
|
||||
|
|
|
@ -26,7 +26,7 @@ public class MoneyCurrency extends Currency {
|
|||
}
|
||||
|
||||
@Override
|
||||
public int getDecimalPlaces() {
|
||||
public int fractionalDigits() {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue