1
0
Fork 0
mirror of https://github.com/xHyroM/lighteco.git synced 2024-12-25 21:51:07 +01:00

feat: handle pay + improve tax system

This commit is contained in:
Jozef Steinhübl 2023-08-26 22:52:56 +02:00
parent f9a72910ee
commit 0cd8e50f9c
6 changed files with 65 additions and 11 deletions

View file

@ -1,5 +1,7 @@
package dev.xhyrom.lighteco.api.model.currency;
import dev.xhyrom.lighteco.api.model.user.User;
import java.math.BigDecimal;
public abstract class Currency {
@ -36,10 +38,10 @@ public abstract class Currency {
* Used for payables
*
* @param amount The amount to calculate the tax for
* @return The tax
* @return Amount that should be taxed
*/
public BigDecimal calculateTax(BigDecimal amount) {
return getDefaultBalance();
public BigDecimal calculateTax(User user, BigDecimal amount) {
return BigDecimal.ZERO;
};
/**

View file

@ -91,6 +91,18 @@ public class BukkitCommandManager extends AbstractCommandManager {
this.handleTake(sender, args, currency);
})
)
.withSubcommand(new CommandAPICommand("pay")
.withPermission(permissionBase + "pay")
.withArguments(
new OfflinePlayerArgument("target"),
currency.getProxy().getDecimalPlaces() > 0
? new DoubleArgument("amount", 1)
: new IntegerArgument("amount", 1)
)
.executesPlayer((sender, args) -> {
this.handlePay(sender, args, currency);
})
)
.withSubcommand(balanceCommands.get(0))
.withSubcommands(balanceCommands.get(1))
.register();
@ -147,7 +159,7 @@ public class BukkitCommandManager extends AbstractCommandManager {
});
}
public void handleTake(CommandSender originalSender, CommandArguments args, Currency currency) {
private void handleTake(CommandSender originalSender, CommandArguments args, Currency currency) {
BukkitCommandSender sender = new BukkitCommandSender(originalSender, this.audienceFactory);
OfflinePlayer target = (OfflinePlayer) args.get("target");
BigDecimal amount = BigDecimal.valueOf(Double.parseDouble(args.getRaw("amount")));
@ -162,4 +174,20 @@ public class BukkitCommandManager extends AbstractCommandManager {
this.onTake(sender, currency, result, amount);
});
}
private void handlePay(CommandSender originalSender, CommandArguments args, Currency currency) {
BukkitCommandSender sender = new BukkitCommandSender(originalSender, this.audienceFactory);
OfflinePlayer target = (OfflinePlayer) args.get("target");
BigDecimal amount = BigDecimal.valueOf(Double.parseDouble(args.getRaw("amount")));
if (!this.canUse(sender)) return;
this.plugin.getUserManager().loadUser(target.getUniqueId())
.thenAcceptAsync(result -> {
String name = target.getName() != null ? target.getName() : args.getRaw("target");
result.setUsername(name);
this.onPay(sender, currency, result, amount);
});
}
}

View file

@ -24,9 +24,4 @@ public class TestCurrency extends Currency {
public BigDecimal getDefaultBalance() {
return BigDecimal.ZERO;
}
@Override
public BigDecimal calculateTax(BigDecimal amount) {
return BigDecimal.ZERO;
}
}

View file

@ -1,6 +1,7 @@
package dev.xhyrom.lighteco.bukkittest;
import dev.xhyrom.lighteco.api.model.currency.Currency;
import dev.xhyrom.lighteco.api.model.user.User;
import java.math.BigDecimal;
@ -31,7 +32,7 @@ public class TestCurrency2 extends Currency {
}
@Override
public BigDecimal calculateTax(BigDecimal amount) {
return BigDecimal.ZERO;
public BigDecimal calculateTax(User user, BigDecimal amount) {
return amount.multiply(BigDecimal.valueOf(0.2));
}
}

View file

@ -10,6 +10,7 @@ import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public abstract class AbstractCommandManager implements CommandManager {
protected final LightEcoPlugin plugin;
@ -110,4 +111,30 @@ public abstract class AbstractCommandManager implements CommandManager {
this.plugin.getUserManager().saveUser(target)
.thenAccept(v -> removeFromMustWait(target.getUniqueId(), sender.getUniqueId()));
}
@Override
public void onPay(CommandSender sender, Currency currency, User target, BigDecimal amount) {
addToMustWait(sender.getUniqueId(), target.getUniqueId());
User user = this.plugin.getUserManager().getIfLoaded(sender.getUniqueId());
// calculate tax using Currency#calculateTax
BigDecimal tax = currency.getProxy().calculateTax(user.getProxy(), amount);
// subtract tax from amount
amount = amount.subtract(tax);
target.setBalance(currency, target.getBalance(currency).add(amount));
user.setBalance(currency, user.getBalance(currency).subtract(amount));
// send message and also include tax rate (percentage) with tax amount
sender.sendMessage(
miniMessage.deserialize("<yellow>Paid <gold>" + amount.toPlainString() + " <yellow>" + currency.getIdentifier() + " to " + target.getUsername() + " with a tax rate of <gold>" + tax + "% <yellow>(<gold>" + tax.toPlainString() + " <yellow>" + currency.getIdentifier() + ")")
);
CompletableFuture.allOf(
this.plugin.getUserManager().saveUser(user),
this.plugin.getUserManager().saveUser(target)
).thenAccept(v -> removeFromMustWait(sender.getUniqueId(), target.getUniqueId()));
}
}

View file

@ -18,4 +18,5 @@ public interface CommandManager {
void onSet(CommandSender sender, Currency currency, User target, BigDecimal amount);
void onGive(CommandSender sender, Currency currency, User target, BigDecimal amount);
void onTake(CommandSender sender, Currency currency, User target, BigDecimal amount);
void onPay(CommandSender sender, Currency currency, User target, BigDecimal amount);
}