From 2768980f6ee2828bdd078ba575e5da3f3b279cfc Mon Sep 17 00:00:00 2001 From: MagicBot Date: Fri, 29 Mar 2024 08:44:12 -0400 Subject: [PATCH] Handler created for CommitToTradeMsg --- src/engine/gameManager/TradeManager.java | 22 ------ src/engine/net/client/ClientMessagePump.java | 8 +- src/engine/net/client/Protocol.java | 2 +- .../handlers/CommitToTradeMsgHandler.java | 77 +++++++++++++++++++ src/engine/objects/CharacterItemManager.java | 41 ---------- 5 files changed, 82 insertions(+), 68 deletions(-) create mode 100644 src/engine/net/client/handlers/CommitToTradeMsgHandler.java diff --git a/src/engine/gameManager/TradeManager.java b/src/engine/gameManager/TradeManager.java index ba5e524d..c8846855 100644 --- a/src/engine/gameManager/TradeManager.java +++ b/src/engine/gameManager/TradeManager.java @@ -13,7 +13,6 @@ import engine.net.Dispatch; import engine.net.DispatchMessage; import engine.net.client.ClientConnection; import engine.net.client.msg.CloseTradeWindowMsg; -import engine.net.client.msg.CommitToTradeMsg; import engine.net.client.msg.InvalidTradeRequestMsg; import engine.net.client.msg.UncommitToTradeMsg; import engine.objects.CharacterItemManager; @@ -24,26 +23,6 @@ public enum TradeManager { TRADEMANAGER; - public static void commitToTrade(CommitToTradeMsg msg, ClientConnection origin) { - - PlayerCharacter source = origin.getPlayerCharacter(); - - if (source == null || !source.isAlive()) - return; - - CharacterItemManager sourceItemMan = source.charItemManager; - - if (sourceItemMan == null) - return; - - try { - sourceItemMan.commitToTrade(msg); - } catch (Exception e) { - // TODO Auto-generated catch block - Logger.error(e); - } - } - public static void uncommitToTrade(UncommitToTradeMsg msg, ClientConnection origin) { PlayerCharacter source = origin.getPlayerCharacter(); @@ -65,7 +44,6 @@ public enum TradeManager { } - public static void closeTradeWindow(CloseTradeWindowMsg msg, ClientConnection origin) { PlayerCharacter source = origin.getPlayerCharacter(); diff --git a/src/engine/net/client/ClientMessagePump.java b/src/engine/net/client/ClientMessagePump.java index 89260301..194b2c96 100644 --- a/src/engine/net/client/ClientMessagePump.java +++ b/src/engine/net/client/ClientMessagePump.java @@ -13,7 +13,10 @@ import engine.gameManager.SessionManager; import engine.gameManager.TradeManager; import engine.net.NetMsgHandler; import engine.net.client.handlers.AbstractClientMsgHandler; -import engine.net.client.msg.*; +import engine.net.client.msg.ClientNetMsg; +import engine.net.client.msg.CloseTradeWindowMsg; +import engine.net.client.msg.InvalidTradeRequestMsg; +import engine.net.client.msg.UncommitToTradeMsg; import engine.server.world.WorldServer; import engine.session.Session; import engine.util.StringUtils; @@ -73,9 +76,6 @@ public class ClientMessagePump implements NetMsgHandler { break; case OPENVAULT: break; - case TRADECONFIRM: - TradeManager.commitToTrade((CommitToTradeMsg) msg, origin); - break; case TRADEUNCONFIRM: TradeManager.uncommitToTrade((UncommitToTradeMsg) msg, origin); break; diff --git a/src/engine/net/client/Protocol.java b/src/engine/net/client/Protocol.java index 5d7775ec..38b7f3e5 100644 --- a/src/engine/net/client/Protocol.java +++ b/src/engine/net/client/Protocol.java @@ -214,7 +214,7 @@ public enum Protocol { TRADEADDGOLD(0x654ACB45, AddGoldToTradeWindowMsg.class, AddGoldToTradeWindowMsgHandler.class), // Add Gold to Trade Window TRADEADDOBJECT(0x55D363E9, AddItemToTradeWindowMsg.class, AddItemToTradeWindowMsgHandler.class), // Add an Item to the Trade Window TRADECLOSE(0x5008D7FC, CloseTradeWindowMsg.class, null), // Cancel trade/ACK trade complete - TRADECONFIRM(0x6911E65E, CommitToTradeMsg.class, null), // Commit to trade + TRADECONFIRM(0x6911E65E, CommitToTradeMsg.class, CommitToTradeMsgHandler.class), // Commit to trade TRADECONFIRMSTATUS(0x9F85DAFC, null, null), // Other player commit/uncommit/add item TRADEUNCONFIRM(0xEBE280E0, UncommitToTradeMsg.class, null), // Uncommit to trade TRAINERLIST(0x41FABA62, TrainerInfoMsg.class, TrainerInfoMsgHandler.class), //Req/Send Trainer Info/Pricing diff --git a/src/engine/net/client/handlers/CommitToTradeMsgHandler.java b/src/engine/net/client/handlers/CommitToTradeMsgHandler.java new file mode 100644 index 00000000..81e890ae --- /dev/null +++ b/src/engine/net/client/handlers/CommitToTradeMsgHandler.java @@ -0,0 +1,77 @@ +// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ . +// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌· +// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀ +// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌ +// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀ +// Magicbane Emulator Project © 2013 - 2022 +// www.magicbane.com + +package engine.net.client.handlers; + +import engine.exception.MsgSendException; +import engine.net.client.ClientConnection; +import engine.net.client.msg.ClientNetMsg; +import engine.net.client.msg.CloseTradeWindowMsg; +import engine.net.client.msg.CommitToTradeMsg; +import engine.objects.CharacterItemManager; +import engine.objects.PlayerCharacter; + +import static engine.objects.CharacterItemManager.canTrade; + +public class CommitToTradeMsgHandler extends AbstractClientMsgHandler { + + public CommitToTradeMsgHandler() { + super(CommitToTradeMsg.class); + } + + @Override + protected boolean _handleNetMsg(ClientNetMsg baseMsg, ClientConnection origin) throws MsgSendException { + + PlayerCharacter source = origin.getPlayerCharacter(); + + // Member variable declaration + + CommitToTradeMsg msg; + + // Member variable assignment + + msg = (CommitToTradeMsg) baseMsg; + + if (source == null || !source.isAlive()) + return false; + + source.charItemManager.setTradeCommitted((byte) 1); + + ClientConnection ccOther = source.charItemManager.tradingWith; + + if (ccOther == null) + return false; + + PlayerCharacter other = ccOther.getPlayerCharacter(); + + if (other == null || !other.isAlive()) + return false; + + CharacterItemManager tradingWith = other.charItemManager; + + if (tradingWith == null) + return false; + + if (!canTrade(source, other)) + return false; + + source.charItemManager.modifyCommitToTrade(); + + if (source.charItemManager.getTradeCommitted() == (byte) 1 && tradingWith.getTradeCommitted() == (byte) 1) { + int tradeID = source.charItemManager.tradeID; + CloseTradeWindowMsg ctwm1 = new CloseTradeWindowMsg(source, tradeID); + CloseTradeWindowMsg ctwm2 = new CloseTradeWindowMsg(other, tradeID); + source.charItemManager.commitTrade(); + source.charItemManager.closeTradeWindow(ctwm1, false); + other.charItemManager.closeTradeWindow(ctwm2, false); + } + + return true; + } + +} \ No newline at end of file diff --git a/src/engine/objects/CharacterItemManager.java b/src/engine/objects/CharacterItemManager.java index 50e98e80..ca2fdda8 100644 --- a/src/engine/objects/CharacterItemManager.java +++ b/src/engine/objects/CharacterItemManager.java @@ -474,47 +474,6 @@ public class CharacterItemManager { return modifyCommitToTrade(); } - public synchronized boolean commitToTrade(CommitToTradeMsg msg) { - - PlayerCharacter source = (PlayerCharacter) this.getOwner(); - - if (source == null || !source.isAlive()) - return false; - - - this.setTradeCommitted((byte) 1); - - ClientConnection ccOther = this.getTradingWith(); - - if (ccOther == null) - return false; - - PlayerCharacter other = ccOther.getPlayerCharacter(); - - if (other == null || !other.isAlive()) - return false; - - CharacterItemManager tradingWith = other.charItemManager; - - if (tradingWith == null) - return false; - - if (!canTrade(source, other)) - return false; - - modifyCommitToTrade(); - - if (this.getTradeCommitted() == (byte) 1 && tradingWith.getTradeCommitted() == (byte) 1) { - int tradeID = this.tradeID; - CloseTradeWindowMsg ctwm1 = new CloseTradeWindowMsg(source, tradeID); - CloseTradeWindowMsg ctwm2 = new CloseTradeWindowMsg(other, tradeID); - this.commitTrade(); - this.closeTradeWindow(ctwm1, false); - other.charItemManager.closeTradeWindow(ctwm2, false); - } - return true; - } - public synchronized boolean modifyCommitToTrade() { CharacterItemManager man1 = this;