Buy/sell x buying loop lagging issue fix general shop using fixes trading amount fix (#133)

* Update NpcHandler.class

Fixed the diagonal safefpotting combat bug. The NPC will move when it detects that the player is standing diagonally to it.

Still have some issue around objects but that's another story.

* Update NpcHandler.java

Fixed the diagonal safefpotting combat bug. The NPC will move when it detects that the player is standing diagonally to it.

Still have some issue around objects but that's another story.

closes #47

* Buy/Sell-X--Buying-loop-lagging-issue-fix--General-shop-using-fixes--Trading-amount-fix

- Added buy/sell X feature
- Fixed a lag with the buying loop when buying high amount. Instead of spam buying 1 at a  times it divide the amount to buy in equal parts and buy in increments.
- Cant remember at all I'll edit after eating.
This commit is contained in:
Gptaqbc
2019-11-03 22:02:39 -05:00
committed by Daniel Ginovker
parent dcf6cbdc45
commit b02950045c
8 changed files with 200 additions and 112 deletions
@@ -7,7 +7,6 @@ import redone.game.items.ItemDefinitions;
import redone.game.players.Client;
import redone.game.players.Player;
import redone.game.players.PlayerHandler;
import redone.net.ActionSender;
import redone.util.GameLogger;
@@ -68,13 +67,13 @@ public class ShopAssistant {
public void resetShop(int ShopID) {
synchronized (player) {
int TotalItems = 0;
for (int i = 0; i < ShopHandler.MaxShopItems; i++) {
for (int i = 0; i < ShopHandler.MaxShopItems; i++) { //adds items in store when items are sold until max value.
if (ShopHandler.ShopItems[ShopID][i] > 0) {
TotalItems++;
}
}
if (TotalItems > ShopHandler.MaxShopItems) {
TotalItems = ShopHandler.MaxShopItems;
TotalItems = ShopHandler.MaxShopItems; //sets the stack of item sold to max value if the resulting amount is higher than max value.
}
player.getOutStream().createFrameVarSizeWord(53);
player.getOutStream().writeWord(3900);
@@ -334,6 +333,7 @@ public class ShopAssistant {
}
public boolean sellItem(int itemID, int fromSlot, int amount) {
player.getItemAssistant();
for (int i : Constants.ITEM_SELLABLE) {
if (i == itemID) {
player.getItemAssistant();
@@ -364,14 +364,11 @@ public class ShopAssistant {
return false;
}
}
if (amount > player.playerItemsN[fromSlot] && (ItemDefinitions.getDef()[player.playerItems[fromSlot] - 1].isNoteable == true || ItemDefinitions.getDef()[player.playerItems[fromSlot] - 1].isStackable == true)) {
amount = player.playerItemsN[fromSlot];
} else if (amount > player.getItemAssistant().getItemAmount(itemID) && ItemDefinitions.getDef()[player.playerItems[fromSlot] - 1].isNoteable == false && ItemDefinitions.getDef()[player.playerItems[fromSlot] - 1].isStackable == false) {
amount = player.getItemAssistant().getItemAmount(itemID);
}
// double ShopValue;
// double TotPrice;
String itemName = ItemAssistant.getItemName(itemID).toLowerCase();
int TotPrice2 = 0;
if (player.myShopId == 138 || player.myShopId == 58 || player.myShopId == 139) {
@@ -381,9 +378,13 @@ public class ShopAssistant {
}
if (player.getItemAssistant().freeSlots() > 0 || player.getItemAssistant().playerHasItem(995) || player.getItemAssistant().playerHasItem(6529)) { //Checks to see if player has room for coins.
if (!ItemDefinitions.getDef()[itemID].isNoteable) { //Check to see if its notable.
player.getItemAssistant().deleteItem(itemID, player.getItemAssistant().getItemSlot(itemID), amount); //don't really understand if the item isn't notable why it still needs to find the slot.
player.getItemAssistant().deleteItem2(itemID, amount);
} else {
player.getItemAssistant().deleteItem(itemID, fromSlot, amount);
player.getItemAssistant().deleteItem2(itemID, amount);
String ItemNameUnNotedItem = ItemAssistant.getItemName(itemID - 1).toLowerCase();
if (itemName.contains(ItemNameUnNotedItem)) {
itemID = itemID - 1; //Replace the noted item by it's un-noted version.
}
}
if (player.myShopId == 138 || player.myShopId == 139 || player.myShopId == 58) {
player.getItemAssistant().addItem(6529, TotPrice2); //Add the tokkul to your inventory.
@@ -401,7 +402,7 @@ public class ShopAssistant {
player.getItemAssistant().resetItems(3823);
resetShop(player.myShopId);
updatePlayerShop();
player.getActionSender().sendMessage("You sold " + amount + " " +itemName + " for " + TotPrice2 + " gp." );
player.getActionSender().sendMessage("You sold " + amount + " " +itemName + " for " + TotPrice2 + " coins." );
return true;
}
return true;
@@ -437,10 +438,50 @@ public class ShopAssistant {
private static final int FISHING_ITEMS[] = {383, 371, 377, 359, 321, 341, 353, 345, 327, 317};
public boolean buyItem(int itemID, int fromSlot, int amount) {
int iValue = 0;
int boughtQty = 0;
boolean boughtItem = false;
if (amount > 0) {
if (amount > ShopHandler.ShopItemsN[player.myShopId][fromSlot]) {
//S4
if (ShopHandler.ShopItemsN[player.myShopId][fromSlot] == 0) {
player.getActionSender().sendMessage("You can't buy that right now!");
return false;
}
if (amount > ShopHandler.ShopItemsN[player.myShopId][fromSlot] && ShopHandler.ShopItemsN[player.myShopId][fromSlot] > 0) {
amount = ShopHandler.ShopItemsN[player.myShopId][fromSlot];
}
if (amount % 23 == 0) {
amount = amount / 23;
iValue = 23; }
else if (amount % 19 == 0) {
amount = amount / 19;
iValue = 19;
} else if (amount % 17 == 0) {
amount = amount / 17;
iValue = 17;
} else if (amount % 13 == 0) {
amount = amount / 13;
iValue = 13;
} else if (amount % 11 == 0) {
amount = amount / 11;
iValue = 11;
} else if (amount % 7 == 0) {
amount = amount / 7;
iValue = 7;
} else if (amount % 5 == 0) {
amount = amount / 5;
iValue = 5;
}
else if (amount % 3 == 0) {
amount = amount / 3;
iValue = 3;
} else if (amount % 2 == 0) {
amount = amount / 2;
iValue = 2;
} else{
iValue = 1;
}
if(!player.isShopping) {
return false;
}
@@ -456,15 +497,13 @@ public class ShopAssistant {
if (!shopSellsItem(itemID)) {
return false;
}
// double ShopValue;
// double TotPrice;
int TotPrice2 = 0;
// int Overstock;
int Slot = 0;
int tokkulSlot = 0;// Tokkul
int TotPrice2 = 0; //ShopPrice
int RemainingToBuy; //Remaining of item to buy to fill the order. It's the remaining that can't fit in the loop. It has to be processed by itself after the loop.
int Slot = 0; //gp (995)
int tokkulSlot = 0;
int rangeSlot = 0;
int castleSlot = 0;
for (int i = amount; i > 0; i--) {
for (int i = amount; iValue > 0; iValue--) {
if (player.myShopId != 138 && player.myShopId != 58 && player.myShopId != 139 && player.myShopId != RANGE_SHOP && player.myShopId != PEST_SHOP && player.myShopId != CASTLE_SHOP) {
TotPrice2 = (int) Math.floor(getItemShopValue(itemID, 0, false));
} else if (player.myShopId == 138 || player.myShopId == 58 || player.myShopId == 139) {
@@ -577,34 +616,93 @@ public class ShopAssistant {
}
}
} else {
if (player.playerItemsN[Slot] >= TotPrice2) {
if (player.getItemAssistant().freeSlots() > 0 || (player.getItemAssistant().playerHasItem(itemID) && ItemDefinitions.getDef()[itemID].isStackable)) {
if (player.playerItemsN[Slot] >= TotPrice2 * amount) {
if (player.getItemAssistant().freeSlots() >= amount || (player.getItemAssistant().playerHasItem(itemID) && ItemDefinitions.getDef()[itemID].isStackable) || player.getItemAssistant().freeSlots() >= 1 && ItemDefinitions.getDef()[itemID].isStackable) {
player.getItemAssistant().deleteItem(995,
player.getItemAssistant().getItemSlot(995),
TotPrice2);
player.getItemAssistant().addItem(itemID, 1);
ShopHandler.ShopItemsN[player.myShopId][fromSlot] -= 1;
ShopHandler.ShopItemsDelay[player.myShopId][fromSlot] = 0;
TotPrice2 * amount);
player.getItemAssistant().addItem(itemID, amount); //All of these actions are performed in a loop. We are in the loop right now.
boughtQty+=amount;
ShopHandler.ShopItemsN[player.myShopId][fromSlot] -= amount; //Delete X item from shop at the slot the item is.
ShopHandler.ShopItemsDelay[player.myShopId][fromSlot] = 0; //Shit ass delay
if (fromSlot + 1 > ShopHandler.ShopItemsStandard[player.myShopId]) {
ShopHandler.ShopItems[player.myShopId][fromSlot] = 0;
ShopHandler.ShopItems[player.myShopId][fromSlot] = itemID + 1;
}
} else {
player.getActionSender()
.sendMessage(
"You don't have enough space in your inventory.");
if (player.getItemAssistant().freeSlots() == 0) {
player.getActionSender().sendMessage(
"You don't have enough space in your inventory.");
} else {
//Buys the remaining item to fill the inventory slots.
RemainingToBuy = player.getItemAssistant().freeSlots();
amount = RemainingToBuy;
player.getItemAssistant().deleteItem(995,
player.getItemAssistant().getItemSlot(995),
TotPrice2 * amount);
player.getItemAssistant().addItem(itemID, amount);
boughtQty+=amount;
ShopHandler.ShopItemsN[player.myShopId][fromSlot] -= amount;
ShopHandler.ShopItemsDelay[player.myShopId][fromSlot] = 0;
if (fromSlot + 1 > ShopHandler.ShopItemsStandard[player.myShopId]) {
ShopHandler.ShopItems[player.myShopId][fromSlot] = itemID + 1;
}
}
break;
}
boughtItem = true;
} else {
player.getActionSender().sendMessage(
"You don't have enough coins.");
break;
if (player.playerItemsN[Slot] / TotPrice2 > 0) {
amount = (int)Math.floor(player.playerItemsN[Slot] / TotPrice2);
} else {
player.getActionSender().sendMessage("You don't have enough coins.");
player.getItemAssistant().resetItems(3823);
resetShop(player.myShopId);
updatePlayerShop();
return false;
}
if (player.getItemAssistant().freeSlots() >= amount || (player.getItemAssistant().playerHasItem(itemID) && ItemDefinitions.getDef()[itemID].isStackable) || player.getItemAssistant().freeSlots() >= 1 && ItemDefinitions.getDef()[itemID].isStackable) {
player.getItemAssistant().deleteItem(995,
player.getItemAssistant().getItemSlot(995),
TotPrice2 * amount);
player.getItemAssistant().addItem(itemID, amount); //All of these actions are performed in a loop. We are in the loop right now.
boughtQty+=amount;
ShopHandler.ShopItemsN[player.myShopId][fromSlot] -= amount; //Delete X item from shop at the slot the item is.
ShopHandler.ShopItemsDelay[player.myShopId][fromSlot] = 0; //Shit ass delay
if (fromSlot + 1 > ShopHandler.ShopItemsStandard[player.myShopId]) {
ShopHandler.ShopItems[player.myShopId][fromSlot] = itemID + 1;
}
} else {
if (player.getItemAssistant().freeSlots() == 0) {
player.getActionSender().sendMessage(
"You don't have enough space in your inventory.");
} else {
//Buys the remaining item to fill the inventory slots.
RemainingToBuy = player.getItemAssistant().freeSlots();
amount = RemainingToBuy;
player.getItemAssistant().deleteItem(995,
player.getItemAssistant().getItemSlot(995),
TotPrice2 * amount);
player.getItemAssistant().addItem(itemID, amount);
boughtQty+=amount;
ShopHandler.ShopItemsN[player.myShopId][fromSlot] -= amount;
ShopHandler.ShopItemsDelay[player.myShopId][fromSlot] = 0;
if (fromSlot + 1 > ShopHandler.ShopItemsStandard[player.myShopId]) {
ShopHandler.ShopItems[player.myShopId][fromSlot] = itemID + 1;
}
}
break;
}
boughtItem = true;
}
}
}
if (boughtItem) {
player.getActionSender().sendMessage("You bought " + boughtQty + " " + ItemAssistant.getItemName(itemID).toLowerCase() + " for " + TotPrice2 * boughtQty + " coins." );
}
player.getItemAssistant().resetItems(3823);
resetShop(player.myShopId);
updatePlayerShop();
return true;
return true; //return TRUE / FALSE Update = shop&Inventory / Doesnt Update
}
return false;
}