Originally Posted by
Illuminate
The problem isn't really inside of that block, but the problem needs to be fixed inside that block. Merely
breaking from a loop will still cause that boolean to return true. Now, if you have some code that is using this boolean in a loop, such as:
Code:
while(buyItem(itemID, fromSlot, amount))
{
//do stuff
}
This will cause some problems. I have cleaned up your code and modified it to return false when it should. I have also added a new method because I'm guessing the reason for the
breaks were to reach that code.
Note that there may be syntax/other small errors.
Code:
public boolean buyItem(int itemID, int fromSlot, int amount)
{
if ((amount > 0) && (itemID == (server.shopHandler.ShopItems[MyShopID][fromSlot] - 1)))
{
//Ensure that user only buys less than or equal to the current stock
if (amount > server.shopHandler.ShopItemsN[MyShopID][fromSlot])
amount = server.shopHandler.ShopItemsN[MyShopID][fromSlot];
//Some quest/game? If this isn't necessary, remove it
if (QH().q1 == 9)
{
QH().q1 = 10;
CAM().sendMessage("You found the herb!");
}
//Initialize some vars for initial checks
int totPrice = (int) Math.floor(GetItemShopValue(itemID, 0, fromSlot));
int Slot = GetItemSlot(995);
//Check to see if user has enough money
if(Slot == -1 || playerItemsN[Slot] < totPrice)
{
CAM().sendMessage("You don't have enough coins.");
resetShopVars();
return false;
}
//Buy item until amount has been reached or money is gone
for (int i = amount; i > 0; i--)
{
Slot = GetItemSlot(995);
totPrice = (int) Math.floor(GetItemShopValue(itemID, 0, fromSlot));
//We have to check to see if user has money each run through
if (Slot == -1)
{
CAM().sendMessage("You don't have enough coins.");
resetShopVars();
return false;
}
if (playerItemsN[Slot] >= totPrice)
{
//Ensure that user has space for the transaction
if (freeSlots() > 0)
{
//Delete money
deleteItem(995, Slot, totPrice);
//Add item
addItem(itemID, 1);
//Remove 1 item from stock of store
server.shopHandler.ShopItemsN[MyShopID][fromSlot]--;
server.shopHandler.ShopItemsDelay[MyShopID][fromSlot] = 0;
if ((fromSlot + 1) > server.shopHandler.ShopItemsStandard[MyShopID])
server.shopHandler.ShopItems[MyShopID][fromSlot] = 0;
}
else
{
CAM().sendMessage("Not enough space in your inventory.");
resetShopVars();
return false;
}
}
else
{
CAM().sendMessage("You don't have enough coins.");
resetShopVars();
return false;
}
}
}
resetShopVars();
return true;
}
private void resetShopVars()
{
//Ideally these 3 method calls would be grouped somehow
//but I don't know your code
resetItems(3823);
resetShop(MyShopID);
UpdatePlayerShop();
}
I'd also just like to say that if this method isn't ever used to determine if a transaction is fully successful (because I don't see a practical application of this), I suggest changing the method to a void. There isn't really any sense in determining if a user ran out of money while buying shop items, and this method should never be used in a loop.
The code you posted caused nil problems, but it didn't seem to work any greater than my old code :/
I am having this class:
Code:
package server.players.Packets;
import server.players.client;
public class EnterXAmount {
public void parseUsage(client player, int interfaceRoot, int amount) {
switch (interfaceRoot) {
case 5064:
deposit(player, amount);
break;
case 5382:
withdraw(player, amount);
break;
case 3322:
offer(player, amount);
break;
case 3415:
remove(player, amount);
break;
case 3900:
buy(player, amount);
break;
}
}
private void deposit(client c, int amount) {
c.bankItem(c.playerItems[c.XremoveSlot], c.XremoveSlot, amount);
}
private void withdraw(client c, int amount) {
c.fromBank(c.bankItems[c.XremoveSlot], c.XremoveSlot, amount);
}
private void offer(client c, int amount) {
c.getTrade().tradeItem(c.XremoveID, c.XremoveSlot, amount);
}
private void remove(client c, int amount) {
c.getTrade().fromTrade(c.XremoveID, c.XremoveSlot, amount);
}
private void buy(client c, int amount) {
c.buyItem(c.XremoveID, c.XremoveSlot, amount);
}
/*private void sell(client c, int amount) {
c.sellItem(c.XremoveID, c.XremoveSlot, amount);
}*/ //Failed attemp of create X-selling .. meh.
}
Think that'll help at all?
I'm really glad for any help here