Prevent buying items far from merchant.

This commit is contained in:
MobiusDevelopment
2019-11-24 20:06:45 +00:00
parent fb2307337c
commit 8bb1a15199
3 changed files with 67 additions and 33 deletions

View File

@@ -54,8 +54,7 @@ public class MerchantInstance extends NpcInstance
final TradeList list = TradeController.getInstance().getBuyList(val);
if (list != null)
{
final BuyList bl = new BuyList(list, player.getAdena());
player.sendPacket(bl);
player.sendPacket(new BuyList(list, player.getAdena()));
}
else
{
@@ -66,8 +65,7 @@ public class MerchantInstance extends NpcInstance
private void showSellWindow(PlayerInstance player)
{
final SellList sl = new SellList(player);
player.sendPacket(sl);
player.sendPacket(new SellList(player));
player.sendPacket(new ActionFailed());
}
@@ -77,8 +75,7 @@ public class MerchantInstance extends NpcInstance
super.onBypassFeedback(player, command);
if (command.startsWith("Buy"))
{
final int val = Integer.parseInt(command.substring(4));
showBuyWindow(player, val);
showBuyWindow(player, Integer.parseInt(command.substring(4)));
}
else if (command.equals("Sell"))
{

View File

@@ -17,15 +17,14 @@
*/
package org.l2jmobius.gameserver.network.clientpackets;
import java.io.IOException;
import org.l2jmobius.gameserver.data.ItemTable;
import org.l2jmobius.gameserver.data.TradeController;
import org.l2jmobius.gameserver.model.TradeList;
import org.l2jmobius.gameserver.model.WorldObject;
import org.l2jmobius.gameserver.model.actor.instance.ItemInstance;
import org.l2jmobius.gameserver.model.actor.instance.MerchantInstance;
import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import org.l2jmobius.gameserver.network.ClientThread;
import org.l2jmobius.gameserver.network.Connection;
import org.l2jmobius.gameserver.network.serverpackets.ItemList;
import org.l2jmobius.gameserver.network.serverpackets.StatusUpdate;
import org.l2jmobius.gameserver.network.serverpackets.SystemMessage;
@@ -34,54 +33,89 @@ public class RequestBuyItem extends ClientBasePacket
{
private static final String _C__1F_REQUESTBUYITEM = "[C] 1F RequestBuyItem";
public RequestBuyItem(byte[] decrypt, ClientThread client) throws IOException
public RequestBuyItem(byte[] decrypt, ClientThread client)
{
super(decrypt);
int i;
final int listId = readD();
final int count = readD();
final ItemInstance[] items = new ItemInstance[count];
for (int i2 = 0; i2 < count; ++i2)
for (int i = 0; i < count; ++i)
{
final int itemId = readD();
final int cnt = readD();
final ItemInstance inst = ItemTable.getInstance().createItem(itemId);
inst.setCount(cnt);
items[i2] = inst;
items[i] = inst;
}
final PlayerInstance activeChar = client.getActiveChar();
final Connection con = client.getConnection();
double neededMoney = 0.0;
final long currentMoney = activeChar.getAdena();
final TradeList list = TradeController.getInstance().getBuyList(listId);
for (i = 0; i < items.length; ++i)
final PlayerInstance player = client.getActiveChar();
// Prevent buying items far from merchant.
if (!(player.getTarget() instanceof MerchantInstance))
{
final double count2 = items[i].getCount();
final int id = items[i].getItemId();
return;
}
boolean found = false;
for (WorldObject object : player.getKnownObjects())
{
if ((object instanceof MerchantInstance) && (player.calculateDistance2D(object) < 250))
{
found = true;
break;
}
}
if (!found)
{
return;
}
double neededMoney = 0;
final long currentMoney = player.getAdena();
final TradeList list = TradeController.getInstance().getBuyList(listId);
for (ItemInstance item : items)
{
final double itemCount = item.getCount();
final int id = item.getItemId();
int price = list.getPriceForItemId(id);
if (price == -1)
{
_log.warning("ERROR, no price found .. wrong buylist ??");
price = 1000000;
}
neededMoney += Math.abs(count2) * price;
neededMoney += Math.abs(itemCount) * price;
}
if ((neededMoney > currentMoney) || (neededMoney < 0.0) || (currentMoney <= 0L))
if ((neededMoney > currentMoney) || (neededMoney < 0) || (currentMoney <= 0))
{
final SystemMessage sm = new SystemMessage(SystemMessage.YOU_NOT_ENOUGH_ADENA);
con.sendPacket(sm);
player.sendPacket(new SystemMessage(SystemMessage.YOU_NOT_ENOUGH_ADENA));
return;
}
activeChar.reduceAdena((int) neededMoney);
for (i = 0; i < items.length; ++i)
player.reduceAdena((int) neededMoney);
final SystemMessage sma = new SystemMessage(SystemMessage.DISSAPEARED_ADENA);
sma.addNumber((int) neededMoney);
player.sendPacket(sma);
for (ItemInstance item : items)
{
activeChar.getInventory().addItem(items[i]);
player.getInventory().addItem(item);
if (item.getCount() > 1)
{
final SystemMessage sm = new SystemMessage(SystemMessage.EARNED_S2_S1_S);
sm.addItemName(item.getItemId());
sm.addNumber(item.getCount());
player.sendPacket(sm);
}
else
{
final SystemMessage sm = new SystemMessage(SystemMessage.EARNED_ITEM);
sm.addItemName(item.getItemId());
player.sendPacket(sm);
}
}
final ItemList il = new ItemList(activeChar, false);
con.sendPacket(il);
final StatusUpdate su = new StatusUpdate(activeChar.getObjectId());
su.addAttribute(StatusUpdate.CUR_LOAD, activeChar.getCurrentLoad());
activeChar.sendPacket(su);
player.sendPacket(new ItemList(player, false));
final StatusUpdate su = new StatusUpdate(player.getObjectId());
su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
player.sendPacket(su);
}
@Override

View File

@@ -41,6 +41,8 @@ public class SystemMessage extends ServerBasePacket
public static final int CRITICAL_HIT = 44;
public static final int USE_S1 = 46;
public static final int S1_EQUIPPED = 49;
public static final int EARNED_S2_S1_S = 53;
public static final int EARNED_ITEM = 54;
public static final int NOTHING_HAPPENED = 61;
public static final int S1_INVITED_YOU_TO_PARTY = 66;
public static final int EFFECT_S1_DISAPPEARED = 92;
@@ -109,6 +111,7 @@ public class SystemMessage extends ServerBasePacket
public static final int S1_INVITED_YOU_TO_PARTY_FINDER_KEEPER = 572;
public static final int S1_INVITED_YOU_TO_PARTY_RANDOM = 573;
public static final int S1_S2 = 614;
public static final int DISSAPEARED_ADENA = 672;
public static final int OTHER_PARTY_IS_DROZEN = 692;
public static final int THE_PURCHASE_IS_COMPLETE = 700;
public static final int THE_PURCHASE_PRICE_IS_HIGHER_THAN_MONEY = 720;