New Handler created for SELLOBJECT
This commit is contained in:
@@ -12,7 +12,6 @@ package engine.net.client;
|
||||
import engine.Enum.DispatchChannel;
|
||||
import engine.Enum.GameObjectType;
|
||||
import engine.Enum.ItemContainerType;
|
||||
import engine.Enum.ProtectionState;
|
||||
import engine.InterestManagement.WorldGrid;
|
||||
import engine.exception.MsgSendException;
|
||||
import engine.gameManager.*;
|
||||
@@ -36,7 +35,6 @@ import engine.util.StringUtils;
|
||||
import org.pmw.tinylog.Logger;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
@@ -1026,172 +1024,6 @@ public class ClientMessagePump implements NetMsgHandler {
|
||||
|
||||
dispatch = Dispatch.borrow(sourcePlayer, msg);
|
||||
DispatchMessage.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);
|
||||
|
||||
}
|
||||
|
||||
private static void sellToNPC(SellToNPCMsg msg, ClientConnection origin) {
|
||||
|
||||
PlayerCharacter player = SessionManager.getPlayerCharacter(origin);
|
||||
Dispatch dispatch;
|
||||
|
||||
if (player == null)
|
||||
return;
|
||||
|
||||
CharacterItemManager itemMan = player.charItemManager;
|
||||
|
||||
if (itemMan == null)
|
||||
return;
|
||||
|
||||
NPC npc = NPC.getFromCache(msg.getNPCID());
|
||||
|
||||
if (npc == null)
|
||||
return;
|
||||
|
||||
Item gold = itemMan.getGoldInventory();
|
||||
|
||||
if (gold == null)
|
||||
return;
|
||||
|
||||
if (origin.sellLock.tryLock()) {
|
||||
try {
|
||||
Item sell;
|
||||
int cost = 0;
|
||||
|
||||
|
||||
if (npc.charItemManager.getInventoryCount() > 150) {
|
||||
if (npc.getParentZone() != null && npc.getParentZone().playerCityUUID == 0) {
|
||||
ArrayList<Item> inv = npc.getInventory();
|
||||
for (int i = 0; i < 20; i++) {
|
||||
try {
|
||||
Item toRemove = inv.get(i);
|
||||
if (toRemove != null)
|
||||
npc.charItemManager.delete(toRemove);
|
||||
} catch (Exception e) {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Early exit sanity check
|
||||
|
||||
if (msg.getItemType() == GameObjectType.Item.ordinal() == false)
|
||||
return;
|
||||
|
||||
sell = Item.getFromCache(msg.getItemID());
|
||||
|
||||
if (sell == null)
|
||||
return;
|
||||
|
||||
//get item to sell
|
||||
|
||||
if (sell.template == null)
|
||||
return;
|
||||
|
||||
if (npc.getParentZone() != null && npc.getParentZone().playerCityUUID != 0)
|
||||
if (!npc.charItemManager.hasRoomInventory(sell.template.item_wt)) {
|
||||
|
||||
ErrorPopupMsg.sendErrorPopup(player, 21);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!sell.validForInventory(origin, player, itemMan))
|
||||
return;
|
||||
|
||||
//get goldItem cost to sell
|
||||
|
||||
|
||||
cost = sell.template.item_value;
|
||||
|
||||
//apply damaged value reduction
|
||||
float durabilityCurrent = (short) sell.durabilityCurrent;
|
||||
float durabilityMax = sell.template.item_health_full;
|
||||
float damagedModifier = durabilityCurrent / durabilityMax;
|
||||
cost *= damagedModifier;
|
||||
float bargain = player.getBargain();
|
||||
|
||||
float profit = npc.getBuyPercent(player) + bargain;
|
||||
|
||||
if (profit > 1)
|
||||
profit = 1;
|
||||
|
||||
|
||||
cost *= profit;
|
||||
|
||||
if (gold.getNumOfItems() + cost > 10000000) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (gold.getNumOfItems() + cost < 0)
|
||||
return;
|
||||
|
||||
//TODO make sure npc can buy item type
|
||||
//test room available for item on npc
|
||||
|
||||
// if (!npc.isStatic() && npc.getCharItemManager().getInventoryCount() > 150) {
|
||||
// // chatMan.chatSystemInfo(pc, "This vendor's inventory is full");
|
||||
// return;
|
||||
// }
|
||||
|
||||
//make sure item is in player inventory
|
||||
|
||||
Building building = (!npc.isStatic()) ? npc.getBuilding() : null;
|
||||
|
||||
if (building != null && building.getProtectionState().equals(ProtectionState.NPC))
|
||||
building = null;
|
||||
if (npc.getParentZone().playerCityUUID == 0)
|
||||
building = null;
|
||||
|
||||
//make sure npc can afford item
|
||||
|
||||
if (building != null && !building.hasFunds(cost)) {
|
||||
ErrorPopupMsg.sendErrorPopup(player, 17);
|
||||
return;
|
||||
}
|
||||
if (building != null && (building.getStrongboxValue() - cost) < 0) {
|
||||
ErrorPopupMsg.sendErrorPopup(player, 17);
|
||||
return;
|
||||
}
|
||||
|
||||
//TODO transfer item and goldItem transfer should be handled together incase failure
|
||||
//transfer the item
|
||||
|
||||
if (!itemMan.sellToNPC(sell, npc))
|
||||
return;
|
||||
|
||||
if (!itemMan.sellToNPC(building, cost, sell))
|
||||
return;
|
||||
|
||||
//handle goldItem transfer
|
||||
|
||||
if (sell == null)
|
||||
return;
|
||||
|
||||
// ***REFACTOR: SellToNpc sends this message, is this a duplicate?
|
||||
|
||||
//update player's goldItem count
|
||||
UpdateGoldMsg ugm = new UpdateGoldMsg(player);
|
||||
ugm.configure();
|
||||
|
||||
dispatch = Dispatch.borrow(player, ugm);
|
||||
DispatchMessage.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);
|
||||
|
||||
//send the sell message back to update player
|
||||
msg.setItemType(sell.getObjectType().ordinal());
|
||||
msg.setItemID(sell.getObjectUUID());
|
||||
msg.setUnknown01(cost); //not sure if this is correct
|
||||
|
||||
dispatch = Dispatch.borrow(player, msg);
|
||||
DispatchMessage.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);
|
||||
|
||||
} finally {
|
||||
origin.sellLock.unlock();
|
||||
}
|
||||
} else {
|
||||
ErrorPopupMsg.sendErrorPopup(player, 12);
|
||||
}
|
||||
}
|
||||
|
||||
private static void openBuyFromNPCWindow(BuyFromNPCWindowMsg msg, ClientConnection origin) {
|
||||
@@ -1542,9 +1374,6 @@ public class ClientMessagePump implements NetMsgHandler {
|
||||
case SHOPINFO:
|
||||
openSellToNPCWindow((SellToNPCWindowMsg) msg, origin);
|
||||
break;
|
||||
case SELLOBJECT:
|
||||
sellToNPC((SellToNPCMsg) msg, origin);
|
||||
break;
|
||||
case TRAINERLIST:
|
||||
WorldServer.trainerInfo((TrainerInfoMsg) msg, origin);
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user