diff --git a/src/engine/net/client/handlers/OrderNPCMsgHandler.java b/src/engine/net/client/handlers/OrderNPCMsgHandler.java index d115bdb0..c1f71219 100644 --- a/src/engine/net/client/handlers/OrderNPCMsgHandler.java +++ b/src/engine/net/client/handlers/OrderNPCMsgHandler.java @@ -1,5 +1,6 @@ package engine.net.client.handlers; +import engine.Enum; import engine.Enum.DispatchChannel; import engine.Enum.GameObjectType; import engine.Enum.ProfitType; @@ -36,6 +37,365 @@ public class OrderNPCMsgHandler extends AbstractClientMsgHandler { super(OrderNPCMsg.class); } + public static void processRedeedMob(Mob mob, Building building, ClientConnection origin) { + + PlayerCharacter player; + Contract contract; + CharacterItemManager itemMan; + ItemBase itemBase; + Item item; + + player = SessionManager.getPlayerCharacter(origin); + itemMan = player.getCharItemManager(); + + contract = mob.getContract(); + + if (!player.getCharItemManager().hasRoomInventory((short) 1)) { + ErrorPopupMsg.sendErrorPopup(player, 21); + return; + } + + if (!building.getHirelings().containsKey(mob)) + return; + + if (!NPCManager.removeMobileFromBuilding(mob, building)) { + PlaceAssetMsg.sendPlaceAssetError(player.getClientConnection(), 1, "A Serious error has occurred. Please post details for to ensure transaction integrity"); + return; + } + + building.getHirelings().remove(mob); + + itemBase = ItemBase.getItemBase(contract.getContractID()); + + if (itemBase == null) { + Logger.error("Could not find Contract for npc: " + mob.getObjectUUID()); + return; + } + + boolean itemWorked = false; + + item = new Item(itemBase, player.getObjectUUID(), Enum.OwnerType.PlayerCharacter, (byte) ((byte) mob.getRank() - 1), (byte) ((byte) mob.getRank() - 1), (short) 1, (short) 1, true, false, Enum.ItemContainerType.INVENTORY, (byte) 0, new ArrayList<>(), ""); + item.setNumOfItems(1); + item.containerType = Enum.ItemContainerType.INVENTORY; + + try { + item = DbManager.ItemQueries.ADD_ITEM(item); + itemWorked = true; + } catch (Exception e) { + Logger.error(e); + } + + if (itemWorked) { + itemMan.addItemToInventory(item); + itemMan.updateInventory(); + } + + ManageCityAssetsMsg mca = new ManageCityAssetsMsg(); + mca.actionType = NPC.SVR_CLOSE_WINDOW; + mca.setTargetType(building.getObjectType().ordinal()); + mca.setTargetID(building.getObjectUUID()); + origin.sendMsg(mca); + + } + + private static void modifyBuyProfit(OrderNPCMsg msg, ClientConnection origin) { + NPC npc; + PlayerCharacter player; + Building building; + float percent; + + ProfitType profitType = null; + player = origin.getPlayerCharacter(); + + if (player == null) + return; + + npc = NPC.getFromCache(msg.getNpcUUID()); + + if (npc == null) + return; + + building = npc.getBuilding(); + + if (building == null) + return; + + NPCProfits profit = NPC.GetNPCProfits(npc); + + if (profit == null) + return; + + switch (msg.getActionType()) { + case 10: + profitType = ProfitType.BuyNormal; + break; + case 11: + profitType = ProfitType.BuyGuild; + break; + case 12: + profitType = ProfitType.BuyNation; + } + + percent = msg.getBuySellPercent(); + percent = FastMath.clamp(percent, 0.0f, 1.0f); + + NPCProfits.UpdateProfits(npc, profit, profitType, percent); + } + + private static void modifySellProfit(OrderNPCMsg orderNPCMsg, ClientConnection origin) { + NPC npc; + PlayerCharacter player; + Building building; + float percent; + + ProfitType profitType = null; + + player = origin.getPlayerCharacter(); + + if (player == null) + return; + + npc = NPC.getFromCache(orderNPCMsg.getNpcUUID()); + + if (npc == null) + return; + + building = npc.getBuilding(); + + if (building == null) + return; + + NPCProfits profit = NPC.GetNPCProfits(npc); + + if (profit == null) + return; + + switch (orderNPCMsg.getActionType()) { + case 7: + profitType = ProfitType.SellNormal; + break; + case 8: + profitType = ProfitType.SellGuild; + break; + case 9: + profitType = ProfitType.SellNation; + } + + percent = orderNPCMsg.getBuySellPercent(); + + percent -= 1f; + percent = FastMath.clamp(percent, 0.0f, 3.0f); + + NPCProfits.UpdateProfits(npc, profit, profitType, percent); + } + + private static void handleCityCommand(OrderNPCMsg orderNpcMsg, PlayerCharacter player) { + + Building building = BuildingManager.getBuildingFromCache(orderNpcMsg.getBuildingUUID()); + + if (building == null) + return; + + if (ManageCityAssetMsgHandler.playerCanManageNotFriends(player, building) == false) + return; + + if (orderNpcMsg.getPatrolSize() >= 20) + Logger.info(player.getName() + " is attempting to add patrol points amount " + orderNpcMsg.getPatrolSize()); + + if (orderNpcMsg.getSentrySize() >= 20) + Logger.info(player.getName() + " is attempting to add patrol points amount " + orderNpcMsg.getSentryPoints()); + + if (orderNpcMsg.getPatrolPoints() != null) { + + if (!AddPatrolPoints(building.getObjectUUID(), orderNpcMsg.getPatrolPoints())) { + ErrorPopupMsg.sendErrorMsg(player, "Patrol Points must be placed on city zone."); + return; + } + + for (AbstractCharacter guard : building.getHirelings().keySet()) { + if (guard.getObjectType() == GameObjectType.Mob) + ((Mob) guard).setPatrolPointIndex(0); + } + } else if (building.getPatrolPoints() != null) + ClearPatrolPoints(building.getObjectUUID()); + + if (orderNpcMsg.getSentryPoints() != null) { + AddSentryPoints(building.getObjectUUID(), orderNpcMsg.getSentryPoints()); + } else if (building.getSentryPoints() != null) + ClearSentryPoints(building.getObjectUUID()); + + // Dispatch dispatch = Dispatch.borrow(pc, msg); + // DispatchMessage.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY); + + } + + private static void processUpgradeNPC(PlayerCharacter player, AbstractCharacter abstractCharacter) { + + Building building; + + switch (abstractCharacter.getObjectType()) { + + case NPC: + NPC npc = (NPC) abstractCharacter; + building = npc.getBuilding(); + + // Cannot upgrade an npc not within a building + + if (building == null) + return; + + City buildingCity = building.getCity(); + + if (buildingCity == null) { + npc.processUpgradeNPC(player); + return; + } + + buildingCity.transactionLock.writeLock().lock(); + + try { + npc.processUpgradeNPC(player); + } catch (Exception e) { + Logger.error(e); + } finally { + buildingCity.transactionLock.writeLock().unlock(); + } + break; + case Mob: + + Mob mob = (Mob) abstractCharacter; + building = mob.building; + + if (mob.building == null) + return; + + City mobCity = building.getCity(); + + if (mobCity == null) { + mob.processUpgradeMob(player); + return; + } + + mobCity.transactionLock.writeLock().lock(); + + try { + mob.processUpgradeMob(player); + } catch (Exception e) { + // TODO Auto-generated catch block + Logger.error(e); + } finally { + mobCity.transactionLock.writeLock().unlock(); + } + break; + } + } + + private static synchronized void processRedeedNPC(AbstractCharacter abstractCharacter, Building building, ClientConnection origin) { + + // Member variable declaration + + switch (abstractCharacter.getObjectType()) { + case NPC: + NPC npc = (NPC) abstractCharacter; + + Building cityBuilding = npc.getBuilding(); + + if (cityBuilding == null) + return; + + NPC.processRedeedNPC(npc, npc.building, origin); + break; + case Mob: + Mob mob = (Mob) abstractCharacter; + processRedeedMob(mob, mob.building, origin); + break; + } + } + + private static boolean AddPatrolPoints(int buildingID, ArrayList patrolPoints) { + + Building building = BuildingManager.getBuildingFromCache(buildingID); + + if (building == null) + return false; + + Zone zone = building.getParentZone(); + + if (zone == null) + return false; + + if (zone.getPlayerCityUUID() == 0) + return false; + + City city = building.getCity(); + + if (city == null) + return false; + + + //clear first. + + for (Vector3fImmutable point : patrolPoints) { + + if (city.isLocationOnCityZone(point) == false) { + return false; + } + + } + + DbManager.BuildingQueries.CLEAR_PATROL(buildingID); + + for (Vector3fImmutable point : patrolPoints) { + + if (!DbManager.BuildingQueries.ADD_TO_PATROL(buildingID, point)) + return false; + } + building.patrolPoints = patrolPoints; + return true; + } + + private static boolean AddSentryPoints(int buildingID, ArrayList sentryPoints) { + + Building building = BuildingManager.getBuildingFromCache(buildingID); + + if (building == null) + return false; + + building.sentryPoints = sentryPoints; + return true; + } + + private static boolean ClearPatrolPoints(int buildingID) { + + Building building = BuildingManager.getBuildingFromCache(buildingID); + + if (building == null) + return false; + + if (building.patrolPoints == null) + return true; + + if (DbManager.BuildingQueries.CLEAR_PATROL(buildingID) == false) + return false; + + building.patrolPoints.clear(); + return true; + } + + private static boolean ClearSentryPoints(int buildingID) { + + Building building = BuildingManager.getBuildingFromCache(buildingID); + + if (building == null) + return false; + + if (building.sentryPoints == null) + return true; + + building.sentryPoints.clear(); + return true; + } + @Override protected boolean _handleNetMsg(ClientNetMsg baseMsg, ClientConnection origin) throws MsgSendException { @@ -274,303 +634,4 @@ public class OrderNPCMsgHandler extends AbstractClientMsgHandler { return true; } - private static void modifyBuyProfit(OrderNPCMsg msg, ClientConnection origin) { - NPC npc; - PlayerCharacter player; - Building building; - float percent; - - ProfitType profitType = null; - player = origin.getPlayerCharacter(); - - if (player == null) - return; - - npc = NPC.getFromCache(msg.getNpcUUID()); - - if (npc == null) - return; - - building = npc.getBuilding(); - - if (building == null) - return; - - NPCProfits profit = NPC.GetNPCProfits(npc); - - if (profit == null) - return; - - switch (msg.getActionType()) { - case 10: - profitType = ProfitType.BuyNormal; - break; - case 11: - profitType = ProfitType.BuyGuild; - break; - case 12: - profitType = ProfitType.BuyNation; - } - - percent = msg.getBuySellPercent(); - percent = FastMath.clamp(percent, 0.0f, 1.0f); - - NPCProfits.UpdateProfits(npc, profit, profitType, percent); - } - - private static void modifySellProfit(OrderNPCMsg orderNPCMsg, ClientConnection origin) { - NPC npc; - PlayerCharacter player; - Building building; - float percent; - - ProfitType profitType = null; - - player = origin.getPlayerCharacter(); - - if (player == null) - return; - - npc = NPC.getFromCache(orderNPCMsg.getNpcUUID()); - - if (npc == null) - return; - - building = npc.getBuilding(); - - if (building == null) - return; - - NPCProfits profit = NPC.GetNPCProfits(npc); - - if (profit == null) - return; - - switch (orderNPCMsg.getActionType()) { - case 7: - profitType = ProfitType.SellNormal; - break; - case 8: - profitType = ProfitType.SellGuild; - break; - case 9: - profitType = ProfitType.SellNation; - } - - percent = orderNPCMsg.getBuySellPercent(); - - percent -= 1f; - percent = FastMath.clamp(percent, 0.0f, 3.0f); - - NPCProfits.UpdateProfits(npc, profit, profitType, percent); - } - - private static void handleCityCommand(OrderNPCMsg orderNpcMsg, PlayerCharacter player) { - - Building building = BuildingManager.getBuildingFromCache(orderNpcMsg.getBuildingUUID()); - - if (building == null) - return; - - if (ManageCityAssetMsgHandler.playerCanManageNotFriends(player, building) == false) - return; - - if (orderNpcMsg.getPatrolSize() >= 20) - Logger.info(player.getName() + " is attempting to add patrol points amount " + orderNpcMsg.getPatrolSize()); - - if (orderNpcMsg.getSentrySize() >= 20) - Logger.info(player.getName() + " is attempting to add patrol points amount " + orderNpcMsg.getSentryPoints()); - - if (orderNpcMsg.getPatrolPoints() != null) { - - if ( !AddPatrolPoints(building.getObjectUUID(), orderNpcMsg.getPatrolPoints())){ - ErrorPopupMsg.sendErrorMsg(player, "Patrol Points must be placed on city zone."); - return; - } - - for (AbstractCharacter guard : building.getHirelings().keySet()) { - if (guard.getObjectType() == GameObjectType.Mob) - ((Mob) guard).setPatrolPointIndex(0); - } - } else if (building.getPatrolPoints() != null) - ClearPatrolPoints(building.getObjectUUID()); - - if (orderNpcMsg.getSentryPoints() != null) { - AddSentryPoints(building.getObjectUUID(), orderNpcMsg.getSentryPoints()); - } else if (building.getSentryPoints() != null) - ClearSentryPoints(building.getObjectUUID()); - - // Dispatch dispatch = Dispatch.borrow(pc, msg); - // DispatchMessage.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY); - - } - - private static void processUpgradeNPC(PlayerCharacter player, AbstractCharacter abstractCharacter) { - - Building building; - - switch (abstractCharacter.getObjectType()) { - - case NPC: - NPC npc = (NPC) abstractCharacter; - building = npc.getBuilding(); - - // Cannot upgrade an npc not within a building - - if (building == null) - return; - - City buildingCity = building.getCity(); - - if (buildingCity == null) { - npc.processUpgradeNPC(player); - return; - } - - buildingCity.transactionLock.writeLock().lock(); - - try { - npc.processUpgradeNPC(player); - } catch (Exception e) { - Logger.error(e); - } finally { - buildingCity.transactionLock.writeLock().unlock(); - } - break; - case Mob: - - Mob mob = (Mob) abstractCharacter; - building = mob.building; - - if (mob.building == null) - return; - - City mobCity = building.getCity(); - - if (mobCity == null) { - mob.processUpgradeMob(player); - return; - } - - mobCity.transactionLock.writeLock().lock(); - - try { - mob.processUpgradeMob(player); - } catch (Exception e) { - // TODO Auto-generated catch block - Logger.error(e); - } finally { - mobCity.transactionLock.writeLock().unlock(); - } - break; - } - } - - private synchronized void processRedeedNPC(AbstractCharacter abstractCharacter, Building building, ClientConnection origin) { - - // Member variable declaration - - switch (abstractCharacter.getObjectType()) { - case NPC: - NPC npc = (NPC) abstractCharacter; - - Building cityBuilding = npc.getBuilding(); - - if (cityBuilding == null) - return; - - npc.processRedeedNPC(origin); - break; - case Mob: - Mob mob = (Mob) abstractCharacter; - mob.processRedeedMob(origin); - break; - } - } - - private static boolean AddPatrolPoints(int buildingID, ArrayList patrolPoints) { - - Building building = BuildingManager.getBuildingFromCache(buildingID); - - if (building == null) - return false; - - Zone zone = building.getParentZone(); - - if (zone == null) - return false; - - if (zone.getPlayerCityUUID() == 0) - return false; - - City city = building.getCity(); - - if (city == null) - return false; - - - - //clear first. - - for (Vector3fImmutable point : patrolPoints) { - - if (city.isLocationOnCityZone(point) == false){ - return false; - } - - } - - DbManager.BuildingQueries.CLEAR_PATROL(buildingID); - - for (Vector3fImmutable point : patrolPoints) { - - if (!DbManager.BuildingQueries.ADD_TO_PATROL(buildingID, point)) - return false; - } - building.patrolPoints = patrolPoints; - return true; - } - - private static boolean AddSentryPoints(int buildingID, ArrayList sentryPoints) { - - Building building = BuildingManager.getBuildingFromCache(buildingID); - - if (building == null) - return false; - - building.sentryPoints = sentryPoints; - return true; - } - - private static boolean ClearPatrolPoints(int buildingID) { - - Building building = BuildingManager.getBuildingFromCache(buildingID); - - if (building == null) - return false; - - if (building.patrolPoints == null) - return true; - - if (DbManager.BuildingQueries.CLEAR_PATROL(buildingID) == false) - return false; - - building.patrolPoints.clear(); - return true; - } - - private static boolean ClearSentryPoints(int buildingID) { - - Building building = BuildingManager.getBuildingFromCache(buildingID); - - if (building == null) - return false; - - if (building.sentryPoints == null) - return true; - - building.sentryPoints.clear(); - return true; - } - } \ No newline at end of file diff --git a/src/engine/objects/Mob.java b/src/engine/objects/Mob.java index b1685e88..78a96f81 100644 --- a/src/engine/objects/Mob.java +++ b/src/engine/objects/Mob.java @@ -24,9 +24,6 @@ import engine.math.Vector3fImmutable; import engine.net.ByteBufferWriter; import engine.net.Dispatch; import engine.net.DispatchMessage; -import engine.net.client.ClientConnection; -import engine.net.client.msg.ErrorPopupMsg; -import engine.net.client.msg.ManageCityAssetsMsg; import engine.net.client.msg.PetMsg; import engine.net.client.msg.PlaceAssetMsg; import engine.powers.EffectsBase; @@ -53,7 +50,7 @@ public class Mob extends AbstractIntelligenceAgent { //mob specific public final ConcurrentHashMap playerAgroMap = new ConcurrentHashMap<>(); public final ConcurrentHashMap siegeMinionMap = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW); - private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + public final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); public long nextCastTime = 0; public long nextCallForHelp = 0; public ReentrantReadWriteLock minionLock = new ReentrantReadWriteLock(); @@ -2145,76 +2142,6 @@ public class Mob extends AbstractIntelligenceAgent { } } - public void processRedeedMob(ClientConnection origin) { - - PlayerCharacter player; - Contract contract; - CharacterItemManager itemMan; - ItemBase itemBase; - Item item; - - this.lock.writeLock().lock(); - - try { - - player = SessionManager.getPlayerCharacter(origin); - itemMan = player.getCharItemManager(); - - contract = this.getContract(); - - if (!player.getCharItemManager().hasRoomInventory((short) 1)) { - ErrorPopupMsg.sendErrorPopup(player, 21); - return; - } - - if (!building.getHirelings().containsKey(this)) - return; - - if (!NPCManager.removeMobileFromBuilding(this, building)) { - PlaceAssetMsg.sendPlaceAssetError(player.getClientConnection(), 1, "A Serious error has occurred. Please post details for to ensure transaction integrity"); - return; - } - - building.getHirelings().remove(this); - - itemBase = ItemBase.getItemBase(contract.getContractID()); - - if (itemBase == null) { - Logger.error("Could not find Contract for npc: " + this.getObjectUUID()); - return; - } - - boolean itemWorked = false; - - item = new Item(itemBase, player.getObjectUUID(), OwnerType.PlayerCharacter, (byte) ((byte) this.getRank() - 1), (byte) ((byte) this.getRank() - 1), (short) 1, (short) 1, true, false, Enum.ItemContainerType.INVENTORY, (byte) 0, new ArrayList<>(), ""); - item.setNumOfItems(1); - item.containerType = Enum.ItemContainerType.INVENTORY; - - try { - item = DbManager.ItemQueries.ADD_ITEM(item); - itemWorked = true; - } catch (Exception e) { - Logger.error(e); - } - - if (itemWorked) { - itemMan.addItemToInventory(item); - itemMan.updateInventory(); - } - - ManageCityAssetsMsg mca = new ManageCityAssetsMsg(); - mca.actionType = NPC.SVR_CLOSE_WINDOW; - mca.setTargetType(building.getObjectType().ordinal()); - mca.setTargetID(building.getObjectUUID()); - origin.sendMsg(mca); - - } catch (Exception e) { - Logger.error(e); - } finally { - this.lock.writeLock().unlock(); - } - } - public void dismiss() { if (this.isPet()) { diff --git a/src/engine/objects/NPC.java b/src/engine/objects/NPC.java index ad79ad27..cea3cf73 100644 --- a/src/engine/objects/NPC.java +++ b/src/engine/objects/NPC.java @@ -53,7 +53,7 @@ public class NPC extends AbstractCharacter { protected boolean isMob; protected MobBase mobBase; protected String name; - protected Building building; + public Building building; protected Contract contract; protected int dbID; protected int currentID; @@ -99,10 +99,6 @@ public class NPC extends AbstractCharacter { public Vector3fImmutable inBuildingLoc = Vector3fImmutable.ZERO; private int repairCost = 5; - - public int classID = 0; - public int professionID = 0; - public int extraRune = 0; public int extraRune2 = 0; @@ -1716,7 +1712,7 @@ public class NPC extends AbstractCharacter { } } - public void processRedeedNPC( ClientConnection origin) { + public static void processRedeedNPC(NPC npc, Building building, ClientConnection origin) { // Member variable declaration PlayerCharacter player; @@ -1725,7 +1721,7 @@ public class NPC extends AbstractCharacter { ItemBase itemBase; Item item; - this.lock.writeLock().lock(); + npc.lock.writeLock().lock(); try{ @@ -1735,7 +1731,7 @@ public class NPC extends AbstractCharacter { player = SessionManager.getPlayerCharacter(origin); itemMan = player.getCharItemManager(); - contract = this.getContract(); + contract = npc.getContract(); if (!player.getCharItemManager().hasRoomInventory((short)1)){ ErrorPopupMsg.sendErrorPopup(player, 21); @@ -1743,26 +1739,26 @@ public class NPC extends AbstractCharacter { } - if (!building.getHirelings().containsKey(this)) + if (!building.getHirelings().containsKey(npc)) return; - if (!this.remove()) { + if (!npc.remove()) { PlaceAssetMsg.sendPlaceAssetError(player.getClientConnection(), 1, "A Serious error has occurred. Please post details for to ensure transaction integrity"); return; } - building.getHirelings().remove(this); + building.getHirelings().remove(npc); itemBase = ItemBase.getItemBase(contract.getContractID()); if (itemBase == null) { - Logger.error("Could not find Contract for npc: " + this.getObjectUUID()); + Logger.error("Could not find Contract for npc: " + npc.getObjectUUID()); return; } boolean itemWorked = false; - item = new Item( itemBase, player.getObjectUUID(), OwnerType.PlayerCharacter, (byte) ((byte) this.getRank() - 1), (byte) ((byte) this.getRank() - 1), + item = new Item( itemBase, player.getObjectUUID(), OwnerType.PlayerCharacter, (byte) ((byte) npc.getRank() - 1), (byte) ((byte) npc.getRank() - 1), (short) 1, (short) 1, true, false, Enum.ItemContainerType.INVENTORY, (byte) 0, new ArrayList<>(),""); item.setNumOfItems(1); @@ -1788,7 +1784,7 @@ public class NPC extends AbstractCharacter { }catch(Exception e){ Logger.error(e); }finally{ - this.lock.writeLock().unlock(); + npc.lock.writeLock().unlock(); } }