actionworldcraft.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. class WorldCraftActionReciveData : ActionReciveData
  2. {
  3. int m_RecipeID;
  4. }
  5. class WorldCraftActionData : ActionData
  6. {
  7. int m_RecipeID;
  8. }
  9. class ActionWorldCraftCB : ActionContinuousBaseCB
  10. {
  11. override void CreateActionComponent()
  12. {
  13. m_ActionData.m_ActionComponent = new CAContinuousCraft(UATimeSpent.DEFAULT_CRAFT); //default value can be set in recipes
  14. }
  15. /*override void OnFinish(bool pCanceled)
  16. {
  17. super.OnFinish(pCanceled);
  18. if( !GetGame().IsDedicatedServer() )
  19. {
  20. PlayerBase player;
  21. if( Class.CastTo(player, GetGame().GetPlayer()) )
  22. {
  23. if( player.GetCraftingManager().IsInventoryCraft() )
  24. player.GetCraftingManager().CancelInventoryCraft();
  25. }
  26. }
  27. }*/
  28. };
  29. class ActionWorldCraft: ActionContinuousBase
  30. {
  31. private string m_ActionPrompt;
  32. void ActionWorldCraft()
  33. {
  34. m_CallbackClass = ActionWorldCraftCB;
  35. m_CommandUID = DayZPlayerConstants.CMD_ACTIONFB_CRAFTING;
  36. m_FullBody = true;
  37. m_StanceMask = DayZPlayerConstants.STANCEMASK_CROUCH;
  38. }
  39. override ActionData CreateActionData()
  40. {
  41. ActionData action_data = new WorldCraftActionData;
  42. return action_data;
  43. }
  44. override void CreateConditionComponents()
  45. {
  46. m_ConditionItem = new CCINone;
  47. m_ConditionTarget = new CCTObject(UAMaxDistances.DEFAULT);
  48. }
  49. override void OnActionInfoUpdate( PlayerBase player, ActionTarget target, ItemBase item )
  50. {
  51. PluginRecipesManager module_recipes_manager;
  52. Class.CastTo(module_recipes_manager, GetPlugin(PluginRecipesManager) );
  53. m_Text = module_recipes_manager.GetRecipeName( player.GetCraftingManager().GetRecipeID(m_VariantID) );
  54. }
  55. override string GetText()
  56. {
  57. PlayerBase player;
  58. if( Class.CastTo(player, GetGame().GetPlayer()) )
  59. {
  60. PluginRecipesManager moduleRecipesManager;
  61. Class.CastTo(moduleRecipesManager, GetPlugin(PluginRecipesManager) );
  62. return moduleRecipesManager.GetRecipeName( player.GetCraftingManager().GetRecipeID(m_VariantID) );
  63. }
  64. return "Default worldcraft text";
  65. }
  66. override bool ActionCondition( PlayerBase player, ActionTarget target, ItemBase item )
  67. {
  68. if (GetGame().IsServer())
  69. {
  70. if (!target.GetObject() || !item)
  71. return false;
  72. }
  73. return true;
  74. }
  75. override bool ActionConditionContinue(ActionData action_data)
  76. {
  77. if (GetGame().IsServer())
  78. {
  79. if (!action_data.m_Target.GetObject() || !action_data.m_MainItem)
  80. return false;
  81. PluginRecipesManager moduleRecipesManager;
  82. Class.CastTo(moduleRecipesManager, GetPlugin(PluginRecipesManager) );
  83. WorldCraftActionData action_data_wc = WorldCraftActionData.Cast(action_data);
  84. ItemBase item2;
  85. Class.CastTo(item2, action_data_wc.m_Target.GetObject());
  86. if(!moduleRecipesManager.IsRecipePossibleToPerform(action_data_wc.m_RecipeID, action_data.m_MainItem, item2, action_data.m_Player))
  87. {
  88. return false;
  89. }
  90. }
  91. return true;
  92. }
  93. override string GetSoundCategory(ActionData action_data)
  94. {
  95. WorldCraftActionData actionDataWorldCraft = WorldCraftActionData.Cast(action_data);
  96. ItemBase target = ItemBase.Cast(actionDataWorldCraft.m_Target.GetObject());
  97. ItemBase item = actionDataWorldCraft.m_MainItem;
  98. PluginRecipesManager recipesManager = PluginRecipesManager.Cast(GetPlugin(PluginRecipesManager));
  99. string soundCat = recipesManager.GetSoundCategory(actionDataWorldCraft.m_RecipeID,target, item);
  100. return soundCat;
  101. }
  102. override bool SetupAction(PlayerBase player, ActionTarget target, ItemBase item, out ActionData action_data, Param extra_data = NULL )
  103. {
  104. if (super.SetupAction(player, target, item, action_data, extra_data ))
  105. {
  106. WorldCraftActionData action_data_wc = WorldCraftActionData.Cast(action_data);
  107. if (!GetGame().IsDedicatedServer()) // server synchs the recipe id through HandleReceiveData before this
  108. action_data_wc.m_RecipeID = player.GetCraftingManager().GetRecipeID(m_VariantID);
  109. PluginRecipesManager moduleRecipesManager;
  110. Class.CastTo(moduleRecipesManager, GetPlugin(PluginRecipesManager));
  111. moduleRecipesManager.IsRecipePossibleToPerform(action_data_wc.m_RecipeID, action_data_wc.m_MainItem, ItemBase.Cast(action_data_wc.m_Target.GetObject()), player); // sorts items server side, needed for anim selection
  112. m_CommandUID = moduleRecipesManager.GetAnimationCommandUID(action_data_wc.m_RecipeID);
  113. return true;
  114. }
  115. return false;
  116. }
  117. override void Start( ActionData action_data ) //Setup on start of action
  118. {
  119. super.Start(action_data);
  120. if ( action_data.m_Player ) action_data.m_Player.TryHideItemInHands(true);
  121. }
  122. override void OnEndServer( ActionData action_data )
  123. {
  124. if ( action_data.m_Player ) action_data.m_Player.TryHideItemInHands(false);
  125. super.OnEndServer(action_data);
  126. }
  127. override void OnEndClient( ActionData action_data )
  128. {
  129. if ( action_data.m_Player ) action_data.m_Player.TryHideItemInHands(false);
  130. }
  131. override void OnFinishProgressServer(ActionData action_data)
  132. {
  133. WorldCraftActionData action_data_wc;
  134. PluginRecipesManager module_recipes_manager;
  135. ItemBase item2;
  136. Class.CastTo(action_data_wc, action_data);
  137. Class.CastTo(module_recipes_manager, GetPlugin(PluginRecipesManager));
  138. Class.CastTo(item2, action_data.m_Target.GetObject());
  139. if (action_data.m_MainItem && item2)
  140. {
  141. if (GetGame().IsMultiplayer())
  142. ClearActionJuncture(action_data);
  143. else
  144. ClearInventoryReservationEx(action_data);
  145. module_recipes_manager.PerformRecipeServer(action_data_wc.m_RecipeID, action_data.m_MainItem, item2, action_data.m_Player);
  146. if (GetGame().IsMultiplayer())
  147. AddActionJuncture(action_data);
  148. else
  149. InventoryReservation(action_data);
  150. }
  151. }
  152. override void WriteToContext(ParamsWriteContext ctx, ActionData action_data)
  153. {
  154. super.WriteToContext(ctx, action_data);
  155. WorldCraftActionData action_data_wc = WorldCraftActionData.Cast(action_data);
  156. ctx.Write(action_data_wc.m_RecipeID);
  157. }
  158. override bool ReadFromContext(ParamsReadContext ctx, out ActionReciveData action_recive_data )
  159. {
  160. if (!action_recive_data)
  161. {
  162. action_recive_data = new WorldCraftActionReciveData;
  163. }
  164. super.ReadFromContext(ctx, action_recive_data);
  165. int recipeID;
  166. if (!ctx.Read(recipeID))
  167. return false;
  168. WorldCraftActionReciveData recive_data_wc = WorldCraftActionReciveData.Cast(action_recive_data);
  169. recive_data_wc.m_RecipeID = recipeID;
  170. return true;
  171. }
  172. override void HandleReciveData(ActionReciveData action_recive_data, ActionData action_data)
  173. {
  174. WorldCraftActionReciveData recive_data_wc = WorldCraftActionReciveData.Cast(action_recive_data);
  175. WorldCraftActionData action_data_wc = WorldCraftActionData.Cast(action_data);
  176. action_data_wc.m_MainItem = recive_data_wc.m_MainItem;
  177. if (!action_recive_data.m_Target)
  178. {
  179. action_data.m_Target = new ActionTarget(NULL, NULL, -1, vector.Zero, 0);
  180. }
  181. else
  182. {
  183. action_data_wc.m_Target = recive_data_wc.m_Target;
  184. }
  185. action_data_wc.m_RecipeID = recive_data_wc.m_RecipeID;
  186. }
  187. };