Browse Source

Handler created for BuyFromNPCMsg

combat-2
MagicBot 8 months ago
parent
commit
c5c4c62208
  1. 223
      src/engine/net/client/ClientMessagePump.java
  2. 3
      src/engine/net/client/Protocol.java
  3. 261
      src/engine/net/client/handlers/BuyFromNPCMsgHandler.java

223
src/engine/net/client/ClientMessagePump.java

@ -1215,226 +1215,6 @@ public class ClientMessagePump implements NetMsgHandler { @@ -1215,226 +1215,6 @@ public class ClientMessagePump implements NetMsgHandler {
DispatchMessage.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);
}
private static void buyFromNPC(BuyFromNPCMsg msg, ClientConnection origin) {
PlayerCharacter sourcePlayer = SessionManager.getPlayerCharacter(origin);
if (sourcePlayer == null)
return;
if (origin.buyLock.tryLock()) {
try {
CharacterItemManager itemMan = sourcePlayer.getCharItemManager();
if (itemMan == null)
return;
NPC npc = NPC.getFromCache(msg.getNPCID());
if (npc == null)
return;
Item gold = itemMan.getGoldInventory();
if (gold == null)
return;
Item buy = null;
if (msg.getItemType() == GameObjectType.MobEquipment.ordinal()) {
ArrayList<MobEquipment> sellInventory = npc.getContract().getSellInventory();
if (sellInventory == null)
return;
for (MobEquipment me : sellInventory) {
if (me.getObjectUUID() == msg.getItemID()) {
//test room available for item
if (!itemMan.hasRoomInventory(me.template.item_wt))
return;
int cost = me.getMagicValue();
float bargain = sourcePlayer.getBargain();
float profit = npc.getSellPercent(sourcePlayer) - bargain;
if (profit < 1)
profit = 1;
cost *= profit;
if (gold.getNumOfItems() - cost < 0) {
//dont' have enough goldItem exit!
// chatMan.chatSystemInfo(pc, "" + "You dont have enough gold.");
return;
}
Building building = (!npc.isStatic()) ? npc.getBuilding() : null;
if (building != null && building.getProtectionState().equals(ProtectionState.NPC))
building = null;
int buildingDeposit = cost - me.getMagicValue();
if (building != null && (building.getStrongboxValue() + buildingDeposit) > building.getMaxGold()) {
ErrorPopupMsg.sendErrorPopup(sourcePlayer, 206);
return;
}
if (!itemMan.buyFromNPC(building, cost, buildingDeposit)) {
// chatMan.chatSystemInfo(pc, "" + "You Failed to buy the item.");
return;
}
buy = Item.createItemForPlayer(sourcePlayer, me.templateID);
if (buy != null) {
me.transferEnchants(buy);
itemMan.addItemToInventory(buy);
//itemMan.updateInventory();
}
}
}
} else if (msg.getItemType() == GameObjectType.Item.ordinal()) {
CharacterItemManager npcCim = npc.getCharItemManager();
if (npcCim == null)
return;
buy = Item.getFromCache(msg.getItemID());
if (buy == null)
return;
if (!npcCim.inventoryContains(buy))
return;
//test room available for item
if (!itemMan.hasRoomInventory(buy.template.item_wt))
return;
//TODO test cost and subtract goldItem
//TODO CHnage this if we ever put NPc city npcs in buildings.
int cost = buy.template.item_value;
if (buy.isID() || buy.isCustomValue())
cost = buy.getMagicValue();
float bargain = sourcePlayer.getBargain();
float profit = npc.getSellPercent(sourcePlayer) - bargain;
if (profit < 1)
profit = 1;
if (!buy.isCustomValue())
cost *= profit;
else
cost = buy.getValue();
if (gold.getNumOfItems() - cost < 0) {
ErrorPopupMsg.sendErrorPopup(sourcePlayer, 128); // Insufficient Gold
return;
}
Building building = (!npc.isStatic()) ? npc.getBuilding() : null;
if (building != null)
if (building.getProtectionState().equals(ProtectionState.NPC))
building = null;
int buildingDeposit = cost;
if (building != null && (building.getStrongboxValue() + buildingDeposit) > building.getMaxGold()) {
ErrorPopupMsg.sendErrorPopup(sourcePlayer, 206);
return;
}
if (!itemMan.buyFromNPC(building, cost, buildingDeposit)) {
ErrorPopupMsg.sendErrorPopup(sourcePlayer, 110);
return;
}
if (buy != null)
itemMan.buyFromNPC(buy, npc);
} else if (msg.getItemType() == GameObjectType.MobLoot.ordinal()) {
CharacterItemManager npcCim = npc.getCharItemManager();
if (npcCim == null)
return;
buy = MobLoot.getFromCache(msg.getItemID());
if (buy == null)
return;
if (!npcCim.inventoryContains(buy))
return;
//test room available for item
if (!itemMan.hasRoomInventory(buy.template.item_wt))
return;
//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;
}
Building building = (!npc.isStatic()) ? npc.getBuilding() : null;
if (building != null && building.getProtectionState().equals(ProtectionState.NPC))
building = null;
int buildingDeposit = cost;
if (building != null && (building.getStrongboxValue() + buildingDeposit) > building.getMaxGold()) {
ErrorPopupMsg.sendErrorPopup(sourcePlayer, 206);
return;
}
if (!itemMan.buyFromNPC(building, cost, buildingDeposit))
return;
if (buy != null)
itemMan.buyFromNPC(buy, npc);
} else
return;
if (buy != null) {
msg.setItem(buy);
//send the buy message back to update player
// msg.setItemType(buy.getObjectType().ordinal());
// msg.setItemID(buy.getObjectUUID());
Dispatch dispatch = Dispatch.borrow(sourcePlayer, msg);
DispatchMessage.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);
itemMan.updateInventory();
}
} finally {
origin.buyLock.unlock();
}
} else {
ErrorPopupMsg.sendErrorPopup(origin.getPlayerCharacter(), 12); // All production slots taken
}
}
private static void Repair(RepairMsg msg, ClientConnection origin) {
PlayerCharacter player = SessionManager.getPlayerCharacter(origin);
@ -1847,9 +1627,6 @@ public class ClientMessagePump implements NetMsgHandler { @@ -1847,9 +1627,6 @@ public class ClientMessagePump implements NetMsgHandler {
case SHOPLIST:
openBuyFromNPCWindow((BuyFromNPCWindowMsg) msg, origin);
break;
case BUYFROMNPC:
buyFromNPC((BuyFromNPCMsg) msg, origin);
break;
case SHOPINFO:
openSellToNPCWindow((SellToNPCWindowMsg) msg, origin);
break;

3
src/engine/net/client/Protocol.java

@ -20,7 +20,6 @@ import java.util.HashMap; @@ -20,7 +20,6 @@ import java.util.HashMap;
public enum Protocol {
NONE(0x0, null, null),
ABANDONASSET(0xFDDBB233, AbandonAssetMsg.class, AbandonAssetMsgHandler.class), // AbandonAsset
ACTIVATECHARTER(0x296C0B22, UseCharterMsg.class, null),// Use Guild Charter
@ -55,7 +54,7 @@ public enum Protocol { @@ -55,7 +54,7 @@ public enum Protocol {
BANISHMEMBER(0x31AA3368, BanishUnbanishMsg.class, BanishUnbanishHandler.class), // Banish/Unbanish
BANKINVENTORY(0x32F3F503, ShowBankInventoryMsg.class, null), // ShowCombatInfo Bank Inventory
BREAKFEALTY(0x479A4C19, BreakFealtyMsg.class, BreakFealtyHandler.class),
BUYFROMNPC(0xA2B8DFA5, BuyFromNPCMsg.class, null), // Buy Item From NPC
BUYFROMNPC(0xA2B8DFA5, BuyFromNPCMsg.class, BuyFromNPCMsgHandler.class), // Buy Item From NPC
CANCELGUILDCREATION(0x385EA922, GuildCreationCloseMsg.class, GuildCreationCloseHandler.class), //Close the window
CHANGEALTITUDE(0x624F08BA, ChangeAltitudeMsg.class, ChangeAltitudeHandler.class), //Change Altitude
CHANGEGUILDLEADER(0xE40BC95D, ChangeGuildLeaderMsg.class, ChangeGuildLeaderHandler.class),

261
src/engine/net/client/handlers/BuyFromNPCMsgHandler.java

@ -0,0 +1,261 @@ @@ -0,0 +1,261 @@
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.net.client.handlers;
import engine.Enum;
import engine.Enum.DispatchChannel;
import engine.exception.MsgSendException;
import engine.gameManager.SessionManager;
import engine.net.Dispatch;
import engine.net.DispatchMessage;
import engine.net.client.ClientConnection;
import engine.net.client.msg.BuyFromNPCMsg;
import engine.net.client.msg.ClientNetMsg;
import engine.net.client.msg.ErrorPopupMsg;
import engine.objects.*;
import java.util.ArrayList;
public class BuyFromNPCMsgHandler extends AbstractClientMsgHandler {
public BuyFromNPCMsgHandler() {
super(BuyFromNPCMsg.class);
}
@Override
protected boolean _handleNetMsg(ClientNetMsg baseMsg, ClientConnection origin) throws MsgSendException {
// Member variable declaration
BuyFromNPCMsg buyFromNPCMsg;
// Member variable assignment
buyFromNPCMsg = (BuyFromNPCMsg) baseMsg;
PlayerCharacter sourcePlayer = SessionManager.getPlayerCharacter(origin);
if (sourcePlayer == null)
return true;
if (origin.buyLock.tryLock()) {
try {
CharacterItemManager itemMan = sourcePlayer.getCharItemManager();
if (itemMan == null)
return true;
NPC npc = NPC.getFromCache(buyFromNPCMsg.getNPCID());
if (npc == null)
return true;
Item gold = itemMan.getGoldInventory();
if (gold == null)
return true;
Item buy = null;
if (buyFromNPCMsg.getItemType() == Enum.GameObjectType.MobEquipment.ordinal()) {
ArrayList<MobEquipment> sellInventory = npc.getContract().getSellInventory();
if (sellInventory == null)
return true;
for (MobEquipment me : sellInventory) {
if (me.getObjectUUID() == buyFromNPCMsg.getItemID()) {
//test room available for item
if (!itemMan.hasRoomInventory(me.template.item_wt))
return true;
int cost = me.getMagicValue();
float bargain = sourcePlayer.getBargain();
float profit = npc.getSellPercent(sourcePlayer) - bargain;
if (profit < 1)
profit = 1;
cost *= profit;
if (gold.getNumOfItems() - cost < 0) {
//dont' have enough goldItem exit!
// chatMan.chatSystemInfo(pc, "" + "You dont have enough gold.");
return true;
}
Building building = (!npc.isStatic()) ? npc.getBuilding() : null;
if (building != null && building.getProtectionState().equals(Enum.ProtectionState.NPC))
building = null;
int buildingDeposit = cost - me.getMagicValue();
if (building != null && (building.getStrongboxValue() + buildingDeposit) > building.getMaxGold()) {
ErrorPopupMsg.sendErrorPopup(sourcePlayer, 206);
return true;
}
if (!itemMan.buyFromNPC(building, cost, buildingDeposit)) {
// chatMan.chatSystemInfo(pc, "" + "You Failed to buy the item.");
return true;
}
buy = Item.createItemForPlayer(sourcePlayer, me.templateID);
if (buy != null) {
me.transferEnchants(buy);
itemMan.addItemToInventory(buy);
//itemMan.updateInventory();
}
}
}
} else if (buyFromNPCMsg.getItemType() == Enum.GameObjectType.Item.ordinal()) {
CharacterItemManager npcCim = npc.getCharItemManager();
if (npcCim == null)
return true;
buy = Item.getFromCache(buyFromNPCMsg.getItemID());
if (buy == null)
return true;
if (!npcCim.inventoryContains(buy))
return true;
//test room available for item
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.template.item_value;
if (buy.isID() || buy.isCustomValue())
cost = buy.getMagicValue();
float bargain = sourcePlayer.getBargain();
float profit = npc.getSellPercent(sourcePlayer) - bargain;
if (profit < 1)
profit = 1;
if (!buy.isCustomValue())
cost *= profit;
else
cost = buy.getValue();
if (gold.getNumOfItems() - cost < 0) {
ErrorPopupMsg.sendErrorPopup(sourcePlayer, 128); // Insufficient Gold
return true;
}
Building building = (!npc.isStatic()) ? npc.getBuilding() : null;
if (building != null)
if (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)) {
ErrorPopupMsg.sendErrorPopup(sourcePlayer, 110);
return true;
}
if (buy != null)
itemMan.buyFromNPC(buy, npc);
} else if (buyFromNPCMsg.getItemType() == Enum.GameObjectType.MobLoot.ordinal()) {
CharacterItemManager npcCim = npc.getCharItemManager();
if (npcCim == null)
return true;
buy = MobLoot.getFromCache(buyFromNPCMsg.getItemID());
if (buy == null)
return true;
if (!npcCim.inventoryContains(buy))
return true;
//test room available for item
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 {
origin.buyLock.unlock();
}
} else {
ErrorPopupMsg.sendErrorPopup(origin.getPlayerCharacter(), 12); // All production slots taken
}
return true;
}
}
Loading…
Cancel
Save