Added convenience methods to VaultHook

VaultHook#getBalance(User) : double ;
VaultHook#withdraw(User, double) : EconomyResponse ;
VaultHook#deposit(User, double) : EconomyResponse
VaultHook#has(User, double) : boolean.

And made use of these nice new methods where it was needed.
This commit is contained in:
Florian CUNY 2018-10-31 15:35:40 +01:00
parent a6357ed157
commit c3d57acc11
7 changed files with 68 additions and 6 deletions

View File

@ -72,7 +72,7 @@ public class AdminDeleteCommand extends ConfirmableCommand {
target.getPlayer().getInventory().clear();
}
if (getSettings().isUseEconomy() && getIWM().isOnLeaveResetMoney(getWorld())) {
getPlugin().getVault().ifPresent(vault -> vault.getEconomy().withdrawPlayer(target.getPlayer(), vault.getEconomy().getBalance(target.getPlayer())));
getPlugin().getVault().ifPresent(vault -> vault.withdraw(target, vault.getBalance(target)));
}
}
getIslands().deleteIsland(oldIsland, true);

View File

@ -82,7 +82,7 @@ public class IslandResetCommand extends ConfirmableCommand {
user.getPlayer().getInventory().clear();
}
if (getSettings().isUseEconomy() && getIWM().isOnLeaveResetMoney(getWorld())) {
getPlugin().getVault().ifPresent(vault -> vault.getEconomy().withdrawPlayer(user.getPlayer(), vault.getEconomy().getBalance(user.getPlayer())));
getPlugin().getVault().ifPresent(vault -> vault.withdraw(user, vault.getBalance(user)));
}
// Add a reset
getPlayers().addReset(getWorld(), user.getUniqueId());

View File

@ -85,7 +85,7 @@ public class IslandTeamInviteAcceptCommand extends CompositeCommand {
user.getPlayer().getInventory().clear();
}
if (getSettings().isUseEconomy() && (getIWM().isOnLeaveResetMoney(getWorld()) || getIWM().isOnJoinResetMoney(getWorld()))) {
getPlugin().getVault().ifPresent(vault -> vault.getEconomy().withdrawPlayer(user.getPlayer(), vault.getEconomy().getBalance(user.getPlayer())));
getPlugin().getVault().ifPresent(vault -> vault.withdraw(user, vault.getBalance(user)));
}
// Add the player as a team member of the new island
getIslands().setJoinTeam(teamIsland, playerUUID);

View File

@ -73,7 +73,7 @@ public class IslandTeamKickCommand extends ConfirmableCommand {
user.getPlayer().getInventory().clear();
}
if (getSettings().isUseEconomy() && getIWM().isOnLeaveResetMoney(getWorld())) {
getPlugin().getVault().ifPresent(vault -> vault.getEconomy().withdrawPlayer(target.getPlayer(), vault.getEconomy().getBalance(target.getPlayer())));
getPlugin().getVault().ifPresent(vault -> vault.withdraw(target, vault.getBalance(target)));
}
user.sendMessage("general.success");

View File

@ -54,7 +54,7 @@ public class IslandTeamLeaveCommand extends ConfirmableCommand {
user.getPlayer().getInventory().clear();
}
if (getSettings().isUseEconomy() && getIWM().isOnLeaveResetMoney(getWorld())) {
getPlugin().getVault().ifPresent(vault -> vault.getEconomy().withdrawPlayer(user.getPlayer(), vault.getEconomy().getBalance(user.getPlayer())));
getPlugin().getVault().ifPresent(vault -> vault.withdraw(user, vault.getBalance(user)));
}
user.sendMessage("general.success");
}

View File

@ -1,9 +1,11 @@
package world.bentobox.bentobox.hooks;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;
import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider;
import world.bentobox.bentobox.api.hooks.Hook;
import world.bentobox.bentobox.api.user.User;
/**
* @author Poslovitch
@ -34,4 +36,64 @@ public class VaultHook extends Hook {
public Economy getEconomy() {
return economy;
}
// ------ CONVENIENCE METHODS ------
/**
* Gets balance of this User.
* If this User is not a Player (or OfflinePlayer), it will always return {@code 0.0D}.
*
* @param user the User to get the balance from.
* @return the balance of this User.
*/
public double getBalance(User user) {
return (user.isPlayer()) ? economy.getBalance(user.getPlayer()) : 0.0D;
}
/**
* Withdraws an amount from this User.
* @param user the User to withdraw from. Must be a Player or an OfflinePlayer.
* @param amount the amount to withdraw. Must be positive.
* @return the EconomyResponse of this withdrawal.
*/
public EconomyResponse withdraw(User user, double amount) {
if (!user.isPlayer()) {
throw new IllegalArgumentException("User must be a Player or an OfflinePlayer");
}
if (amount < 0.0D) {
throw new IllegalArgumentException("Amount must be positive.");
}
return economy.withdrawPlayer(user.getPlayer(), amount);
}
/**
* Deposits an amount to this User.
* @param user the User to deposit to. Must be a Player or an OfflinePlayer.
* @param amount the amount to deposit. Must be positive.
* @return the EconomyResponse of this deposit.
*/
public EconomyResponse deposit(User user, double amount) {
if (!user.isPlayer()) {
throw new IllegalArgumentException("User must be a Player or an OfflinePlayer");
}
if (amount < 0.0D) {
throw new IllegalArgumentException("Amount must be positive.");
}
return economy.depositPlayer(user.getPlayer(), amount);
}
/**
* Checks if this User has the amount.
* If this User is not a Player (or OfflinePlayer), it will always return {@code false}.
*
* @param user the User to check.
* @param amount the amount to check. Must be positive.
* @return whether the User has the amount or not.
*/
public boolean has(User user, double amount) {
if (amount < 0.0D) {
throw new IllegalArgumentException("Amount must be positive.");
}
return user.isPlayer() && economy.has(user.getPlayer(), amount);
}
}

View File

@ -568,7 +568,7 @@ public class IslandsManager {
user.getPlayer().getInventory().clear();
}
if (plugin.getSettings().isUseEconomy() && plugin.getIWM().isOnJoinResetMoney(world)) {
plugin.getVault().ifPresent(vault -> vault.getEconomy().withdrawPlayer(user.getPlayer(), vault.getEconomy().getBalance(user.getPlayer())));
plugin.getVault().ifPresent(vault -> vault.withdraw(user, vault.getBalance(user)));
}
}
}