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

View File

@@ -17,15 +17,14 @@
*/ */
package org.l2jmobius.gameserver.network.clientpackets; package org.l2jmobius.gameserver.network.clientpackets;
import java.io.IOException;
import org.l2jmobius.gameserver.data.ItemTable; import org.l2jmobius.gameserver.data.ItemTable;
import org.l2jmobius.gameserver.data.TradeController; import org.l2jmobius.gameserver.data.TradeController;
import org.l2jmobius.gameserver.model.TradeList; 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.ItemInstance;
import org.l2jmobius.gameserver.model.actor.instance.MerchantInstance;
import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import org.l2jmobius.gameserver.network.ClientThread; 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.ItemList;
import org.l2jmobius.gameserver.network.serverpackets.StatusUpdate; import org.l2jmobius.gameserver.network.serverpackets.StatusUpdate;
import org.l2jmobius.gameserver.network.serverpackets.SystemMessage; 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"; 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); super(decrypt);
int i;
final int listId = readD(); final int listId = readD();
final int count = readD(); final int count = readD();
final ItemInstance[] items = new ItemInstance[count]; 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 itemId = readD();
final int cnt = readD(); final int cnt = readD();
final ItemInstance inst = ItemTable.getInstance().createItem(itemId); final ItemInstance inst = ItemTable.getInstance().createItem(itemId);
inst.setCount(cnt); inst.setCount(cnt);
items[i2] = inst; items[i] = inst;
} }
final PlayerInstance activeChar = client.getActiveChar();
final Connection con = client.getConnection(); final PlayerInstance player = client.getActiveChar();
double neededMoney = 0.0;
final long currentMoney = activeChar.getAdena(); // Prevent buying items far from merchant.
final TradeList list = TradeController.getInstance().getBuyList(listId); if (!(player.getTarget() instanceof MerchantInstance))
for (i = 0; i < items.length; ++i)
{ {
final double count2 = items[i].getCount(); return;
final int id = items[i].getItemId(); }
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); int price = list.getPriceForItemId(id);
if (price == -1) if (price == -1)
{ {
_log.warning("ERROR, no price found .. wrong buylist ??"); _log.warning("ERROR, no price found .. wrong buylist ??");
price = 1000000; 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); player.sendPacket(new SystemMessage(SystemMessage.YOU_NOT_ENOUGH_ADENA));
con.sendPacket(sm);
return; return;
} }
activeChar.reduceAdena((int) neededMoney); player.reduceAdena((int) neededMoney);
for (i = 0; i < items.length; ++i) 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); player.sendPacket(new ItemList(player, false));
final StatusUpdate su = new StatusUpdate(activeChar.getObjectId()); final StatusUpdate su = new StatusUpdate(player.getObjectId());
su.addAttribute(StatusUpdate.CUR_LOAD, activeChar.getCurrentLoad()); su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
activeChar.sendPacket(su); player.sendPacket(su);
} }
@Override @Override

View File

@@ -41,6 +41,8 @@ public class SystemMessage extends ServerBasePacket
public static final int CRITICAL_HIT = 44; public static final int CRITICAL_HIT = 44;
public static final int USE_S1 = 46; public static final int USE_S1 = 46;
public static final int S1_EQUIPPED = 49; 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 NOTHING_HAPPENED = 61;
public static final int S1_INVITED_YOU_TO_PARTY = 66; public static final int S1_INVITED_YOU_TO_PARTY = 66;
public static final int EFFECT_S1_DISAPPEARED = 92; 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_FINDER_KEEPER = 572;
public static final int S1_INVITED_YOU_TO_PARTY_RANDOM = 573; public static final int S1_INVITED_YOU_TO_PARTY_RANDOM = 573;
public static final int S1_S2 = 614; 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 OTHER_PARTY_IS_DROZEN = 692;
public static final int THE_PURCHASE_IS_COMPLETE = 700; public static final int THE_PURCHASE_IS_COMPLETE = 700;
public static final int THE_PURCHASE_PRICE_IS_HIGHER_THAN_MONEY = 720; public static final int THE_PURCHASE_PRICE_IS_HIGHER_THAN_MONEY = 720;