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

fix: handle franctional digits

This commit is contained in:
Jozef Steinhübl 2023-08-27 18:01:16 +02:00
parent c902a6a989
commit c82b04970b
10 changed files with 24 additions and 15 deletions

View file

@ -17,12 +17,11 @@ public abstract class Currency {
public abstract boolean isPayable(); public abstract boolean isPayable();
/** /**
* Get the number of decimal places this currency has * Get the number of fractional digits this currency has
* If zero, the currency is considered to be a whole number (integer)
* *
* @return The number of decimal places * @return The number of fractional digits
*/ */
public int getDecimalPlaces() { public int fractionalDigits() {
return 0; return 0;
}; };

View file

@ -10,8 +10,6 @@ import lombok.RequiredArgsConstructor;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import java.util.List;
@RequiredArgsConstructor @RequiredArgsConstructor
public class BalanceCommand implements Command { public class BalanceCommand implements Command {
private final BukkitCommandManager manager; private final BukkitCommandManager manager;

View file

@ -26,7 +26,7 @@ public class GiveCommand implements Command {
.withPermission(permissionBase + "give") .withPermission(permissionBase + "give")
.withArguments( .withArguments(
new OfflinePlayerArgument("target"), new OfflinePlayerArgument("target"),
currency.getProxy().getDecimalPlaces() > 0 currency.getProxy().fractionalDigits() > 0
? new DoubleArgument("amount", 1) ? new DoubleArgument("amount", 1)
: new IntegerArgument("amount", 1) : new IntegerArgument("amount", 1)
) )

View file

@ -26,7 +26,7 @@ public class PayCommand implements Command {
.withPermission(permissionBase + "pay") .withPermission(permissionBase + "pay")
.withArguments( .withArguments(
new OfflinePlayerArgument("target"), new OfflinePlayerArgument("target"),
currency.getProxy().getDecimalPlaces() > 0 currency.getProxy().fractionalDigits() > 0
? new DoubleArgument("amount", 1) ? new DoubleArgument("amount", 1)
: new IntegerArgument("amount", 1) : new IntegerArgument("amount", 1)
) )

View file

@ -26,7 +26,7 @@ public class SetCommand implements Command {
.withPermission(permissionBase + "set") .withPermission(permissionBase + "set")
.withArguments( .withArguments(
new OfflinePlayerArgument("target"), new OfflinePlayerArgument("target"),
currency.getProxy().getDecimalPlaces() > 0 currency.getProxy().fractionalDigits() > 0
? new DoubleArgument("amount", 0) ? new DoubleArgument("amount", 0)
: new IntegerArgument("amount", 0) : new IntegerArgument("amount", 0)
) )

View file

@ -26,7 +26,7 @@ public class TakeCommand implements Command {
.withPermission(permissionBase + "take") .withPermission(permissionBase + "take")
.withArguments( .withArguments(
new OfflinePlayerArgument("target"), new OfflinePlayerArgument("target"),
currency.getProxy().getDecimalPlaces() > 0 currency.getProxy().fractionalDigits() > 0
? new DoubleArgument("amount", 1) ? new DoubleArgument("amount", 1)
: new IntegerArgument("amount", 1) : new IntegerArgument("amount", 1)
) )

View file

@ -22,7 +22,7 @@ public class TestCurrency2 extends Currency {
} }
@Override @Override
public int getDecimalPlaces() { public int fractionalDigits() {
return 2; return 2;
} }

View file

@ -19,7 +19,7 @@ public class TestPlugin extends JavaPlugin {
getLogger().info("TestCurrency registered!"); getLogger().info("TestCurrency registered!");
currencyManager.getRegisteredCurrencies().forEach(currency -> { 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")); provider.getCommandManager().registerCurrencyCommand(currencyManager.getCurrency("test"));

View file

@ -7,6 +7,7 @@ import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.UUID; import java.util.UUID;
@ -73,6 +74,7 @@ public abstract class AbstractCommandManager implements CommandManager {
@Override @Override
public void onSet(CommandSender sender, Currency currency, User target, BigDecimal amount) { public void onSet(CommandSender sender, Currency currency, User target, BigDecimal amount) {
addToMustWait(sender.getUniqueId(), target.getUniqueId()); addToMustWait(sender.getUniqueId(), target.getUniqueId());
amount = amount.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN);
target.setBalance(currency, amount); target.setBalance(currency, amount);
@ -87,6 +89,7 @@ public abstract class AbstractCommandManager implements CommandManager {
@Override @Override
public void onGive(CommandSender sender, Currency currency, User target, BigDecimal amount) { public void onGive(CommandSender sender, Currency currency, User target, BigDecimal amount) {
addToMustWait(sender.getUniqueId(), target.getUniqueId()); addToMustWait(sender.getUniqueId(), target.getUniqueId());
amount = amount.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN);
target.setBalance(currency, target.getBalance(currency).add(amount)); target.setBalance(currency, target.getBalance(currency).add(amount));
@ -101,6 +104,7 @@ public abstract class AbstractCommandManager implements CommandManager {
@Override @Override
public void onTake(CommandSender sender, Currency currency, User target, BigDecimal amount) { public void onTake(CommandSender sender, Currency currency, User target, BigDecimal amount) {
addToMustWait(sender.getUniqueId(), target.getUniqueId()); addToMustWait(sender.getUniqueId(), target.getUniqueId());
amount = amount.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN);
target.setBalance(currency, target.getBalance(currency).subtract(amount)); target.setBalance(currency, target.getBalance(currency).subtract(amount));
@ -122,11 +126,19 @@ public abstract class AbstractCommandManager implements CommandManager {
return; return;
} }
addToMustWait(sender.getUniqueId(), target.getUniqueId()); amount = amount.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN);
User user = this.plugin.getUserManager().getIfLoaded(sender.getUniqueId()); 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 // calculate tax using Currency#calculateTax
BigDecimal tax = currency.getProxy().calculateTax(user.getProxy(), amount); BigDecimal tax = currency.getProxy().calculateTax(user.getProxy(), amount);

View file

@ -26,7 +26,7 @@ public class MoneyCurrency extends Currency {
} }
@Override @Override
public int getDecimalPlaces() { public int fractionalDigits() {
return 2; return 2;
} }
} }