Skip to content

Commit

Permalink
Economy: For economies that prison supports that has a method to chec…
Browse files Browse the repository at this point in the history
…k if the player has an account, prison now tries to check if there is an account for the player before trying to use the economy.

This could potentially prevent issues or run time failures.
  • Loading branch information
rbluer committed Mar 17, 2024
1 parent 25bf92c commit 7aee372
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
5 changes: 5 additions & 0 deletions docs/changelog_v3.3.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ These change logs represent the work that has been going on within prison.
# 3.3.0-alpha.16c 2024-03-15



* **Economy: For economies that prison supports that has a method to check if the player has an account, prison now tries to check if there is an account for the player before trying to use the economy.**
This could potentially prevent issues or run time failures.


* **Prison API: Added a few new functions to work with ItemStacks.**


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ class EssEconomyWrapper {

double getBalance(Player player) {
try {
return Economy.getMoneyExact(player.getName()).doubleValue();
if ( Economy.playerExists( player.getName() ) ) {
return Economy.getMoneyExact(player.getName()).doubleValue();
}

player.sendMessage( "You don't exist in the economy plugin." );
return 0;

} catch (UserDoesNotExistException e) {
player.sendMessage("You don't exist in the economy plugin.");
return 0.0;
Expand All @@ -48,7 +54,14 @@ class EssEconomyWrapper {

void setBalance(Player player, double amount) {
try {
Economy.setMoney(player.getName(), new BigDecimal(amount));
if ( Economy.playerExists( player.getName() ) ) {
Economy.setMoney(player.getName(), new BigDecimal(amount));
}
else {

player.sendMessage( "You don't exist in the economy plugin." );
}

} catch (UserDoesNotExistException | NoLoanPermittedException e) {
player.sendMessage("You don't exist in the economy plugin.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ public double getBalance(Player player) {
double result = 0;

try {
result = economyManager.getBalance(toEconomablePlayer(player));
EconomablePlayer p = toEconomablePlayer(player);

if ( !economyManager.accountExists( p ) ) {
player.sendMessage( "Economy Error: You don't have an account.");
}
else {

result = economyManager.getBalance( p );
}
}
catch ( Exception e ) {
Output.get().logError( "Failed to get SaneEconomy balance. " +
Expand All @@ -39,7 +47,16 @@ public double getBalance(Player player) {

public void setBalance(Player player, double amount) {
try {
economyManager.setBalance(toEconomablePlayer(player), amount);
EconomablePlayer p = toEconomablePlayer(player);

if ( !economyManager.accountExists( p ) ) {
player.sendMessage( "Economy Error: You don't have an account.");
}
else {

economyManager.setBalance(toEconomablePlayer(player), amount);
}

}
catch ( Exception e ) {
Output.get().logError( "Failed to set SaneEconomy balance. " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,13 @@ private OfflinePlayer getOfflinePlayer(Player player) {
@SuppressWarnings( "deprecation" )
public double getBalance(Player player) {
double results = 0;

if ( economy != null && !economy.hasAccount( getOfflinePlayer( player ) ) ) {
player.sendMessage( "Economy Error: You don't have an account.");
}
else
if (economy != null) {

if ( isPreV1_4() ) {
results = economy.getBalance(player.getName());
}
Expand Down Expand Up @@ -152,6 +158,11 @@ public double getBalance(Player player) {
@SuppressWarnings( "deprecation" )
public boolean setBalance(Player player, double amount) {
boolean results = false;

if ( economy != null && !economy.hasAccount( getOfflinePlayer( player ) ) ) {
player.sendMessage( "Economy Error: You don't have an account.");
}
else
if (economy != null) {

if ( isPreV1_4() ) {
Expand Down Expand Up @@ -191,6 +202,10 @@ public boolean addBalance(Player player, double amount) {
if ( amount < 0 ) {
results = removeBalance( player, amount );
}

else if ( economy != null && !economy.hasAccount( getOfflinePlayer( player ) ) ) {
player.sendMessage( "Economy Error: You don't have an account.");
}
else if (economy != null) {
if ( isPreV1_4() ) {
economy.depositPlayer( player.getName(), amount );
Expand Down Expand Up @@ -239,6 +254,10 @@ public boolean removeBalance(Player player, double amount) {
amount *= -1;
}

if ( economy != null && !economy.hasAccount( getOfflinePlayer( player ) ) ) {
player.sendMessage( "Economy Error: You don't have an account.");
}
else
if (economy != null) {
if ( isPreV1_4() ) {
economy.withdrawPlayer( player.getName(), amount );
Expand Down Expand Up @@ -284,6 +303,11 @@ public boolean removeBalance(Player player, double amount) {
@SuppressWarnings( "deprecation" )
public boolean canAfford(Player player, double amount) {
boolean results = false;

if ( economy != null && !economy.hasAccount( getOfflinePlayer( player ) ) ) {
player.sendMessage( "Economy Error: You don't have an account.");
}
else
if (economy != null) {
if ( isPreV1_4() ) {
results = economy.has(player.getName(), amount);
Expand Down

0 comments on commit 7aee372

Please sign in to comment.