From 2d246a67733d8e98dad08e66c7859470e457a8ed Mon Sep 17 00:00:00 2001 From: MagicBot Date: Wed, 27 Mar 2024 13:57:36 -0400 Subject: [PATCH] Handler created for TrainMsg --- src/engine/net/client/ClientMessagePump.java | 3 - src/engine/net/client/Protocol.java | 2 +- .../net/client/handlers/TrainMsgHandler.java | 289 ++++++++++++++++++ src/engine/net/client/msg/TrainMsg.java | 269 +--------------- 4 files changed, 298 insertions(+), 265 deletions(-) create mode 100644 src/engine/net/client/handlers/TrainMsgHandler.java diff --git a/src/engine/net/client/ClientMessagePump.java b/src/engine/net/client/ClientMessagePump.java index b0dd40e8..835232c8 100644 --- a/src/engine/net/client/ClientMessagePump.java +++ b/src/engine/net/client/ClientMessagePump.java @@ -853,9 +853,6 @@ public class ClientMessagePump implements NetMsgHandler { case ARCUNTRAINLIST: WorldServer.refinerScreen((RefinerScreenMsg) msg, origin); break; - case TRAINSKILL: - TrainMsg.train((TrainMsg) msg, origin); - break; case ARCUNTRAINABILITY: RefineMsg.refine((RefineMsg) msg, origin); break; diff --git a/src/engine/net/client/Protocol.java b/src/engine/net/client/Protocol.java index d9fb20c4..29eafe48 100644 --- a/src/engine/net/client/Protocol.java +++ b/src/engine/net/client/Protocol.java @@ -218,7 +218,7 @@ public enum Protocol { TRADECONFIRMSTATUS(0x9F85DAFC, null, null), // Other player commit/uncommit/add item TRADEUNCONFIRM(0xEBE280E0, UncommitToTradeMsg.class, null), // Uncommit to trade TRAINERLIST(0x41FABA62, TrainerInfoMsg.class, null), //Req/Send Trainer Info/Pricing - TRAINSKILL(0xB0BF68CD, TrainMsg.class, null), //Train skills/powers + TRAINSKILL(0xB0BF68CD, TrainMsg.class, TrainMsgHandler.class), //Train skills/powers TRANSFERASSET(0x3EA1C4C9, TransferAssetMsg.class, TransferAssetMsgHandler.class), // Transfer Building TRANSFERGOLDTOFROMBUILDING(0x1B1AC8C7, TransferGoldToFromBuildingMsg.class, TransferGoldToFromBuildingMsgHandler.class), // Transfer Gold to/From Building, Transfer Error TRANSFERITEMFROMBANK(0x9D04977B, TransferItemFromBankMsg.class, TransferItemFromBankMsgHandler.class), // Transfer Item from Bank to Inventory diff --git a/src/engine/net/client/handlers/TrainMsgHandler.java b/src/engine/net/client/handlers/TrainMsgHandler.java new file mode 100644 index 00000000..d8adf6de --- /dev/null +++ b/src/engine/net/client/handlers/TrainMsgHandler.java @@ -0,0 +1,289 @@ +// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ . +// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌· +// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀ +// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌ +// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀ +// Magicbane Emulator Project © 2013 - 2022 +// www.magicbane.com + +package engine.net.client.handlers; + +import engine.Enum; +import engine.exception.MsgSendException; +import engine.gameManager.ChatManager; +import engine.gameManager.DbManager; +import engine.gameManager.PowersManager; +import engine.gameManager.SessionManager; +import engine.net.Dispatch; +import engine.net.DispatchMessage; +import engine.net.client.ClientConnection; +import engine.net.client.msg.ClientNetMsg; +import engine.net.client.msg.ErrorPopupMsg; +import engine.net.client.msg.TrainMsg; +import engine.net.client.msg.TrainerInfoMsg; +import engine.objects.*; +import engine.powers.PowersBase; +import engine.server.MBServerStatics; +import org.pmw.tinylog.Logger; + +import java.util.concurrent.ConcurrentHashMap; + +public class TrainMsgHandler extends AbstractClientMsgHandler { + + public TrainMsgHandler() { + super(TrainMsg.class); + } + + @Override + protected boolean _handleNetMsg(ClientNetMsg baseMsg, ClientConnection origin) throws MsgSendException { + + TrainMsg msg = (TrainMsg) baseMsg; + origin.getPlayerCharacter().recalculate(); + PlayerCharacter playerCharacter = SessionManager.getPlayerCharacter(origin); + Dispatch dispatch; + + if (playerCharacter == null) + return true; + + NPC npc = NPC.getFromCache(msg.npcID); + + if (npc == null) + return true; + + if (origin.trainLock.tryLock()) { + try { + Item gold = playerCharacter.charItemManager.getGoldInventory(); + + if (gold == null) + return true; + + if (!gold.validForInventory(origin, playerCharacter, playerCharacter.charItemManager)) + return true; + + boolean canTrain = false; + if (msg.isSkill) { + + //Get skill + SkillsBase sb = DbManager.SkillsBaseQueries.GET_BASE_BY_TOKEN(msg.token); + ConcurrentHashMap skills = playerCharacter.getSkills(); + + if (sb == null || skills == null) + return true; + + CharacterSkill sk = skills.get(sb.getName()); + + if (sk == null) + return true; + + if (sk.getSkillsBase().getToken() == 40661438) { + int maxValue = 15; + + + if (MaxSkills.MaxSkillsSet.get(252647) != null) + for (MaxSkills maxSkills : MaxSkills.MaxSkillsSet.get(252647)) { + if (maxSkills.getSkillToken() != sk.getToken()) + continue; + + if (maxSkills.getSkillLevel() > npc.getLevel()) + continue; + maxValue += maxSkills.getMaxSkillPercent(); + } + if (maxValue > sk.getModifiedAmountBeforeMods()) { + canTrain = true; + } + + } + if (canTrain == false) + if (npc.getContract() != null && npc.getContract().getExtraRune() != 0) { + int maxValue = 15; + + + if (MaxSkills.MaxSkillsSet.get(npc.getContract().getExtraRune()) != null) + for (MaxSkills maxSkills : MaxSkills.MaxSkillsSet.get(npc.getContract().getExtraRune())) { + if (maxSkills.getSkillToken() != sk.getToken()) + continue; + + if (maxSkills.getSkillLevel() > npc.getLevel()) + continue; + maxValue += maxSkills.getMaxSkillPercent(); + } + if (maxValue > sk.getModifiedAmountBeforeMods()) { + canTrain = true; + } + + + } + if (canTrain == false) { + int maxValue = 15; + if (MaxSkills.MaxSkillsSet.get(npc.getContractID()) != null) + for (MaxSkills maxSkills : MaxSkills.MaxSkillsSet.get(npc.getContractID())) { + if (maxSkills.getSkillToken() != sk.getToken()) + continue; + + if (maxSkills.getSkillLevel() > npc.getLevel()) + continue; + maxValue += maxSkills.getMaxSkillPercent(); + } + if (maxValue > sk.getModifiedAmountBeforeMods()) { + canTrain = true; + } + } + + if (canTrain == false) { + int maxValue = 15; + if (MaxSkills.MaxSkillsSet.get(npc.getContract().getClassID()) != null) + for (MaxSkills maxSkills : MaxSkills.MaxSkillsSet.get(npc.getContract().getClassID())) { + if (maxSkills.getSkillToken() != sk.getToken()) + continue; + + if (maxSkills.getSkillLevel() > npc.getLevel()) + continue; + maxValue += maxSkills.getMaxSkillPercent(); + } + if (maxValue > sk.getModifiedAmountBeforeMods()) { + canTrain = true; + } + } + + if (canTrain == false) { + int maxValue = 15; + if (MaxSkills.MaxSkillsSet.get(npc.extraRune2) != null) + for (MaxSkills maxSkills : MaxSkills.MaxSkillsSet.get(npc.getContract().getClassID())) { + if (maxSkills.getSkillToken() != sk.getToken()) + continue; + + if (maxSkills.getSkillLevel() > npc.getLevel()) + continue; + maxValue += maxSkills.getMaxSkillPercent(); + } + if (maxValue > sk.getModifiedAmountBeforeMods()) { + canTrain = true; + } + } + + if (canTrain == false) { + ChatManager.chatSystemError(playerCharacter, "NPC cannot train that skill any higher"); + return true; + } + + float cost = sk.getTrainingCost(playerCharacter, npc); + float profitCost = cost * npc.getSellPercent(playerCharacter); + + profitCost += .5f; + + if (profitCost > playerCharacter.charItemManager.getGoldInventory().getNumOfItems()) + return true; + + Building building = npc.getBuilding(); + + if (building != null && building.getProtectionState().equals(Enum.ProtectionState.NPC)) + building = null; + + if (building != null && building.getStrongboxValue() + (profitCost - cost) > building.getMaxGold()) { + ErrorPopupMsg.sendErrorPopup(playerCharacter, 206); + return true; + } + + if (playerCharacter.charItemManager.getGoldInventory().getNumOfItems() - profitCost < 0) + return true; + + if (playerCharacter.charItemManager.getGoldInventory().getNumOfItems() - profitCost > MBServerStatics.PLAYER_GOLD_LIMIT) + return true; + + //attempt to train + + if (sk.train(playerCharacter)) { + playerCharacter.charItemManager.buyFromNPC(building, (int) profitCost, (int) (profitCost - cost)); + + dispatch = Dispatch.borrow(playerCharacter, msg); + DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY); + + //update trainer window + + if (npc != null) { + TrainerInfoMsg tim = new TrainerInfoMsg(msg.npcType, msg.npcID, npc.getSellPercent(playerCharacter)); + tim.setTrainPercent(npc.getSellPercent(playerCharacter)); + dispatch = Dispatch.borrow(playerCharacter, tim); + DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY); + } + } + + } else { + //Get Power + int token = msg.token; + + if (MBServerStatics.POWERS_DEBUG) { + ChatManager.chatSayInfo(playerCharacter, "Training Power: " + + Integer.toHexString(msg.token) + " (" + msg.token + ')'); + System.out.println("Training Power: " + + Integer.toHexString(msg.token) + " (" + msg.token + ')'); + } + + PowersBase pb = PowersManager.getPowerByToken(token); + ConcurrentHashMap powers = playerCharacter.getPowers(); + + if (pb == null || powers == null) + return true; + + if (pb.isWeaponPower) + return true; + + CharacterPower cp = null; + + if (powers.containsKey(token)) + cp = powers.get(token); + + if (cp == null) + return true; + + //attempt to train + float cost = (int) cp.getTrainingCost(playerCharacter, npc); + float profitCost = cost * npc.getSellPercent(playerCharacter); + + profitCost += .5f; + + if (profitCost > playerCharacter.charItemManager.getGoldInventory().getNumOfItems()) + return true; + + Building building = npc.getBuilding(); + + if (building != null && building.getProtectionState().equals(Enum.ProtectionState.NPC)) + building = null; + + if (building != null && building.getStrongboxValue() + (profitCost - cost) > building.getMaxGold()) { + ErrorPopupMsg.sendErrorPopup(playerCharacter, 206); + return true; + } + + if (cp.train(playerCharacter)) { + + if (!playerCharacter.charItemManager.buyFromNPC(building, (int) profitCost, (int) (profitCost - cost))) + ChatManager.chatSystemError(playerCharacter, "Failed to Withdrawl gold from inventory. Contact CCR"); + + //train succeeded + + dispatch = Dispatch.borrow(playerCharacter, msg); + DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY); + + //update trainer window + + if (npc != null) { + TrainerInfoMsg tim = new TrainerInfoMsg(msg.npcType, msg.npcID, npc.getSellPercent(playerCharacter)); + tim.setTrainPercent(npc.getSellPercent(playerCharacter)); + dispatch = Dispatch.borrow(playerCharacter, tim); + DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY); + } + } + } + } catch (Exception e) { + Logger.error(e); + } finally { + origin.trainLock.unlock(); + } + + } + + return true; + } + +} \ No newline at end of file diff --git a/src/engine/net/client/msg/TrainMsg.java b/src/engine/net/client/msg/TrainMsg.java index 8e8cb7b7..7cd3213f 100644 --- a/src/engine/net/client/msg/TrainMsg.java +++ b/src/engine/net/client/msg/TrainMsg.java @@ -9,33 +9,22 @@ package engine.net.client.msg; -import engine.Enum; -import engine.Enum.ProtectionState; -import engine.exception.MsgSendException; -import engine.gameManager.ChatManager; -import engine.gameManager.DbManager; -import engine.gameManager.PowersManager; -import engine.gameManager.SessionManager; -import engine.net.*; -import engine.net.client.ClientConnection; +import engine.net.AbstractConnection; +import engine.net.ByteBufferReader; +import engine.net.ByteBufferWriter; import engine.net.client.Protocol; -import engine.objects.*; -import engine.powers.PowersBase; -import engine.server.MBServerStatics; -import org.pmw.tinylog.Logger; - -import java.util.concurrent.ConcurrentHashMap; +import engine.objects.NPC; public class TrainMsg extends ClientNetMsg { - private int npcType; - private int npcID; + public int npcType; + public int npcID; private int unknown01 = 1; private int trainCost01; //why two trainer costs? private int trainCost02; //why two trainer costs? - private boolean isSkill; //true: skill; false: power - private int token; + public boolean isSkill; //true: skill; false: power + public int token; private boolean unknown02 = true; private String ok = ""; private int unknown03 = 0; @@ -57,248 +46,6 @@ public class TrainMsg extends ClientNetMsg { super(Protocol.TRAINSKILL, origin, reader); } - public static void train(TrainMsg msg, ClientConnection origin) throws MsgSendException { - origin.getPlayerCharacter().recalculate(); - PlayerCharacter playerCharacter = SessionManager.getPlayerCharacter(origin); - Dispatch dispatch; - - if (playerCharacter == null) - return; - - NPC npc = NPC.getFromCache(msg.npcID); - - if (npc == null) - return; - - if (origin.trainLock.tryLock()) { - try { - Item gold = playerCharacter.charItemManager.getGoldInventory(); - - if (gold == null) - return; - - if (!gold.validForInventory(origin, playerCharacter, playerCharacter.charItemManager)) - return; - - boolean canTrain = false; - if (msg.isSkill) { - - //Get skill - SkillsBase sb = DbManager.SkillsBaseQueries.GET_BASE_BY_TOKEN(msg.token); - ConcurrentHashMap skills = playerCharacter.getSkills(); - - if (sb == null || skills == null) - return; - - CharacterSkill sk = skills.get(sb.getName()); - - if (sk == null) - return; - - if (sk.getSkillsBase().getToken() == 40661438) { - int maxValue = 15; - - - if (MaxSkills.MaxSkillsSet.get(252647) != null) - for (MaxSkills maxSkills : MaxSkills.MaxSkillsSet.get(252647)) { - if (maxSkills.getSkillToken() != sk.getToken()) - continue; - - if (maxSkills.getSkillLevel() > npc.getLevel()) - continue; - maxValue += maxSkills.getMaxSkillPercent(); - } - if (maxValue > sk.getModifiedAmountBeforeMods()) { - canTrain = true; - } - - } - if (canTrain == false) - if (npc.getContract() != null && npc.getContract().getExtraRune() != 0) { - int maxValue = 15; - - - if (MaxSkills.MaxSkillsSet.get(npc.getContract().getExtraRune()) != null) - for (MaxSkills maxSkills : MaxSkills.MaxSkillsSet.get(npc.getContract().getExtraRune())) { - if (maxSkills.getSkillToken() != sk.getToken()) - continue; - - if (maxSkills.getSkillLevel() > npc.getLevel()) - continue; - maxValue += maxSkills.getMaxSkillPercent(); - } - if (maxValue > sk.getModifiedAmountBeforeMods()) { - canTrain = true; - } - - - } - if (canTrain == false) { - int maxValue = 15; - if (MaxSkills.MaxSkillsSet.get(npc.getContractID()) != null) - for (MaxSkills maxSkills : MaxSkills.MaxSkillsSet.get(npc.getContractID())) { - if (maxSkills.getSkillToken() != sk.getToken()) - continue; - - if (maxSkills.getSkillLevel() > npc.getLevel()) - continue; - maxValue += maxSkills.getMaxSkillPercent(); - } - if (maxValue > sk.getModifiedAmountBeforeMods()) { - canTrain = true; - } - } - - if (canTrain == false) { - int maxValue = 15; - if (MaxSkills.MaxSkillsSet.get(npc.getContract().getClassID()) != null) - for (MaxSkills maxSkills : MaxSkills.MaxSkillsSet.get(npc.getContract().getClassID())) { - if (maxSkills.getSkillToken() != sk.getToken()) - continue; - - if (maxSkills.getSkillLevel() > npc.getLevel()) - continue; - maxValue += maxSkills.getMaxSkillPercent(); - } - if (maxValue > sk.getModifiedAmountBeforeMods()) { - canTrain = true; - } - } - - if (canTrain == false) { - int maxValue = 15; - if (MaxSkills.MaxSkillsSet.get(npc.extraRune2) != null) - for (MaxSkills maxSkills : MaxSkills.MaxSkillsSet.get(npc.getContract().getClassID())) { - if (maxSkills.getSkillToken() != sk.getToken()) - continue; - - if (maxSkills.getSkillLevel() > npc.getLevel()) - continue; - maxValue += maxSkills.getMaxSkillPercent(); - } - if (maxValue > sk.getModifiedAmountBeforeMods()) { - canTrain = true; - } - } - - if (canTrain == false) { - ChatManager.chatSystemError(playerCharacter, "NPC cannot train that skill any higher"); - return; - } - - float cost = sk.getTrainingCost(playerCharacter, npc); - float profitCost = cost * npc.getSellPercent(playerCharacter); - - profitCost += .5f; - if (profitCost > playerCharacter.charItemManager.getGoldInventory().getNumOfItems()) - return; - Building b = npc.getBuilding(); - if (b != null && b.getProtectionState().equals(ProtectionState.NPC)) - b = null; - - if (b != null && b.getStrongboxValue() + (profitCost - cost) > b.getMaxGold()) { - ErrorPopupMsg.sendErrorPopup(playerCharacter, 206); - return; - } - - if (playerCharacter.charItemManager.getGoldInventory().getNumOfItems() - profitCost < 0) - return; - - if (playerCharacter.charItemManager.getGoldInventory().getNumOfItems() - profitCost > MBServerStatics.PLAYER_GOLD_LIMIT) - return; - - - //attempt to train - if (sk.train(playerCharacter)) { - playerCharacter.charItemManager.buyFromNPC(b, (int) profitCost, (int) (profitCost - cost)); - - dispatch = Dispatch.borrow(playerCharacter, msg); - DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY); - - //update trainer window - - if (npc != null) { - TrainerInfoMsg tim = new TrainerInfoMsg(msg.npcType, msg.npcID, npc.getSellPercent(playerCharacter)); - tim.setTrainPercent(npc.getSellPercent(playerCharacter)); - dispatch = Dispatch.borrow(playerCharacter, tim); - DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY); - } - } - - } else { - //Get Power - int token = msg.token; - - if (MBServerStatics.POWERS_DEBUG) { - ChatManager.chatSayInfo(playerCharacter, "Training Power: " + - Integer.toHexString(msg.token) + " (" + msg.token + ')'); - System.out.println("Training Power: " + - Integer.toHexString(msg.token) + " (" + msg.token + ')'); - } - - PowersBase pb = PowersManager.getPowerByToken(token); - ConcurrentHashMap powers = playerCharacter.getPowers(); - if (pb == null || powers == null) - return; - - if (pb.isWeaponPower) - return; - CharacterPower cp = null; - if (powers.containsKey(token)) - cp = powers.get(token); - if (cp == null) - return; - - //attempt to train - float cost = (int) cp.getTrainingCost(playerCharacter, npc); - float profitCost = cost * npc.getSellPercent(playerCharacter); - profitCost += .5f; - if (profitCost > playerCharacter.charItemManager.getGoldInventory().getNumOfItems()) { - // ChatManager.chatSystemError(pc, "You do not have enough gold to train this skill."); - return; - } - - Building b = npc.getBuilding(); - - if (b != null && b.getProtectionState().equals(ProtectionState.NPC)) - b = null; - - if (b != null && b.getStrongboxValue() + (profitCost - cost) > b.getMaxGold()) { - ErrorPopupMsg.sendErrorPopup(playerCharacter, 206); - return; - } - if (cp.train(playerCharacter)) { - - if (!playerCharacter.charItemManager.buyFromNPC(b, (int) profitCost, (int) (profitCost - cost))) - ChatManager.chatSystemError(playerCharacter, "Failed to Withdrawl gold from inventory. Contact CCR"); - - //train succeeded - - dispatch = Dispatch.borrow(playerCharacter, msg); - DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY); - - //update trainer window - - if (npc != null) { - TrainerInfoMsg tim = new TrainerInfoMsg(msg.npcType, msg.npcID, npc.getSellPercent(playerCharacter)); - tim.setTrainPercent(npc.getSellPercent(playerCharacter)); - dispatch = Dispatch.borrow(playerCharacter, tim); - DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY); - } - } - } - } catch (Exception e) { - Logger.error(e); - } finally { - origin.trainLock.unlock(); - } - - - } - - - } - public static float getTrainPercent(NPC npc) { return 0f; }