hsyyid/AdminShop

Overflowing Signed Integer Causes "Not Enough Money"

Closed this issue · 2 comments

When messing around with your plugin, I found that $20,000,000,000 (20 billion) causes a "Not Enough Money" message when attempting to buy something that costs $100.

By digging into your code, I found that it's because of how you check whether a player can afford something or not, specifically this line.

if (accountManager.getBalance(player.getUniqueId()).intValue() > amount.intValue())

The signed bit for $20,000,000,000 is 1, therefore causing it to appear negative when calling intValue(). There are two solutions to this:

if (accountManager.hasMoney(player.getUniqueId(), amount)

That one might cease working if the Total Economy API changes. But there's also one that will work until the Java API changes, which is unlikely for backwards compatibility

if (accountManager.getBalance(player.getUniqueId()).compareTo(amount) >= 0)

Here we're effectively checking if player's balance is greater than or equal to amount. In this case, we're not limited by a signed integer's size because we're not converting the BigDecimals to integers.

Alright, making the changes now. Thanks for the input 😄

Fixed in most recent commit.