Skip to content

Commit

Permalink
Added convenience methods to VaultHook
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Poslovitch committed Oct 31, 2018
1 parent a6357ed commit c3d57ac
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private void deletePlayer(User user, UUID targetUUID) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private boolean resetIsland(User user) {
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public boolean execute(User user, String label, List<String> args) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private void kick(User user, UUID targetUUID) {
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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private void leave(User user) {
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");
}
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/world/bentobox/bentobox/hooks/VaultHook.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -34,4 +36,64 @@ public String getFailureCause() {
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ public void homeTeleport(World world, Player player, int number, boolean newIsla
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)));
}
}
}
Expand Down

0 comments on commit c3d57ac

Please sign in to comment.