ScrapYard.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using GadgetCore.API;
  2. using ScrapYard.API;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Reflection;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8. namespace ScrapYard
  9. {
  10. [Gadget("ScrapYard", RequiredOnClients: true, GadgetVersionSpecificity: VersionSpecificity.NONBREAKING)]
  11. public class ScrapYard : Gadget<ScrapYard>
  12. {
  13. public const string MOD_VERSION = "1.7.1"; // Set this to the version of your mod.
  14. public const string CONFIG_VERSION = "1.0"; // Increment this whenever you change your mod's config file.
  15. protected override void LoadConfig()
  16. {
  17. Config.Load();
  18. string fileVersion = Config.ReadString("ConfigVersion", CONFIG_VERSION, comments: "The Config Version (not to be confused with mod version)");
  19. if (fileVersion != CONFIG_VERSION)
  20. {
  21. Config.Reset();
  22. Config.WriteString("ConfigVersion", CONFIG_VERSION, comments: "The Config Version (not to be confused with mod version)");
  23. }
  24. Config.Save();
  25. }
  26. public override string GetModDescription()
  27. {
  28. return "A mod that adds a scryp yard planet. Lite Version.";
  29. }
  30. protected override void Initialize()
  31. {
  32. Logger.Log("Scrap Yard Lite v" + Info.Mod.Version);
  33. Core.logger = Logger;
  34. Texture2D textureScrapTrophy = GadgetCoreAPI.LoadTexture2D("iScrapTrophy.png");
  35. ItemInfo item = new ItemInfo(ItemType.GENERIC, "Scrap Trophy", "A rusty trophy.\nUsed for purchasing\nspecial items.", textureScrapTrophy);
  36. int itemID = ItemRegistry.Singleton.Register(item, "Scrap Trophy");
  37. if (itemID == -1)
  38. throw new System.Exception("Could not register item " + "Scrap Trophy");
  39. var assetStand = GadgetCoreAPI.LoadAssetBundle("scrapyardstand");
  40. { // Shop Stand
  41. scrapYardStand = UnityEngine.Object.Instantiate((GameObject)assetStand.LoadAsset("assets/resources/scrapyardstand.prefab"));
  42. scrapYardStand.SetActive(false);
  43. scrapYardStand.AddComponent<ScrapYardShopStandScript>();
  44. scrapYardStand.name = "scrapyardstand";
  45. scrapYardStand.transform.GetChild(0).GetComponent<Renderer>().material = new Material(Shader.Find("Unlit/Transparent"))
  46. {
  47. mainTexture = GadgetCoreAPI.LoadTexture2D("scrapYardStand.png")
  48. };
  49. scrapYardStand.transform.GetChild(2).GetComponent<Renderer>().material = new Material(Shader.Find("Unlit/Transparent"))
  50. {
  51. mainTexture = GadgetCoreAPI.LoadTexture2D("scrapYardStandInfo.png")
  52. };
  53. GadgetCoreAPI.AddCustomResource("prop/ScrapYard/stand", scrapYardStand);
  54. ItemInfo tileItem = new ItemInfo(ItemType.GENERIC, "scrapyardstand", "Degug item.", null as Texture2D).Register();
  55. TileInfo tile = new TileInfo(TileType.INTERACTIVE, null as Texture2D, new GameObject("scrapyardstand"), tileItem);
  56. int tileID = TileRegistry.Singleton.Register(tile, "scrapyardstand");
  57. if (tileID == -1)
  58. throw new System.Exception("Could not register Tile " + "scrapyardstand");
  59. tile.OnInteract += OnInteractStand;
  60. }
  61. { // Shop Stand Chip
  62. var scrapYardStand = UnityEngine.Object.Instantiate((GameObject)assetStand.LoadAsset("assets/resources/scrapyardstand.prefab"));
  63. scrapYardStand.SetActive(false);
  64. scrapYardStand.AddComponent<ScrapYardShopStandChipScript>();
  65. scrapYardStand.name = "scrapyardstandchip";
  66. scrapYardStand.transform.GetChild(0).GetComponent<Renderer>().material = new Material(Shader.Find("Unlit/Transparent"))
  67. {
  68. mainTexture = GadgetCoreAPI.LoadTexture2D("scrapYardStand.png")
  69. };
  70. scrapYardStand.transform.GetChild(2).GetComponent<Renderer>().material = new Material(Shader.Find("Unlit/Transparent"))
  71. {
  72. mainTexture = GadgetCoreAPI.LoadTexture2D("scrapYardStandInfo.png")
  73. };
  74. GadgetCoreAPI.AddCustomResource("prop/ScrapYard/standchip", scrapYardStand);
  75. ItemInfo tileItem = new ItemInfo(ItemType.GENERIC, "scrapyardstandchip", "Degug item.", null as Texture2D).Register();
  76. TileInfo tile = new TileInfo(TileType.INTERACTIVE, null as Texture2D, new GameObject("scrapyardstandchip"), tileItem);
  77. int tileID = TileRegistry.Singleton.Register(tile, "scrapyardstandchip");
  78. if (tileID == -1)
  79. throw new System.Exception("Could not register Tile " + "scrapyardstandchip");
  80. tile.OnInteract += OnInteractStand;
  81. }
  82. ShopPlatform.Reset();
  83. // Setting up default shop platforms
  84. new ShopPlatform("Default Objects", ShopPlatformSpacingType.Near).Register("DefaultObjects");
  85. new ShopPlatform("Default Blocks", ShopPlatformSpacingType.Near).Register("DefaultBlocks");
  86. new ShopPlatform("Default Walls", ShopPlatformSpacingType.Near).Register("DefaultWalls");
  87. SceneManager.sceneLoaded += OnSceneLoaded;
  88. }
  89. private static GameObject scrapYardStand;
  90. internal static void OnSceneLoaded(Scene scene, LoadSceneMode mode)
  91. {
  92. if (scene.buildIndex == 1)
  93. {
  94. InstanceTracker.GameScript.StartCoroutine(CreateStands());
  95. try
  96. {
  97. var storageMenu = GameObject.Find("Ship").transform.Find("SHIPPLACES").transform.Find("NPCs").transform.Find("npcFlora").localPosition = new Vector3(-134.58f, 11.72f, -0.19f);
  98. }
  99. catch (System.Exception e) { Core.logger.LogError(e.Message); }
  100. }
  101. }
  102. private static IEnumerator CreateStands()
  103. {
  104. while (!Network.isServer && !Network.isClient)
  105. {
  106. yield return new WaitForSeconds(0.25f);
  107. }
  108. yield return new WaitForSeconds(0.75f);
  109. if (Network.isServer)
  110. {
  111. try
  112. {
  113. {
  114. var stand = (GameObject)Network.Instantiate((GameObject)Resources.Load("prop/ScrapYard/stand"), new Vector3(-250f + 4 * -5.5f, -5.99f - 2 + 0.5f, 0.19f), Quaternion.identity, 0);
  115. stand.GetComponent<ScrapYardShopStandScript>().GetComponent<ScrapYardShopStandScript>().StartCallSet(ItemRegistry.GetItemIDByRegistryName("ScrapYard:Scrap Trophy"), 9999, 1, 57);
  116. }
  117. List<ShopPlatformEntry> blocks = new List<ShopPlatformEntry>();
  118. List<ShopPlatformEntry> objects = new List<ShopPlatformEntry>();
  119. foreach (var p in ShopPlatform.GetShopPlatforms())
  120. if (p == ShopPlatform.DefaultBlocks || p == ShopPlatform.DefaultWalls)
  121. foreach (var e in p.Entries)
  122. blocks.Add(e);
  123. else
  124. foreach (var e in p.Entries)
  125. objects.Add(e);
  126. for (int i = 0; i < System.Math.Min(objects.Count, 3); i++)
  127. {
  128. var entry = objects[i];
  129. if (entry.EntryType == ShopPlatformEntryType.Item)
  130. {
  131. var stand = (GameObject)Network.Instantiate((GameObject)Resources.Load("prop/ScrapYard/stand"), new Vector3(-250f + 4 * i, -5.99f - 2 + 0.5f, 0.19f), Quaternion.identity, 0);
  132. stand.GetComponent<ScrapYardShopStandScript>().StartCallSet(entry.Id, entry.Price, entry.Quantity, entry.CurrencyItemID);
  133. }
  134. else if (entry.EntryType == ShopPlatformEntryType.Chip)
  135. {
  136. var stand = (GameObject)Network.Instantiate((GameObject)Resources.Load("prop/ScrapYard/standchip"), new Vector3(-250f + 4 * i, -5.99f - 2 + 0.5f, 0.19f), Quaternion.identity, 0);
  137. stand.GetComponent<ScrapYardShopStandChipScript>().StartCallSet(entry.Id, entry.Price, entry.CurrencyItemID);
  138. }
  139. }
  140. for (int i = 0; i < System.Math.Min(blocks.Count, 5); i++)
  141. {
  142. var entry = blocks[i];
  143. if (entry.EntryType == ShopPlatformEntryType.Item)
  144. {
  145. var stand = (GameObject)Network.Instantiate((GameObject)Resources.Load("prop/ScrapYard/stand"), new Vector3(-250f + 4 * 7 + 4 * i, -5.99f + 6 + 0.5f, 0.19f), Quaternion.identity, 0);
  146. stand.GetComponent<ScrapYardShopStandScript>().StartCallSet(entry.Id, entry.Price, entry.Quantity, entry.CurrencyItemID);
  147. }
  148. else if (entry.EntryType == ShopPlatformEntryType.Chip)
  149. {
  150. var stand = (GameObject)Network.Instantiate((GameObject)Resources.Load("prop/ScrapYard/standchip"), new Vector3(-250f + 4 * 7 + 4 * i, -5.99f + 6 + 0.5f, 0.19f), Quaternion.identity, 0);
  151. stand.GetComponent<ScrapYardShopStandChipScript>().StartCallSet(entry.Id, entry.Price, entry.CurrencyItemID);
  152. }
  153. }
  154. if (objects.Count > 3 || blocks.Count > 5)
  155. Core.logger.LogWarning("There are more stands than can be added to your ship. U should use the non Lite version of Scrap Yard");
  156. }
  157. catch (System.Exception e)
  158. {
  159. Core.logger.LogError(e);
  160. }
  161. }
  162. yield break;
  163. }
  164. public static readonly FieldInfo canInteractField = typeof(PlayerScript).GetField("canInteract", BindingFlags.NonPublic | BindingFlags.Instance);
  165. public static readonly FieldInfo interactingField = typeof(PlayerScript).GetField("interacting", BindingFlags.NonPublic | BindingFlags.Instance);
  166. private IEnumerator OnInteractStand()
  167. {
  168. try
  169. {
  170. PlayerScript.curInteractObj.SendMessage("Request");
  171. canInteractField.SetValue(InstanceTracker.PlayerScript, true);
  172. interactingField.SetValue(InstanceTracker.PlayerScript, false);
  173. InstanceTracker.PlayerScript.w.SetActive(true);
  174. }
  175. catch (System.Exception e)
  176. {
  177. Core.logger.Log(e);
  178. }
  179. yield break;
  180. }
  181. }
  182. }