Post: [317] Money Pouch with 10.7B limit
03-16-2015, 05:52 PM #1
Stunz
Former Staff
(adsbygoogle = window.adsbygoogle || []).push({});
Explanation: when an integer goes over Integer.MAX_VALUE it goes negative. Everything else is self explanatory. Enjoy.

    package org.model.entity.player.content;

import org.model.entity.player.Player;
import org.model.entity.player.PlayerConstants;
import org.model.item.Item;
import org.util.Misc;

public class MoneyPouch {

private final Player player;

private int[] stored = new int[5];

public MoneyPouch(Player player) {
this.player = player;
}

public void update() {
String coins;
if (stored[0] >= 1000000000) {
coins = Misc.formatBillionCoins(stored);
} else {
coins = Misc.formatCoins(stored[0]);
}

player.getActionSender().sendString(coins, 8135);
}

public boolean hasAmount(int am) {
return am <= stored[0];
}

public boolean hasSpaceFor(int am) {
for (int i = 0; i < stored.length; i++) {
if (stored[i] == Integer.MAX_VALUE) {
continue;
}

int newAm = stored[i] + am;

if (newAm < 0) {
am -= Integer.MAX_VALUE - stored[i];
} else {
return true;
}
}
return false;
}

public void deposit(int amount, boolean remove) {
if (player.getCombat().inCombat()) {
player.getActionSender().sendMessage("Your cannot deposit coins while in combat.");
return;
}

if (amount <= 0) {
return;
}

if (stored[4] == Integer.MAX_VALUE) {
player.getActionSender().sendMessage("Your money pouch is full.");
return;
}

int invAm = player.getInventory().getItemAmount(995);

if (remove && invAm == 0) {
player.getActionSender().sendMessage("You don't have any coins.");
return;
} else if (remove && invAm < amount) {
amount = invAm;
}

int deposit = 0;

for (int i = 0; i < stored.length; i++) {
if (stored[i] == Integer.MAX_VALUE) {
continue;
}

int newAmount = stored[i] + amount;
if (newAmount < 0) {
int added = Integer.MAX_VALUE - stored[i];
deposit += added;
amount -= added;
stored[i] = Integer.MAX_VALUE;
if (i == 4) {
player.getActionSender().sendMessage("Your money pouch has been filled.");
}
if (amount == 0) {
break;
}
} else {
stored[i] = newAmount;
deposit += amount;
if (newAmount == Integer.MAX_VALUE && i == 4) {
player.getActionSender().sendMessage("Your money pouch has been filled.");
}
break;
}
}

if (remove) {
player.getInventory().removeItem(new Item(995, deposit));
}
update();
}

public void withdraw(int amount, boolean add) {
if (amount <= 0) {
return;
}

if (stored[0] == 0) {
player.getActionSender().sendMessage("Your money pouch is empty.");
return;
}

int[] copy = stored.clone();

int withdraw = 0;
boolean empty = false;

for (int i = copy.length - 1; i > -1; i--) {
if (copy[i] == 0) {
continue;
}

int newAmount = copy[i] - amount;

if (newAmount < 0) {
amount -= copy[i];
withdraw += copy[i];
copy[i] = 0;
if (i == 0) {
empty = true;
}
} else {
withdraw += amount;
copy[i] = newAmount;
if (i == 0 && newAmount == 0) {
empty = true;
}
break;
}
}

Item coins = new Item(995, withdraw);

if (add && !player.getInventory().hasSpaceFor(coins)) {
player.getActionSender().sendMessage(PlayerConstants.NOT_ENOUGH_INV_SPACE_MESSAGE);
} else {
if (empty) {
player.getActionSender().sendMessage("Your money pouch has been emptied.");
}
if (add) {
player.getInventory().addItem(coins);
}
stored = copy;
update();
}
}

}


    
public static String formatCoins(int amount) {
if (amount >= 10000000) {
return (int) (amount / 1000000) + "M";
} else if (amount >= 100000) {
return (int) (amount / 1000) + "K";
} else {
return "" + amount;
}
}

public static String formatBillionCoins(int[] amount) {
int num = 0;
int rem = 0;

for (int i : amount) {
num += i / 1000;
rem += i % 1000;
}

if (rem >= 1000) {
num += rem / 1000;
}

int bill = (int) (num / 1000000);
num -= bill * 1000000;

int mill = (int) (num / 1000);

String z = "";
if (mill < 10) {
z = "00";
} else if (mill < 100) {
z = "0";
}

return bill + "." + z + mill + "B";
}
(adsbygoogle = window.adsbygoogle || []).push({});

The following user thanked Stunz for this useful post:

bvsxdmodz
03-16-2015, 07:11 PM #2
Thanks need this!
03-16-2015, 07:12 PM #3
Stunz
Former Staff
Originally posted by bvsxdmodz View Post
Thanks need this!
You're welcome :p

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo