diff --git a/src/engine/gameManager/TradeManager.java b/src/engine/gameManager/TradeManager.java index 9b19c7a8..1355aad2 100644 --- a/src/engine/gameManager/TradeManager.java +++ b/src/engine/gameManager/TradeManager.java @@ -21,21 +21,6 @@ public enum TradeManager { TRADEMANAGER; - public static void addItemToTradeWindow(AddItemToTradeWindowMsg msg, ClientConnection origin) { - - - PlayerCharacter source = origin.getPlayerCharacter(); - if (source == null || !source.isAlive()) - return; - try { - source.charItemManager.addItemToTradeWindow(msg); - - } catch (Exception e) { - Logger.error(e); - } - - } - public static void addGoldToTradeWindow(AddGoldToTradeWindowMsg msg, ClientConnection origin) { PlayerCharacter source = origin.getPlayerCharacter(); diff --git a/src/engine/net/client/ClientMessagePump.java b/src/engine/net/client/ClientMessagePump.java index 50028369..06b610a9 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 TRADEADDOBJECT: - TradeManager.addItemToTradeWindow((AddItemToTradeWindowMsg) msg, origin); - break; case TRADEADDGOLD: TradeManager.addGoldToTradeWindow((AddGoldToTradeWindowMsg) msg, origin); break; diff --git a/src/engine/net/client/Protocol.java b/src/engine/net/client/Protocol.java index e5ec4fbf..18dff99a 100644 --- a/src/engine/net/client/Protocol.java +++ b/src/engine/net/client/Protocol.java @@ -212,7 +212,7 @@ public enum Protocol { TERRITORYCHANGE(0x6B388C8C, TerritoryChangeMessage.class, null), //Hey rich, look what I found? :) TOGGLESITSTAND(0x624F3C0F, ToggleSitStandMsg.class, ToggleSitStandMsgHandler.class), //Toggle Sit/Stand TRADEADDGOLD(0x654ACB45, AddGoldToTradeWindowMsg.class, null), // Add Gold to Trade Window - TRADEADDOBJECT(0x55D363E9, AddItemToTradeWindowMsg.class, null), // Add an Item to the 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 TRADECONFIRMSTATUS(0x9F85DAFC, null, null), // Other player commit/uncommit/add item diff --git a/src/engine/net/client/handlers/AddItemToTradeWindowMsgHandler.java b/src/engine/net/client/handlers/AddItemToTradeWindowMsgHandler.java new file mode 100644 index 00000000..40d09e5a --- /dev/null +++ b/src/engine/net/client/handlers/AddItemToTradeWindowMsgHandler.java @@ -0,0 +1,106 @@ +// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ . +// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌· +// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀ +// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌ +// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀ +// 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.AddItemToTradeWindowMsg; +import engine.net.client.msg.ClientNetMsg; +import engine.net.client.msg.UpdateTradeWindowMsg; +import engine.objects.CharacterItemManager; +import engine.objects.Item; +import engine.objects.PlayerCharacter; + +import static engine.objects.CharacterItemManager.canTrade; + +public class AddItemToTradeWindowMsgHandler extends AbstractClientMsgHandler { + + public AddItemToTradeWindowMsgHandler() { + super(AddItemToTradeWindowMsg.class); + } + + @Override + protected boolean _handleNetMsg(ClientNetMsg baseMsg, ClientConnection origin) throws MsgSendException { + + // Member variable declaration + + AddItemToTradeWindowMsg msg; + + // Member variable assignment + + msg = (AddItemToTradeWindowMsg) baseMsg; + + PlayerCharacter source = origin.getPlayerCharacter(); + Dispatch dispatch; + + if (source == null || !source.isAlive()) + return false; + + 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; + + Item item = Item.getFromCache(msg.getItemID()); + + if (item == null) + return false; + + if (!source.charItemManager.doesCharOwnThisItem(item.getObjectUUID())) + return false; + + //can't add item to trade window twice + if (source.charItemManager.tradingContains(item)) + return false; + + //dupe check + if (!item.validForInventory(source.getClientConnection(), source, source.charItemManager)) + return false; + + if (!tradingWith.hasRoomTrade(item.template.item_wt)) { + dispatch = Dispatch.borrow(source, msg); + DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.PRIMARY); + return false; + } + + UpdateTradeWindowMsg utwm = new UpdateTradeWindowMsg(source, other); + + source.charItemManager.setTradeCommitted((byte) 0); + tradingWith.setTradeCommitted((byte) 0); + + source.charItemManager.addItemToTrade(item); + + dispatch = Dispatch.borrow(other, msg); + DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.PRIMARY); + + source.charItemManager.modifyCommitToTrade(); + + dispatch = Dispatch.borrow(other, utwm); + 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 fc07e1da..5e4b3bdd 100644 --- a/src/engine/objects/CharacterItemManager.java +++ b/src/engine/objects/CharacterItemManager.java @@ -444,71 +444,6 @@ public class CharacterItemManager { return true; } - public synchronized boolean addItemToTradeWindow(AddItemToTradeWindowMsg msg) { - PlayerCharacter source = (PlayerCharacter) this.getOwner(); - Dispatch dispatch; - - if (source == null || !source.isAlive()) - return false; - - - 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; - - Item i = Item.getFromCache(msg.getItemID()); - - if (i == null) - return false; - - if (!this.doesCharOwnThisItem(i.getObjectUUID())) - return false; - - //can't add item to trade window twice - if (this.tradingContains(i)) - return false; - - //dupe check - if (!i.validForInventory(source.getClientConnection(), source, this)) - return false; - - if (!tradingWith.hasRoomTrade(i.template.item_wt)) { - dispatch = Dispatch.borrow(source, msg); - DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.PRIMARY); - return false; - } - - UpdateTradeWindowMsg utwm = new UpdateTradeWindowMsg(source, other); - - this.setTradeCommitted((byte) 0); - tradingWith.setTradeCommitted((byte) 0); - - this.addItemToTrade(i); - - dispatch = Dispatch.borrow(other, msg); - DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.PRIMARY); - - modifyCommitToTrade(); - - dispatch = Dispatch.borrow(other, utwm); - DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.PRIMARY); - return true; - } - public synchronized boolean addGoldToTradeWindow(AddGoldToTradeWindowMsg msg) { PlayerCharacter source = (PlayerCharacter) this.getOwner(); @@ -648,7 +583,7 @@ public class CharacterItemManager { return true; } - private synchronized boolean modifyCommitToTrade() { + public synchronized boolean modifyCommitToTrade() { CharacterItemManager man1 = this; if (this.getTradingWith() == null)