Guest User

FarmTools.cs

a guest
Oct 4th, 2024
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Oxide.Core;
  6. using Oxide.Core.Libraries.Covalence;
  7. using UnityEngine;
  8.  
  9. namespace Oxide.Plugins
  10. {
  11. [Info("Farm Tools", "Clearshot", "1.2.2")]
  12. [Description("Farming made easy. Take control of farming with binds.")]
  13. class FarmTools : CovalencePlugin
  14. {
  15. private PluginConfig _config;
  16. private Game.Rust.Libraries.Player _rustPlayer = Interface.Oxide.GetLibrary<Game.Rust.Libraries.Player>("Player");
  17. private List<ulong> _usedCommand = new List<ulong>();
  18. private Dictionary<ulong, float> _cooldown = new Dictionary<ulong, float>();
  19. private int _debrisLayerMask = LayerMask.GetMask("Debris");
  20. private int _deployedLayerMask = LayerMask.GetMask("Deployed");
  21.  
  22. private const string PERM_CLONE = "farmtools.clone";
  23. private const string PERM_CLONE_ALL = "farmtools.clone.all";
  24. private const string PERM_HARVEST_ALL = "farmtools.harvest.all";
  25. private const string PERM_PLANT_ALL = "farmtools.plant.all";
  26. private const string PERM_GENES = "farmtools.genes";
  27.  
  28. private void SendChatMsg(BasePlayer pl, string msg, string prefix = null) =>
  29. _rustPlayer.Message(pl, msg, prefix != null ? prefix : lang.GetMessage("ChatPrefix", this, pl.UserIDString), Convert.ToUInt64(_config.chatIconID), Array.Empty<object>());
  30.  
  31. private void Init()
  32. {
  33. permission.RegisterPermission(PERM_CLONE, this);
  34. permission.RegisterPermission(PERM_CLONE_ALL, this);
  35. permission.RegisterPermission(PERM_HARVEST_ALL, this);
  36. permission.RegisterPermission(PERM_PLANT_ALL, this);
  37. permission.RegisterPermission(PERM_GENES, this);
  38. }
  39.  
  40. private void ShowHelp(BasePlayer pl)
  41. {
  42. StringBuilder sb = new StringBuilder();
  43. sb.AppendLine(lang.GetMessage("HelpTitle", this, pl.UserIDString));
  44. sb.AppendLine(lang.GetMessage("Help", this, pl.UserIDString));
  45. sb.AppendLine(lang.GetMessage("HelpClone", this, pl.UserIDString));
  46.  
  47. if (permission.UserHasPermission(pl.UserIDString, PERM_CLONE_ALL))
  48. sb.AppendLine(lang.GetMessage("HelpCloneAll", this, pl.UserIDString));
  49.  
  50. if (permission.UserHasPermission(pl.UserIDString, PERM_HARVEST_ALL))
  51. sb.AppendLine(lang.GetMessage("HelpHarvestAll", this, pl.UserIDString));
  52.  
  53. if (permission.UserHasPermission(pl.UserIDString, PERM_PLANT_ALL))
  54. sb.AppendLine(lang.GetMessage("HelpPlantAll", this, pl.UserIDString));
  55.  
  56. if (permission.UserHasPermission(pl.UserIDString, PERM_GENES))
  57. sb.AppendLine(lang.GetMessage("HelpGenes", this, pl.UserIDString));
  58.  
  59. SendChatMsg(pl, sb.ToString(), "");
  60. }
  61.  
  62. #region Commands
  63. [Command("farmtools")]
  64. private void FarmToolsCommand(IPlayer player, string command, string[] args)
  65. {
  66. if (player.IsServer) return;
  67. BasePlayer pl = player.Object as BasePlayer;
  68. if (pl == null) return;
  69.  
  70. ShowHelp(pl);
  71. }
  72.  
  73. [Command("farmtools.clone", "clone")]
  74. private void CloneCommand(IPlayer player, string command, string[] args)
  75. {
  76. if (player.IsServer) return;
  77. BasePlayer pl = player.Object as BasePlayer;
  78. if (pl == null) return;
  79.  
  80. if (!player.HasPermission(PERM_CLONE))
  81. {
  82. SendChatMsg(pl, string.Format(lang.GetMessage("NoPerms", this, pl.UserIDString), command));
  83. return;
  84. }
  85.  
  86. if (!_usedCommand.Contains(pl.userID)) _usedCommand.Add(pl.userID);
  87.  
  88. RaycastHit hit;
  89. if (Physics.Raycast(pl.eyes.HeadRay(), out hit, 4f))
  90. {
  91. Collider[] hitColliders = new Collider[5];
  92. int numColliders = Physics.OverlapSphereNonAlloc(hit.point, 0.65f, hitColliders, _debrisLayerMask);
  93. for (int i = 0; i < numColliders; i++)
  94. {
  95. GrowableEntity growableEntity = hitColliders[i]?.gameObject?.GetComponent<GrowableEntity>();
  96. if (growableEntity != null)
  97. {
  98. growableEntity.TakeClones(pl);
  99. break;
  100. }
  101. }
  102. }
  103. }
  104.  
  105. [Command("farmtools.cloneall", "cloneall")]
  106. private void CloneAllCommand(IPlayer player, string command, string[] args)
  107. {
  108. if (player.IsServer) return;
  109. BasePlayer pl = player.Object as BasePlayer;
  110. if (pl == null) return;
  111.  
  112. if (!player.HasPermission(PERM_CLONE_ALL))
  113. {
  114. SendChatMsg(pl, string.Format(lang.GetMessage("NoPerms", this, pl.UserIDString), command));
  115. return;
  116. }
  117.  
  118. if (!_usedCommand.Contains(pl.userID)) _usedCommand.Add(pl.userID);
  119.  
  120. PlanterBox planter = EyeTraceToEntity(pl, 4f, _deployedLayerMask) as PlanterBox;
  121. if (planter == null) return;
  122.  
  123. foreach (BaseEntity baseEntity in planter.children.ToList())
  124. {
  125. if (baseEntity == null) continue;
  126. GrowableEntity growableEntity = baseEntity as GrowableEntity;
  127. if (growableEntity == null) continue;
  128. growableEntity.TakeClones(pl);
  129. }
  130. }
  131.  
  132. [Command("farmtools.harvestall", "harvestall")]
  133. private void HarvestAllCommand(IPlayer player, string command, string[] args)
  134. {
  135. if (player.IsServer) return;
  136. BasePlayer pl = player.Object as BasePlayer;
  137. if (pl == null) return;
  138.  
  139. if (!player.HasPermission(PERM_HARVEST_ALL))
  140. {
  141. SendChatMsg(pl, string.Format(lang.GetMessage("NoPerms", this, pl.UserIDString), command));
  142. return;
  143. }
  144.  
  145. if (!_usedCommand.Contains(pl.userID)) _usedCommand.Add(pl.userID);
  146.  
  147. PlanterBox planter = EyeTraceToEntity(pl, 4f, _deployedLayerMask) as PlanterBox;
  148. if (planter == null) return;
  149.  
  150. foreach (BaseEntity baseEntity in planter.children.ToList())
  151. {
  152. if (baseEntity == null) continue;
  153. GrowableEntity growableEntity = baseEntity as GrowableEntity;
  154. if (growableEntity == null) continue;
  155.  
  156. if (growableEntity.State == PlantProperties.State.Dying)
  157. growableEntity.RemoveDying(pl);
  158. else
  159. growableEntity.PickFruit(pl);
  160. }
  161. }
  162.  
  163. [Command("farmtools.plantall", "plantall")]
  164. private void PlantAllCommand(IPlayer player, string command, string[] args)
  165. {
  166. if (player.IsServer) return;
  167. BasePlayer pl = player.Object as BasePlayer;
  168. if (pl == null) return;
  169.  
  170. if (!player.HasPermission(PERM_PLANT_ALL))
  171. {
  172. SendChatMsg(pl, string.Format(lang.GetMessage("NoPerms", this, pl.UserIDString), command));
  173. return;
  174. }
  175.  
  176. if (!_usedCommand.Contains(pl.userID)) _usedCommand.Add(pl.userID);
  177.  
  178. Planner planner = pl.GetHeldEntity() as Planner;
  179. if (planner == null) return;
  180.  
  181. PlanterBox planter = EyeTraceToEntity(pl, 4f, _deployedLayerMask) as PlanterBox;
  182. if (planter == null) return;
  183.  
  184. /****************************************************************************
  185. * *
  186. * Credits to: Auto Plant by rostov114 (https://umod.org/plugins/auto-plant) *
  187. * *
  188. ****************************************************************************/
  189.  
  190. Construction construction = PrefabAttribute.server.Find<Construction>(planner.GetDeployable().prefabID);
  191. List<Construction.Target> targets = Facepunch.Pool.Get<List<Construction.Target>>();
  192. foreach (Socket_Base socket in PrefabAttribute.server.FindAll<Socket_Base>(planter.prefabID).Where(x => x.female))
  193. {
  194. Vector3 pos = planter.transform.TransformPoint(socket.worldPosition);
  195. Construction.Target target = new Construction.Target();
  196.  
  197. target.entity = planter;
  198. target.ray = new Ray(pos + Vector3.up * 1.0f, Vector3.down);
  199. target.onTerrain = false;
  200. target.position = pos;
  201. target.normal = Vector3.up;
  202. target.rotation = new Vector3();
  203. target.player = pl;
  204. target.valid = true;
  205. target.socket = socket;
  206. target.inBuildingPrivilege = true;
  207.  
  208. Socket_Base maleSocket = construction.allSockets.Where(x => x.male).FirstOrDefault();
  209. var placement = maleSocket.DoPlacement(target);
  210. if (maleSocket != null && !maleSocket.CheckSocketMods(ref placement))
  211. continue;
  212.  
  213. targets.Add(target);
  214. }
  215.  
  216. foreach (Construction.Target target in targets)
  217. {
  218. planner.DoBuild(target, construction);
  219. }
  220.  
  221. Facepunch.Pool.FreeUnmanaged(ref targets);
  222. // end credits
  223. }
  224.  
  225. [Command("farmtools.genes", "genes")]
  226. private void GenesCommand(IPlayer player, string command, string[] args)
  227. {
  228. if (player.IsServer) return;
  229. BasePlayer pl = player.Object as BasePlayer;
  230. if (pl == null) return;
  231.  
  232. if (!player.HasPermission(PERM_GENES))
  233. {
  234. SendChatMsg(pl, string.Format(lang.GetMessage("NoPerms", this, pl.UserIDString), command));
  235. return;
  236. }
  237.  
  238. if (IsPlayerOnCooldown(pl)) return;
  239. if (!_usedCommand.Contains(pl.userID)) _usedCommand.Add(pl.userID);
  240.  
  241. BaseEntity entity = EyeTraceToEntity(pl, 4f, _deployedLayerMask);
  242. PlanterBox planter = entity as PlanterBox;
  243. if (planter != null)
  244. {
  245. Dictionary<string, List<string>> genes = new Dictionary<string, List<string>>();
  246. foreach (BaseEntity baseEntity in planter.children.ToList())
  247. {
  248. if (baseEntity == null) continue;
  249. GrowableEntity growableEntity = baseEntity as GrowableEntity;
  250. if (growableEntity == null || growableEntity.State == PlantProperties.State.Dying) continue;
  251.  
  252. string g = "";
  253. string itemName = growableEntity.SourceItemDef.displayName.english;
  254. if (!genes.ContainsKey(itemName))
  255. {
  256. genes.Add(itemName, new List<string>());
  257. }
  258.  
  259. foreach (GrowableGene gene in growableEntity.Genes.Genes)
  260. {
  261. g += gene.GetDisplayCharacter();
  262. }
  263.  
  264. if (!genes[itemName].Contains(g))
  265. {
  266. genes[itemName].Add(g);
  267. }
  268. }
  269.  
  270. GiveGenesNote(pl, genes, lang.GetMessage("PlanterGenes", this, pl.UserIDString));
  271. return;
  272. }
  273.  
  274. StorageContainer container = entity as StorageContainer;
  275. if (container != null)
  276. {
  277. Dictionary<string, List<string>> genes = new Dictionary<string, List<string>>();
  278. foreach (Item i in container.inventory.itemList)
  279. {
  280. if (i.info.amountType != ItemDefinition.AmountType.Genetics || i?.instanceData?.dataInt == null) continue;
  281.  
  282. string g = "";
  283. string itemName = i.info.displayName.english;
  284. if (!genes.ContainsKey(itemName))
  285. {
  286. genes.Add(itemName, new List<string>());
  287. }
  288.  
  289. GrowableGenes growGenes = new GrowableGenes();
  290. GrowableGeneEncoding.DecodeIntToGenes(i.instanceData.dataInt, growGenes);
  291. foreach (GrowableGene gene in growGenes.Genes)
  292. {
  293. g += gene.GetDisplayCharacter();
  294. }
  295.  
  296. if (!genes[itemName].Contains(g))
  297. {
  298. genes[itemName].Add(g);
  299. }
  300. }
  301.  
  302. GiveGenesNote(pl, genes, lang.GetMessage("ContainerGenes", this, pl.UserIDString));
  303. return;
  304. }
  305.  
  306. SendChatMsg(pl, lang.GetMessage("InvalidGeneEntity", this, pl.UserIDString));
  307. }
  308. #endregion
  309.  
  310. #region Hooks
  311. private void OnUserDisconnected(IPlayer player)
  312. {
  313. BasePlayer pl = player.Object as BasePlayer;
  314. if (pl == null) return;
  315.  
  316. _usedCommand.Remove(pl.userID);
  317. _cooldown.Remove(pl.userID);
  318. }
  319.  
  320. private void CanTakeCutting(BasePlayer pl, GrowableEntity plant)
  321. {
  322. if (permission.UserHasPermission(pl.UserIDString, PERM_CLONE) || permission.UserHasPermission(pl.UserIDString, PERM_CLONE_ALL))
  323. {
  324. if (!_usedCommand.Contains(pl.userID))
  325. {
  326. ShowHelp(pl);
  327. _usedCommand.Add(pl.userID);
  328. }
  329. }
  330. }
  331. #endregion
  332.  
  333. #region Helpers
  334. public bool IsPlayerOnCooldown(BasePlayer pl)
  335. {
  336. if (_cooldown.ContainsKey(pl.userID) && Time.realtimeSinceStartup < _cooldown[pl.userID])
  337. {
  338. SendChatMsg(pl, string.Format(lang.GetMessage("CommandCooldown", this, pl.UserIDString), Math.Ceiling(_cooldown[pl.userID] - Time.realtimeSinceStartup)));
  339. return true;
  340. }
  341. return false;
  342. }
  343.  
  344. private void GiveGenesNote(BasePlayer pl, Dictionary<string, List<string>> genes, string title)
  345. {
  346. if (genes.Count < 1)
  347. {
  348. SendChatMsg(pl, lang.GetMessage("InvalidGeneEntity", this, pl.UserIDString));
  349. return;
  350. }
  351.  
  352. Item item = ItemManager.CreateByName("note");
  353. item.text = $"{title}\n\n";
  354. foreach (KeyValuePair<string, List<string>> i in genes)
  355. {
  356. item.text += i.Key + "\n";
  357. foreach (string gene in i.Value)
  358. {
  359. item.text += gene + "\n";
  360. }
  361. item.text += "\n";
  362. }
  363.  
  364. if (_config.printGenesToConsole)
  365. pl.ConsoleMessage(item.text);
  366.  
  367. pl.GiveItem(item, BaseEntity.GiveItemReason.PickedUp);
  368. SendChatMsg(pl, lang.GetMessage("GeneNote", this, pl.UserIDString));
  369. _cooldown[pl.userID] = Time.realtimeSinceStartup + _config.geneCooldown;
  370. }
  371.  
  372. private BaseEntity EyeTraceToEntity(BasePlayer pl, float distance, int mask = ~0)
  373. {
  374. RaycastHit hit;
  375. return Physics.Raycast(pl.eyes.HeadRay(), out hit, distance, mask) ? hit.GetEntity() : null;
  376. }
  377. #endregion
  378.  
  379. #region Config
  380. protected override void LoadDefaultMessages()
  381. {
  382. lang.RegisterMessages(new Dictionary<string, string>
  383. {
  384. ["ChatPrefix"] = $"<color=#00a7fe>[{Title}]</color>",
  385. ["NoPerms"] = "<size=12>You do not have permission to use <color=#00a7fe>{0}</color>!</size>",
  386. ["HelpTitle"] = $"<size=16><color=#00a7fe>{Title}</color> Help</size>\n",
  387. ["Help"] = "<size=12>Bind a key to a FarmTools command using the F1 console</size>\n",
  388. ["HelpClone"] = "<size=12>Take clone from a single plant\n<color=#00a7fe>bind <key> farmtools.clone</color>\nchat command: <color=#00a7fe>/clone</color></size>",
  389. ["HelpCloneAll"] = "\n<size=12>Take all clones from a planter\n<color=#00a7fe>bind <key> farmtools.cloneall</color>\nchat command: <color=#00a7fe>/cloneall</color></size>",
  390. ["HelpHarvestAll"] = "\n<size=12>Harvest all plants or remove dying plants from a planter\n<color=#00a7fe>bind <key> farmtools.harvestall</color>\nchat command: <color=#00a7fe>/harvestall</color></size>",
  391. ["HelpPlantAll"] = "\n<size=12>Plant all seeds in the target planter\n<color=#00a7fe>bind <key> farmtools.plantall</color>\nchat command: <color=#00a7fe>/plantall</color></size>",
  392. ["HelpGenes"] = "\n<size=12>Copy genes from a planter box or storage container\n<color=#00a7fe>bind <key> farmtools.genes</color>\nchat command: <color=#00a7fe>/genes</color></size>",
  393. ["PlanterGenes"] = "][ Planter Genes ][",
  394. ["ContainerGenes"] = "][ Container Genes ][",
  395. ["GeneNote"] = "A note with genes has been added to your inventory.\n\nGenes can also be copied from the F1 console with <color=#00a7fe>console.copy</color>.",
  396. ["InvalidGeneEntity"] = "Unable to find genes! Look at a <color=#00a7fe>Planter Box</color> or <color=#00a7fe>Storage Container</color> and try again.",
  397. ["CommandCooldown"] = "Please wait <color=#00a7fe>{0}s</color>!"
  398. }, this);
  399. }
  400.  
  401. protected override void LoadDefaultConfig()
  402. {
  403. Config.WriteObject(GetDefaultConfig(), true);
  404. }
  405.  
  406. private PluginConfig GetDefaultConfig()
  407. {
  408. return new PluginConfig();
  409. }
  410.  
  411. protected override void LoadConfig()
  412. {
  413. base.LoadConfig();
  414. _config = Config.ReadObject<PluginConfig>();
  415. Config.WriteObject(_config, true);
  416. }
  417.  
  418. private class PluginConfig
  419. {
  420. public string chatIconID = "0";
  421. public bool printGenesToConsole = true;
  422. public float geneCooldown = 10;
  423. }
  424. #endregion
  425. }
  426. }
Add Comment
Please, Sign In to add comment