|
|
@ -9,16 +9,14 @@ |
|
|
|
package engine.net.client.handlers; |
|
|
|
package engine.net.client.handlers; |
|
|
|
|
|
|
|
|
|
|
|
import engine.Enum; |
|
|
|
import engine.Enum; |
|
|
|
import engine.Enum.DispatchChannel; |
|
|
|
|
|
|
|
import engine.exception.MsgSendException; |
|
|
|
import engine.exception.MsgSendException; |
|
|
|
import engine.gameManager.SessionManager; |
|
|
|
import engine.gameManager.SessionManager; |
|
|
|
import engine.net.Dispatch; |
|
|
|
|
|
|
|
import engine.net.DispatchMessage; |
|
|
|
|
|
|
|
import engine.net.client.ClientConnection; |
|
|
|
import engine.net.client.ClientConnection; |
|
|
|
import engine.net.client.msg.BuyFromNPCMsg; |
|
|
|
import engine.net.client.msg.BuyFromNPCMsg; |
|
|
|
import engine.net.client.msg.ClientNetMsg; |
|
|
|
import engine.net.client.msg.ClientNetMsg; |
|
|
|
import engine.net.client.msg.ErrorPopupMsg; |
|
|
|
import engine.net.client.msg.ErrorPopupMsg; |
|
|
|
import engine.objects.*; |
|
|
|
import engine.objects.*; |
|
|
|
|
|
|
|
import org.pmw.tinylog.Logger; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
|
|
|
@ -39,6 +37,7 @@ public class BuyFromNPCMsgHandler extends AbstractClientMsgHandler { |
|
|
|
|
|
|
|
|
|
|
|
buyFromNPCMsg = (BuyFromNPCMsg) baseMsg; |
|
|
|
buyFromNPCMsg = (BuyFromNPCMsg) baseMsg; |
|
|
|
PlayerCharacter sourcePlayer = SessionManager.getPlayerCharacter(origin); |
|
|
|
PlayerCharacter sourcePlayer = SessionManager.getPlayerCharacter(origin); |
|
|
|
|
|
|
|
Item vendorItem; |
|
|
|
|
|
|
|
|
|
|
|
if (sourcePlayer == null) |
|
|
|
if (sourcePlayer == null) |
|
|
|
return true; |
|
|
|
return true; |
|
|
@ -61,21 +60,15 @@ public class BuyFromNPCMsgHandler extends AbstractClientMsgHandler { |
|
|
|
if (gold == null) |
|
|
|
if (gold == null) |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
|
|
Item buy = Item.getFromCache(buyFromNPCMsg.getItemID()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (buy == null) |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (buyFromNPCMsg.getItemType() == Enum.GameObjectType.Item.ordinal()) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ArrayList<Item> sellInventory = npc.getContract().getSellInventory(); |
|
|
|
ArrayList<Item> sellInventory = npc.getContract().getSellInventory(); |
|
|
|
|
|
|
|
|
|
|
|
if (sellInventory == null) |
|
|
|
if (sellInventory == null) |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (buyFromNPCMsg.getItemID() < 0) { |
|
|
|
for (Item me : sellInventory) { |
|
|
|
for (Item me : sellInventory) { |
|
|
|
|
|
|
|
|
|
|
|
if (me.templateID == buy.templateID) { |
|
|
|
if (me.objectUUID == buyFromNPCMsg.getItemID()) { |
|
|
|
|
|
|
|
|
|
|
|
//test room available for item
|
|
|
|
//test room available for item
|
|
|
|
if (!itemMan.hasRoomInventory(me.template.item_wt)) |
|
|
|
if (!itemMan.hasRoomInventory(me.template.item_wt)) |
|
|
@ -115,41 +108,37 @@ public class BuyFromNPCMsgHandler extends AbstractClientMsgHandler { |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
buy = Item.createItemForPlayer(sourcePlayer, me.templateID); |
|
|
|
vendorItem = Item.createItemForPlayer(sourcePlayer, me.templateID); |
|
|
|
|
|
|
|
|
|
|
|
if (buy != null) { |
|
|
|
if (vendorItem != null) { |
|
|
|
// me.transferEnchants(buy);
|
|
|
|
// me.transferEnchants(buy);
|
|
|
|
itemMan.addItemToInventory(buy); |
|
|
|
itemMan.addItemToInventory(vendorItem); |
|
|
|
//itemMan.updateInventory();
|
|
|
|
//itemMan.updateInventory();
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} else if (buyFromNPCMsg.getItemType() == Enum.GameObjectType.Item.ordinal()) { |
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
CharacterItemManager npcCim = npc.charItemManager; |
|
|
|
CharacterItemManager npcCim = npc.charItemManager; |
|
|
|
|
|
|
|
|
|
|
|
if (npcCim == null) |
|
|
|
if (npcCim == null) |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
|
|
buy = Item.getFromCache(buyFromNPCMsg.getItemID()); |
|
|
|
vendorItem = Item.getFromCache(buyFromNPCMsg.getItemID()); |
|
|
|
|
|
|
|
|
|
|
|
if (buy == null) |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!npcCim.inventoryContains(buy)) |
|
|
|
if (vendorItem == null) |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
|
|
//test room available for item
|
|
|
|
//test room available for item
|
|
|
|
if (!itemMan.hasRoomInventory(buy.template.item_wt)) |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//TODO test cost and subtract goldItem
|
|
|
|
if (!itemMan.hasRoomInventory(vendorItem.template.item_wt)) |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
|
|
//TODO CHnage this if we ever put NPc city npcs in buildings.
|
|
|
|
int cost = vendorItem.template.item_value; |
|
|
|
int cost = buy.template.item_value; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (buy.flags.contains(Enum.ItemFlags.Identified) || buy.isCustomValue()) |
|
|
|
if (vendorItem.flags.contains(Enum.ItemFlags.Identified) || vendorItem.isCustomValue()) |
|
|
|
cost = buy.getMagicValue(); |
|
|
|
cost = vendorItem.getMagicValue(); |
|
|
|
|
|
|
|
|
|
|
|
float bargain = sourcePlayer.getBargain(); |
|
|
|
float bargain = sourcePlayer.getBargain(); |
|
|
|
|
|
|
|
|
|
|
@ -158,11 +147,10 @@ public class BuyFromNPCMsgHandler extends AbstractClientMsgHandler { |
|
|
|
if (profit < 1) |
|
|
|
if (profit < 1) |
|
|
|
profit = 1; |
|
|
|
profit = 1; |
|
|
|
|
|
|
|
|
|
|
|
if (!buy.isCustomValue()) |
|
|
|
if (!vendorItem.isCustomValue()) |
|
|
|
cost *= profit; |
|
|
|
cost *= profit; |
|
|
|
else |
|
|
|
else |
|
|
|
cost = buy.getValue(); |
|
|
|
cost = vendorItem.getValue(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (gold.getNumOfItems() - cost < 0) { |
|
|
|
if (gold.getNumOfItems() - cost < 0) { |
|
|
|
ErrorPopupMsg.sendErrorPopup(sourcePlayer, 128); // Insufficient Gold
|
|
|
|
ErrorPopupMsg.sendErrorPopup(sourcePlayer, 128); // Insufficient Gold
|
|
|
@ -187,78 +175,15 @@ public class BuyFromNPCMsgHandler extends AbstractClientMsgHandler { |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (buy != null) |
|
|
|
if (vendorItem != null) |
|
|
|
itemMan.buyFromNPC(buy, npc); |
|
|
|
itemMan.buyFromNPC(vendorItem, npc); |
|
|
|
|
|
|
|
|
|
|
|
} else if (buyFromNPCMsg.getItemType() == Enum.GameObjectType.MobLoot.ordinal()) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CharacterItemManager npcCim = npc.charItemManager; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (npcCim == null) |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
buy = MobLoot.getFromCache(buyFromNPCMsg.getItemID()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (buy == null) |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!npcCim.inventoryContains(buy)) |
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
//test room available for item
|
|
|
|
Logger.error(e); |
|
|
|
if (!itemMan.hasRoomInventory(buy.template.item_wt)) |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//TODO test cost and subtract goldItem
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//TODO CHnage this if we ever put NPc city npcs in buildings.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cost = buy.getMagicValue(); |
|
|
|
|
|
|
|
cost *= npc.getSellPercent(sourcePlayer); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (gold.getNumOfItems() - cost < 0) { |
|
|
|
|
|
|
|
ErrorPopupMsg.sendErrorPopup(sourcePlayer, 128); // Insufficient Gold
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Building building = (!npc.isStatic()) ? npc.getBuilding() : null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (building != null && building.getProtectionState().equals(Enum.ProtectionState.NPC)) |
|
|
|
|
|
|
|
building = null; |
|
|
|
|
|
|
|
int buildingDeposit = cost; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (building != null && (building.getStrongboxValue() + buildingDeposit) > building.getMaxGold()) { |
|
|
|
|
|
|
|
ErrorPopupMsg.sendErrorPopup(sourcePlayer, 206); |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!itemMan.buyFromNPC(building, cost, buildingDeposit)) |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (buy != null) |
|
|
|
|
|
|
|
itemMan.buyFromNPC(buy, npc); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (buy != null) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
buyFromNPCMsg.setItem(buy); |
|
|
|
|
|
|
|
//send the buy message back to update player
|
|
|
|
|
|
|
|
// msg.setItemType(buy.getObjectType().ordinal());
|
|
|
|
|
|
|
|
// msg.setItemID(buy.getObjectUUID());
|
|
|
|
|
|
|
|
Dispatch dispatch = Dispatch.borrow(sourcePlayer, buyFromNPCMsg); |
|
|
|
|
|
|
|
DispatchMessage.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY); |
|
|
|
|
|
|
|
itemMan.updateInventory(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
origin.buyLock.unlock(); |
|
|
|
origin.buyLock.unlock(); |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
|
|
|
|
ErrorPopupMsg.sendErrorPopup(origin.getPlayerCharacter(), 12); // All production slots taken
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |