mirror of
https://github.com/xHyroM/lighteco.git
synced 2024-11-14 03:18:07 +01:00
feat: vault, papi
This commit is contained in:
parent
eafed192d3
commit
4213601132
7 changed files with 155 additions and 17 deletions
|
@ -36,4 +36,22 @@ public interface User {
|
||||||
* @param balance the balance
|
* @param balance the balance
|
||||||
*/
|
*/
|
||||||
void setBalance(@NonNull Currency currency, @NonNull BigDecimal balance);
|
void setBalance(@NonNull Currency currency, @NonNull BigDecimal balance);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add the specified amount to the balance of this user for the specified currency.
|
||||||
|
*
|
||||||
|
* @param currency the currency
|
||||||
|
* @param amount the amount
|
||||||
|
* @throws IllegalArgumentException if the amount is negative
|
||||||
|
*/
|
||||||
|
void deposit(@NonNull Currency currency, @NonNull BigDecimal amount) throws IllegalArgumentException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Subtract the specified amount from the balance of this user for the specified currency.
|
||||||
|
*
|
||||||
|
* @param currency the currency
|
||||||
|
* @param amount the amount
|
||||||
|
* @throws IllegalArgumentException if the amount is negative
|
||||||
|
*/
|
||||||
|
void withdraw(@NonNull Currency currency, @NonNull BigDecimal amount) throws IllegalArgumentException;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
package dev.xhyrom.lighteco.bukkit.hooks;
|
package dev.xhyrom.lighteco.bukkit.hooks;
|
||||||
|
|
||||||
import dev.xhyrom.lighteco.bukkit.BukkitLightEcoPlugin;
|
import dev.xhyrom.lighteco.bukkit.BukkitLightEcoPlugin;
|
||||||
|
import dev.xhyrom.lighteco.common.model.currency.Currency;
|
||||||
|
import dev.xhyrom.lighteco.common.model.user.User;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||||
import org.bukkit.OfflinePlayer;
|
import org.bukkit.OfflinePlayer;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
@ -28,6 +31,21 @@ public class PlaceholderAPIExpansion extends PlaceholderExpansion {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @Nullable String onRequest(OfflinePlayer player, @NotNull String params) {
|
public @Nullable String onRequest(OfflinePlayer player, @NotNull String params) {
|
||||||
|
String[] args = params.split("_");
|
||||||
|
if (args.length < 2) return null;
|
||||||
|
|
||||||
|
String currencyIdentifier = args[0];
|
||||||
|
String type = args[1];
|
||||||
|
|
||||||
|
Currency currency = this.plugin.getCurrencyManager().getIfLoaded(currencyIdentifier);
|
||||||
|
if (currency == null) return null;
|
||||||
|
|
||||||
|
if (type.equalsIgnoreCase("balance")) {
|
||||||
|
User user = this.plugin.getUserManager().loadUser(player.getUniqueId()).join();
|
||||||
|
|
||||||
|
return user.getBalance(currency).toPlainString();
|
||||||
|
}
|
||||||
|
|
||||||
return "lighteco";
|
return "lighteco";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,6 +52,7 @@ public class BukkitCommandManager extends AbstractCommandManager {
|
||||||
|
|
||||||
// Dont expose set, give, take directly - only through main command
|
// Dont expose set, give, take directly - only through main command
|
||||||
new CommandAPICommand(currency.getIdentifier())
|
new CommandAPICommand(currency.getIdentifier())
|
||||||
|
.withPermission("lighteco.currency." + currency.getIdentifier() + ".command")
|
||||||
.withSubcommand(new SetCommand(this, currency, permissionBase).build())
|
.withSubcommand(new SetCommand(this, currency, permissionBase).build())
|
||||||
.withSubcommand(new GiveCommand(this, currency, permissionBase).build())
|
.withSubcommand(new GiveCommand(this, currency, permissionBase).build())
|
||||||
.withSubcommand(new TakeCommand(this, currency, permissionBase).build())
|
.withSubcommand(new TakeCommand(this, currency, permissionBase).build())
|
||||||
|
|
|
@ -42,4 +42,22 @@ public class ApiUser implements User {
|
||||||
|
|
||||||
this.handler.setBalance(internal, balance);
|
this.handler.setBalance(internal, balance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deposit(@NonNull Currency currency, @NonNull BigDecimal amount) {
|
||||||
|
dev.xhyrom.lighteco.common.model.currency.Currency internal = this.handler.getPlugin()
|
||||||
|
.getCurrencyManager()
|
||||||
|
.getIfLoaded(currency.getIdentifier());
|
||||||
|
|
||||||
|
this.handler.deposit(internal, amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void withdraw(@NonNull Currency currency, @NonNull BigDecimal amount) {
|
||||||
|
dev.xhyrom.lighteco.common.model.currency.Currency internal = this.handler.getPlugin()
|
||||||
|
.getCurrencyManager()
|
||||||
|
.getIfLoaded(currency.getIdentifier());
|
||||||
|
|
||||||
|
this.handler.withdraw(internal, amount);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,7 +100,16 @@ public abstract class AbstractCommandManager implements CommandManager {
|
||||||
addToMustWait(sender.getUniqueId(), target.getUniqueId());
|
addToMustWait(sender.getUniqueId(), target.getUniqueId());
|
||||||
amount = amount.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN);
|
amount = amount.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN);
|
||||||
|
|
||||||
|
try {
|
||||||
target.setBalance(currency, amount);
|
target.setBalance(currency, amount);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
sender.sendMessage(
|
||||||
|
miniMessage.deserialize("<red>Cannot set negative money!")
|
||||||
|
);
|
||||||
|
|
||||||
|
removeFromMustWait(target.getUniqueId(), sender.getUniqueId());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
sender.sendMessage(
|
sender.sendMessage(
|
||||||
miniMessage.deserialize("<yellow>Set " + target.getUsername() + "'s balance to <gold>" + amount.toPlainString() + " <yellow>" + currency.getIdentifier())
|
miniMessage.deserialize("<yellow>Set " + target.getUsername() + "'s balance to <gold>" + amount.toPlainString() + " <yellow>" + currency.getIdentifier())
|
||||||
|
@ -115,7 +124,16 @@ public abstract class AbstractCommandManager implements CommandManager {
|
||||||
addToMustWait(sender.getUniqueId(), target.getUniqueId());
|
addToMustWait(sender.getUniqueId(), target.getUniqueId());
|
||||||
amount = amount.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN);
|
amount = amount.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN);
|
||||||
|
|
||||||
target.setBalance(currency, target.getBalance(currency).add(amount));
|
try {
|
||||||
|
target.deposit(currency, amount);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
sender.sendMessage(
|
||||||
|
miniMessage.deserialize("<red>Cannot give negative money!")
|
||||||
|
);
|
||||||
|
|
||||||
|
removeFromMustWait(target.getUniqueId(), sender.getUniqueId());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
sender.sendMessage(
|
sender.sendMessage(
|
||||||
miniMessage.deserialize("<yellow>Gave " + target.getUsername() + " <gold>" + amount.toPlainString() + " <yellow>" + currency.getIdentifier())
|
miniMessage.deserialize("<yellow>Gave " + target.getUsername() + " <gold>" + amount.toPlainString() + " <yellow>" + currency.getIdentifier())
|
||||||
|
@ -130,7 +148,16 @@ public abstract class AbstractCommandManager implements CommandManager {
|
||||||
addToMustWait(sender.getUniqueId(), target.getUniqueId());
|
addToMustWait(sender.getUniqueId(), target.getUniqueId());
|
||||||
amount = amount.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN);
|
amount = amount.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN);
|
||||||
|
|
||||||
target.setBalance(currency, target.getBalance(currency).subtract(amount));
|
try {
|
||||||
|
target.withdraw(currency, amount);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
sender.sendMessage(
|
||||||
|
miniMessage.deserialize("<red>Cannot take negative money!")
|
||||||
|
);
|
||||||
|
|
||||||
|
removeFromMustWait(target.getUniqueId(), sender.getUniqueId());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
sender.sendMessage(
|
sender.sendMessage(
|
||||||
miniMessage.deserialize("<yellow>Took <gold>" + amount.toPlainString() + " <yellow>" + currency.getIdentifier() + " from " + target.getUsername())
|
miniMessage.deserialize("<yellow>Took <gold>" + amount.toPlainString() + " <yellow>" + currency.getIdentifier() + " from " + target.getUsername())
|
||||||
|
@ -170,8 +197,8 @@ public abstract class AbstractCommandManager implements CommandManager {
|
||||||
// subtract tax from amount
|
// subtract tax from amount
|
||||||
BigDecimal taxedAmount = amount.subtract(tax);
|
BigDecimal taxedAmount = amount.subtract(tax);
|
||||||
|
|
||||||
target.setBalance(currency, target.getBalance(currency).add(taxedAmount));
|
target.deposit(currency, taxedAmount);
|
||||||
user.setBalance(currency, user.getBalance(currency).subtract(amount));
|
user.withdraw(currency, amount);
|
||||||
|
|
||||||
// send message that will include original amount, taxed amount, tax rate - percentage amount and tax amount
|
// send message that will include original amount, taxed amount, tax rate - percentage amount and tax amount
|
||||||
sender.sendMessage(
|
sender.sendMessage(
|
||||||
|
|
|
@ -39,9 +39,34 @@ public class User {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBalance(@NonNull Currency currency, @NonNull BigDecimal balance) {
|
public void setBalance(@NonNull Currency currency, @NonNull BigDecimal balance) {
|
||||||
|
if (balance.compareTo(BigDecimal.ZERO) < 0) {
|
||||||
|
throw new IllegalArgumentException("Balance cannot be negative");
|
||||||
|
}
|
||||||
|
|
||||||
balances.put(currency, balance);
|
balances.put(currency, balance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void deposit(@NonNull Currency currency, @NonNull BigDecimal amount) throws IllegalArgumentException {
|
||||||
|
if (amount.compareTo(BigDecimal.ZERO) < 0) {
|
||||||
|
throw new IllegalArgumentException("Amount cannot be negative");
|
||||||
|
}
|
||||||
|
|
||||||
|
setBalance(currency, getBalance(currency).add(amount));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void withdraw(@NonNull Currency currency, @NonNull BigDecimal amount) throws IllegalArgumentException {
|
||||||
|
if (amount.compareTo(BigDecimal.ZERO) < 0) {
|
||||||
|
throw new IllegalArgumentException("Amount cannot be negative");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (getBalance(currency).compareTo(amount) < 0) {
|
||||||
|
// Withdraw all
|
||||||
|
amount = getBalance(currency);
|
||||||
|
}
|
||||||
|
|
||||||
|
setBalance(currency, getBalance(currency).subtract(amount));
|
||||||
|
}
|
||||||
|
|
||||||
public void invalidateCaches() {
|
public void invalidateCaches() {
|
||||||
balances.clear();
|
balances.clear();
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,10 +7,12 @@ import dev.xhyrom.lighteco.api.model.user.User;
|
||||||
import net.milkbowl.vault.economy.AbstractEconomy;
|
import net.milkbowl.vault.economy.AbstractEconomy;
|
||||||
import net.milkbowl.vault.economy.EconomyResponse;
|
import net.milkbowl.vault.economy.EconomyResponse;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
public class Vault extends AbstractEconomy {
|
public class Vault extends AbstractEconomy {
|
||||||
private final LightEco provider;
|
private final LightEco provider;
|
||||||
|
@ -105,14 +107,39 @@ public class Vault extends AbstractEconomy {
|
||||||
UUID uniqueId = Bukkit.getPlayerUniqueId(playerName);
|
UUID uniqueId = Bukkit.getPlayerUniqueId(playerName);
|
||||||
User user = provider.getUserManager().loadUser(uniqueId).join();
|
User user = provider.getUserManager().loadUser(uniqueId).join();
|
||||||
|
|
||||||
BigDecimal balance = user.getBalance(currency);
|
try {
|
||||||
BigDecimal newBalance = balance.subtract(BigDecimal.valueOf(amount));
|
user.withdraw(currency, BigDecimal.valueOf(amount));
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
return new EconomyResponse(
|
||||||
|
amount,
|
||||||
|
bigDecimalToDouble(user.getBalance(currency)),
|
||||||
|
EconomyResponse.ResponseType.FAILURE,
|
||||||
|
"Cannot withdraw negative funds"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
user.setBalance(currency, newBalance);
|
return saveUser(amount, user);
|
||||||
|
}
|
||||||
|
|
||||||
provider.getUserManager().saveUser(user);
|
@NotNull
|
||||||
|
private EconomyResponse saveUser(double amount, User user) {
|
||||||
|
try {
|
||||||
|
provider.getUserManager().saveUser(user).get();
|
||||||
|
} catch (InterruptedException | ExecutionException e) {
|
||||||
|
return new EconomyResponse(
|
||||||
|
amount,
|
||||||
|
bigDecimalToDouble(user.getBalance(currency)),
|
||||||
|
EconomyResponse.ResponseType.FAILURE,
|
||||||
|
"Cannot save user"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return new EconomyResponse(amount, bigDecimalToDouble(newBalance), EconomyResponse.ResponseType.SUCCESS, "");
|
return new EconomyResponse(
|
||||||
|
amount,
|
||||||
|
bigDecimalToDouble(user.getBalance(currency)),
|
||||||
|
EconomyResponse.ResponseType.SUCCESS,
|
||||||
|
""
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -125,14 +152,18 @@ public class Vault extends AbstractEconomy {
|
||||||
UUID uniqueId = Bukkit.getPlayerUniqueId(playerName);
|
UUID uniqueId = Bukkit.getPlayerUniqueId(playerName);
|
||||||
User user = provider.getUserManager().loadUser(uniqueId).join();
|
User user = provider.getUserManager().loadUser(uniqueId).join();
|
||||||
|
|
||||||
BigDecimal balance = user.getBalance(currency);
|
try {
|
||||||
BigDecimal newBalance = balance.add(BigDecimal.valueOf(amount));
|
user.deposit(currency, BigDecimal.valueOf(amount));
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
return new EconomyResponse(
|
||||||
|
amount,
|
||||||
|
bigDecimalToDouble(user.getBalance(currency)),
|
||||||
|
EconomyResponse.ResponseType.FAILURE,
|
||||||
|
"Cannot deposit negative funds"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
user.setBalance(currency, newBalance);
|
return saveUser(amount, user);
|
||||||
|
|
||||||
provider.getUserManager().saveUser(user);
|
|
||||||
|
|
||||||
return new EconomyResponse(amount, bigDecimalToDouble(newBalance), EconomyResponse.ResponseType.SUCCESS, "");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in a new issue