From 364fb40e0bb7c7ee7c87c40357223f9df3be911d Mon Sep 17 00:00:00 2001 From: MagicBot Date: Sat, 20 Apr 2024 16:52:24 -0400 Subject: [PATCH] junk, complete and produce wrapped with same lock as warehouse. --- src/engine/gameManager/ForgeManager.java | 16 +- .../handlers/ItemProductionMsgHandler.java | 148 ++++++++++-------- 2 files changed, 89 insertions(+), 75 deletions(-) diff --git a/src/engine/gameManager/ForgeManager.java b/src/engine/gameManager/ForgeManager.java index b02ce744..6250ce78 100644 --- a/src/engine/gameManager/ForgeManager.java +++ b/src/engine/gameManager/ForgeManager.java @@ -14,10 +14,7 @@ import engine.loot.WorkOrder; import engine.mbEnums; import engine.net.DispatchMessage; import engine.net.client.msg.ItemProductionMsg; -import engine.objects.Item; -import engine.objects.ItemTemplate; -import engine.objects.NPC; -import engine.objects.Warehouse; +import engine.objects.*; import engine.powers.EffectsBase; import org.pmw.tinylog.Logger; @@ -108,6 +105,10 @@ public enum ForgeManager implements Runnable { if (validation_result != 0) return validation_result; + City city = workOrder.vendor.building.getCity(); + city.transactionLock.writeLock().lock(); + + try { // Configure this production run. workOrder.workOrderID = wordOrderCounter.incrementAndGet(); @@ -137,8 +138,11 @@ public enum ForgeManager implements Runnable { forge.add(workOrder); - Logger.info(workOrder.toString()); - + } catch (Exception e) { + Logger.error(e); + } finally { + city.transactionLock.writeLock().unlock(); + } return validation_result; } diff --git a/src/engine/net/client/handlers/ItemProductionMsgHandler.java b/src/engine/net/client/handlers/ItemProductionMsgHandler.java index 7c9116ef..48a7eea7 100644 --- a/src/engine/net/client/handlers/ItemProductionMsgHandler.java +++ b/src/engine/net/client/handlers/ItemProductionMsgHandler.java @@ -24,10 +24,7 @@ import engine.net.client.msg.ClientNetMsg; import engine.net.client.msg.ErrorPopupMsg; import engine.net.client.msg.ItemProductionMsg; import engine.net.client.msg.ManageNPCMsg; -import engine.objects.CharacterItemManager; -import engine.objects.Item; -import engine.objects.NPC; -import engine.objects.PlayerCharacter; +import engine.objects.*; import org.pmw.tinylog.Logger; import java.util.HashMap; @@ -132,44 +129,54 @@ public class ItemProductionMsgHandler extends AbstractClientMsgHandler { Item virtualItem = Item.getFromCache(itemUUID); WorkOrder workOrder = ForgeManager.itemWorkOrderLookup.get(virtualItem); - // Refresh the window. SetPrice is used only to force a window refresh. - // What controls items displayed are the contents of the workOrders. (ManageNPCMsg) + City city = workOrder.vendor.building.getCity(); + city.transactionLock.writeLock().lock(); - ItemProductionMsg outMsg = new ItemProductionMsg(workOrder.vendor.building, workOrder.vendor, virtualItem, mbEnums.ProductionActionType.CONFIRM_SETPRICE, true); - DispatchMessage.dispatchMsgToInterestArea(workOrder.vendor, outMsg, mbEnums.DispatchChannel.SECONDARY, 700, false, false); + try { - // Remove virtualItem from collections + // Refresh the window. SetPrice is used only to force a window refresh. + // What controls items displayed are the contents of the workOrders. (ManageNPCMsg) - workOrder.cooking.remove(virtualItem); - ForgeManager.itemWorkOrderLookup.remove(virtualItem); - DbManager.removeFromCache(virtualItem); + ItemProductionMsg outMsg = new ItemProductionMsg(workOrder.vendor.building, workOrder.vendor, virtualItem, mbEnums.ProductionActionType.CONFIRM_SETPRICE, true); + DispatchMessage.dispatchMsgToInterestArea(workOrder.vendor, outMsg, mbEnums.DispatchChannel.SECONDARY, 700, false, false); - // WorkOrder exists until all items are completed or junked - // this is to hold a reference to the virtual item for serialization (NPCMsgHandler) + // Remove virtualItem from collections - workOrder.slots_used = workOrder.slots_used - 1; + workOrder.cooking.remove(virtualItem); + ForgeManager.itemWorkOrderLookup.remove(virtualItem); + DbManager.removeFromCache(virtualItem); - if (workOrder.slots_used == 0) - ForgeManager.vendorWorkOrderLookup.get(workOrder.vendor).remove(workOrder); + // WorkOrder exists until all items are completed or junked + // this is to hold a reference to the virtual item for serialization (NPCMsgHandler) - // Persist item and add to vendor inventory + workOrder.slots_used = workOrder.slots_used - 1; - Item completedItem = DbManager.ItemQueries.PERSIST(virtualItem); + if (workOrder.slots_used == 0) + ForgeManager.vendorWorkOrderLookup.get(workOrder.vendor).remove(workOrder); - // Apply Item effects for Prefix and Suffix tokens + // Persist item and add to vendor inventory - completedItem.prefixToken = virtualItem.prefixToken; - completedItem.suffixToken = virtualItem.suffixToken; + Item completedItem = DbManager.ItemQueries.PERSIST(virtualItem); - ItemManager.applyItemEffects(completedItem); + // Apply Item effects for Prefix and Suffix tokens - vendor.charItemManager.addItemToInventory(completedItem); + completedItem.prefixToken = virtualItem.prefixToken; + completedItem.suffixToken = virtualItem.suffixToken; - ItemProductionMsg outMsg1 = new ItemProductionMsg(vendor.building, vendor, completedItem, mbEnums.ProductionActionType.DEPOSIT, true); - DispatchMessage.dispatchMsgToInterestArea(vendor, outMsg1, mbEnums.DispatchChannel.SECONDARY, 700, false, false); - ItemProductionMsg outMsg2 = new ItemProductionMsg(vendor.building, vendor, completedItem, mbEnums.ProductionActionType.CONFIRM_DEPOSIT, true); - DispatchMessage.dispatchMsgToInterestArea(vendor, outMsg2, mbEnums.DispatchChannel.SECONDARY, 700, false, false); + ItemManager.applyItemEffects(completedItem); + vendor.charItemManager.addItemToInventory(completedItem); + + ItemProductionMsg outMsg1 = new ItemProductionMsg(vendor.building, vendor, completedItem, mbEnums.ProductionActionType.DEPOSIT, true); + DispatchMessage.dispatchMsgToInterestArea(vendor, outMsg1, mbEnums.DispatchChannel.SECONDARY, 700, false, false); + ItemProductionMsg outMsg2 = new ItemProductionMsg(vendor.building, vendor, completedItem, mbEnums.ProductionActionType.CONFIRM_DEPOSIT, true); + DispatchMessage.dispatchMsgToInterestArea(vendor, outMsg2, mbEnums.DispatchChannel.SECONDARY, 700, false, false); + + } catch (Exception e) { + Logger.error(e); + } finally { + city.transactionLock.writeLock().unlock(); + } } private static void setItemPrice(int itemUUID, int itemPrice, NPC vendor, ClientConnection origin) { @@ -189,18 +196,18 @@ public class ItemProductionMsgHandler extends AbstractClientMsgHandler { return; if (!DbManager.ItemQueries.UPDATE_VALUE(targetItem, itemPrice)) { - ChatManager.chatInfoError(origin.getPlayerCharacter(), "Failed to set price! Contact CCR For help."); - return; - } + ChatManager.chatInfoError(origin.getPlayerCharacter(), "Failed to set price! Contact CCR For help."); + return; + } - targetItem.setValue(itemPrice); - outMsg = new ItemProductionMsg(vendor.getBuilding(), vendor, targetItem, mbEnums.ProductionActionType.DEPOSIT, true); - dispatch = Dispatch.borrow(player, outMsg); - DispatchMessage.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.SECONDARY); + targetItem.setValue(itemPrice); + outMsg = new ItemProductionMsg(vendor.getBuilding(), vendor, targetItem, mbEnums.ProductionActionType.DEPOSIT, true); + dispatch = Dispatch.borrow(player, outMsg); + DispatchMessage.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.SECONDARY); - outMsg = new ItemProductionMsg(vendor.getBuilding(), vendor, targetItem, mbEnums.ProductionActionType.SETPRICE, true); - dispatch = Dispatch.borrow(player, outMsg); - DispatchMessage.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.SECONDARY); + outMsg = new ItemProductionMsg(vendor.getBuilding(), vendor, targetItem, mbEnums.ProductionActionType.SETPRICE, true); + dispatch = Dispatch.borrow(player, outMsg); + DispatchMessage.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.SECONDARY); } @@ -312,7 +319,7 @@ public class ItemProductionMsgHandler extends AbstractClientMsgHandler { if (!vendor.charItemManager.doesCharOwnThisItem(targetItem.getObjectUUID())) continue; - if (vendor.charItemManager.inventoryContains(targetItem) == false) + if (!vendor.charItemManager.inventoryContains(targetItem)) continue; itemValue = targetItem.template.item_value; @@ -361,48 +368,51 @@ public class ItemProductionMsgHandler extends AbstractClientMsgHandler { ManageNPCMsg outMsg; Dispatch dispatch; - if (origin.sellLock.tryLock()) { + targetItem = Item.getFromCache(itemUUID); - try { + PlayerCharacter player = origin.getPlayerCharacter(); - targetItem = Item.getFromCache(itemUUID); + if (player == null) + return; - PlayerCharacter player = origin.getPlayerCharacter(); + // junk nothing? - if (player == null) - return; + if (targetItem == null) + return; - // junk nothing? + // Cannot junk items without a forge! - if (targetItem == null) - return; + if (vendor.getBuilding() == null) + return; - // Cannot junk items without a forge! + WorkOrder workOrder = ForgeManager.itemWorkOrderLookup.get(targetItem); - if (vendor.getBuilding() == null) - return; + City city = workOrder.vendor.building.getCity(); + city.transactionLock.writeLock().lock(); - WorkOrder workOrder = ForgeManager.itemWorkOrderLookup.get(targetItem); - workOrder.cooking.remove(targetItem); - DbManager.removeFromCache(targetItem); + try { - // WorkOrder exists until all items are completed or junked - // this is to hold a reference to the virtual item for serialization (NPCMsgHandler) + workOrder.cooking.remove(targetItem); + DbManager.removeFromCache(targetItem); - workOrder.slots_used = workOrder.slots_used - 1; + // WorkOrder exists until all items are completed or junked + // this is to hold a reference to the virtual item for serialization (NPCMsgHandler) - if (workOrder.slots_used == 0) - ForgeManager.vendorWorkOrderLookup.get(workOrder.vendor).remove(workOrder); + workOrder.slots_used = workOrder.slots_used - 1; - // Refresh vendor's inventory to client + if (workOrder.slots_used == 0) + ForgeManager.vendorWorkOrderLookup.get(workOrder.vendor).remove(workOrder); - outMsg = new ManageNPCMsg(vendor); - outMsg.setMessageType(1); - dispatch = Dispatch.borrow(player, outMsg); - DispatchMessage.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.SECONDARY); - } finally { - origin.sellLock.unlock(); - } + // Refresh vendor's inventory to client + + outMsg = new ManageNPCMsg(vendor); + outMsg.setMessageType(1); + dispatch = Dispatch.borrow(player, outMsg); + DispatchMessage.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.SECONDARY); + } catch (Exception e) { + Logger.error(e); + } finally { + city.transactionLock.writeLock().unlock(); } } @@ -425,10 +435,10 @@ public class ItemProductionMsgHandler extends AbstractClientMsgHandler { if (targetItem.template.item_type.equals(ItemType.GOLD)) return; - if (vendor.charItemManager.inventoryContains(targetItem) == false) + if (!vendor.charItemManager.inventoryContains(targetItem)) return; - if (player.charItemManager.hasRoomInventory(targetItem.template.item_wt) == false) + if (!player.charItemManager.hasRoomInventory(targetItem.template.item_wt)) return; player.charItemManager.buyFromNPC(targetItem, vendor);