Fix a bug where the CONSOLE account does not have an infinite balance on databases upgraded from older versions.

This commit is contained in:
AppleDash 2020-01-24 02:06:54 -05:00
parent eab4b59501
commit 2856305330
3 changed files with 23 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import org.appledash.saneeconomy.ISaneEconomy;
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;
@ -67,7 +68,7 @@ public class EconomyManager {
* @return Player's balance
*/
public BigDecimal getBalance(Economable targetPlayer) {
if (targetPlayer == Economable.CONSOLE) {
if (EconomableConsole.isConsole(targetPlayer)) {
return new BigDecimal(Double.MAX_VALUE);
}
@ -82,7 +83,7 @@ public class EconomyManager {
* @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);
}
@ -120,7 +121,7 @@ public class EconomyManager {
public void setBalance(Economable targetPlayer, BigDecimal amount) {
amount = NumberUtils.filterAmount(this.currency, amount);
if (targetPlayer == Economable.CONSOLE) {
if (EconomableConsole.isConsole(targetPlayer)) {
return;
}

View File

@ -1,10 +1,14 @@
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";
@ -12,6 +16,17 @@ public class EconomableConsole implements Economable {
@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;
}
}
}

View File

@ -2,6 +2,7 @@ package org.appledash.saneeconomy.economy.transaction;
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;
@ -46,7 +47,7 @@ public class Transaction {
}
public boolean isSenderAffected() {
if (this.sender == Economable.CONSOLE) {
if (EconomableConsole.isConsole(this.sender)) {
return false;
}
@ -54,7 +55,7 @@ public class Transaction {
}
public boolean isReceiverAffected() {
if (this.receiver == Economable.CONSOLE) {
if (EconomableConsole.isConsole(this.receiver)) {
return false;
}