From d4e38c113be5c17114b62b028860d41c0ddf1066 Mon Sep 17 00:00:00 2001 From: MagicBot Date: Fri, 29 Mar 2024 08:23:53 -0400 Subject: [PATCH] Handler created for AceeptTradeMsg --- src/engine/gameManager/TradeManager.java | 16 --- src/engine/net/client/ClientMessagePump.java | 3 - src/engine/net/client/Protocol.java | 2 +- .../AcceptTradeRequestMsgHandler.java | 129 ++++++++++++++++++ src/engine/objects/CharacterItemManager.java | 91 +----------- 5 files changed, 132 insertions(+), 109 deletions(-) create mode 100644 src/engine/net/client/handlers/AcceptTradeRequestMsgHandler.java diff --git a/src/engine/gameManager/TradeManager.java b/src/engine/gameManager/TradeManager.java index c90204c7..29ba69e3 100644 --- a/src/engine/gameManager/TradeManager.java +++ b/src/engine/gameManager/TradeManager.java @@ -22,22 +22,6 @@ public enum TradeManager { TRADEMANAGER; - public static void acceptTradeRequest(AcceptTradeRequestMsg msg, ClientConnection origin) { - - PlayerCharacter source = origin.getPlayerCharacter(); - - if (source == null) - return; - - try { - source.charItemManager.acceptTradeRequest(msg); - } catch (Exception e) { - Logger.error(e); - // TODO Auto-generated catch block - } - - } - public static void rejectTradeRequest(RejectTradeRequestMsg msg, ClientConnection origin) { // TODO Do nothing? If so, delete this method & case above } diff --git a/src/engine/net/client/ClientMessagePump.java b/src/engine/net/client/ClientMessagePump.java index 27037d10..f8c05296 100644 --- a/src/engine/net/client/ClientMessagePump.java +++ b/src/engine/net/client/ClientMessagePump.java @@ -73,9 +73,6 @@ public class ClientMessagePump implements NetMsgHandler { break; case OPENVAULT: break; - case REQUESTTRADEOK: - TradeManager.acceptTradeRequest((AcceptTradeRequestMsg) msg, origin); - break; case REQUESTTRADECANCEL: TradeManager.rejectTradeRequest((RejectTradeRequestMsg) msg, origin); break; diff --git a/src/engine/net/client/Protocol.java b/src/engine/net/client/Protocol.java index f2343d4e..e5ec4fbf 100644 --- a/src/engine/net/client/Protocol.java +++ b/src/engine/net/client/Protocol.java @@ -179,7 +179,7 @@ public enum Protocol { REQUESTTOOPENBANK(0xF26E453F, null, null), // RequestToOpenBankMsg REQUESTTOTRADE(0x4D84259B, TradeRequestMsg.class, TradeRequestMsgHandler.class), // Trade Request REQUESTTRADECANCEL(0xCB0C5735, RejectTradeRequestMsg.class, null), // Reject RequestToTrade - REQUESTTRADEOK(0xFFD29841, AcceptTradeRequestMsg.class, null), // Accept Trade Request + REQUESTTRADEOK(0xFFD29841, AcceptTradeRequestMsg.class, AcceptTradeRequestMsgHandler.class), // Accept Trade Request RESETAFTERDEATH(0xFDCBB98F, RespawnMsg.class, RespawnMsgHandler.class), //Respawn Request/Response ROTATEMSG(0x57F2088E, RotateObjectMsg.class, null), SAFEMODE(0x9CF3922A, SafeModeMsg.class, null), //Tell client they're in safe mode diff --git a/src/engine/net/client/handlers/AcceptTradeRequestMsgHandler.java b/src/engine/net/client/handlers/AcceptTradeRequestMsgHandler.java new file mode 100644 index 00000000..5cc736b9 --- /dev/null +++ b/src/engine/net/client/handlers/AcceptTradeRequestMsgHandler.java @@ -0,0 +1,129 @@ +// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ . +// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌· +// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀ +// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌ +// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀ +// Magicbane Emulator Project © 2013 - 2022 +// www.magicbane.com + +package engine.net.client.handlers; + +import engine.Enum; +import engine.exception.MsgSendException; +import engine.net.Dispatch; +import engine.net.DispatchMessage; +import engine.net.client.ClientConnection; +import engine.net.client.msg.AcceptTradeRequestMsg; +import engine.net.client.msg.ClientNetMsg; +import engine.net.client.msg.OpenTradeWindowMsg; +import engine.net.client.msg.UpdateVaultMsg; +import engine.objects.Account; +import engine.objects.CharacterItemManager; +import engine.objects.PlayerCharacter; +import engine.server.MBServerStatics; + +import static engine.math.FastMath.sqr; +import static engine.objects.CharacterItemManager.canTrade; + +public class AcceptTradeRequestMsgHandler extends AbstractClientMsgHandler { + + public AcceptTradeRequestMsgHandler() { + super(AcceptTradeRequestMsg.class); + } + + @Override + protected boolean _handleNetMsg(ClientNetMsg baseMsg, ClientConnection origin) throws MsgSendException { + + // Member variable declaration + + AcceptTradeRequestMsg msg; + + // Member variable assignment + + msg = (AcceptTradeRequestMsg) baseMsg; + + PlayerCharacter source = (PlayerCharacter) origin.getPlayerCharacter(); + PlayerCharacter target = PlayerCharacter.getFromCache(msg.getTargetID()); + + Dispatch dispatch; + + if (source == null || !source.isAlive()) + return false; + + if (target == null || !target.isAlive()) + return false; + + if (source.charItemManager.tradingWith != null) + return false; + + if (!canTrade(source, target)) + return false; + + // verify characterTarget is in range + if (source.getLoc().distanceSquared2D(target.getLoc()) > sqr(MBServerStatics.TRADE_RANGE)) + return false; + + // TODO uncomment this block after we determine when we + // setBankOpen(false) and setVaultOpen(false) + /* + * CharacterItemManager cim1 = source.getCharItemManager(); + * CharacterItemManager cim2 = characterTarget.getCharItemManager(); if (cim1 == + * null) return false; if (cim2 == null) return false; if (cim1.isBankOpen()) + * return false; if (cim2.isVaultOpen()) return false; + */ + ClientConnection sourceConn = origin; + ClientConnection targetConn = target.getClientConnection(); + + if (sourceConn == null) + return false; + + if (targetConn == null) + return false; + + CharacterItemManager toTradeWith = target.charItemManager; + + if (toTradeWith == null) + return false; + + Account sourceAccount = source.getAccount(); + Account targetAccount = target.getAccount(); + + UpdateVaultMsg uvmSource = new UpdateVaultMsg(sourceAccount); + UpdateVaultMsg uvmTarget = new UpdateVaultMsg(targetAccount); + + dispatch = Dispatch.borrow(source, uvmSource); + DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.PRIMARY); + + dispatch = Dispatch.borrow(target, uvmTarget); + DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.PRIMARY); + + source.charItemManager.setVaultOpen(false); + toTradeWith.setVaultOpen(false); + source.charItemManager.setBankOpen(false); + toTradeWith.setBankOpen(false); + + OpenTradeWindowMsg otwm = new OpenTradeWindowMsg(msg.getUnknown01(), source, target); + + // Only start trade if both players aren't already trading with + // someone + + if (source.charItemManager.tradingWith != null || toTradeWith.getTradingWith() != null) + return false; + + source.charItemManager.initializeTrade(); + toTradeWith.initializeTrade(); + source.charItemManager.setTradingWith(targetConn); + toTradeWith.setTradingWith(sourceConn); + source.charItemManager.tradeID = msg.getUnknown01(); + toTradeWith.tradeID = msg.getUnknown01(); + + dispatch = Dispatch.borrow(source, otwm); + DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.PRIMARY); + + dispatch = Dispatch.borrow(target, otwm); + DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.PRIMARY); + + return true; + } + +} \ No newline at end of file diff --git a/src/engine/objects/CharacterItemManager.java b/src/engine/objects/CharacterItemManager.java index af4b5baa..fc07e1da 100644 --- a/src/engine/objects/CharacterItemManager.java +++ b/src/engine/objects/CharacterItemManager.java @@ -53,12 +53,12 @@ public class CharacterItemManager { private short inventoryWeight; private short equipWeight; private short vaultWeight; - private ClientConnection tradingWith; + public ClientConnection tradingWith; private byte tradeCommitted; private boolean tradeSuccess; private HashSet trading; private int goldTradingAmount; - private int tradeID = 0; + public int tradeID = 0; public CharacterItemManager(AbstractCharacter ac) { super(); @@ -444,93 +444,6 @@ public class CharacterItemManager { return true; } - public synchronized boolean acceptTradeRequest(AcceptTradeRequestMsg msg) { - - PlayerCharacter source = (PlayerCharacter) this.getOwner(); - PlayerCharacter target = PlayerCharacter.getFromCache(msg.getTargetID()); - - Dispatch dispatch; - - if (source == null || !source.isAlive()) - return false; - - if (target == null || !target.isAlive()) - return false; - - if (this.tradingWith != null) - return false; - - if (!canTrade(source, target)) - return false; - - // verify characterTarget is in range - if (source.getLoc().distanceSquared2D(target.getLoc()) > sqr(MBServerStatics.TRADE_RANGE)) - return false; - - // TODO uncomment this block after we determine when we - // setBankOpen(false) and setVaultOpen(false) - /* - * CharacterItemManager cim1 = source.getCharItemManager(); - * CharacterItemManager cim2 = characterTarget.getCharItemManager(); if (cim1 == - * null) return false; if (cim2 == null) return false; if (cim1.isBankOpen()) - * return false; if (cim2.isVaultOpen()) return false; - */ - ClientConnection sourceConn = source.getClientConnection(); - ClientConnection targetConn = target.getClientConnection(); - - if (sourceConn == null) - return false; - - if (targetConn == null) - return false; - - - CharacterItemManager toTradeWith = target.charItemManager; - - if (toTradeWith == null) - return false; - - Account sourceAccount = source.getAccount(); - Account targetAccount = target.getAccount(); - - UpdateVaultMsg uvmSource = new UpdateVaultMsg(sourceAccount); - UpdateVaultMsg uvmTarget = new UpdateVaultMsg(targetAccount); - - dispatch = Dispatch.borrow(source, uvmSource); - DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.PRIMARY); - - dispatch = Dispatch.borrow(target, uvmTarget); - DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.PRIMARY); - - this.setVaultOpen(false); - toTradeWith.setVaultOpen(false); - this.setBankOpen(false); - toTradeWith.setBankOpen(false); - - OpenTradeWindowMsg otwm = new OpenTradeWindowMsg(msg.getUnknown01(), source, target); - - // Only start trade if both players aren't already trading with - // someone - - if (this.getTradingWith() != null || toTradeWith.getTradingWith() != null) - return false; - - this.initializeTrade(); - toTradeWith.initializeTrade(); - this.setTradingWith(targetConn); - toTradeWith.setTradingWith(sourceConn); - this.tradeID = msg.getUnknown01(); - toTradeWith.tradeID = msg.getUnknown01(); - - dispatch = Dispatch.borrow(source, otwm); - DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.PRIMARY); - - dispatch = Dispatch.borrow(target, otwm); - DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.PRIMARY); - - return true; - } - public synchronized boolean addItemToTradeWindow(AddItemToTradeWindowMsg msg) { PlayerCharacter source = (PlayerCharacter) this.getOwner(); Dispatch dispatch;