Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
Fix a bug where the CONSOLE account does not have an infinite balance…
Browse files Browse the repository at this point in the history
… on databases upgraded from older versions.
  • Loading branch information
AppleDash committed Jan 24, 2020
1 parent eab4b59 commit 2856305
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.appledash.saneeconomy.SaneEconomy;
import org.appledash.saneeconomy.economy.backend.EconomyStorageBackend;
import org.appledash.saneeconomy.economy.economable.Economable;
import org.appledash.saneeconomy.economy.economable.EconomableConsole;
import org.appledash.saneeconomy.economy.transaction.Transaction;
import org.appledash.saneeconomy.economy.transaction.TransactionResult;
import org.appledash.saneeconomy.event.SaneEconomyTransactionEvent;
Expand Down Expand Up @@ -67,7 +68,7 @@ public boolean accountExists(Economable player) {
* @return Player's balance
*/
public BigDecimal getBalance(Economable targetPlayer) {
if (targetPlayer == Economable.CONSOLE) {
if (EconomableConsole.isConsole(targetPlayer)) {
return new BigDecimal(Double.MAX_VALUE);
}

Expand All @@ -82,7 +83,7 @@ public BigDecimal getBalance(Economable targetPlayer) {
* @return True if they have requiredBalance or more, false otherwise
*/
public boolean hasBalance(Economable targetPlayer, BigDecimal requiredBalance) {
return (targetPlayer == Economable.CONSOLE) || (this.getBalance(targetPlayer).compareTo(requiredBalance) >= 0);
return (EconomableConsole.isConsole(targetPlayer)) || (this.getBalance(targetPlayer).compareTo(requiredBalance) >= 0);

}

Expand Down Expand Up @@ -120,7 +121,7 @@ private void subtractBalance(Economable targetPlayer, BigDecimal amount) {
public void setBalance(Economable targetPlayer, BigDecimal amount) {
amount = NumberUtils.filterAmount(this.currency, amount);

if (targetPlayer == Economable.CONSOLE) {
if (EconomableConsole.isConsole(targetPlayer)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
package org.appledash.saneeconomy.economy.economable;

import java.util.UUID;

/**
* Created by appledash on 9/21/16.
* Blackjack is best pony.
*/
public class EconomableConsole implements Economable {
public static final UUID CONSOLE_UUID = new UUID( 0xf88708c237d84a0bL, 0x944259c68e517557L); // Pregenerated for performance

@Override
public String getName() {
return "CONSOLE";
}

@Override
public String getUniqueIdentifier() {
return "CONSOLE";
return "console:" + CONSOLE_UUID.toString();
}

public static boolean isConsole(Economable economable) {
try {
UUID uuid = UUID.fromString(economable.getUniqueIdentifier().split(":")[1]);

// Fast comparison + fix for bugs with older databases
return economable == Economable.CONSOLE || (uuid.getLeastSignificantBits() == CONSOLE_UUID.getLeastSignificantBits() || uuid.getMostSignificantBits() == CONSOLE_UUID.getMostSignificantBits());
} catch (Exception e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.appledash.saneeconomy.economy.Currency;
import org.appledash.saneeconomy.economy.economable.Economable;
import org.appledash.saneeconomy.economy.economable.EconomableConsole;
import org.appledash.saneeconomy.economy.transaction.TransactionReason.AffectedParties;
import org.appledash.saneeconomy.utils.NumberUtils;

Expand Down Expand Up @@ -46,15 +47,15 @@ public TransactionReason getReason() {
}

public boolean isSenderAffected() {
if (this.sender == Economable.CONSOLE) {
if (EconomableConsole.isConsole(this.sender)) {
return false;
}

return (this.reason.getAffectedParties() == AffectedParties.SENDER) || (this.reason.getAffectedParties() == AffectedParties.BOTH);
}

public boolean isReceiverAffected() {
if (this.receiver == Economable.CONSOLE) {
if (EconomableConsole.isConsole(this.receiver)) {
return false;
}

Expand Down

0 comments on commit 2856305

Please sign in to comment.